2016-04-01 15 views
0

JButtonを使用してGUIでTicTacToeゲームを書きました。以下は、勝者がいるかどうかを確認するためのコードです。また、ドローをチェックするコードも含まれています。しかしながら、すべてのボタンがクリックされ、最後の移動がプレーヤーの1人に勝利をもたらした場合、それはすべてのボタンがクリックされたため引き分けとみなされる。どのようにしてドローを正確にチェックできるように修正できますか?TicTacToe Draw For Draw

public void checkWin(char[] values) 
{ 
    //Check if player 1 (xs) win 
    if ((values[0] == 'x') && (values[1] == 'x') && (values[2] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[3] == 'x') && (values[4] == 'x') && (values[5] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[6] == 'x') && (values[7] == 'x') && (values[8] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[0] == 'x') && (values[4] == 'x') && (values[8] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[2] == 'x') && (values[4] == 'x') && (values[6] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[0] == 'x') && (values[3] == 'x') && (values[6] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[1] == 'x') && (values[4] == 'x') && (values[7] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    else if ((values[2] == 'x') && (values[5] == 'x') && (values[8] == 'x')) 
    { 
     playerLabel.setText("Player 1 wins!"); 
     disableButtons(); 
    } 
    // check for draw 
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) && 
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o')) 
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o')) 
    && ((values[8] == 'x') || (values[8] == 'o'))) 
    { 
     playerLabel.setText("DRAW!"); 
     disableButtons(); 
    } 

    //Check is player 2 (os) wins 
    if ((values[0] == 'o') && (values[1] == 'o') && (values[2] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[3] == 'o') && (values[4] == 'o') && (values[5] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[6] == 'o') && (values[7] == 'o') && (values[8] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[0] == 'o') && (values[4] == 'o') && (values[8] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[2] == 'o') && (values[4] == 'o') && (values[6] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[0] == 'o') && (values[3] == 'o') && (values[6] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[1] == 'o') && (values[4] == 'o') && (values[7] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    else if ((values[2] == 'o') && (values[5] == 'o') && (values[8] == 'o')) 
    { 
     playerLabel.setText("Player 2 wins!"); 
     disableButtons(); 
    } 
    // check for draw 
    else if (((values[0] == 'x') || (values[0] == 'o')) && ((values[1] == 'x') || (values[1] == 'o')) && 
    ((values[2] == 'x') || (values[2] == 'o')) && ((values[3] == 'x') || (values[3] == 'o')) && ((values[4] == 'x') || (values[4] == 'o')) 
    && ((values[5] == 'x') || (values[5] == 'o')) && ((values[6] == 'x') || (values[6] == 'o')) && ((values[7] == 'x') || (values[7] == 'o')) 
    && ((values[8] == 'x') || (values[8] == 'o'))) 
    { 
     playerLabel.setText("DRAW!"); 
     disableButtons(); 
    } 
} 
+1

だから、すべての可能な組み合わせをチェックするより良い方法があります。あなたが何を持っているのか、もしあなたがすべてのifステートメントを通過すれば、なぜドローをチェックする必要がありますか?すべてのボックスがチェックされ、それを遠くにすると、デフォルトでは描画されませんか? –

+0

もう1つの選択肢は、新しいゲームを開始するときなど、すべてのボックスが空白であることです。また、単にelseステートメントを作成すると、ゲームは常に描画されます。 – Evan

+0

次に、ゲームが開始されたかどうかを決定するブール変数を作成し、最初の移動後にfalseに設定します。ゲームが常にelse文で描画される場合、コード内の条件付きロジックにはいくつかの作業が必要です。 –

答えて

0

ちょうどあなたは本当にジェネリックを作成することによって、あなたのコードをここに短縮することができ、別のノートで...最後else声明の中で、それは引き分けでなければならず、すべての勝利のシナリオについて

else{ 
    playerLabel.setText("DRAW!"); 
    disableButtons(); 
} 

をチェックメソッドに渡し、適切な引数を渡します。このコードは非常に面倒です。

0

あなたは、いくつかの関数を使用してループできます

public void checkGameOver(char[] values) { 
    if (checkWin(values, 'x')) { 
     playerLabel.setText("Player 1 wins!"); 
    } else if (checkWin(values, 'o')) { 
     playerLabel.setText("Player 2 wins!"); 
    } else if (checkDraw(values)) { 
     playerLabel.setText("DRAW"); 
    } else { 
     return; 
    } 

    disableButtons(); 
} 

public boolean checkWins(char[] values, char player) { 
    if (((values[0] == player) && (values[1] == player) && (values[2] == player)) || 
     ((values[3] == player) && (values[4] == player) && (values[5] == player)) || 
     ((values[6] == player) && (values[7] == player) && (values[8] == player)) || 
     ((values[0] == player) && (values[4] == player) && (values[8] == player)) || 
     ((values[2] == player) && (values[4] == player) && (values[6] == player)) || 
     ((values[0] == player) && (values[3] == player) && (values[6] == player)) || 
     ((values[1] == player) && (values[4] == player) && (values[7] == player)) || 
     ((values[2] == player) && (values[5] == player) && (values[8] == player))) 
    { 
     return true; 
    } 

    return false; 
} 

public boolean checkDraw(char[] values) { 
    for (char c : values) { // check if each box holds either an 'x' or an 'o' 
     if (c != 'o' && c != 'x') { 
      return false; 
     } 
    } 

    return true; 
} 
0

あなたがこれを行うことができます。

1 - あなたはプレーヤーのラベルにsettedする必要のあるテキストを格納する文字列のテキスト変数を使用します。

2 - それぞれifで、設定する必要のあるテキストに従ってテキスト変数を設定します。たとえば、プレーヤー1が獲得するifsの場合、text = "PLAYER 1 WINS"となります。

3 - メソッドの終わりにプレーヤーのラベルを1回だけ設定し、上記のコードを置くごとに "if"には設定しないでください。

4- booleanのような2つのブール変数を使用します。ブール値は、player1 = false、player2 = falseです。そして、player1が勝利するとtrueにセットされ、そうでなければfalseになります。同じことがplayer2にも当てはまります。言い換えれば、それぞれの場合、例えば、プレーヤー1が勝った場合、player1 = trueとなります。

5-最終的に、player1とplayer2の両方の変数が真であれば、それをチェックするだけです。 (そして、変数text = "DRAW"を設定するのを忘れないでください)。

関連する問題