首页 > 解决方案 > Java 2D 数组特定移动

问题描述

我制作了一个类,其中生成了一个 6x10 2D 数组来充当板。

然后在构造函数中生成一个随机的起始位置。我只希望相邻的移动是可能的。

例如,如果随机位置已生成为 (2,3),则例如用户输入 (1,2) 这将是一个有效的移动,但 (6,1) 将是一个无效的移动。

然后,如果用户输入说 (1,2),他们就可以从 (1,2) 转到任何相邻的单元格。

我已经包含了下面的类,以及我试图测试它的相邻方法,但我对如何处理这个有点困惑。

import java.util.Arrays;
import java.util.Random;

public class Test {

    public static final int ROWS = 6;
    public static final int COLUMNS = 10;
    public int[][] board;

    public static void main(String[] args)
    {
        Test t = new Test();
        t.getBoard();
        t.makeMove(6,1);  //I want this to be an invalid move.
        t.getBoard();
        t.makeMove(1,2);  // this should be a valid move
        t.getBoard();

    }

    public Test()
    {
        board = new int[ROWS][COLUMNS];
        createRandomLocation();
    }

    public void createRandomLocation()
    {
        Random rand = new Random();
        int x = rand.nextInt(6);
        int y = rand.nextInt(10);
        board[x][y] = 1;
    }



    public void makeMove(int x,int y){
    if (Math.abs(x-cur_x)==0 || Math.abs(y-cur_y)==0) {
        board[x][y] = 1;
    }

    public String getBoard() {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();

        return Arrays.deepToString(board);
    }



}

邻近的:

/*public boolean isMoveAllowed(int [][] array,int x, int y){
            boolean adjacent = false;
            int trueCount = 0;
            if(array[x-1][y-1] == 0) trueCount++; //topleft
            if(array[x-1][y] == 0) trueCount++; //top
            if(array[x-1][y+1] == 0) trueCount++;//topright
            if(array[x][y+1] == 0) trueCount++;//right
            if(array[x][y-1] == 0) trueCount++;//left
            if(array[x+1][y-1] == 0) trueCount++;//bottomleft
            if(array[x+1][y] == 0) trueCount++;//bottom
            if(array[x+1][y+1] == 0) trueCount++; //bottomright

            if (trueCount == 8)
            {
                adjacent = true;
            }
            return adjacent;
        }*/

标签: java

解决方案


Your problem description has the answer baked into it already. You want any move from (a,b) to (c,d) to be legal if the distance between a and c, and b and d, is zero or one. So if you see Math.abs(a-c)>1, that's an illegal move. So: have the current position stored in some variables, and compare them to the desired new location:

public static void main(String[] args)
{
    Board b = new Board(6, 10);
    try {
      b.tryMove(6,1);
    } catch(IllegalMoveException e) {
      // do whatever you need to do to inform the user that move is illegal
    }
}

With the Board class responsible for tracking coordinates:

class Board {
  protected int cur_x, cur_y, rows, cols;

  public Board(int rows, int cols) {
    this.rows = rows;
    this.cols = cols;
    this.setRandomPosition();
  }

  public void setRandomPosition() {
    cur_x = (int) Math.round(Math.random() * cols);
    cur_y = (int) Math.round(Math.random() * rows);
  }

  public void tryMove(int x, int y) throws IllegalMoveException {
    if (Math.abs(x-cur_x)>1 || Math.abs(y-cur_y)>1) {
      throw new IllegalMoveException(...);
    }
    // bounds check omitted here, but: ensure that
    // 0<=x<cols and 0<=y<rows, otherwise throw an
    // IllegalMoveException as well.
    cur_x = x;
    cur_y = y;
  }
  // with getters for the current x and y, etc.
}

推荐阅读