TicTacToe游戏 - 如何为每个用户更改阵列输入

时间:2013-10-29 19:21:41

标签: java

我正在尝试使用数组等创建一个简单的tic tac toe游戏。

我知道如何编写下面的代码,但我的用户输入有问题。

问题

如何为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");
    }
}

1 个答案:

答案 0 :(得分: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;