-2

For some reason the function the checkForWin is returning always a NO.

Because of that I am not able to retrieve the winner.

Else if suggest a different logic to judge winner I am using this function every time user puts up a symbol

-(BOOL) checkForWin{
        NSLog(@"yes");
        // HORIZONTAL WINS
        if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL))
        {
            return YES;
        }
        else if((s4.image == s5.image) & (s5.image == s6.image) & (s4.image != NULL))
        {
            return YES;
        }
        else if((s7.image == s8.image) & (s8.image == s9.image) & (s7.image != NULL))
        {
            return YES;
        }
        // VERTICAL WINS
        else if((s1.image == s4.image) & (s4.image == s7.image) & (s1.image != NULL))
        {
            return YES;
        }
        else if((s2.image == s5.image) & (s5.image == s8.image) & (s2.image != NULL))
        {
            return YES;
        }
        else if((s3.image == s6.image) & (s6.image == s9.image) & (s3.image != NULL))
        {
            return YES;
        }
        // DIAGONAL WINS
        else if((s1.image == s5.image) & (s5.image == s9.image) & (s1.image != NULL))
        {
            return YES;
        }
        else if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image != NULL))
        {
            return YES;
        }
        //right now return 1 becuase we havn't implemented this yet
        else{
        return NO;
        }
    }

    -(void)displayWinner{
        if([self checkForWin] == YES){
            if(playerToken==1){
                lbl2.text =@"X is the WINNER";
            } else {
                lbl2.text =@"O is the WINNER";
            }
        }

    }
Larme
  • 24,190
  • 6
  • 51
  • 81
supreet singh
  • 79
  • 1
  • 7
  • 2
    are you sure you want to boolean and the bits, instead of using a logical condition operator && ? not that it really makes a difference in this case.. – Henrik Aug 04 '15 at 07:38
  • oiii... what an ugly composition of an `if..else..` statement for checking winner combinations... it hurts my eye... :( – holex Aug 04 '15 at 08:05

2 Answers2

0

Maybe you can write

if([s1.image isEqual:s2.image2]){

}

this code instead of this control statement

if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL))
    {
        return YES;
    }
0

The answer by Yılmaz Gürsoy is probably correct.

The reason, is that you are comparing the references to the images, and not the image data. The references are probably not the same, while the data probably is.

He is suggesting that you compare the data instead of the references.

In my oppinion though, you should actually add a member to the s# object, containing a tristate value. could be an integer.

/**
  s.x is avalue indicating which image is shown (x, O, or empty)
  x > 0 means 'X'
  x == 0 means 'O'
  x < 0 means empty
**/

and then set it when you assign the image. and check against that. otherwise you will be wasting time comparing data, to get the exact same end result.

Community
  • 1
  • 1
Henrik
  • 2,180
  • 16
  • 29