Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created May 1, 2016 04:46
Show Gist options
  • Save bhnascar/25b9febb443b4abdad8fa8a52d0bb568 to your computer and use it in GitHub Desktop.
Save bhnascar/25b9febb443b4abdad8fa8a52d0bb568 to your computer and use it in GitHub Desktop.
Free response #6
Let's write (part of) a tic-tac-toe game!
Let's suppose the game board is represented using a 3x3 2D array of
integers like this:
______________________
| | | |
| 1 | O | O |
|______|______|______|
| | | |
| -1 | 1 | -1 |
|______|______|______|
| | | |
| -1 | 1 | O |
|______|______|______|
Here, a 1 represents an "X", and a "-1" represents a "O", and 0
means no player has played in that slot yet.
I want you to write a method that tells me, given any board, whether
someone has gotten 3-in-a-row yet. For example, in the above board,
if we add a 1 in row 0 column 1, then the player who plays 1 ("X")
will have 3-in-a-row.
/* Returns true if there are three of the same thing in a row
* (which means that there's a winner). Assume that the board is
* a valid game, i.e. there's not going to be like, only 1s or only
* -1s, and there's going to be the right number 1s and -1s like there
* should be when one player goes after the other.
*/
public static boolean isThereAWinner(int[][] board) {
/* TODO */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment