Last active
December 11, 2015 07:09
-
-
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
This file contains hidden or 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
| - (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