Last active
August 29, 2015 14:08
-
-
Save keshavsaharia/1e4e59c42886cc537d5d to your computer and use it in GitHub Desktop.
TicTacToe board
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 Board { | |
private int[][] board; | |
private int current = 1; | |
private int empty = 9; | |
private int winner = 0; | |
/** | |
* Creates a new board. | |
*/ | |
public Board() { | |
board = new int[3][3]; | |
current = 1; | |
empty = 9; | |
} | |
/** | |
* Creates a copy of the given board. | |
* @param other - a reference to another board. | |
*/ | |
public Board(Board other) { | |
for (int x = 0 ; x < 3 ; x++) { | |
for (int y = 0 ; y < 3 ; y++) { | |
board[x][y] = other.board[x][y]; | |
} | |
} | |
current = other.current; | |
empty = other.empty; | |
} | |
/** | |
* Resets this board. | |
*/ | |
public void reset() { | |
for (int x = 0 ; x < 3 ; x++) { | |
for (int y = 0 ; y < 3 ; y++) { | |
board[x][y] = 0; | |
} | |
} | |
current = 1; | |
winner = 0; | |
} | |
/** | |
* Puts a piece on the given square and switch the current player if the move was made. | |
*/ | |
public boolean move(int x, int y) { | |
if (canMove(x, y)) { | |
board[x][y] = current; | |
empty--; | |
current = 1 + current % 2; | |
checkWinner(); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Applies the move object to this board. | |
*/ | |
public boolean move(Move m) { | |
return move(m.getX(), m.getY()); | |
} | |
/** | |
* Returns whether or not this board can make a move on the given square. | |
*/ | |
public boolean canMove(int x, int y) { | |
return board[x][y] == 0; | |
} | |
/** | |
* Returns whether or not this board can apply the given Move object. | |
*/ | |
public boolean canMove(Move m) { | |
return canMove(m.getX(), m.getY()); | |
} | |
/** | |
* Whether this board has any open moves. | |
*/ | |
public boolean isDraw() { | |
return empty == 0; | |
} | |
/** | |
* Whether this board has a winner. | |
*/ | |
public boolean isWon() { | |
return winner > 0; | |
} | |
/** | |
* Returns the winner of this board. | |
*/ | |
public int getWinner() { | |
return winner; | |
} | |
/** | |
* Checks for a winner on the board. | |
*/ | |
private void checkWinner() { | |
if (empty < 7) { | |
for (int row = 0 ; row < 3 ; row++) { | |
if (board[row][0] > 0 && board[row][0] == board[row][1] && board[row][1] == board[row][2]) { | |
winner = board[row][0]; | |
return; | |
} | |
} | |
for (int col = 0 ; col < 3 ; col++) { | |
if (board[0][col] > 0 && board[0][col] == board[1][col] && board[1][col] == board[2][col]) { | |
winner = board[0][col]; | |
return; | |
} | |
} | |
if (board[0][0] > 0 && board[0][0] == board[1][1] && board[1][1] == board[2][2]) { | |
winner = board[0][0]; | |
return; | |
} | |
if (board[2][0] > 0 && board[2][0] == board[1][1] && board[1][1] == board[0][2]) { | |
winner = board[2][0]; | |
return; | |
} | |
} | |
} | |
/** | |
* Returns a list of possible moves for this board. | |
*/ | |
public Move[] possibleMoves() { | |
int index = 0; | |
Move[] possible = new Move[empty]; | |
for (int x = 0 ; x < 3 ; x++) { | |
for (int y = 0 ; y < 3 ; y++) { | |
if (board[x][y] == 0) { | |
possible[index] = new Move(x, y); | |
index++; | |
} | |
} | |
} | |
return possible; | |
} | |
// Prints out the board. | |
public void print() { | |
for (int x = 0 ; x < 3 ; x++) { | |
for (int y = 0 ; y < 3 ; y++) { | |
if (board[x][y] == 0) { | |
System.out.print(" "); | |
} | |
if (board[x][y] == 1) { | |
System.out.print(" X "); | |
} | |
if (board[x][y] == 2) { | |
System.out.print(" O "); | |
} | |
if (y < 2) { | |
System.out.print("|"); | |
} | |
} | |
if (x < 2) { | |
System.out.println("\n---+---+---"); | |
} | |
} | |
System.out.println("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment