redribben

checkForWin

Oct 30th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (int)checkForWin {
  2.     int i;
  3.     //    Horizontal Check
  4.     for( i = 0; i < 7; i = i + 3 ) {
  5.         if (([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+1]) && ([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+2])) {
  6.             return [self whoWonAtIndex:i];
  7.         }
  8.     }
  9.    
  10.     //    Vertical Check
  11.     for( i = 0; i < 4; i = i + 1 ) {
  12.         if (([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+3]) && ([xoSpots objectAtIndex:i] == [xoSpots objectAtIndex:i+6])) {
  13.             return [self whoWonAtIndex:i];
  14.         }
  15.     }
  16.    
  17.     //    Diagonals
  18.     if (([xoSpots objectAtIndex:0] == [xoSpots objectAtIndex:4]) && ([xoSpots objectAtIndex:4] == [xoSpots objectAtIndex:8])) {
  19.         return [self whoWonAtIndex:0];
  20.     }
  21.    
  22.     else if (([xoSpots objectAtIndex:2] == [xoSpots objectAtIndex:4]) && ([xoSpots objectAtIndex:4] == [xoSpots objectAtIndex:6])) {
  23.         return [self whoWonAtIndex:2];
  24.     }
  25.    
  26.     //    No Winner Yet
  27.     return 0;
  28. }
  29.  
  30. - (int)whoWonAtIndex:(int)oneOfWinningSpaces {
  31.     NSString *winner = [xoSpots objectAtIndex:oneOfWinningSpaces];
  32.     if ([winner  isEqual: @"o"]) {
  33.         return 1;
  34.     }
  35.     else
  36.         return 2;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment