导入java.util.Scanner;
公共类游戏{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int SIDE = 3;
char [][] grid = new char[SIDE][SIDE];
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
grid [row][col] = '_';
}
}
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
System.out.printf("%3c", grid [row][col]);
}
System.out.printf("%n");
}
boolean playing =true;
while(playing){
System.out.println ("Player1: Please enter coordinates for your move");
int row1 = input.nextInt();
int col1 = input.nextInt();
grid[row1-1][col1-1] = 'x';
if (GameOver(row1-1, col1-1)) {
playing = false;
System.out.println("Game Over");
}
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
System.out.printf("%3c", grid [row][col]);
}
System.out.printf("%n");
}
System.out.println ("Player 2: Please enter coordinates for your move");
int row2 = input.nextInt();
int col2 = input.nextInt();
grid[row2-1][col2-1] = 'O';
if (GameOver(row1-1, col1-1)) {
playing = false;
System.out.println("Game Over");
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
System.out.printf("%3c", grid [row][col]);
}
System.out.printf("%n");
}
} }
}
public static boolean GameOver(int rMove, int cMove) {
//rows and colums win
int SIDE = 3;
char [][] grid = new char[SIDE][SIDE];
if(grid[0][cMove] == grid[1][cMove]
&& grid[0][cMove] == grid[2][cMove])
return true;
if(grid[rMove][0] == grid[rMove][1]
&& grid[rMove][0] == grid[rMove][2])
return true;
//Check diagonal victory
if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2]
&& grid[1][1] != '_')
return true;
if (grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0]
&& grid[1][1] != '_')
return true;
return false;
}
} 因此,基本上,我想尝试在2位人类玩家之间做一个井字游戏,作为初学者的任务。我在控制台中运行程序时遇到了2个问题。第一个问题是已经建立了循环游戏,以便玩家可以接连进行多次走法,这只是让每个玩家只有一次走法。另一个问题是我设置了一组规则,因此,如果您连续获得3条,游戏将结束。问题在于游戏在每次执行之后都说“游戏”结束