简体   繁体   English

Java 2D 阵列问题与更改 position

[英]Java 2D-Array problem with changing position

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.得到我的想法:我想编写一个益智游戏,也称为 slider 或十五游戏益智。 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.因此,使用坐标方法中的所有 while 循环,我现在可以更改我想要的每个 position。 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.我无法让它工作,0 可以改变 position 旁边的数字。 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?看看我的程序,0 就在 8 和 9 旁边。那么我怎样才能只用 8 或 9 更改 position? After the 0 is in the position of 8 or 9, then java has to scan again, which numbers next to him are. 0在8或9的position中后,则java要再次扫描,他旁边的数字是。

I should'nt be able to change the position with a number, which isn't next to the zero.我不应该能够用不在零旁边的数字更改 position。 Do you guys have any idea, how I could solve this problem?你们有什么想法,我该如何解决这个问题?

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).我在执行移动之前添加了一个检查(参见代码中的isSlidable )。 This function takes care to verify if selected position is next to a 0.此 function 会注意验证选定的 position 是否在 0 旁边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM