gpt4 book ai didi

java - 数组编程 - N 个玩家的 Tic Tac Toe nxn 板

转载 作者:行者123 更新时间:2023-12-01 13:45:55 25 4
gpt4 key购买 nike

这段代码是一个更大项目的一部分,该项目为 nxn 棋盘上的 n 个玩家制作一个 ti tac toe 游戏,它代表了 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!");
}
}

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

最佳答案

我认为至少在玩家和广场职业处理上存在问题。当一个方 block 空闲时,您将其设置为 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 - 1player > 0 && player <= size 。因此,一个空闲的方格值(value) 0,第一个玩家占据的方格值(value) 1,第二个玩家占据的方格值(value) 2。

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

关于java - 数组编程 - N 个玩家的 Tic Tac Toe nxn 板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20377983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com