- (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
0 B
|👍
/👎
0 B
|👍
/👎