【问题标题】:2d array for tictactoe class being problematictictactoe 类的二维数组有问题
【发布时间】:2013-04-17 03:48:02
【问题描述】:

对于这个程序,我正在创建棋盘,这将成为我必须构建的 tictactoe 游戏的基础。这将是一个非常简单的程序,只使用 java 的基础知识。目前我无法让我的 setX() 和 setO() 方法正常工作。这是我在构建两种方法时得到的确切文本。

public void setX(int index) 和 public void setO(int index) 如果 index 在 board 和 BLANK 的范围内,此方法应将 X 或 O 分配给 board 中的适当索引(setX 应该放置一个 X;setO,一个 O)。请记住 TicTacToe 传递 1-9 范围内的数字,但棋盘中的索引小于 1 那个(在 0-8 范围内)。

我的具体问题是如何将“x”和“o”设置为正确的索引。当我运行我的单元测试文件(我正在使用 bluejay)时,它会通过这些方法使所有测试用例都失败。它将返回该数组首先在 element[0][2](或 [1][1] 或我的数组中的任何组合)处有所不同;期望(或)但是是:

public class Board
{
    // instance variables - replace the example below with your own
    public int SIZE = 3;
    private char BLANK = 'N';
    private char [][] board;

    /**
     * Constructor for objects of class Board, intializes all elements to BLANK, 
     * creates board with SIZE elements
     */
  public Board()
    {
        // initialise instance variables
        board = new char[SIZE][SIZE];
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                board[i][j] = BLANK;
        }
    }
   }
   /**
    * this method returns the board
    */
   public char [][] getBoard(){

       return board;
    }
    /**
     * prints the elements of the array board in a 3x3 grid
     */
   public void display(){
         for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                System.out.print(board[i][j]);
        }
    }
   }
    /**
     * This method assigns X to the appropriate index in board
     */
   public void setX(int index){
       index = index - 1;
          for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){

                if((BLANK <= index) && (index <= board[i][j])){
                    board[i][j] = 'X';
                }
        }
    }

    }
    /**
    * This method assigns O to the appropriate index in board
    */
   public void setO(int index){

       for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){

                if((BLANK <= index) && (index <= board[i][j])){
                    board[i][j] = 'O';
                }
        }
    }
    }
   /**
    * This method returns true if the index is not occupied by an X or O
    */
    public boolean isAvailable(int index){
        boolean available = false;
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board.length; j++){
                board[i][j] = BLANK;
               available = true;
        }
    }
        return available;
    }
}

【问题讨论】:

  • 如果你要使用二维数组,你应该让 setX 和 setO 为第一个和第二个索引取 2 个变量。

标签: java methods multidimensional-array tic-tac-toe


【解决方案1】:

首先,我想知道为什么您甚至需要一个二维数组。为什么不将棋盘表示为一个九元素的单个数组?

除此之外,将 1-9 位置索引转换为适当的二维索引的方法是:

int i = (index - 1) / 3;
int j = (index - 1) % 3;

然后为那一对索引处理board[i][j]。您的 setXsetO 方法不应循环。

【讨论】:

  • 这太完美了。太感谢了。我使用了一个二维数组,因为在编写我的课程以找到我的获胜者时会更容易。最初做一维会更容易,但我正在寻找更远的路。
【解决方案2】:

想象您的板子看起来像这样行标题是您的 y“坐标”,列标题是您的 x“坐标”,当您调用 setXsetO 时,表格内容是您的索引:

  | 0 | 1 | 2
--+---+---+---
0 | 0 | 1 | 2
--+---+---+---
1 | 3 | 4 | 5
--+---+---+---
2 | 6 | 7 | 8

现在您只需为二维数组计算 x 和 y 值如下:

y = index / 3;
x = index % 3;

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2019-08-08
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多