java - tictactoe 在玩家之间循环
问题描述
我正在做一个 *n tictactoe,玩家可以选择棋盘的大小。但是,当我试图在 checkWinner() 方法中确定获胜者时,无论玩家 1 还是玩家 2 获胜,它总是打印出获胜者是玩家 1。playerTurn 的解析有什么问题吗?当我尝试在 CheckIfWinner 方法中打印 playerNumber 时,它会打印出 2 行 Player number is: 1 and Player number is : 2
private Matrix cells;
// '-' - not occupied,
// 'x' - taken up player 1,
// 'o' - taken up by player 2
char c = '-';
int n;
public TicTacToe() {
// prompt user to choose size of tictactoe
print("Please set the size of cells(e.g. 4 = 4x4 ): ");
Scanner sc = new Scanner(System.in);
String playerInput = sc.nextLine();
n = Integer.parseInt(playerInput);
println("n: " + n);
cells = new Matrix(n);
}
public void executeGame() {
// should allow the player to set the cells to other size (e.g. 4x4 or 5x5)
int turn = 0;
int playerTurn = 1;
int winner = 0;
cells.set(n - 1, n - 1, c);
while (turn < n * n) {
// while turn is less than n*n
// print cells
cells.display();
// get and set tick from Player 1
askGetAndAskPlayerForTick(playerTurn);
println("player turn is : " + playerTurn);
// check if there is a winner - if there is winner break from the while loop
winner = checkWinner();
if (winner != 0) {
break;
}
if (playerTurn == 1) {
playerTurn = 2;
} else {
playerTurn = 1;
}
turn++;
}
// display draw or the winner
cells.display();
if (winner != 0) {
println("The winner is : Player " + winner);
} else {
println("There is no winner");
}
}
private void askGetAndAskPlayerForTick(int playerNumber) {
// should check if the cell is taken up
// set player 1 to 'x' and player 2 to 'o'
if (playerNumber == 1) {
c = 'x';
} else if (playerNumber == 2) {
c = 'o';
}
print("Player " + playerNumber + "(" + c + ")" + ":");
Scanner keyboard = new Scanner(System.in);
String playerInput = keyboard.nextLine();
String[] inputs = playerInput.split(" ");
int row = Integer.parseInt(inputs[0]);
int col = Integer.parseInt(inputs[1]);
if (row - 1 < 0 || col - 1 < 0 || row >= n + 1 || col >= n + 1) {
println("The index is out of bounds of the matrix! Please enter a valid range!");
//askGetAndAskPlayerForTick(playerNumber);
} else if (cells.get(row - 1, col - 1) != '-') {
println("This position is filled! Please try again.");
//askGetAndAskPlayerForTick(playerNumber);
}
cells.set(row - 1, col - 1, c);
}
// 0 - no winner, 1 - player 1 is the winner, 2 - player 2 is the winner
private int checkWinner() {
int winner = 0;
// check for player 1
if (checkIfWinner(1)) {
winner = 1;
} else if (checkIfWinner(2)) {
winner = 2;
}
// check for player 2
return winner;
}
private boolean checkIfWinner(int playerNumber) {
boolean win = false;
println("player number is: " + playerNumber);
// check for row win
for (int i = 0; i < n; i++) {
boolean row = true;
char value = (char) cells.get(i, 0);
// check for unfilled space in a row
if (value == '-') {
row = false;
} else {
for (int j = 0; j < n; j++) {
if (cells.get(i, j) != value) {
row = false;
break;
}
}
}
if (row) {
win = true;
}
}
return win;
}
解决方案
推荐阅读
- c++ - 用cl.exe和/link.exe构建的Vscode C++调试代码——调试器不附加
- python - 令人困惑的python产量行为
- html - 无法读取未定义 Electronjs 错误的属性“发送”
- python - 为什么 Django 模板引擎将 myapp/myview 添加到我的 URL 中?
- python - 使用 Python 将 json 转换为 csv
- javascript - Touchable Opacity onPress 无法调用函数 - 范围问题?
- linux - 如何以csv格式存储ps命令输出?
- angular7 - PrimeNG 表列排序不起作用
- git - 在 GitHub 中,添加到底是什么?
- bash - 在 Git Bash for Windows 中退出后保持屏幕上的输出更少