Skip to content

Instantly share code, notes, and snippets.

@ntsh
Last active December 11, 2015 07:09
Show Gist options
  • Select an option

  • Save ntsh/4564746 to your computer and use it in GitHub Desktop.

Select an option

Save ntsh/4564746 to your computer and use it in GitHub Desktop.
Objective C Tic Tac Toe code for checking if particular move (x,y) by the player was a winning move. where player = 'X' or 'O' TicTacToe size = 3x3 matrix contains the current state of the TicTacToe
- (int) checkWin:(char)player :(int)x :(int)y
{
int row = 0;
int col = 0;
int diag = 0;
int rdiag = 0;
for (int i = 0; i < 3; i++)
{
if(matrix[x][i] == player) col++;
if(matrix[i][y] == player) row++;
if(matrix[i][i] == player) diag++;
if(matrix[i][2-i] == player) rdiag++;
}
if(row == 3 || col == 3 || diag == 3 || rdiag == 3)
return 1; //win
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment