阵列编程 - 适用于n个玩家的Tic Tac Toe nxn板

时间:2013-12-04 14:29:49

标签: java arrays interface

这段代码是一个大型项目的一部分,它为nxn板上的n个玩家制作了一个游戏,它代表了tic tac toe游戏的主板。

/** represents a tic tac toe board of a given size */
public class TTTBoard {

/** 2-dimensional array representing the board
 * coordinates are counted from top-left (0,0) to bottom-right (size-1, size-1)
 * board[x][y] == 0   signifies free at position (x,y)
 * board[x][y] == i   for i > 0 signifies that Player i made a move on (x,y)
 */
private int[][] board;

/** size of the (quadratic) board */
private int size;

/** This private static integer i used to make a counter
 */
private static int movecount = 0;

/** constructor for creating a copy of the board
 * not needed in Part 1 - can be viewed as an example 
 */
public TTTBoard(TTTBoard original) {
        this.size = original.size;
        for (int y = 0; y < this.size; y++) {
            for (int x = 0; x < this.size; x++) {
                this.board[x][y] = original.board[x][y];
            }
        }
    }

/** constructor for creating an empty board for a given number of players */
public TTTBoard(int numPlayers) {
        this.size = numPlayers+1;
        this.board = new int[this.getSize()][this.getSize()];
    }

/** checks whether the board is free at the given position */
public boolean isFree(Coordinate c) {
       if (board[c.getX()][c.getY()] == 0) {
           return true;
       } else {
           return false;
           }
    }

/** returns the player that made a move on (x,y) or 0 if the positon is free */
public int getPlayer(Coordinate c) {
        if (isFree(c) == true){
            return board[c.getX()][c.getY()];
        }else{
            return 0;
        }
    }

/** record that a given player made a move at the given position
 * checks that the given positions is on the board
 * checks that the player number is valid 
 */

public void addMove(Coordinate c, int player) {
        if (c.checkBoundaries(size, size) == true && isFree(c) == true && player >= 0 && player <= size-1){
            board[c.getX()][c.getY()] = player;
            movecount++;
        } else {
         throw new IllegalArgumentException("Ivalid move");
        }
    }

/** returns true if, and only if, there are no more free positions on the board */
public boolean checkFull() {
        if (movecount == size*size) {
           return true;
        } else {
           return false;
            }
    }

/** returns 0 if no player has won (yet)
 * otherwise returns the number of the player that has three in a row
 */
public int checkWinning() {
        int b = 1;
        for(int a = 1; a < size-1; a++){
        if (board[a][b] == board[a+1][b] && board[a][b] == board[a-1][b]){
            return board[a][b];
        }else if(board[a][b] == board[a][b+1] && board[a][b] == board[a][b-1]){
            return board[a][b];
        }else if(board[a][b] == board[a+1][b-1] && board[a][b] == board[a-1][b+1]){
            return board[a][b];
        }else if(board[a][b] == board[a+1][b+1] && board[a][b] == board[a-1][b-1]){
            return board[a][b];
        }
        }
        for(int a = 1; a < size-1; a++){
            if (board[b][a] == board[b+1][a] && board[b][a] == board[b-1][a]){
                return board[b][a];
            }else if(board[b][a] == board[b][a+1] && board[b][a] == board[b][a-1]){
                return board[b][a];
            }else if(board[b][a] == board[b+1][a-1] && board[b][a] == board[b-1][a+1]){
                return board[b][a];
            }else if(board[b][a] == board[b+1][a+1] && board[b][a] == board[b-1][a-1]){
                return board[b][a];
            }
            }
        return 0;
    }

/** getter for size of the board */
public int getSize() {
        return this.size;
    }
}

由于我对运行孔程序的测试显示addMove存在问题,为什么我知道这是因为我没有得到错误按摩,但程序不能做我所做的事情:

public void addMove(Coordinate c, int player) {
        if (c.checkBoundaries(size, size) == true && isFree(c) == true && player >= 0 && player <= size-1){
            board[c.getX()][c.getY()] = player;
            movecount++;
        } else {
         throw new IllegalArgumentException("dumpass!");
        }
    }

我看起来代码没有记录正确的动作,我看不出我在这里做错了什么。 x和y坐标我的板来自另一个类Coordinate,这就是为什么我使用getter来获取它们。

1 个答案:

答案 0 :(得分:1)

我认为至少存在玩家和广场职业处理方面的问题。当一个正方形是免费的时,你将它设置为0(你的isFree(Coordinate c)getPlayer(Coordinate c)方法就可以了。)

但是,在addMove(Coordinate c, int player)方法中,您检查player >= 0 && player <= size - 1。然后,将board[c.getX()][c.getY()]设置为player。 如果有2个玩家,大小等于2,player必须介于0或1之间。当它是第一个玩家回合时,player变量肯定被设置为0,所以棋盘方块也设置为0 ,这意味着广场是免费的。 “占领者”与自由状态之间存在混淆。

我认为您应该将player >= 0 && player <= size - 1替换为player > 0 && player <= size。因此,一个自由方格值为0,第一个玩家占用的方格值为1,第二个玩家占用的方格值为2。

此外,正如@DanielJacobson在其评论中所述,您应该将isFree(c) == truec.checkBoundaries(size, size) == true替换为isFree(c)c.checkBoundaries(size, size)