对于我的作业,我必须制作一个方法来检查TicTacToeArray变量并确定是否有人赢了。特别是,如果有任何列,行或专业,则有赢家 游戏板的对角线完全由Xs或Os填充。当检测到胜利者时 scoreTTT()应将获胜者变量设置为“X”或“O”,具体取决于谁赢了。如果X或O都没有获胜,则获胜者变量应保留“*”。
到目前为止,我有这个:
公共课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 static void 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.我只是我不确定如何让循环遍历整个行/列/对角线。 谢谢你的帮助。
答案 0 :(得分:2)
你是对的,你有三种不同的情况。
首先检查所有列,然后检查所有行,然后检查对角线。
第一个应该是一个循环(对于每一列,检查每一行)。 第二个应该像第一个(只是反向列和行)。 第三种情况很简单,你只有两个选项,左上角到右下角,右上角到左下角。只需确保所有相应的框都相同。
我会提供更具体的细节,但这是作业。
答案 1 :(得分:0)
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);
你必须弄清楚如何检查获胜者。