Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created May 11, 2013 16:33
Show Gist options
  • Select an option

  • Save dheaney/5560504 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/5560504 to your computer and use it in GitHub Desktop.
/* engine.c */
int indexToBoard(int index) {
int row, col;
row = index / 3;
col = index % 3;
return board[row][col];
}
int checkForWinPossibility(void) {
/* Horizontal Check */
for(i = 0, i < 7, i = i + 3) {
if((indexToBoard(i) == 2) && (indexToBoard(i + 1) == 0) && (indexToBoard(i + 2) == 0)) return i;
if((indexToBoard(i) == 0) && (indexToBoard(i + 1) == 2) && (indexToBoard(i + 2) == 0)) return i + 1;
if((indexToBoard(i) == 0) && (indexToBoard(i + 1) == 0) && (indexToBoard(i + 2) == 2)) return i + 2;
}
/* Vertical Check */
for(i = 0, i < 3, i++) {
if((indexToBoard(i) == 2) && (indexToBoard(i + 3) == 0) && (indexToBoard(i + 6) == 0)) return i;
if((indexToBoard(i) == 0) && (indexToBoard(i + 3) == 2) && (indexToBoard(i + 6) == 0)) return i + 3;
if((indexToBoard(i) == 0) && (indexToBoard(i + 3) == 0) && (indexToBoard(i + 6) == 2)) return i + 6;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment