gpt4 book ai didi

java - 如何确定 TicTacToe 中的获胜者 (Java)

转载 作者:行者123 更新时间:2023-11-30 04:27:47 24 4
gpt4 key购买 nike

对于我的家庭作业,我必须创建一个方法来检查 TicTacToeArray 变量并确定是否有人获胜。特别是,如果任何列、行或主要内容,则有一个获胜者游戏板的对角线完全被 X 或 Os 填满。当检测到获胜者时ScoreTTT() 应将获胜者变量设置为“X”或“O”,具体取决于谁获胜。如果 X 或 O 都没有获胜,则 Winner 变量应保存“*”。

到目前为止,我有这个:

公开课 TicTacToe{

public static void main(String[] args){



}

//state variables
static char[][] TicTacToeArray; //the game board
static int step = 0; //the current step number
static char winner = '*'; //who has won (X/O/*) *=nobody
static char player = 'X'; //whose turn it is (X/O) *=nobody

//Creates a game board of size n x n and resets state variables to
//their initial conditions for a new game.
public static void startTTT(int n){
TicTacToeArray = new char [n][n];
for(int i = 0; i < n; i++){
for(int j=0; j < n; j++){
TicTacToeArray [i][j] = '*';
}
}

step = 0;
winner = '*';
player = 'X';

}

public static void displayTTT(){
String row;

int n = TicTacToeArray.length;

//now I'm priting row0
row = " Column";
System.out.println(row);

//row 1
row = " ";
for (int i=0; i<n; i++){
row = row + " "+ i;

}
row = row + " TicTacTow";
System.out.println(row);

//row 2
row = " +";
for (int i=0; i<n; i++){
row = row + "--";

}

System.out.println(row +" Step = " + step);

//row 3
row = " 0 |" ;
for (int i=0; i<n; i++){
row = row + " " + TicTacToeArray [0][i];
}
System.out.println(row + " Player = " + player);

//row 4
row = "Row 1 |";
for (int i=0; i<n; i++){
row = row + " " + TicTacToeArray[1][i];
}
System.out.println(row);


//row 5
row = "";
for( int i=2;i<n;i++){
row = " " + i + " |" ;
for( int j=0; j < n; j++){
row += " " + TicTacToeArray[i][j];
if (j == n)
System.out.println(row);

}
if(i == n-1)
row += " Winner = " + winner;
System.out.println(row);
}



}

//Updates a position on the game board, increments the step counter,
//and toggles the player from X to O (or vica versa). This method should
//test for invalid input (see assignment document) before changing
//the game state. If no error is encountered, it performs the update
//and returns true. Otherwise it returns false.
public static boolean updateTTT(char sym, int row, int col){
if (sym != 'X' && sym != 'O'){
return false;
}
if(row < 0 || col < 0 || row >= TicTacToeArray.length || col >= TicTacToeArray.length){
return false;
}



if (TicTacToeArray[row][col] == '*')
TicTacToeArray [row][col] = sym;
else
return false;

// toggle player
for(;;){
if (player =='X'){
player = 'O';
break;
}
if(player == 'O'){
player = 'X';
break;
}
}
//inc step count
step +=1;


return true;
}

//(这已被注释掉/到目前为止我对此方法的了解,其余代码应该可以工作) //公共(public)静态无效scoreTTT(){

//for(int i=0; i < TicTacToeArray.length; i++){
//for(int j =0; j < TicTacToeArray.length; j++)
//if (TicTacToeArray[i][j] == TicTacToeArray[i][j+1])




}

我认为我需要制作 3 个不同的嵌套循环,检查两条对角线,然后检查行/列,它们还必须检查任何数组大小的完整行/列,例如 4 x 4。我只是不知道如何让循环遍历整个行/列/对角线。谢谢你的帮助。

最佳答案

你是对的,你有三种不同的情况。

首先检查所有列,然后检查所有行,最后检查对角线。

第一个应该是一个循环(对于每一列,检查每一行)。第二个应该与第一个类似(只是反转列和行)。第三种情况很简单,你只有两个选择,从左上角到右下角和从右上角到左下角。只需确保所有相应的框都相等即可。

我会提供更具体的细节,但这是家庭作业。

关于java - 如何确定 TicTacToe 中的获胜者 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15439398/

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