Puzzle
import java.util.Scanner;
public class javaapplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char board1[][] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', ' '}};
printaboard(board1);
char board2[][] = {{'d', 'a', 'b'}, {' ', 'f', 'h'}, {'e', 'g', 'c'}};
printaboard(board2);
boolean endgame;
do {
int x = returnx(board2);
int y = returny(board2);
char letter2 = inputnumberandcheakletter(board2, x, y);
int distance = distance(board2, x, y, letter2);
String HorV = HorV(board2, x, y, letter2);
movechip(board2, x, y, letter2, distance, HorV);
printaboard(board2);
endgame = endgame(board1, board2);
} while (endgame != true);
System.out.println("end game^.^!");
}
public static void printaboard(char board1[][]) {
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[0][0], board1[0][1], board1[0][2]);
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[1][0], board1[1][1], board1[1][2]);
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[2][0], board1[2][1], board1[2][2]);
System.out.printf("+---+---+---+\n\n");
}
public static int returnx(char board2[][]) {
int x = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == ' ') {
x = i;
}
}
}
return x;
}
public static int returny(char board2[][]) {
int y = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == ' ') {
y = j;
}
}
}
return y;
}
public static char inputnumberandcheakletter(char board2[][], int x, int y) {
Scanner input = new Scanner(System.in);
boolean inputletter = false;
char letter2;
do {
System.out.print("Input letter: ");
String letter1 = input.next();
letter2 = letter1.charAt(0);
switch (letter2) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (i == x || j == y) {
inputletter = true;
}
}
}
}
break;
default:
inputletter = false;
}
} while (inputletter != true);
return letter2;
}
public static int distance(char board2[][], int x, int y, char letter2) {
int i = 0, j, distance = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (x == i) {
distance = y - j;
} else {
distance = x - i;
}
}
}
}
return distance;
}
public static String HorV(char board2[][], int x, int y, char letter2) {
String HorV = "";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (x == i) {
HorV = "horizontal";
} else {
HorV = "vertical";
}
}
}
}
return HorV;
}
public static char[][] movechip(char board2[][], int x, int y, char letter2, int distance, String HorV) {
char temp;
if (HorV == "horizontal") {
switch (distance) {
case -1:
temp = board2[x][y];
board2[x][y] = board2[x][y + 1];
board2[x][y + 1] = temp;
break;
case 1:
temp = board2[x][y];
board2[x][y] = board2[x][y - 1];
board2[x][y - 1] = temp;
break;
case -2:
temp = board2[x][y];
board2[x][y] = board2[x][y + 1];
board2[x][y + 1] = temp;
temp = board2[x][y + 1];
board2[x][y + 1] = board2[x][y + 2];
board2[x][y + 2] = temp;
break;
case 2:
temp = board2[x][y];
board2[x][y] = board2[x][y - 1];
board2[x][y - 1] = temp;
temp = board2[x][y - 1];
board2[x][y - 1] = board2[x][y - 2];
board2[x][y - 2] = temp;
break;
}
} else {
if (HorV == "vertical") {
switch (distance) {
case -1:
temp = board2[x][y];
board2[x][y] = board2[x + 1][y];
board2[x + 1][y] = temp;
break;
case 1:
temp = board2[x][y];
board2[x][y] = board2[x - 1][y];
board2[x - 1][y] = temp;
break;
case -2:
temp = board2[x][y];
board2[x][y] = board2[x + 1][y];
board2[x + 1][y] = temp;
temp = board2[x + 1][y];
board2[x + 1][y] = board2[x + 2][y];
board2[x + 2][y] = temp;
break;
case 2:
temp = board2[x][y];
board2[x][y] = board2[x - 1][y];
board2[x - 1][y] = temp;
temp = board2[x - 1][y];
board2[x - 1][y] = board2[x - 2][y];
board2[x - 2][y] = temp;
break;
}
}
}
return board2;
}
public static boolean endgame(char board1[][], char board2[][]) {
boolean endgame = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board1[i][j] != board2[i][j]) {
endgame = false;
}
}
}
return endgame;
}
}
public class javaapplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char board1[][] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', ' '}};
printaboard(board1);
char board2[][] = {{'d', 'a', 'b'}, {' ', 'f', 'h'}, {'e', 'g', 'c'}};
printaboard(board2);
boolean endgame;
do {
int x = returnx(board2);
int y = returny(board2);
char letter2 = inputnumberandcheakletter(board2, x, y);
int distance = distance(board2, x, y, letter2);
String HorV = HorV(board2, x, y, letter2);
movechip(board2, x, y, letter2, distance, HorV);
printaboard(board2);
endgame = endgame(board1, board2);
} while (endgame != true);
System.out.println("end game^.^!");
}
public static void printaboard(char board1[][]) {
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[0][0], board1[0][1], board1[0][2]);
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[1][0], board1[1][1], board1[1][2]);
System.out.printf("+---+---+---+\n");
System.out.printf("| %C | %C | %C |\n", board1[2][0], board1[2][1], board1[2][2]);
System.out.printf("+---+---+---+\n\n");
}
public static int returnx(char board2[][]) {
int x = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == ' ') {
x = i;
}
}
}
return x;
}
public static int returny(char board2[][]) {
int y = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == ' ') {
y = j;
}
}
}
return y;
}
public static char inputnumberandcheakletter(char board2[][], int x, int y) {
Scanner input = new Scanner(System.in);
boolean inputletter = false;
char letter2;
do {
System.out.print("Input letter: ");
String letter1 = input.next();
letter2 = letter1.charAt(0);
switch (letter2) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (i == x || j == y) {
inputletter = true;
}
}
}
}
break;
default:
inputletter = false;
}
} while (inputletter != true);
return letter2;
}
public static int distance(char board2[][], int x, int y, char letter2) {
int i = 0, j, distance = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (x == i) {
distance = y - j;
} else {
distance = x - i;
}
}
}
}
return distance;
}
public static String HorV(char board2[][], int x, int y, char letter2) {
String HorV = "";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board2[i][j] == letter2) {
if (x == i) {
HorV = "horizontal";
} else {
HorV = "vertical";
}
}
}
}
return HorV;
}
public static char[][] movechip(char board2[][], int x, int y, char letter2, int distance, String HorV) {
char temp;
if (HorV == "horizontal") {
switch (distance) {
case -1:
temp = board2[x][y];
board2[x][y] = board2[x][y + 1];
board2[x][y + 1] = temp;
break;
case 1:
temp = board2[x][y];
board2[x][y] = board2[x][y - 1];
board2[x][y - 1] = temp;
break;
case -2:
temp = board2[x][y];
board2[x][y] = board2[x][y + 1];
board2[x][y + 1] = temp;
temp = board2[x][y + 1];
board2[x][y + 1] = board2[x][y + 2];
board2[x][y + 2] = temp;
break;
case 2:
temp = board2[x][y];
board2[x][y] = board2[x][y - 1];
board2[x][y - 1] = temp;
temp = board2[x][y - 1];
board2[x][y - 1] = board2[x][y - 2];
board2[x][y - 2] = temp;
break;
}
} else {
if (HorV == "vertical") {
switch (distance) {
case -1:
temp = board2[x][y];
board2[x][y] = board2[x + 1][y];
board2[x + 1][y] = temp;
break;
case 1:
temp = board2[x][y];
board2[x][y] = board2[x - 1][y];
board2[x - 1][y] = temp;
break;
case -2:
temp = board2[x][y];
board2[x][y] = board2[x + 1][y];
board2[x + 1][y] = temp;
temp = board2[x + 1][y];
board2[x + 1][y] = board2[x + 2][y];
board2[x + 2][y] = temp;
break;
case 2:
temp = board2[x][y];
board2[x][y] = board2[x - 1][y];
board2[x - 1][y] = temp;
temp = board2[x - 1][y];
board2[x - 1][y] = board2[x - 2][y];
board2[x - 2][y] = temp;
break;
}
}
}
return board2;
}
public static boolean endgame(char board1[][], char board2[][]) {
boolean endgame = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board1[i][j] != board2[i][j]) {
endgame = false;
}
}
}
return endgame;
}
}