Java Tic Tac Toe游戏确认获胜者

时间:2015-04-01 01:04:36

标签: java arrays java.util.scanner tic-tac-toe

以下是我的Tic Tac Toe游戏代码。我遇到了两个问题。首先,我不知道如何返回哪个玩家获胜(X或O),如果有赢家我可以返回。同样,当我尝试运行我的程序时,我可以出错。我哪里出错了?

我有两个文件,一个是TicTacToe,另一个是TTTBoard。

TicTaeToe.java

import java.util.Scanner;
import java.util.Random;

public class TicTacToe
{
    public static void main(String[]args){
        Scanner reader = new Scanner(System.in);
        TTTBoard board = new TTTBoard();

        System.out.println(board);

        Random gen = new Random();
        char player;
        if(gen.nextInt(2)==1)
            player = 'o';
        else
            player = 'x';
        while(!board.checkWinner() && !board.full()){

            System.out.println(player + " 's turn");
            System.out.println("Enter the row and column [1-3, space, 1-3]: ");
            int row = reader.nextInt();
            int column = reader.nextInt();    
            boolean success = board.placeXor0(player, row, column);
            if(!success)
                System.out.println("Error: cell already occupied!");
            else{
                System.out.println(board);
                if(player == 'x')
                    player = 'o';
                else
                    player = 'x';
                }
            }        
}
}

TTTBoard.java

public class TTTBoard{
    private char[][] board;

    public TTTBoard(){
        board = new char[3][3];
        reset();
    }

    public void reset(){
        for(int row = 0; row < 3; row++)
            for(int column = 0; column < 3; column++)
                board[row][column] = '-';
        }
    public String toString(){
        String result = "\n";
        for(int row = 0; row <3; row++){
            for (int column = 0; column < 3; column++)
                result += board[row][column] + " ";
            result +="\n";
        }
        return result;
    }
    public boolean placeXor0(char player, int row, int column){
        if(board[row -1][column -1]=='-'){
            board[row-1][column-1]= player;
            return true;
        }
        else
            return false;

    }
    public boolean checkWinner(){
        return(checkRowsForWin()||checkColumnsForWin()||checkDiagnalsForWin());
    }

    public boolean full(){
        boolean full = true;
        for(int row = 0; row < 3; row++){
            for(int column = 0; column < 3; column++){
                if(board[row][column] == '-'){
                    full = false;
                }
            }
        }
        return full;
    }
    public boolean checkRowsForWin(){
        for(int row = 0; row < 3; row++){
                if(placeXor0(board[row][0], board[row][1], board[row][2]) == true){
                    return true;
                }
            }

        return false;
    }
    public boolean checkColumnsForWin(){
        for(int column = 0; column < 3; column++){
            if(placeXor0(board[0][column], board[1][column], board[2][column]) == true){
                return true;
                }
            }
        return false;
    }
    public boolean checkDiagnalsForWin(){
        return((placeXor0(board[0][0], board[1][1], board[2][2]) == true) || (placeXor0(board[0][2], board[1][1], board[2][0]) == true));
        }    
}

新守则:

public class TTTBoard{
    private char[][] board;

    public TTTBoard(){
        board = new char[3][3];
        reset();
    }

    public void reset(){
        for(int row = 0; row < 3; row++)
            for(int column = 0; column < 3; column++)
                board[row][column] = '-';
        }
    public String toString(){
        String result = "\n";
        for(int row = 0; row <3; row++){
            for (int column = 0; column < 3; column++)
                result += board[row][column] + " ";
            result +="\n";
        }
        return result;
    }
    public boolean placeXor0(char player, int row, int column){
        if(board[row -1][column -1]=='-'){
            board[row-1][column-1]= player;
            return true;
        }
        else
            return false;

    }
    public boolean checkWinner(){
        return(checkRowsForWin()||checkColumnsForWin()||checkDiagnalsForWin());
    }
    /*public String getWinner(){
        for(int row = 0; row <3; row++){
            for (int column = 0; column < 3; column++)
            }
        }*/
    public boolean full(){
        boolean full = true;
        for(int row = 0; row < 3; row++){
            for(int column = 0; column < 3; column++){
                if(board[row][column] == '-'){
                    full = false;
                }
            }
        }
        return full;
    }
    public boolean checkRowsForWin(){
        for(int row = 0; row < 3; row++){
                if(board[row][0]== board[row][1]&& board[row][0]== board[row][2]){
                    return true;
                }
            }

        return false;
    }
    public boolean checkColumnsForWin(){
        for(int column = 0; column < 3; column++){
            if (board[0][column] == board[1][column] && board[0][column] == board[2][column]) {
                return true;
                }
            }
        return false;
    }
    public boolean checkDiagnalsForWin(){
        return((board[0][0]== board[1][1]&& board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2]== board[2][0]));
        }


}

1 个答案:

答案 0 :(得分:0)

checkRowsForWin中,您有代码行:

if(placeXor0(board[0][column], board[1][column], board[2][column]) == true) {
    return true;
}

您想将其替换为:

if (board[0][column] == board[1][column] && board[0][column] == board[2][column]) {
    return true;
}

checkColumnsForWincheckDiagnalsForWin

做类似的事情