Tic Tac Toe控制台游戏Java - 检查获胜者方法不起作用

时间:2015-06-23 17:31:22

标签: java

我用Java创建了一个简单的tic tac toe游戏。然而,当有人获胜时,游戏和代码会起作用,让其他玩家在终止之前再玩一次,然后说特定玩家赢了。我试图想办法让一个玩家获胜后让游戏停止。我已经针对此类情景herehere查看了几种不同的算法。但我仍然感到困惑。

有什么建议吗?

编辑:此代码现已完全正常工作,不再允许无效移动。

import java.util.Scanner;

public class TicTacToe{
    public static Scanner scan = new Scanner(System.in);
    private static int boardSize = 3;
    private static char[][] board = new char[boardSize][boardSize];
    private static char turn = 'O';
    private static char moveNum = 0;

public static void initiliaze(){
    for(int i = 0; i<boardSize;i++){
        for(int j = 0;j<boardSize;j++){
            board[i][j]='-';
        }
    }
}

public static void printBoard(){
    for(int i=0; i<boardSize;i++){
        for(int j=0; j<boardSize;j++){
            System.out.print(board[i][j]);
            if (j != boardSize-1){
                System.out.print("|");
            }
        }
            System.out.println();
            if(i!=boardSize-1){
                System.out.println("-----");
            }
    }
    System.out.println();
}

public static boolean checkWin(){
    if(((board[0][0]==turn && board[0][1]==turn && board[0][2]==turn)||
    (board[1][0]==turn && board[1][1]==turn && board[1][2]==turn)||
    (board[2][0]==turn && board[2][2]==turn && board[2][2]==turn)||
    (board[0][0]==turn && board[1][0]==turn && board[2][0]==turn)||
    (board[0][1]==turn && board[1][1]==turn && board[2][1]==turn)||
    (board[0][2]==turn && board[1][2]==turn && board[2][2]==turn)||
    (board[0][0]==turn && board[1][1]==turn && board[2][2]==turn)||
    (board[0][2]==turn && board[1][1]==turn && board[2][0]==turn)))
    return true;

    return false;
}

public static void makeMove(){
    int row, col;
    boolean validInput = false;

    do{
    if(turn == 'X'){
        System.out.print("Player X enter your move (row:1,2,3)!");
    }else{
        System.out.print("Player O enter your move (row:1,2,3)!");
    }
    row = scan.nextInt() - 1;

    if(turn == 'X'){
        System.out.print("Player X enter your move (col:1,2,3)!");
    }else{
        System.out.print("Player O enter your move (col:1,2,3)!");
    }
    col = scan.nextInt() - 1;
    if(row >= 0 && row < boardSize && col >=0 && col < boardSize && board[row][col] == '-'){
    board[row][col] = turn;
    validInput = true;
    }else{
         System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
    }
    }while(!validInput);
}

public static void changeMove(){
    if(turn == 'X'){
        turn = 'O';
    }else{
        turn = 'X';
    }
    moveNum++;
}

public static void play(){

do{
    changeMove();
    makeMove();
    printBoard();
  }while(checkWin()==false && (moveNum < boardSize*boardSize));
  if (turn=='X' && checkWin()==true){
    System.out.println("X has won");
  }
  if(checkWin() == true && turn=='O'){
    System.out.println("O has won");
  }else{
    System.out.println("This is a draw!");
  }
}


}


class PlayGame {
    public static void main (String[] args){
        TicTacToe.initiliaze();
        TicTacToe.printBoard();
        TicTacToe.play();

    }
}

2 个答案:

答案 0 :(得分:1)

你有问题:你要求用户输入一个动作,然后在同一个功能中你将转弯改为下一个玩家。在你的checkWin功能中,你只检查轮到它的玩家是否赢了。通过在移动后立即改变转弯,然后检查获胜,转弯已经切换到另一个玩家,并且它不再是刚刚赢了的玩家,从checkWin()产生错误。 / p>

换句话说,你正在做的是要求玩家移动,然后检查下一个玩家是否赢了,然后让他移动。因此,不是做一个移动然后检查胜利,你正在检查胜利,然后移动。

此外:

  1. play()函数的do while循环中,checkWin();语句本身没有任何作用,应该删除。
  2. checkWin()中,你的第一个if条件是关于它是X还是O转,这是无用的,因为它总是只有X转或O转

答案 1 :(得分:0)

允许用户进行无效移动。

  

同一动作[1,1]不止一次

     

对手的同样举动[因为前X和O都可以进入1,2]

     

无效的值,如[String或1,2,3以外的值] throw   例外