如何在Tictaoe(Java)中确定获胜者
在我的家庭作业中,我必须制作一个方法来检查TicTacToeArray变量并确定是否有人赢了。特别是,如果有任何列、行或专业,都会有赢家 游戏板的对角线完全由Xs或Os填充。当检测到赢家时 scoreTTT()应将winner变量设置为“X”或“O”,具体取决于谁赢了。如果X或O均未获胜,则获胜变量应包含“*” 到目前为止,我有: 公共类Tictatcoe{如何在Tictaoe(Java)中确定获胜者,java,Java,在我的家庭作业中,我必须制作一个方法来检查TicTacToeArray变量并确定是否有人赢了。特别是,如果有任何列、行或专业,都会有赢家 游戏板的对角线完全由Xs或Os填充。当检测到赢家时 scoreTTT()应将winner变量设置为“X”或“O”,具体取决于谁赢了。如果X或O均未获胜,则获胜变量应包含“*” 到目前为止,我有: 公共类Tictatcoe{ public static void main(String[] args){ } //state variables stati
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';
publicstaticvoidmain(字符串[]args){
}
//状态变量
静态字符[][]TicTacToeArray;//游戏板
static int step=0;//当前步骤号
静态字符赢家='*';//谁赢了(X/O/*)*=无人
静态字符播放器='X';//轮到谁了(X/O)*=无人
//创建大小为n x n的游戏板,并将状态变量重置为
//新游戏的初始条件。
公共静态无效起始TTTT(int n){
TicTacToeArray=新字符[n][n];
对于(int i=0;i
}
publicstaticvoiddisplayttt(){
字符串行;
int n=TicTacToeArray.length;
//现在我正在划船
row=“Column”;
系统输出打印项次(行);
//第1行
行=”;
对于(inti=0;i你是对的,你有三种不同的情况
首先检查所有列,然后检查所有行,然后检查对角线
第一个应该是循环(对于每一列,检查每一行)。
第二个应该与第一个类似(只是反转列和行)。
第三种情况很简单,您只有两个选项,从左上到右下和从右上到左下。只需确保所有对应的框都相等即可
我会给出更具体的细节,但这是家庭作业。char[]board=new char[]{
char[][] board = new char[][] {
{'x', 'o', 'x'},
{'x', 'o', 'o'},
{'o', 'o', 'x'}
};
//check rows/cols
for (int i = 0; i < board.length; i++) {
String row = "";
String col = "";
for (int j = 0; j < board.length; j++) {
row += board[i][j];
col += board[j][i];
}
System.out.println("Row: " + row);
System.out.println("Col: " + col);
}
//check diagonals -- the logic in this loop could be folded into the previous one
String diag1 = ""; //top-left to bottom-right
String diag2 = ""; //bottom-left to top-right
for (int i = 0; i < board.length; i++) {
diag1 += board[i][i];
diag2 += board[board.length-i-1][board.length-i-1];
}
System.out.println("Diag1: " + diag1);
System.out.println("Diag2: " + diag2);
{'x','o','x'},
{'x','o','o'},
{'o','o','x'}
};
//检查行/列
对于(int i=0;i
你必须弄清楚如何检查获胜者
//for(int i=0; i < TicTacToeArray.length; i++){
//for(int j =0; j < TicTacToeArray.length; j++)
//if (TicTacToeArray[i][j] == TicTacToeArray[i][j+1])
}
char[][] board = new char[][] {
{'x', 'o', 'x'},
{'x', 'o', 'o'},
{'o', 'o', 'x'}
};
//check rows/cols
for (int i = 0; i < board.length; i++) {
String row = "";
String col = "";
for (int j = 0; j < board.length; j++) {
row += board[i][j];
col += board[j][i];
}
System.out.println("Row: " + row);
System.out.println("Col: " + col);
}
//check diagonals -- the logic in this loop could be folded into the previous one
String diag1 = ""; //top-left to bottom-right
String diag2 = ""; //bottom-left to top-right
for (int i = 0; i < board.length; i++) {
diag1 += board[i][i];
diag2 += board[board.length-i-1][board.length-i-1];
}
System.out.println("Diag1: " + diag1);
System.out.println("Diag2: " + diag2);