gpt4 book ai didi

java - (Java) 使用二维数组的 Tic-Tac-Toe 游戏

转载 作者:行者123 更新时间:2023-11-30 07:58:55 25 4
gpt4 key购买 nike

在类里面,我们的作业是创建一个二维数组并围绕它创建一个井字游戏。除了当整个棋盘已满并且游戏是平局时显示之外,我已经完成了所有操作。我尝试了一些方法,但没有找到解决方案,我需要一些帮助...这是我的代码:

import java.util.Scanner;

public class TicTacToe {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row, column;
char player = 'X';

//create 2 dimensional array for tic tac toe board
char[][] board = new char[3][3];
char ch = '1';
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
board[i][j] = ch++;
}
}
displayBoard(board);
while(!winner(board) == true){

//get input for row/column
System.out.println("Enter a row and column (0, 1, or 2); for player " + player + ":");
row = in.nextInt();
column = in.nextInt();

//occupied
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
//place the X
board[row][column] = player;
displayBoard(board);

if (winner(board)){
System.out.println("Player " + player + " is the winner!");
}

//time to swap players after each go.
if (player == 'O') {
player = 'X';

}
else {
player = 'O';
}
if (winner(board) == false) {
System.out.println("The game is a draw. Please try again.");

}

}

private static void displayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == board[i].length - 1) System.out.print(board[i][j]);
else System.out.print( board[i][j] + " | ");
}
System.out.println();
}


}
//method to determine whether there is an x or an o in the spot
public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}

return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
}

我想要输出说板已满,但我什么也没得到。这是我输出的最后一行,正如您所看到的,我当前的策略不起作用,因为它继续要求输入。 -->

输入行和列(0、1或2);对于玩家 X:2 0X |哦| X哦|哦| XX | X |氧输入行和列(0、1 或 2);对于玩家 O:

最佳答案

首先:

 while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}

这将创建一个无限循环,因为 rowcolumn 不应更改,您应该要求新的输入!

还有

public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}

一旦您点击“O”或“X”,您将以错误退出方法(没有获胜者)

您可能想要检查的是是否每个位置都被占用

public static Boolean winner(char[][] board){
//Boolean which is true until there is a empty spot
boolean occupied = true;
//loop and check if there is empty space or if its a draw
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
//Check if spot is not 'O' or not 'X' => empty
if (board[i][j] != 'O' || board[i][j] != 'X') {
occupied = false;
}
}
}
if(occupied)
return false;
//Check if someone won
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}

现在将检查是否有获胜者或平局

Occupied == true == tie == return false

Winner == return true

但是你有三种状态:

  • 获胜
  • 领带
  • 未完成

使用更改后的方法,在获胜之前您将无法完成游戏。

原因:

 while(!winner(board) == true)

这使得游戏在没有获胜者的情况下继续运行(winner() 将为 false,因为所有内容都已被占用或没有获胜者)

while(!false==true) => while(true) 

您可以编写类似于 Winner 的方法,但它只检查棋盘上是否有空位:

public static Boolean hasEmptySpot(char[][] board){
//loop and check if there is empty space
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
}
}
}
return false;
}

//New code
while(hasEmptySpot(board) || !winner(board)){
//Your code for the game here
....
}

当没有空位时,这将结束游戏当你完成游戏后,你可以调用winner(board),如果你平局或获胜,它就会返回!

通过创建hasEmptySpot(),您可以将获胜者方法更改为

public static Boolean winner(char[][] board){
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
为什么?因为您完成了游戏并且知道只有两种可能的结果:获胜或平局。

希望这对你有一点帮助。

编辑我自己也有逻辑错误!

第一个错误:您仍然需要在游戏运行时检查是否有获胜者忘记了这一点!

while(hasEmptySpot(board) || !winner(board)){
}

现在,当有获胜者或没有空位时,这将退出游戏循环

第二个错误:在 hasEmptySpot() 中

 if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;

不是

 if (board[i][j] != 'O' || board[i][j] != 'X') {
return true;

在上面的示例中修复了它。

对于给您带来的不便,我们深表歉意!

关于java - (Java) 使用二维数组的 Tic-Tac-Toe 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945881/

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