Created
October 1, 2014 01:11
-
-
Save tnsicdr/e51cccfc501efcc60813 to your computer and use it in GitHub Desktop.
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
| public GameStatus getGameStatus() { | |
| // iterate for both players | |
| for(int p = 0; p < 2; p++) { | |
| int counter = 0; | |
| // Check1 | |
| for(int r = 0; r < 3; r++) { | |
| // rows 0,1,2 | |
| for (int c = 0; c < 3; c++) { | |
| // columns 0,1,2 | |
| if(board[r][c] == playerPiece(p)) { | |
| counter++; | |
| } else { | |
| counter = 0; | |
| } | |
| } | |
| } | |
| } | |
| // if we've made it this far, the game still has empty spaces | |
| // or no spaces | |
| for(int r = 0;r < board.length;r++) { | |
| for(int c = 0;c < board[0].length;c++) { | |
| if(board[r][c] == Cell.EMPTY) | |
| return GameStatus.IN_PROGRESS; | |
| } | |
| } | |
| // still haven't exited, so cat game | |
| return GameStatus.CATS; | |
| } | |
| private Cell playerPiece(int player) { | |
| if(player == 0) | |
| return Cell.X; | |
| if(player == 1) | |
| return Cell.O; | |
| else | |
| throw new IllegalArgumentException(Integer.toString(player)); | |
| } | |
| private GameStatus playerWin(int player) { | |
| if(player == 0) | |
| return GameStatus.X_WON; | |
| if(player == 1) | |
| return GameStatus.O_WON; | |
| else | |
| throw new IllegalArgumentException(Integer.toString(player)); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment