redribben icon

checkForWin

redribben | PRO | 10/31/15 01:40:55 AM UTC | 0 ⭐ | 7640 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

- (int)checkForWin {
    int i;
    //    Horizontal Check
    for( i = 0; i < 7; i = i + 3 ) {
        if (([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+1]) && ([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+2])) {
            return [self whoWonAtIndex:i];
        }
    }
    
    //    Vertical Check
    for( i = 0; i < 4; i = i + 1 ) {
        if (([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+3]) && ([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+6])) {
            return [self whoWonAtIndex:i];
        }
    }
    
    //    Diagonals
    if (([xoSpots objectAtIndex:0] == [xoSpots objectAtIndex:4]) && ([xoSpots objectAtIndex:4] == [xoSpots objectAtIndex:8])) {
        return [self whoWonAtIndex:0];
    }
    
    else if (([xoSpots objectAtIndex:2] == [xoSpots objectAtIndex:4]) && ([xoSpots objectAtIndex:4] == [xoSpots objectAtIndex:6])) {
        return [self whoWonAtIndex:2];
    }
    
    //    No Winner Yet
    return 0;
}
 
- (int)whoWonAtIndex:(int)oneOfWinningSpaces {
    NSString *winner = [xoSpots objectAtIndex:oneOfWinningSpaces];
    if ([winner  isEqual: @"o"]) {
        return 1;
    }
    else
        return 2;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎