申请会员ID:dtking
申请邮箱:fangming78@sina.com
我解决这个问题的办法,无奈没有账号发帖
Java实现九宫格
https://www.52pojie.cn/thread-1484202-1-1.html
(出处: 吾爱破解论坛)
package com.ming;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Spfpc {
private int data_init[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}; // 初始
private int data_success[][] = {{8, 7, 6}, {5, 0, 4}, {3, 2, 1}}; // 成功
private int data_fail[][] = {{6, 7, 8}, {5, 0, 4}, {3, 2, 1}}; // 失败
private String success; // 正确
private String fail; // 错误
private List<int[][]> history = new ArrayList<>(); // 移动记录
public Spfpc() {
init();
}
/**
* @return void
* @Description 正确答案追加成字符串
* @date 2021/7/28 17:18
* @auther FMm
*/
public void init() {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < data_success.length; i++) {
for (int j = 0; j < data_success[i].length; j++) {
stringBuffer.append(data_success[i][j]);
}
}
success = stringBuffer.toString();
stringBuffer.delete(0, stringBuffer.length());
for (int i = 0; i < data_fail.length; i++) {
for (int j = 0; j < data_fail[i].length; j++) {
stringBuffer.append(data_fail[i][j]);
}
}
fail = stringBuffer.toString();
}
/**
* @return void
* @Description 移动后每次转换
* @date 2021/7/28 17:20
* @auther FMm
*/
public String arrayToString() {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < data_init.length; i++) {
for (int j = 0; j < data_init[i].length; j++) {
stringBuffer.append(data_init[i][j]);
}
}
return stringBuffer.toString();
}
/**
* @param params 移动的数字
* @return java.lang.String
* @Description 通过选择要移动的数字 查找对应下标 x y
* @date 2021/7/28 17:23
* @auther FMm
*/
public String getIndex(int params) {
for (int i = 0; i < data_init.length; i++) {
for (int j = 0; j < data_init[i].length; j++) {
int i1 = data_init[i][j];
if (i1 == params) {
return i + "," + j;
}
}
}
return "";
}
/**
* @param i
* @return boolean
* @Description 验证 上下左右 是否正确
* @date 2021/7/28 17:24
* @auther FMm
*/
public static boolean checkPos(int i) {
if (i == 2 | i == 4 | i == 6 | i == 8) {
return true;
}
return false;
}
/**
* @param pos 移动的方位
* @param input 移动的数字
* @return void
* @Description 移动数字
* @date 2021/7/28 17:27
* @auther FMm
*/
public void moveIndex(int pos, int input) {
String index = getIndex(input);
String[] split = index.split(",");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
// 上8下2左4右6
if (pos == 8) {
if (x == 0 || data_init[x - 1][y] != 0) {
System.out.println("无法向上移动");
} else {
data_init[x - 1][y] = input;
data_init[x][y] = 0;
}
} else if (pos == 2) {
if (x == 2 || data_init[x + 1][y] != 0) {
System.out.println("无法向下移动");
} else {
data_init[x + 1][y] = input;
data_init[x][y] = 0;
}
} else if (pos == 4) {
if (y == 0 || data_init[x][y - 1] != 0) {
System.out.println("无法向左移动");
} else {
data_init[x][y - 1] = input;
data_init[x][y] = 0;
}
} else if (pos == 6) {
if (y == 2 || data_init[x][y + 1] != 0) {
System.out.println("无法向右移动");
} else {
data_init[x][y + 1] = input;
data_init[x][y] = 0;
}
}
history.add(data_init);
outResult();
}
/**
* @return void
* @Description 输出移动后的 结果
* @date 2021/7/28 17:26
* @auther FMm
*/
public void outResult() {
for (int i = 0; i < data_init.length; i++) {
System.out.printf("[");
System.out.printf("\t");
for (int j = 0; j < data_init[i].length; j++) {
int i1 = data_init[i][j];
if (i1 == 0) {
System.out.printf("-");
} else {
System.out.printf(String.valueOf(i1));
}
System.out.printf("\t");
}
System.out.printf("]");
System.out.println();
}
}
public static void main(String[] args) {
Spfpc spfpc = new Spfpc();
System.out.printf("【游戏说明】有一种游戏,其规则如下:\n有一个 3×3 的九宫方格棋盘,在8个方格中摆棋子,每个棋子上标有1~8中的某一个数字。\n" +
"棋盘中留有一个空格,空格周围的棋子可以移到空格中,一次只能移动一个棋子。\n" +
"图 (a) 定义了棋盘的初始局面。经过连续的若干次移动棋子后,当 3×3 方格呈现出\n" +
"图 (b) 所示的局面时,表示获胜;当呈现出 \n" +
"图 (c) 所示的局面时,表示失败。游戏获胜或失败时,本轮游戏结束\n");
System.out.println("图 (a)\t\t\t图 (b)\t\t\t图 (c)\t\t");
System.out.println("1\t" + "2\t" + "3\t\t" + "8\t" + "7\t" + "6\t\t" + "6\t" + "7\t" + "8\t");
System.out.println("4\t" + "5\t" + "6\t\t" + "5\t" + "-\t" + "4\t\t" + "5\t" + "-\t" + "4\t");
System.out.println("7\t" + "8\t" + "-\t\t" + "3\t" + "2\t" + "1\t\t" + "3\t" + "2\t" + "1\t");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要移动的数字【1-8】数字:");
int number = spfpc.getNumber(scanner);
System.out.print("请输入要移动的方向【上8下2左4有6】:");
int pos = spfpc.getPos(scanner);
spfpc.moveIndex(pos, number);
while (true) {
String s = spfpc.arrayToString();
if (s.equals(spfpc.fail)) {
System.out.printf("失败");
break;
} else {
if (s.equals(spfpc.success)) {
System.out.println("成功");
System.out.println("步数:" + spfpc.history.size());
break;
} else {
System.out.print("请输入要移动的数字【1-8】数字:");
number = spfpc.getNumber(scanner);
System.out.print("请输入要移动的方向【上8下2左4有6】:");
pos = spfpc.getPos(scanner);
spfpc.moveIndex(pos, number);
}
}
}
}
/**
* @param scanner
* @return int
* @Description 移动数字
* @date 2021/7/28 17:30
* @auther FMm
*/
public int getNumber(Scanner scanner) {
String params = scanner.next();
int i = 0;
while (true) {
try {
i = Integer.parseInt(params);
if (i > 0 && i < 9) {
return i;
} else {
System.err.println("您输入的是:" + params);
System.out.print("请输入要移动的数字【1-8】数字:");
params = scanner.next();
}
} catch (Exception e) {
System.err.println("您输入的是:" + params);
System.out.print("请输入要移动的数字【1-8】数字:");
params = scanner.next();
}
}
}
/**
* @param scanner
* @return int
* @Description 移动位置
* @date 2021/7/28 17:30
* @auther FMm
*/
public int getPos(Scanner scanner) {
String params = scanner.next();
int i = 0;
while (true) {
try {
i = Integer.parseInt(params);
if (checkPos(i)) {
return i;
} else {
System.err.println("您输入的是:" + params);
System.err.print("请输入要移动的方向【上8下2左4有6】:");
params = scanner.next();
}
} catch (Exception e) {
System.err.println("您输入的是:" + params);
System.err.print("请输入要移动的方向【上8下2左4有6】:");
params = scanner.next();
}
}
}
}
|