Created
February 25, 2017 18:31
-
-
Save qaisjp/fd49ca4c029e2756261f254783999fd5 to your computer and use it in GitHub Desktop.
Some magnificently shite code
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
public class NoughtsAndCrosses { | |
boolean drawn; | |
int winner; | |
public NoughtsAndCrosses(int[][] board) { | |
// First check all columns | |
for (int x = 0; x < 3; x++) { | |
Integer winner = board[x][0]; | |
// Check if all items in this column are the same | |
for (int y = 0; y < 3; y++) { | |
if (board[x][y] == 0 || board[x][y] != winner) { | |
winner = null; | |
break; | |
} | |
} | |
if (winner != null) { | |
this.winner = winner; | |
return; | |
} | |
} | |
// First check all rows | |
for (int y = 0; y < 3; y++) { | |
Integer winner = board[0][y]; | |
// Check if all items in this row are the same | |
for (int x = 0; x < 3; x++) { | |
if (board[x][y] == 0 || board[x][y] != winner) { | |
winner = null; | |
break; | |
} | |
} | |
if (winner != null) { | |
this.winner = winner; | |
return; | |
} | |
} | |
// Check diag 1 | |
winner = (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == board[2][2]) ? board[0][0] : 0; | |
if (winner != 0) { | |
return; | |
} | |
// Check diag 2 | |
winner = (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == board[2][0]) ? board[2][0] : 0; | |
if (winner != 0) { | |
return; | |
} | |
drawn = true; | |
} | |
public boolean isDraw() { | |
return drawn; | |
} | |
public int whoWon() { | |
return winner; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment