使用Java中的方法来回移动

时间:2016-10-25 22:41:31

标签: java

我使用java构建了一个tic tac toe游戏,但我只有一个问题。我希望我的代码能够从validPlayerOneInputvalidPlayerTwoInput方法来回反弹。正如你在我的主要部分所看到的,我在程序上调用这两个方法是不正确的,因为它在调用方法后就停止了。我想让它继续运行直到确定胜利者。

我该怎么办?

import java.util.*;

public class tictactoe {

    private static char board[][] = {{1,2,3}, {4,5,6}, {7,8,9}};
    char p1Sym, p2Sym;

    public tictactoe() {
        p1Sym ='X';
        p2Sym = 'O';
        boardFill();
    }

    void boardFill() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(board[i][j]);  
                System.out.print(" | ");
            }
            System.out.println();
        }
    }

    void validInputPlayerOne() {
        boolean isSet = true;
        int player1Input, player1CorrectedInput;
        System.out.println("Player 1, enter a number between 1-9: ");

        Scanner player1 = new Scanner(System.in);
        player1Input = player1.nextInt();

        Scanner correctedInput = new Scanner(System.in);

        while(player1Input < 1 || player1Input >= 10) {
            System.out.println("This isn't a number between 1-9, try again: ");
            player1CorrectedInput = correctedInput.nextInt();
            player1Input = player1CorrectedInput;
        }   

        // or
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == player1Input) {
                    // set new value
                    board[i][j] = p1Sym;
                    // set
                    isSet = true;
                }
            }
        }

        if (!isSet) {
            System.out.println("not found");
        }   
    }

    void validInputPlayerTwo() {
        boolean isSet = true;
        int player2Input, player2CorrectedInput;
        System.out.println("Player 2, enter a number between 1-9: ");

        Scanner player2 = new Scanner(System.in);
        player2Input = player2.nextInt();

        Scanner correctedInput = new Scanner(System.in);

        while(player2Input < 1 || player2Input >= 10) {
            System.out.println("This isn't a number between 1-9, try again: ");
            player2CorrectedInput = correctedInput.nextInt();
            player2Input = player2CorrectedInput;
        }   

        // or
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == player2Input){

                    board[i][j] = p2Sym;

                    isSet = true;
                }
            }
        }

        if (!isSet) {
            System.out.println("not found");
        }   
    }

    public static void main(String[] args) {
        tictactoe t = new tictactoe();      

        t.validInputPlayerOne();
        t.boardFill();
        t.validInputPlayerTwo();
        t.boardFill();              
    }
}

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

int turn = 0;
while (t.noWinner()) {
  if (turn % 2 == 0) t.validInputPlayerOne();
  else t.validInputPlayerTwo();
  t.boardFill();
  turn += 1;
}

当然,现在你必须实际编写noWinner函数。

答案 1 :(得分:1)

直接回答你的问题,你可以有一个在每次移动时切换的布尔值:

boolean firstPlayer = true;
while (t.gameIsNotFinished()) {
    if (firstPlayer)
        t.validInputPlayerOne();
    else
        t.validInputPlayerTwo();
    firstPlayer = !firstPlayer;
}

但是,您需要解决的代码还有很多其他问题。例如,如果玩家输入无效值,那么它将转到下一个玩家,而不是要求他们重新输入该值。

您还应尝试使用单个validInputPlayer方法,该方法适用于传入firstPlayer变量的两个玩家。目前,您在这些方法中有大量重复代码。