gpt4 book ai didi

Java Tic Tac Toe 获胜条件

转载 作者:行者123 更新时间:2023-12-01 16:45:43 26 4
gpt4 key购买 nike

我目前正在做一项任务,即创建一个 Tic Tac Toe 游戏。我已经做到了玩家可以在棋盘上放置标记、绘制标记并随后切换回合。但是,只有当玩家将其标记放在左上角(第一个)字段时,我检查是否存在获胜条件似乎才评估为 true。

我一直在尝试与 this answer 合作让它正确,但到目前为止还没有运气,我不想只是复制那个答案,但知道我的编码有缺陷。调试器没有给我太多信息。我怀疑这与playerTurn()的设置方式有关。我缺少什么?任何帮助将不胜感激。

这是我的代码:

import java.util.Scanner;

public class tictactoe {

private static char[][] board;
private char currentPlayer = 'X';

public static void main(String[] args) {
board = new char [3][3];
tictactoe game = new tictactoe();

game.welcomeAndInstruct();
game.boardInit();
game.playerTurn();

}

public void playerTurn() {
do {
Scanner input = new Scanner(System.in);
System.out.print("Player " + currentPlayer + "'s turn: ");
int move = input.nextInt();
if (move == 1) {
placeMark(0, 0);
drawBoard();
} else if (move == 2) {
placeMark(0, 1);
drawBoard();
} else if (move == 3) {
placeMark(0, 2);
drawBoard();
} else if (move == 4) {
placeMark(1, 0);
drawBoard();
} else if (move == 5) {
placeMark(1, 1);
drawBoard();
} else if (move == 6) {
placeMark(1, 2);
drawBoard();
} else if (move == 7) {
placeMark(2, 0);
drawBoard();
} else if (move == 8) {
placeMark(2, 1);
drawBoard();
} else if (move == 9) {
placeMark(2, 2);
drawBoard();
} else {
System.out.println("Invalid, choose a number between 1-9");
continue;
}

if (checkForWin()) {
System.out.println("Player " + currentPlayer + " has won!");
break;
}

if (isBoardFull()) {
System.out.println("It's a tie!");
break;
}
changePlayer();
} while (true);

}

//Empties the board by replacing all X/O with -
public void boardInit() {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = '-';
}
}
}

//Draws the board in it's current state
public void drawBoard() {
System.out.println("+---+---+---+");
for (int r = 0; r < 3; r++) {
System.out.print("| ");
for (int c = 0; c < 3; c++) {
System.out.print(board[r][c] + " | ");
}
System.out.println();
System.out.println("+---+---+---+");
}
System.out.println("\n-------------------------------------\n");

}

//Place's the current players mark in the indicated position
public void placeMark(int row, int col) {
if ((row >= 0) && (row < 3)) {
if ((col >= 0) && (col < 3)) {
if (board[row][col] == '-') {
board[row][col] = currentPlayer;
}
}
}
}



//switches players
public char changePlayer() {
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else
currentPlayer = 'X';
return currentPlayer;
}


//checks whether the board is full or not
public boolean isBoardFull() {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '-') {
return false;
}
}
}
return true;
}

//checks whether a win-condition exists or not
public boolean checkForWin() {
//loops through rows checking if win-condition exists
for (int r = 0; r < 3; r++) {
if (board[r][0] == board[r][1] && board[r][1] == board[r][2] && board[r][0] != '-')
return true;
}
//loops through columns checking if win-condition exists
for (int c = 0; c < 3; c++) {
if (board[0][c] == board[1][c] && board[1][c] == board[2][c] && board[0][c] != '-' )
return true;
}
//checks diagonals for win-condition
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-')
return true;

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][0] != '-')
return true;

return false;
}

// prints the instructions at the start of the game
public void welcomeAndInstruct() {
System.out.println("Welcome to Tic Tac Toe, a 2 player game for console.");
System.out.println("Two players take turns in marking the squares on a 3 x 3 grid.");
System.out.println("The player who succeeds in placing three of their marks in a horizontal, vertical or diagonal row wins the game.");
System.out.println("To select a square on the grid by entering it's corresponding number when prompted, as shown below:");
System.out.println();

board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';

drawBoard();

}
}

最佳答案

在:

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][0] ! = '-')

而不是board[0][0],请检查board[0][2]

关于Java Tic Tac Toe 获胜条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711766/

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