Java - Tic Tac Toe - 为什么我的“切换播放器”方法不起作用?

时间:2015-05-12 03:11:58

标签: java

为什么我的switchPlayer()方法只能工作一次?该方法应该可以独立工作,当我把它放在我的playGame()方法之后,在placeMark()之后,它会连续工作,但这会弄乱我的currentPlayer变量,这会弄乱其他方法。我不想制作一个玩家对象,所以我假设我只是缺少一些简单的东西,比如放置switchPlayer()方法的位置。

我的结果是这样的:

  • 当前播放器1 ,放置您的商标。
  • [place mark,all good]
  • 当前播放器2 ,放置您的标记。
  • [place mark,all good]
  • 当前播放器2 ,放置您的标记。 //问题行..不会切换
  • [place mark,all good]

子类的相关代码:

public void playGame(){

    assignNames();

    assignMarks();

    explainGame();

    while(checkWin() == false && fullBoard() == false){

        getNum();

        placeMark();

        printBoard(); 

    }

    gameResult();

}


public void placeMark(){

    if(validNumber() == true){
        if(num == 1){
            board[0][0] = currentMark;
            switchPlayer();
        }
        if(num == 2){
            board[0][1] = currentMark;
            switchPlayer();
        }
        if(num == 3){
            board[0][2] = currentMark;
            switchPlayer();
        }
        if(num == 4){
            board[1][0] = currentMark;
            switchPlayer();
        }
        if(num == 5){
            board[1][1] = currentMark;
            switchPlayer();
        }
        if(num == 6){
            board[1][2] = currentMark;
            switchPlayer();
        }
        if(num == 7){
            board[2][0] = currentMark;    
            switchPlayer();
        }
        if(num == 8){
            board[2][1] = currentMark;
            switchPlayer();
        }
        if(num == 9){
            board[2][2] = currentMark;
            switchPlayer();
        }


    }
}




public void switchPlayer(){

    if(currentMark == 'X')
        currentMark = 'O';
        else{
            currentMark = 'X';
        }


    if(currentMark == 'O' && playerMark1 == 'O'){
        currentPlayer = playerName1;
    }
        else{
            currentPlayer = playerName2;
        }
} 

主类的相关代码:

    TicTacToe game = new TicTacToe();

    game.playGame();

好像我应该发布整个代码......所以这就是。

public class TicTacToe {
        private static char[][] board;
        Scanner input = new Scanner(System.in);
        int num;
        String entry;
        String playerName1;
        char playerMark1;
        String playerName2;
        char playerMark2;
        char currentMark;
        String currentPlayer;


public void playGame(){

    assignNames();

    assignMarks();

    explainGame();

    while(checkWin() == false && fullBoard() == false){

        getNum();

        placeMark();

        printBoard(); 

    }

    gameResult();

}


public TicTacToe(){

    board = new char[3][3];
    clearBoard();        
} // end TicTacToe()


public void clearBoard(){
    for(int a = 0; a < 3; a++){
        for(int b = 0; b < 3; b++){
            board[a][b] = ' ';
        }
    }
}// end clearBoard()


public void printBoard(){
    System.out.println("-------------");

    for (int a = 0; a < 3; a++) {
        System.out.print("| ");
        for (int b = 0; b < 3; b++) {
            System.out.print(board[a][b] + " | ");
        }
        System.out.println();
        System.out.println("-------------");
    }
}


    public boolean fullBoard() {

        boolean full = true;

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                if (board[i][j] == ' ') {

                full = false;
                }
            }
        }
        return full;
    }


    public void explainGame(){

    System.out.println("-------------");
    System.out.println("| 1 | 2 | 3 |");
    System.out.println("-------------");
    System.out.println("| 4 | 5 | 6 |");
    System.out.println("-------------");
    System.out.println("| 7 | 8 | 9 |");
    System.out.println("-------------");
    System.out.println("The above board shows which numbers you must enter to place your mark in their respective locations.\n");
    System.out.println("Randomly assigning player marks. . .");
    System.out.println(playerName1+" will be Player "+playerMark1+", and "+playerName2+" will be Player "+playerMark2 + ".");
    System.out.println("Player X will go first.  Press any key to begin playing.");
    entry = input.nextLine();
}


public void assignNames(){

    System.out.println("Enter a name for Player 1.");
        entry = input.nextLine();
        playerName1 = entry;

    System.out.println("Enter a name for Player 2.");
        entry = input.nextLine();
        playerName2 = entry;
}


public void assignMarks(){

    Random rand = new Random();
    int i = rand.nextInt(2);

    if( i == 0 ){
        playerMark1 = 'X';
        playerMark2 = 'O';
    }
    else{ 
            playerMark1 = 'O';
            playerMark2 = 'X';
    }

    currentMark = 'X';

    if(playerMark1 == 'X')
        currentPlayer = playerName1;
    else
        currentPlayer = playerName2;
}


public void switchPlayer(){

    if(currentMark == 'X')
        currentMark = 'O';
        else{
            currentMark = 'X';
        }


    if(currentMark == 'O' && playerMark1 == 'O'){
        currentPlayer = playerName1;
    }
        else{
            currentPlayer = playerName2;
        }
} 


public int getNum(){

    System.out.println(currentPlayer + ", place your mark (1-9).");

    num = input.nextInt();

    return num;
    }


public boolean validSpot(int num){

    boolean goodSpot = false;

    if(num == 1 && board[0][0] == ' ')
        goodSpot = true;
    if(num == 2 && board[0][1] == ' ')
        goodSpot = true;
    if(num == 3 && board[0][2] == ' ')
        goodSpot = true;
    if(num == 4 && board[1][0] == ' ')
        goodSpot = true;
    if(num == 5 && board[1][1] == ' ')
        goodSpot = true;
    if(num == 6 && board[1][2] == ' ')
        goodSpot = true;
    if(num == 7 && board[2][0] == ' ')
        goodSpot = true;
    if(num == 8 && board[2][1] == ' ')
        goodSpot = true;
    if(num == 9 && board[2][2] == ' ')
        goodSpot = true;

    if(goodSpot == false){
        System.out.println("Input error.  Make sure the spot is not taken.");
    }

    return goodSpot;
}


public boolean validRange(int num){

    boolean goodRange = false;

    if(num >= 1 && num <= 9 ){
        goodRange = true;  
    }

    if(goodRange == false){
        System.out.println("Input error.  Make sure the number is between 1-9.");
    }

    return goodRange;
}


public boolean validNumber(){

    return ((validRange(num) && validSpot(num)));
}


public void placeMark(){

    if(validNumber() == true){
        if(num == 1){
            board[0][0] = currentMark;
            switchPlayer();
        }
        if(num == 2){
            board[0][1] = currentMark;
            switchPlayer();
        }
        if(num == 3){
            board[0][2] = currentMark;
            switchPlayer();
        }
        if(num == 4){
            board[1][0] = currentMark;
            switchPlayer();
        }
        if(num == 5){
            board[1][1] = currentMark;
            switchPlayer();
        }
        if(num == 6){
            board[1][2] = currentMark;
            switchPlayer();
        }
        if(num == 7){
            board[2][0] = currentMark;    
            switchPlayer();
        }
        if(num == 8){
            board[2][1] = currentMark;
            switchPlayer();
        }
        if(num == 9){
            board[2][2] = currentMark;
            switchPlayer();
        }


    }
}


public boolean checkSpot(char c1, char c2, char c3) {

    return ((c1 != ' ') && (c1 == c2) && (c2 == c3));

}


public boolean checkWin() {

    return (checkWinRows() || checkWinColumns() || checkWinDiagonals());
}


public boolean checkWinRows() {

    for (int i = 0; i < 3; i++) {

        if (checkSpot(board[i][0], board[i][1], board[i][2]) == true) {

            return true;
        }
    }
    return false;
}


public boolean checkWinColumns() {

    for (int i = 0; i < 3; i++) {

        if (checkSpot(board[0][i], board[1][i], board[2][i]) == true) {

            return true;
        }
    }
    return false;
}


public boolean checkWinDiagonals() {

    return ((checkSpot(board[0][0], board[1][1], board[2][2]) == true) || (checkSpot(board[0][2], board[1][1], board[2][0]) == true));
}


public void gameResult(){

    switchPlayer();

    if(checkWin()){

        System.out.println("\nGAME OVER -- " +currentPlayer+ " WINS! WOO HOO!");
    }

    else if(fullBoard()){

        System.out.println("\nGAME OVER -- DRAW!");
    }
}


} // end TicTacToe

2 个答案:

答案 0 :(得分:2)

更改

 if(currentMark == 'O' && playerMark1 == 'O')

if(currentPlayer==playerName2)

您的代码并不总是有效,因为它只会在当前标记= 0且播放器1标记= 0时更改为播放器1.这只适用于玩家1在游戏开始时被指定为“O”的情况。

希望它有所帮助。

答案 1 :(得分:1)

如果当前和播放器1标记都是X,则切换播放器代码不执行任何操作。尝试:

if (currentMark == playerMark1){
  currentPlayer = playerName1;
} else {
  currentPlayer = playerName2;
}