1

少し前に質問しました。多くのコードを書き直した後、1 つの明白な問題を除いて、私の三目並べは良さそうに見えます。プレイしようとすると、勝利条件がランダムにアクティブになっているように見えます-行に ​​x - o - x があり、「プレイヤー X が勝った!」と言いますが、o - o - o の場合、a を検出しません。勝つ。描画は正常に動作します。

勝利条件コード:

 // win conditions. if true, set win==true; else set win==false
            if (square[0].getText().equals(square[1].getText())
                    && square[1].getText().equals(square[2].getText())
                    != square[0].getText().isEmpty()) {
                    win = true;}

            if (square[3].getText().equals(square[4].getText())
                    && square[4].getText().equals(square[5].getText())
                    != square[3].getText().isEmpty()) {
                    win = true;}

            if (square[6].getText().equals(square[7].getText())
                    && square[7].getText().equals(square[8].getText())
                    != square[6].getText().isEmpty()) {
                    win = true;}

            if (square[0].getText().equals(square[3].getText())
                    && square[3].getText().equals(square[6].getText())
                    != square[0].getText().isEmpty()) {
                    win = true;}

            if (square[1].getText().equals(square[4].getText())
                    && square[4].getText().equals(square[7].getText())
                    != square[1].getText().isEmpty()) {
                    win = true;}

            if (square[2].getText().equals(square[5].getText())
                    && square[5].getText().equals(square[8].getText())
                    != square[2].getText().isEmpty()) {
                    win = true;}

            if (square[0].getText().equals(square[4].getText())
                    && square[4].getText().equals(square[8].getText())
                    != square[0].getText().isEmpty()) {
                    win = true;}

            if (square[6].getText().equals(square[4].getText())
                    && square[4].getText().equals(square[2].getText())
                    != square[6].getText().isEmpty()) {
                    win = true;}

            else{win = false;
            }

そしてアクションコード:

  public void actionPerformed (ActionEvent e) {
            //one more move has gone by, calculate player turn + player letter
            move++;
            if (move % 2 == 0) {
                    player = 1; letter = "X";
            }else{
                    player = 2; letter = "O";
            }

            playergo.setText("It is player " + player + "'s go!");

            //set square letter to player's letter, disable square so no further moves can be made there
            for (int i=0; i<=8; i++){
                    if (e.getSource() == square[i]){
                            square[i].setText(letter);
                            square[i].setEnabled(false);
                    }
            }

完全なペーストビンコードはこちら. よろしくお願いします!!!

4

2 に答える 2

0

あなたはそうのようなことをすることができます(疑似コードで)

int winPositions[][] = { {0,1,2}, {3,4,5}, {6,7,8}, //horizontal
                         {0,3,6}, {1,4,7}, {2,5,8}, //vertical
                         {0,4,8}, {2,4,6} }; //diagnol 

for i = 0 to winPosition.size
  int positions[] = winPosition[i]; //know its size is 3
  bool hasWon = board[ positions[i] ] == board[ positions[i+1] ] && board[positions[i+1]] == board[positions[i+2]];
  if(hasWon) return true;
于 2013-07-15T00:34:51.590 に答える