2

我正在尝试创建一个简单的井字游戏作为练习数组等。

我知道我想如何在下面对其进行编码,但是我对用户输入有疑问。

问题

如何为 X 或 O 的每个用户输入更改数组输入。

import java.util.Scanner;
public class TicTacToe {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      String[] array;
      array = new String [9]; 

      int uInput;
      int pCounter=1;
      while ( uInput <= 9) {
         System.out.println("Enter X coordinate:");
         uInput = input.nextInt();
         array[uInput] = ("x");

         System.out.println("Enter X coordinate:");
         uInput = input.nextInt();
         array[uInput] = ("o");

         for (int yCounter = 0; yCounter < 6; yCounter++) {
            System.out.print("--");
         }
         System.out.println();

         for (int fCounter=0; fCounter<(3); fCounter++) {
            for (int xCounter=0; xCounter<1; xCounter++) {
               for (pCounter=1; pCounter<4; pCounter++) {   
                  System.out.printf("  %d| ", array[pCounter]);
               }
            } 
         }
         System.out.println();

         for (int zCounter=0; zCounter<4; zCounter++)  {
             System.out.print("---");
         }
         System.out.println();
  }
  System.out.print("0  1  2\n3  4  5\n6  7  8\n");
    }
}
4

1 回答 1

1

逻辑应该是这样的(伪代码):

boolean win = false, tie = false;
String winner = "";
while(!win && !tie){
  getInputFromUser1();
  updateBoard();
  if (win || tie) break;
  getInputFromUser2();
  updateBoard();
}

//check if win or tie and display message respectfully
displayEndMessage();
return;
于 2013-10-29T19:29:40.833 回答