Reputation: 43
I'll try to explain my problem as good as I can. So first of all, I'll just put all of my code here, so you can see what I did here and get a better point of view.
package co.edureka;
import java.util.Scanner;
import java.util.Formatter;
public class Puzzle {
static void output(int[][] gamefield) {
for (int i = 0; i < 4; i++) {
System.out.println("|");
System.out.println("+-------+-------+-------+-------+");
for (int y = 0; y < 4; y++) {
System.out.print("| " + gamefield[i][y] + "\t");
}
}
}
static void coordinate(int[][] gamefield) {
Scanner entry = new Scanner(System.in);
System.out.println("\n\nEntry a number to move position");
System.out.print("Row: ");
int row = entry.nextInt();
System.out.print("Column: ");
int column = entry.nextInt();
while (true) {
while ((row == 0 && column == 0)) {
if (row == 0 || column == 0) {
gamefield[0][0] = 0;
}
gamefield[3][0] = 11;
break;
}
while ((row == 0 && column == 1)) {
if (row == 0 || column == 1) {
gamefield[0][1] = 0;
}
gamefield[3][0] = 12;
break;
}
while ((row == 0 && column == 2)) {
if (row == 0 || column == 2) {
gamefield[0][2] = 0;
}
gamefield[3][0] = 13;
break;
}
while ((row == 0 && column == 3)) {
if (row == 0 || column == 3) {
gamefield[0][3] = 0;
}
gamefield[3][0] = 14;
break;
}
while ((row == 1 && column == 0)) {
if (row == 1 || column == 0) {
gamefield[1][0] = 0;
}
gamefield[3][0] = 1;
break;
}
while ((row == 1 && column == 1)) {
if (row == 1 || column == 1) {
gamefield[1][1] = 0;
}
gamefield[3][0] = 2;
break;
}
while ((row == 1 && column == 2)) {
if (row == 1 || column == 2) {
gamefield[1][2] = 0;
}
gamefield[3][0] = 3;
break;
}
while ((row == 1 && column == 3)) {
if (row == 1 || column == 3) {
gamefield[1][3] = 0;
}
gamefield[3][0] = 4;
break;
}
while ((row == 2 && column == 0)) {
if (row == 2 || column == 0) {
gamefield[2][0] = 0;
}
gamefield[3][0] = 8;
break;
}
while ((row == 2 && column == 1)) {
if (row == 2 || column == 1) {
gamefield[2][1] = 0;
}
gamefield[3][0] = 7;
break;
}
while ((row == 2 && column == 2)) {
if (row == 2 || column == 2) {
gamefield[2][2] = 0;
}
gamefield[3][0] = 6;
break;
}
while ((row == 2 && column == 3)) {
if (row == 2 || column == 3) {
gamefield[2][3] = 0;
}
gamefield[3][0] = 5;
break;
}
while ((row == 3 && column == 0)) {
if (row == 3 || column == 0) {
gamefield[3][0] = 0;
}
gamefield[3][0] = 0;
break;
}
while ((row == 3 && column == 1)) {
if (row == 3 || column == 1) {
gamefield[3][1] = 0;
}
gamefield[3][0] = 9;
break;
}
while ((row == 3 && column == 2)) {
if (row == 3 || column == 2) {
gamefield[3][2] = 0;
}
gamefield[3][0] = 10;
break;
}
while ((row == 3 && column == 3)) {
if (row == 3 || column == 3) {
gamefield[3][3] = 0;
}
gamefield[3][0] = 15;
}
if(row >4 || column >4) {
System.out.println("Please put a number lower then 4");
break;
} else {
break;
}
}
}
public static void main(String[] args) {
int[][] gamefield = new int[4][4];
gamefield[0][0] = 11;
gamefield[0][1] = 12;
gamefield[0][2] = 13;
gamefield[0][3] = 14;
gamefield[1][0] = 1;
gamefield[1][1] = 2;
gamefield[1][2] = 3;
gamefield[1][3] = 4;
gamefield[2][0] = 8;
gamefield[2][1] = 7;
gamefield[2][2] = 6;
gamefield[2][3] = 5;
gamefield[3][0] = 0;
gamefield[3][1] = 9;
gamefield[3][2] = 10;
gamefield[3][3] = 15;
output(gamefield);
coordinate(gamefield);
output(gamefield);
}
}
Context:
To get my idea: I want to program a puzzle game, which is also called slider or fifteen game puzzle. My program does print a table, with all the array-elements in it. So with all the while-loops in the coordinate method I can change now every position I want. But that's not the point, I have to look for zero, so the empty spot.
I can't get it to work, that the 0 can just change the position with the numbers next to it. Just have a look in my program, the 0 is next to the 8 and 9. So how can I change the position just with the 8 or the 9? After the 0 is in the position of 8 or 9, then java has to scan again, which numbers next to him are.
I should'nt be able to change the position with a number, which isn't next to the zero. Do you guys have any idea, how I could solve this problem?
Upvotes: 2
Views: 93
Reputation: 765
A possible solution (based on provided code) :
package co.edureka;
import java.util.Scanner;
public class Puzzle {
static void output(int[][] gamefield) {
for (int i = 0; i < 4; i++) {
System.out.println("|");
System.out.println("+-------+-------+-------+-------+");
for (int y = 0; y < 4; y++) {
System.out.print("| " + gamefield[i][y] + "\t");
}
}
}
static void coordinate(int[][] gamefield) {
Scanner entry = new Scanner(System.in);
System.out.println("\n\nEntry a number to move position");
System.out.print("Row: ");
int row = entry.nextInt();
System.out.print("Column: ");
int column = entry.nextInt();
if (!isSlidable(row, column, gamefield)) {
System.out.println("Position is not slidable");
return;
}
while (true) {
while ((row == 0 && column == 0)) {
if (row == 0 || column == 0) {
gamefield[0][0] = 0;
}
gamefield[3][0] = 11;
break;
}
while ((row == 0 && column == 1)) {
if (row == 0 || column == 1) {
gamefield[0][1] = 0;
}
gamefield[3][0] = 12;
break;
}
while ((row == 0 && column == 2)) {
if (row == 0 || column == 2) {
gamefield[0][2] = 0;
}
gamefield[3][0] = 13;
break;
}
while ((row == 0 && column == 3)) {
if (row == 0 || column == 3) {
gamefield[0][3] = 0;
}
gamefield[3][0] = 14;
break;
}
while ((row == 1 && column == 0)) {
if (row == 1 || column == 0) {
gamefield[1][0] = 0;
}
gamefield[3][0] = 1;
break;
}
while ((row == 1 && column == 1)) {
if (row == 1 || column == 1) {
gamefield[1][1] = 0;
}
gamefield[3][0] = 2;
break;
}
while ((row == 1 && column == 2)) {
if (row == 1 || column == 2) {
gamefield[1][2] = 0;
}
gamefield[3][0] = 3;
break;
}
while ((row == 1 && column == 3)) {
if (row == 1 || column == 3) {
gamefield[1][3] = 0;
}
gamefield[3][0] = 4;
break;
}
while ((row == 2 && column == 0)) {
if (row == 2 || column == 0) {
gamefield[2][0] = 0;
}
gamefield[3][0] = 8;
break;
}
while ((row == 2 && column == 1)) {
if (row == 2 || column == 1) {
gamefield[2][1] = 0;
}
gamefield[3][0] = 7;
break;
}
while ((row == 2 && column == 2)) {
if (row == 2 || column == 2) {
gamefield[2][2] = 0;
}
gamefield[3][0] = 6;
break;
}
while ((row == 2 && column == 3)) {
if (row == 2 || column == 3) {
gamefield[2][3] = 0;
}
gamefield[3][0] = 5;
break;
}
while ((row == 3 && column == 0)) {
if (row == 3 || column == 0) {
gamefield[3][0] = 0;
}
gamefield[3][0] = 0;
break;
}
while ((row == 3 && column == 1)) {
if (row == 3 || column == 1) {
gamefield[3][1] = 0;
}
gamefield[3][0] = 9;
break;
}
while ((row == 3 && column == 2)) {
if (row == 3 || column == 2) {
gamefield[3][2] = 0;
}
gamefield[3][0] = 10;
break;
}
while ((row == 3 && column == 3)) {
if (row == 3 || column == 3) {
gamefield[3][3] = 0;
}
gamefield[3][0] = 15;
}
if(row >3 || column >3) {
System.out.println("Please put a number lower then 3");
break;
} else {
break;
}
}
}
private static boolean isSlidable(int row, int column, int[][] gamefield) {
return (column < 3 && gamefield[row][column+1] == 0)
|| (row < 3 && gamefield[row+1][column] == 0)
|| (column > 0 && gamefield[row][column-1] == 0)
|| (row > 0 && gamefield[row-1][column] == 0);
}
public static void main(String[] args) {
int[][] gamefield = new int[4][4];
gamefield[0][0] = 11;
gamefield[0][1] = 12;
gamefield[0][2] = 13;
gamefield[0][3] = 14;
gamefield[1][0] = 1;
gamefield[1][1] = 2;
gamefield[1][2] = 3;
gamefield[1][3] = 4;
gamefield[2][0] = 8;
gamefield[2][1] = 7;
gamefield[2][2] = 6;
gamefield[2][3] = 5;
gamefield[3][0] = 0;
gamefield[3][1] = 9;
gamefield[3][2] = 10;
gamefield[3][3] = 15;
output(gamefield);
coordinate(gamefield);
output(gamefield);
}
}
Changes:
I have added a check before to perform the move (see isSlidable
in code). This function takes care to verify if selected position is next to a 0.
Upvotes: 1