Sphyxx
Sphyxx

Reputation: 15

Tic Tac Toe win conditions not activating correctly

I asked a question a while back. After much code rewriting, my tic tac toe is looking good, except for one glaring problem. When I try to play, the win conditions seemingly activate at random - I'll have x - o - x in a line and say "Player X won!", but when I have o - o - o it won't detect a win. Draw works ok though.

Win Conditions code:

 // 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;
            }

And action code:

  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);
                    }
            }

Full pastebin code here. Thank you all in advance!!!

Upvotes: 1

Views: 266

Answers (2)

dchhetri
dchhetri

Reputation: 7136

You can do something like so( in psuedo-code)

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;

Upvotes: 0

akf
akf

Reputation: 39485

Because you are not using if/else in your win conditions, you will always check the last if, and if that resolves to false, you will set win = false

Upvotes: 4

Related Questions