Created
August 1, 2012 15:16
-
-
Save chunkyguy/3227715 to your computer and use it in GitHub Desktop.
The two checkWin methods accomplish the same task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Logic 1 */ | |
-(BOOL)funcOfBase:(int)b multiple:(int)n{ | |
return ((_slots[b]==_aType) && (_slots[b+n*1]==_aType) && (_slots[b+n*2])==_aType); | |
} | |
-(BOOL)checkWin{ | |
int bases[5] = {0, 1, 2, 3, 6}; | |
int multiples[5][4] = { | |
{1, 3, 4, 0}, | |
{3, 0, 0, 0}, | |
{2, 3, 0, 0}, | |
{1, 0, 0, 0}, | |
{1, 0, 0, 0} | |
}; | |
static int debug_toggle = 0; | |
BOOL win = NO; | |
for(int bno = 0; bno < 5 && !win; bno++){ | |
int base = bases[bno]; | |
for(int mno = 0; mno < 4 && !win; mno++){ | |
int mul = multiples[bno][mno]; | |
if(!mul) break; | |
win = [self funcOfBase:base multiple:mul]; | |
} | |
} | |
debug_toggle++; | |
return win; | |
} | |
/** Logic 2 */ | |
-(BOOL)isAlignWinnable:(int *)align{ | |
int winCases[8][3] = { | |
{0,1,2},{3,4,5},{6,7,8}, //row win | |
{0,3,6},{1,4,7},{2,5,8}, //column win | |
{0,4,8},{2,4,6} //diagnol win | |
}; | |
BOOL win = NO; | |
for(int caseno = 0; caseno < 8 && !win; caseno++){ | |
int matches = 0; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++) | |
if(winCases[caseno][i] == align[j]) | |
matches++; | |
} | |
win = (matches == 3)?YES:NO; | |
} | |
return win; | |
} | |
-(BOOL)checkWin{ | |
int align[3]; | |
int j = 0; | |
for(int i = 0; i < 9; i++){ | |
if(_slots[i] == _aType){ | |
align[j++] = i; | |
} | |
} | |
return (j < 3)?NO:[self isAlignWinnable:align]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment