少し前に質問しました。多くのコードを書き直した後、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);
}
}
完全なペーストビンコードはこちら. よろしくお願いします!!!