我目前正在从事一项任务,即创建一个Tic Tac Toe游戏。我达到了让玩家可以在棋盘上放置自己的标记,进行绘制并在之后切换转弯的地步。但是,当玩家将标记放在左上方(第1个)字段中时,我检查是否存在获胜条件的评估似乎为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();
}
}
答案 0 :(得分:4)
在:
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][0] != '-')
而不是board[0][0]
选中board[0][2]
。
答案 1 :(得分:1)
好,所以问题已经回答-我将其保留为评论,但没有代表。
通过简单地玩游戏,我自然要做的第一件事就是在方框1中放置一个O,然后游戏说玩家O赢了。马上,您会看到此框导致了问题(记住,不一定是唯一的问题)。
从那里开始,很容易查看利用此正方形的所有条件,并检查是否有任何不合理的地方。很显然,从左下角到右上角的对角线都一样,并且[0,0]并非'-',与最终检查状态完全相同。
忠告-提出问题时,请尽量具体和准确。来自
但是,当玩家将标记放在左上方(第1个)字段中时,我对是否存在获胜条件的检查似乎评估为true。
我(错误地)认为,只有在涉及左上角正方形的获胜条件下,游戏才被标记为获胜。实际上,check-if-win函数在此之前错误地返回了真实的 way 。更为准确的问题描述是“有时过早并错误地在游戏的左上角放置一个标记,从而导致游戏结束”