2016-05-15 3 views
0

私はチック・タック・トゥ・プログラムに取り組んでいます。プレーヤーが勝ったかどうかを検出する方法に取り組んでいます。トリガー条件を取得していない場合は

ゲームは次のようにプログラムされています。ゲームボードは9個の整数を保持するint配列です。プレイヤー1は配列の指定された部分に "1"を書き込むことができ、プレイヤー2は配列の指定された部分を "2"で埋めます。

勝利を確認する私の方法では、かなり長く畳み込まれたif/elseステートメントを使用していますが、最終的にはコンパイルエラーはなく、プログラムは正しく動作しますが、勝利条件を引き起こすような方法でいっぱいになると、私の方法では、それが想定されている行が印刷されません。例えば

:あなたが最後の行に見ることができるように、配列の最初の3つの整数は、プレイヤー1が持っているというメッセージをトリガしているはずプレイヤー1、で埋められている

run: 
Player 1, please enter the number of the square that you want to mark (1 - 9) 
1 
[1, 0, 0, 0, 0, 0, 0, 0, 0] 
Player 2, please enter the number of the square that you want to mark (1 - 9) 
6 
[1, 0, 0, 0, 0, 2, 0, 0, 0] 
Player 1, please enter the number of the square that you want to mark (1 - 9) 
2 
[1, 1, 0, 0, 0, 2, 0, 0, 0] 
Player 2, please enter the number of the square that you want to mark (1 - 9) 
8 
[1, 1, 0, 0, 0, 2, 0, 2, 0] 
Player 1, please enter the number of the square that you want to mark (1 - 9) 
3 
[1, 1, 1, 0, 0, 2, 0, 2, 0] 
Player 2, please enter the number of the square that you want to mark (1 - 9) 

しかし、そのようなメッセージは出ません。ここで

は、勝利条件を拾うことになっている方法です。

public void checkWin() { 
    int row1 = board[0] + board[1] + board[2]; 
    int row2 = board[3] + board[4] + board[5]; 
    int row3 = board[6] + board[7] + board[8]; 

    int column1 = board[0] + board[3] + board[6]; 
    int column2 = board[1] + board[4] + board[7]; 
    int column3 = board[2] + board[5] + board[8]; 

    int cross1 = board[0] + board[4] + board[8]; 
    int cross2 = board[2] + board[4] + board[6]; 

    int square0 = board[0]; 
    int square1 = board[1]; 
    int square2 = board[2]; 
    int square3 = board[3]; 
    int square4 = board[4]; 
    int square5 = board[5]; 
    int square6 = board[6]; 
    int square7 = board[7]; 
    int square8 = board[8]; 

    if (row1 == 3 | row1 == 6 | row2 == 3 | row2 == 6 | row3 == 3 | row3 == 6| 
      column1 == 3 | column1 == 6 | column2 == 3 | column2 == 6 | column3 == 3 | column3 == 6| 
      cross1 == 3 | cross1 == 6 | cross2 == 3 | cross2 == 6) { 
    } else if ((square0 == 1 && square1 == 1 && square2 == 1) || 
      (square3 == 1 && square4 == 1 && square5 == 1) || 
      (square6 == 1 && square7 == 1 && square8 == 1) || 
      (square0 == 1 && square3 == 1 && square6 == 1) || 
      (square1 == 1 && square4 == 1 && square7 == 1) || 
      (square2 == 1 && square5 == 1 && square8 == 1) || 
      (square0 == 1 && square4 == 1 && square8 == 1) || 
      (square2 == 1 && square4 == 1 && square6 == 1)) { 
     System.out.println("Player 1 has won this game!"); 
    } else if ((square0 == 2 && square1 == 2 && square2 == 2) || 
      (square3 == 2 && square4 == 2 && square5 == 2) || 
      (square6 == 2 && square7 == 2 && square8 == 2) || 
      (square0 == 2 && square3 == 2 && square6 == 2) || 
      (square1 == 2 && square4 == 2 && square7 == 2) || 
      (square2 == 2 && square5 == 2 && square8 == 2) || 
      (square0 == 2 && square4 == 2 && square8 == 2) || 
      (square2 == 2 && square4 == 2 && square6 == 2)) { 
     System.out.println("Player 2 has won this game!"); 
    } 



} 

私はそれが非常に複雑だけど、私はかなりまだどのようにする方法を説明しながら、それを短縮する方法を見つけることができませんでしたうまくいくはずです。

誰かが私が行方不明または間違っていることを指摘してくれれば、本当に感謝しています。

ありがとうございました!

+1

より良い生活のために配列を使用することを検討してください。 – Maroun

+0

'row1 == 3 'のために空白が表示される – MikeCAT

+0

' || 'の代わりに' | 'を使用していますか? – Idos

答えて

0

まあ、row1 == 3のように思えば、勝利をチェックせずに続行します。 私は誤解しますが、私のために何をやっていることである可能性があります

if A { 
    // does nothing 
} else if B { 
    // Player 1 won 
} else if C { 
    // Player 2 won 
} 

どこをチェックし、プレイヤーが実際に獲得したBとCのチェックがあり、入賞組合せであり、場合。私の感覚で 、それはかなりのようになります。

if A { 
    // Winning game, checks winner 
    if B { 
     // Player 1 won 
    } else { 
     // Player 2 won 
    } 
} 
+1

ちょうど長いので、Aを削除するだけで間違いを犯すことはありません。'board [0] = 1、board [1] = 2、board [2] = 0')、以下のチェックで十分です。 – MikeCAT

+0

真実、この場合は考えられませんでした。 –

1

人はあなたに私はあなたがより良い方法でそれを書くことができると思い、まだ失敗の理由を答えました。

public void checkWin() { 

    List<ResultChecker> resultCheckerList = new ArrayList<ResultChecker>(); 
    int row1 = board[0] + board[1] + board[2]; 
    resultCheckerList.add(new ResultChecker(row1/3,row1%3)); 

    int row2 = board[3] + board[4] + board[5]; 
    resultCheckerList.add(new ResultChecker(row2/3,row2%3)); 
    int row3 = board[6] + board[7] + board[8]; 
    resultCheckerList.add(new ResultChecker(row3/3,row3%3)); 

    int column1 = board[0] + board[3] + board[6]; 
    resultCheckerList.add(new ResultChecker(column1/3,column1%3)); 
    int column2 = board[1] + board[4] + board[7]; 
    resultCheckerList.add(new ResultChecker(column2/3,column2%3)); 
    int column3 = board[2] + board[5] + board[8]; 
    resultCheckerList.add(new ResultChecker(column3/3,column3%3)); 
    int cross1 = board[0] + board[4] + board[8]; 
    resultCheckerList.add(new ResultChecker(cross1/3,cross1%3)); 
    int cross2 = board[2] + board[4] + board[6]; 
    resultCheckerList.add(new ResultChecker(cross2/3,cross2%3)); 

    for(ResultChecker rc:resultCheckerList) { 
    if(rc.isWin()) { 
     if(rc.isFirstPlayer()) { 
      System.out.println("Player 1 has won this game!"); 
     } else { 
      System.out.println("Player 2 has won this game!"); 
     } 
     break; 
    } 
    } 




} 

private Class ResultChecker { 
    private int divResult; 
    private int modResult; 
    public ResultChecker(int divResult,int modResult) { 
    this.divResult = divResult; 
    this.modResult = modResult; 
    } 

    public boolean isWin() { 
    return this.modResult == 0; 
    } 

    public boolean isFirstPlayer() { 
    return this.divResult == 1; 
    } 
} 
+0

コンストラクタを '/ 3''%3' **に移動すると、書き換えがさらに良くなり、勝利が見つかると "ブレーク"が間違っている可能性があります。 –

+0

はい、あなたは間違っています。 –

関連する問題