【发布时间】:2018-08-06 16:13:20
【问题描述】:
我目前正在执行一项任务,即创建一个井字游戏。我已经到了玩家可以在棋盘上放置标记,绘制并在之后切换回合的地步。但是,我检查是否存在获胜条件似乎仅在玩家将他的标记放在左上角(第一个)字段时评估为真。
我一直在尝试与 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();
}
}
【问题讨论】:
-
调试器绝对应该让你看到你的逻辑缺陷在哪里。特别是在您的
checkForWin()方法下,您的最后一个if condition
标签: java tic-tac-toe