2013-09-30 21 views
-1
if (count % 2 == 0) { 

    int columnMove; 
    System.out.print("Player one, please enter a move or q to quit: "); 
    columnMove = scan.nextInt() - 1; 

    if (columnMove <= columns - 1 && columnMove >= 0) { 
     turns = true; 
     game.playerOnePrompt(board, columns, rows, columnMove); 
     ++count; 
     if (board[0][columnMove] != "_") { 
      System.out.println("This column is full"); 
      count = 0; 
     } 
    } else { 
     System.out.println("Invalid move"); 
     count = 0; 
    } 

} else { 

    int columnMove; 
    System.out.print("Player two, please enter a move: "); 
    columnMove = scan.nextInt() - 1; 

    if (columnMove <= columns - 1 && columnMove >= 0) { 
     turns = true; 
     game.playerTwoPrompt(board, columns, rows, columnMove); 
     count++; 
     if (board[0][columnMove] != "_") { 
      System.out.println("This column is full"); 
      count = 1; 
     } 
    } else { 
     System.out.println("Invalid move"); 
     count = 1; 
    } 
} 

您好!以上是我的代碼來確定數組(列)是否已滿,如果它已滿,用戶應該提示再次移動。連接4 Java項目 - 確定列滿時的問題

但是我有一個問題,程序發現它已滿,提示用戶,並且在用戶進行有效的移動之後,程序不會移動玩家(從玩家1-2-2-2等) )。

有什麼建議嗎?

+0

這不是完整的代碼...爲了幫助你,通過提供完整的代碼來幫助我們。 –

回答

0

你的問題是在這裏:

if (board[0][columnMove] != "_") 
    { 
     System.out.println("This column is full"); 
     count = 0; 
    } 

你總是設置計數爲零,每當此舉是有效的。

+0

如果我不重置計數,則 然後,如果用戶輸入完整列的移動,則用戶的「轉向」將被註冊並移動到播放器2上。 有沒有更好的方法來重置計數? – user2517185

+0

「,並且在用戶進行有效的移動之後,程序不會移動玩家」。將分配更改爲第一個分支爲1,並等於第二個分配。 –

0
maximumNumberOfMoves = boardWidth * boardHeight; 
    if (count == maximumNumberOfMoves) { 
     // end game 
    } else if (count % 2 == 0) { 
     int columnMove; 
     System.out.print("Player one, please enter a move or q to quit: "); 
     columnMove = scan.nextInt() - 1; 
     if (columnMove <= columns - 1 && columnMove >= 0) { 
      turns = true; 
      game.playerOnePrompt(board, columns, rows, columnMove); 
      ++count; 
      if (board[0][columnMove] != "_") { 
       System.out.println("This column is full"); 
       --count; // We must decrement count if we want the same 
          // player to move again 
      } 
     } else { 
      System.out.println("Invalid move"); 
      // count = 0; //Remove this, count was not modified 
     } 

    } else { 
     int columnMove; 
     System.out.print("Player two, please enter a move: "); 
     columnMove = scan.nextInt() - 1; 
     if (columnMove <= columns - 1 && columnMove >= 0) { 
      turns = true; 
      game.playerTwoPrompt(board, columns, rows, columnMove); 
      count++; 
      if (board[0][columnMove] != "_") { 
       System.out.println("This column is full"); 
       --count; // We must decrement count if we want the same 
          // player to move again 
      } 
     } else { 
      System.out.println("Invalid move"); 
      // count = 1; //Again, remove this since count is not modified 
     } 
    } 

注爲固定代碼

這個答案假定計數註釋行代表了迄今所取得移動號碼,並開始爲0

+0

一旦我到達遊戲的最後一行,它仍在重複着。 有沒有更好的方式讓我向您展示我的問題? 該計劃相當長,分爲2個部分。 – user2517185

+0

你的意思是什麼時候所有的老虎機都滿了(遊戲以平局結束)?請參閱編輯答案 – Ron

+0

當每一行,除了最後一行被填充。 所以它會看起來像 _ _ _ _ _ X○×○× X○×○× 一旦玩家開始到最後一行 誰放置了第一塊下來,是重複一個上播放。 X X X X X X○×○× X○×○× 或 ○○○○○ X○×○× X○×○× – user2517185