Java程序使Tic Tac Toe无法正常工作

时间:2013-11-26 04:16:53

标签: java methods private-methods

对于我的编程课程,我应该制作一个模拟tic tac toe游戏的程序。我的老师提供了所有的方法,并说我们不需要添加任何方法,也不需要任何方法,并告诉我们不要改变playGame方法,但不管我做什么,似乎在playGame之间存在某种脱节方法和processComputerMove / processHumanMove方法。每当我尝试运行程序时,似乎这些方法都被完全跳过,即使它们中没有任何东西,只是简单的打印语句。我想知道如何解决这个问题。到目前为止,这是我的(未完成和不工作)代码:

import java.util.*;

public class TicTacToe {

private static Scanner keyboard = new Scanner(System.in);
private static char[] board = new char[9];

// *******************************************************
public static void main(String[] args) {
    String answer;
    System.out.println("Welcome to Tic-Tac-Toe!!");
    System.out.print("Do you want to play a game? (Y/N) ");
    answer = keyboard.next();
        if (answer.charAt(0) == 'Y' || answer.charAt(0) == 'y' ) {
            playGame();
        } else if (answer.charAt(0) == 'N' || answer.charAt(0) == 'n'){
            System.out.println("Goodbye!");
        }   
}

// *******************************************************
private static void playGame() {

    char currentPlayer = determineFirstPlayer();
    System.out.println("The computer is X, the human is O.\n");

    initializeGameBoard();

    while (!isDone()) {
        if (currentPlayer == 'c' || currentPlayer == 'C') { // the computer
            processComputerMove();
            currentPlayer = 'h'; // the human
        } else {
            displayGameBoard();
            processHumanMove();
            currentPlayer = 'c'; // the computer
        }
    }

    displayGameBoard();
    System.out.println("Game over!");
}

public static char determineFirstPlayer() {
    System.out.print("Who would you like to make the first move? (h = human/c = computer) ");
     String response = keyboard.next();
     char player = response.charAt(0);

    return player; // return 'c' or 'h' as appropriate
}


// *******************************************************
private static void initializeGameBoard() {
    String playBoard = "   |   |   \n---+---+---\n   |   |   \n---+---+---\n   |   |   ";
    board[0] = playBoard.charAt(2);
    board[1] = playBoard.charAt(6);
    board[2] = playBoard.charAt(10);
    board[3] = playBoard.charAt(13);
    board[4] = playBoard.charAt(17);
    board[5] = playBoard.charAt(21);
    board[6] = playBoard.charAt(24);
    board[7] = playBoard.charAt(28);
    board[8] = playBoard.charAt(32);

}

// *******************************************************
private static void processComputerMove() {
    Random rand = new Random();
    int array = rand.nextInt(8);
    int comp = rand.nextInt(8);
    if (board[array] != 'X' || board[array] != 'O') {
        board[array] = 'X';
        System.out.println("The computer chooses cell "+board[array]+".");
    } else  {
        board[array] = board[comp];
        board[comp] = 'X';
    }

}


// *******************************************************
private static void processHumanMove() {
    System.out.println("Enter an empty position number (0-8) ");
    int position = keyboard.nextInt();
    if (board[position] != 'X' || board[position] != 'O') {
        board[position] = 'O';
    } else {
        System.out.println("Error! Must enter an empty position from 0-9.");
    }

}

// *******************************************************
private static void displayGameBoard() {
    System.out.println(" "+board[0]+" | "+board[1]+" | "+board[2]+" \n---+---+---\n "+board[3]+" | "+board[4]+" | "+board[5]+" \n---+---+---\n "+board[6]+" | "+board[7]+" | "+board[8]+" ");

}

// *******************************************************
private static boolean isDone() {
    isComputerWin();
    isHumanWin();
    isTie();
    if (isComputerWin()) {
        return true;
    } else if(isHumanWin()) {
        return true;
    } else if (isTie()) {
        return true;
    } else {
        return false;
    }
}

// *******************************************************
private static boolean isComputerWin() {
    if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') || (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || 
            (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') || 
            (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || 
            (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || (board[6] == 'X' && board[4] == 'X' && board[2] == 'X')){
        return true;
    } else {
        return false; // return true or false as appropriate
    }
}

// *******************************************************
private static boolean isHumanWin() {
    if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') || (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || 
            (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') || 
            (board[1] == 'O' && board[4] == 'X' && board[7] == 'O') || (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || 
            (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || (board[6] == 'O' && board[4] == 'O' && board[2] == 'O')){
        return true;
    } else {
        return false; // return true or false as appropriate
    }

}

// *******************************************************
private static boolean isTie() {
    if (!((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') || (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || 
            (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') || 
            (board[1] == 'O' && board[4] == 'X' && board[7] == 'O') || (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || 
            (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || (board[6] == 'O' && board[4] == 'O' && board[2] == 'O')) || 
            !((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') || (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || 
            (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') || 
            (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || 
            (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || (board[6] == 'X' && board[4] == 'X' && board[2] == 'X'))) {
        return true;
    } else {
        return false;
    }

}
}
编辑:拍摄,我在打字之前不小心贴了。我认为错误在processComputerMove / processHumanMove方法中,因为即使我只在那里放置print语句,当我运行程序时也没有任何显示。

1 个答案:

答案 0 :(得分:1)

您的isTie()方法是罪魁祸首之一。

使用调试器查看它,我发现每次检查“绑定”条件的布尔语句都是true的评估。

是的,我的意思是这个怪物声明:

(!((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') || (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') ||
            (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||
            (board[1] == 'O' && board[4] == 'X' && board[7] == 'O') || (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') ||
            (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || (board[6] == 'O' && board[4] == 'O' && board[2] == 'O'))

实际评估是错误的,但是由于你否定它,你将值翻转为true,从而使整个表达式短路,导致(最终)返回true

我的直觉告诉我,你对领带的定义是偏斜的 - 不是一个领带更容易定义为既不是人类胜利,也不是计算机胜利?

如果我们减少isDone()方法只查看isHumanWinisComputerWin(以及摆脱顶部无用的方法调用),并删除不必要的{ {1}}方法,我们得到(大多数)可玩的Tic-Tac-Toe游戏。它有点向第四步迈进,但这对读者来说是一种练习。

isTie