由于if语句,p2无法解析为变量?

时间:2016-04-26 10:09:10

标签: java tic-tac-toe

此刻,我正在制作一个小玩意儿并与两种不同类型的电脑玩家进行游戏。我希望用户输入要玩的游戏数量,并且他想玩随机或简单的玩家。所以我分别实现了while循环和if语句。但是,由于if语句,我的p2无法再解析为变量,我不知道如何解决这个问题。任何帮助,将不胜感激。这只是我的代码中有错误的主要部分。

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);

    int Wins = 0;
    int Loses = 0;
    int Draws = 0;

    System.out.println("To choose player type Random or Simple");
    String ChoosePlayer = scan.nextLine();

    System.out.println("Please enter the number of rounds you want to play:");
    int i = scan.nextInt();
    int Rounds = 0;


while(Rounds < i){
    NCGridV3 theGrid = new NCGridV3(gridSize, gridSize);
    GameRunnerV3 theGame = new GameRunnerV3();
    Scanner sc = new Scanner(System.in);        // only needed if we include human players 

    HumanPlayer p1 = new HumanPlayer(sc, theGame);

    if(ChoosePlayer == "Simple"){
        SimpleComputerPlayer p2 = new SimpleComputerPlayer();
        p2.setMySymbol(SquareStatus.NOUGHT);
        }

    else if(ChoosePlayer == "Random"){
        RandomComputerPlayer p2 = new RandomComputerPlayer();
        p2.setMySymbol(SquareStatus.NOUGHT);
        }


    p1.setMySymbol(SquareStatus.CROSS);

    System.out.println(INITIAL_INSTRUCTIONS);


    NCPlayer nextToPlay = p1;       // arbitrary decision that p1 goes first

    while (theGrid.getGameStatus() == GameStatus.STILLPLAYING)
    {
        GridCoordinate nextMove = nextToPlay.getNextMove(theGrid) ;
        try
        {
            theGrid.setSquareStatus(nextMove.getRow(), nextMove.getCol(), nextToPlay.getMySymbol());
        } 
        catch (outOfRangeError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        // take turns 
        if(nextToPlay == p1)
            nextToPlay = p2;  // possible problem here
        else
            nextToPlay = p1;

3 个答案:

答案 0 :(得分:0)

为RandomComputerPlayer和SimpleComputerPlayer创建一个通用的接口/超类,只需在if / else块之外定义变量:

ComputerPlayer p2;
if(......) {
    p2 = new SimpleComputerPlayer();
} else {
    p2 = new RandomComputerPlayer();
}

答案 1 :(得分:0)

您使用两个不同的类来代表'p2'播放器。我建议如下: 首先,创建一个abstract class ComputerPlayer

然后,更改SimpleComputerPlayerRandomComputerPlayer以扩展ComputerPlayer

然后,在你的if语句中:

使用ComputerPlayer p2 = new SimpleComputerPlayer();

ComputerPlayer p2 = new SimpleComputerPlayer();

将每个播放器类中的公共代码放入ComputerPlayer

答案 2 :(得分:0)

if(ChoosePlayer == "Random"){
    RandomComputerPlayer p2 = new RandomComputerPlayer();
    p2.setMySymbol(SquareStatus.NOUGHT);
}

问题是您在p2指令块中定义了if

变量在定义之后可用,在已定义其中的块的内部。一旦编译器在该指令块之外编译代码,就不再定义p2

因此,只需在共享它的块中定义p2(通常是方法体或类的属性)并在那里使用它。当然,变量的类型必须允许分配任何可能的值(在此示例中,它必须是SimpleComputerPlayerRandomComputerPlayer扩展/实现的超类/接口。

ComputerPlayer p2 = null;  <---
if (ChoosePlayer.equals("Simple")) {
   p2 = new SimpleComputerPlayer();
} else if (ChoosePlayer.equals("Random")){{
   p2 = new RandomComputerPlayer();
}

请注意,您应始终为p2指定值;否则,如果您尝试在表达式中使用它,编译器将导致错误,因为它不允许在表达式中使用未初始化的值,并且不清楚该值是否已初始化(如果ChoosePlayer该怎么办?既不是Simple也不是Random

此外,检查在Java中命名变量的正确方法以及如何执行String(和其他对象)比较......