井字游戏Java游戏不结束

问题描述:

运行此代码,游戏永远不会宣布优胜者或领带,它不断地要求下一个球员转会......井字游戏Java游戏不结束

输出示例:

Choose a position to play 
1 | 2 | 3 
--------- 
4 | 5 | 6 
--------- 
7 | 8 | 9 
Player X (Enter a position or -1 to resign): 1 

Choose a position to play. 
1 | 2 | 3 
--------- 
4 | 5 | 6 
--------- 
7 | 8 | 9 
Player O (Enter a position or -1 to resign): 1 

它不会改变角色的空间数量,甚至不会记住甚至宣布获胜者。

这是我的代码的runStandardGame()部分,我认为我的谎言在那里存在问题?

//***RUN STANDARD GAME****************************************************** 

static void runStandardGame() { 

    int position = QUIT; 
    char playerChar = 'X'; 
    boolean isGameOver = false; 

    fillPosition(); 

    System.out.println("Choose a position to play.\n"); 


    displayGrid(); 

    do { 
     System.out.print("Player " + playerChar + 
         " (Enter a position or " + QUIT + " to resign): "); 
     position = keyboard.nextInt(); 

     System.out.println("Choose a position to play.\n"); 

     displayGrid(); 


     if(isWin()) { 
      System.out.print("\nPlayer " + playerChar + " WINS!!!\n"); 
      isGameOver = true; 
     } 
     else if (isTie()) { 
      System.out.print("\nTIE.\n"); 
      isGameOver = true; 
     } 
     else { 
      //switch players because one of the players has just played 
      //and not won;, so, it is the other player's turn 
      if (playerChar == 'X') { 
       playerChar = 'O'; 
      } 
      else{ 
       playerChar = 'X'; 
      } 
     } 
    }while(!isGameOver && position != QUIT); 

}//end of runStandardGame 

甚至可能只是剧中的部分,因为这是在分配给数组...

//***PLAY*******************************************************************  

static void play(int cell, char playerChar) { 
    //the player has entered a number 1 through 9 for the position they want 
    //to play, so, figure out which row and which column of the array this is 
    //For example, if the player wants to play 'X' in position 7, this means 
    //the 'X' must go into row 2, column 0 of the array 
    int row = 0; 
    int column = 0; 

    if (cell > 0 && cell <= (STANDARD_GRID_ROWS * STANDARD_GRID_COLUMNS)){ 
     row = (cell - 1)/STANDARD_GRID_ROWS; 
     column = (cell - 1) % STANDARD_GRID_COLUMNS; 
     grid[row][column] = playerChar; 
    } 
} 

任何帮助表示赞赏。谢谢。

+2

你告诉既不isWin(),也不isTie()方法。我猜测,没有人会回报真实的,所以gameOver从未设置;因此,你继续循环。 – duffymo

+0

您是否确定要使用播放方法?我不知道发生了什么。 – combinatorics

+0

isWin()和isTie方法是正确的,我只是不明白为什么他们没有被调用,但是,我从来没有调用play()...当我做了,它的工作!谢谢!!! :) – ADeck33

我看到你正在用户输入与

position = keyboard.nextInt(); 

,但我没有看到你真正使用它通过调用

play(position, playerChar); 
+0

工作!我想我完全通过了那部分!我知道我有所有的部分,但我忘了叫它。 :) 非常感谢你! – ADeck33

+0

有时候问题隐藏在明显的视野中。 –

在我看来,在

position = keyboard.nextInt(); 

东西需要与您显示,并继续之前的位置(将其写入板)做了什么?