gpt4 book ai didi

java - 需要有关我的 tic tac toe 演示的帮助 无效的顶级声明

转载 作者:行者123 更新时间:2023-12-01 11:45:04 28 4
gpt4 key购买 nike

    public class TicTacToe
{
private char currentPlayer;
private char[][] board;

public TicTacToe()
{
board = new char [3][3];
currentPlayer = 'x';
startBoard();
}

public void startBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = '-';
}
}
}

public void makeBoard()
{
System.out.println("---------------");
for (int i = 0; i < 3; i++)
{
System.out.print("| ");
for (int j = 0; j < 3; j++)
{
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("---------------");
}
}

public boolean fullBoard()
{
boolean full = true;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] == '-')
{
full = false;
}
}
}
return full;
}

public boolean win()
{
return (rowWin() || columnWin() || diagWin());
}

private boolean rowWin()
{
for (int i = 0; i < 3; i++)
{
if (rowColumn(board[i][0], board[i][1], board[i][2]) == true)
{
return true;
}
}
return false;
}

private boolean columnWin()
{
for (int i = 0; i < 3; i++)
{
if (rowColumn(board[0][i], board[1][i], board[2][i]) == true)
{
return true;
}
}
return false;
}

private boolean diagWin()
{
return ((rowColumn(board[0][0], board[1][1], board[2][2]) == true) ||
(rowColumn(board[0][2], board[1][1], board[2][0]) == true));
}

private boolean rowColumn(char rc1, char rc2, char rc3)
{
return ((rc1 != '-') && (rc1 == rc2) && (rc2 == rc3));
}

public void playerChange()
{
if (currentPlayer == 'x')
{
currentPlayer = 'o';
}
else
{
currentPlayer = 'x';
}
}

public boolean placeMark(int row, int column)
{
if ((row >= 0) && (row < 3))
{
if ((column >= 0) && (column < 3))
{
if (board[row][column] == '-')
{
board[row][column] = currentPlayer;
return true;
}
}
}
return false;
}
}

public class TicTacToedemo
{
public static void main(String[] args)
{
TicTacToe demo = new TicTacToe();
demo.makeBoard();
if (demo.win())
System.out.println("Winner! Hooray!");
else if (demo.fullBoard())
System.out.println("Cat Scratch, Draw.");
demo.playerChange();
}
}

我不知道如何正确玩游戏,每次运行游戏时输入数字时都会收到错误代码。我这件事做错了什么?代码可以编译、运行并显示板,但是当我去放置我想要 x 或 o 的位置时,我收到错误代码“无效的顶级语句”

最佳答案

您必须使用 Scanner 类通过 import java.util.Scanner 进行玩家输入,然后存储输入。导入后,它看起来像这样:

Scanner sc = new Scanner(System.in);
int input = sc.nextInt();

并且您必须管理 sc.nextInt() 结果,在本例中是 input 变量。

关于java - 需要有关我的 tic tac toe 演示的帮助 无效的顶级声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215601/

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