我无法发现我的逻辑错误

时间:2014-11-21 05:17:59

标签: java arrays methods

当游戏执行时,玩家可以进入一个移动,但是它会陷入一个循环中,并且逻辑错误是在isWinner方法中但是我无法在不使用一堆的情况下弄清楚如何做到这一点多久的陈述?请帮忙

public class TicTacToe {

    public static char board[] = new char[10];

    public static void resetgame(){
        for (int x = 0; x < board.length; x++) 
            board[x] = '-';    // resets board to all -
    }

    public static void displayBoard(){
        for (int x = 1; x < board.length; x++){
            if (board[x] == '-')
                System.out.print(x + " ");
            else
                System.out.print(board[x] + " ");
            if (x % 3 == 0)
                System.out.println();
        }
    }

    public static boolean isSpotEmpty(int m){
        return (board[m] == '-');           
    }

    public static void getPlayerMove(){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your move (1-9): ");
        int move = input.nextInt();
        while (move < 1 || move > 9 || !isSpotEmpty(move)){  //validate move
            System.out.print("Move is not valid. Reenter move (1-9): ");
            move = input.nextInt();
        }
        board[move] = 'X';
    }

    public static void computerMove(){
        int move = ((int)(Math.random() * 9)+1);               
        while (isSpotEmpty(move)==false)
            move = ((int)(Math.random() * 9)+1);
        board[move] = 'O';        
    }

    public static boolean isWinner(char ch){

        for(int x = 1; x < 4; x++){
            while (board[x] != ch)
                return false;}
        for(int x = 4; x < 7; x++){
            while (board[x] != ch)
                return false;}
        for (int x = 7; x < board.length; x++){
            while (board[x] != ch)
                return false;}
        for (int x = 1; x < 8; x += 3){
            while (board[x] != ch)
                return false;}
        for (int x = 2; x < 9; x += 3){
            while (board[x] != ch)
                return false;}
        for (int x = 3; x < board.length; x += 3){
            while (board[x] != ch)
                return false;}
        for (int x = 1; x < board.length; x += 4){
            while (board[x] != ch)
                return false;}
        for (int x = 3; x < 8; x += 2){
            while (board[x] != ch)
                return false;}
        return true;
    }

    public static boolean isFull(){
        boolean draw = false;
        for (int x = 0; x < board.length; x++){
            while (board[x] != '-')
                draw = true;
        }
        return draw;
    }

    public static boolean isGameDone(){
        if (isWinner('X')){
            System.out.println("Player wins!"); 
            return true;
        }
        if (isWinner('O')){ 
            System.out.println("You suck! Computer wins!"); 
            return true;
        }
        if (isFull()){ 
            System.out.println("Draw!"); 
            return true;
        }
        return false;        
    }

    public static void tictactoe(){
        resetgame();
        displayBoard();
        while (true){ 
            getPlayerMove();           
            displayBoard();
            if (isGameDone()==true) return;           
            System.out.println("Computer's move!");                       
            computerMove();                       
            displayBoard();
            if (isGameDone()==true) return;
            System.out.println("Your move!");
        }
    }

    public static void main(String[] args) {

        tictactoe();       
    }
}

3 个答案:

答案 0 :(得分:0)

更新代码: 下面的代码经过测试和运行。

isWinner方法

public static boolean isWinner(char ch){
    boolean finishFlag = true;
    for(int x = 1; x < 4; x++){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for(int x = 4; x < 7; x++){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 7; x < board.length; x++){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 1; x < 8; x += 3){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 2; x < 9; x += 3){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 3; x < board.length; x += 3){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 1; x < board.length; x += 4){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    if (finishFlag)
        return finishFlag;
    finishFlag = true;
    for (int x = 3; x < 8; x += 2){
        if (board[x] != ch) {
            finishFlag = false;
            break;
        }
    }
    return finishFlag;
}

isFull方法

public static boolean isFull(){
    boolean draw = false;
    for (int x = 1; x < board.length; x++){
        if (board[x] == '-') {
            draw = false;
            break;
        } else {
            draw = true;
        }
    }
    return draw;
}

编辑:在isFull()

中将x初始化为1而不是0

答案 1 :(得分:0)

您需要重构isWinner()isFull()方法。你被困在一个无限循环中,因为你在检查第一个水平行后立即返回。如果这恰好是假的,那么其他循环都不会被执行。

我还没有完成完整的代码审核,但上面提到的错误可以通过以下代码修复。

boolean finishedFlag = true;
for(int x = 1; x < 4; x++){
    if (board[x] != ch){
        finishedFlag = false;
        break;
    }   
}
if(finishedFlag) return finishedFlag;

/*

...
Check similarly for all horizontal,vertical and diagonal
...

*/

return finishedFlag;

在评估所有可能性而不是在每个循环中之后,想法是在函数中返回一次。 以下条件有助于在任何成功结果的情况下立即返回:

if(finishedFlag) return finishedFlag;

答案 2 :(得分:0)

我在这里会有点迟钝。您的问题是由于误用while运算符造成的。让我们来看看其中一个问题:

while (board[x] != '-')
    draw = true;

当我们看循环时,我们想问:

  1. 在循环开始时我的条件是否正确?
  2. 循环期间有什么变化?
  3. 我的条件在循环结束时是否为真?
  4. 条件是board[x] != '-'。在循环开始时,board[x]必须不等于'-'

    while循环结束时,'-'肯定不会改变,因为它是一个常量字符串。

    board[x]会改变吗?让我们看一下while循环的主体。唯一要做的就是将true分配给drawx不会发生变化,board[x]也不会发生变化。

    那么,这个while循环循环多长时间?看一下使用while的其余代码,然后了解它们将如何运行。