【问题标题】:Java Tic Tac Toe win-conditionJava井字游戏获胜条件
【发布时间】: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


【解决方案1】:

在:

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

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

【讨论】:

  • @Woodman 始终注意 Copy+Pasta 错误! (我试图给你我之前评论的提示)
  • 谢谢。您的评论还提醒我更深入地探索 Eclipse 调试器的功能。
【解决方案2】:

好的,所以问题已得到解答 - 我会将此作为评论,但我没有代表。

简单地玩游戏,我做的第一件事自然是在盒子 1 中放了一个 O,游戏说玩家 O 赢了。立即,您可以看到这个框导致了一个问题(不一定是唯一的问题,请注意)。

从那里,很容易查看使用此方块的每个条件,并检查任何不合理的地方。很明显,从左下角到右上角的对角线都是一样的,而且[0,0]不是'-',和你最后的检查状态完全一样。

忠告 - 提问时尽量做到更具体、更准确。来自

但是,我检查是否存在获胜条件似乎仅在玩家将他的标记放在左上角(第一个)字段时才评估为真。

我(错误地)假设只有在存在涉及左上方块的获胜条件时才将游戏标记为获胜。事实上,check-if-win 函数在此之前错误地返回了 true way。更准确的问题描述应该是“在左上角放置一个标记有时会过早地错误地导致游戏结束”

【讨论】:

  • 感谢您的反馈,我将尝试在进一步的问题中实现这一点。我还在学习如何像程序员一样思考,你的回答对我很有价值。
  • 完全没有问题。我也是该网站的新手,所以请注意我的话!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多