現時点では、私は2つの異なるタイプのコンピュータプレーヤーとの打ち合わせとクロスゲームを行っています。ユーザーに再生するゲームの数を入力し、ランダムまたはシンプルなプレーヤーを再生したいと思っています。だから、whileループとif文をそれぞれ実装しました。しかし、if文のおかげで、私のp2はもはや変数に解決されなくなり、私はこれを回避する方法を知らない。どんな助けもありがとう。これは、エラーが発生したコードの主要部分のみです。p2はif文のために変数に解決できませんか?
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;
適切なIDEを使用して、コードを適切にフォーマットし、セミコラムと中括弧が欠落していないことを確認します。 – niyasc