/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
int board = TicTacToe.NORTH | TicTacToe.MID | TicTacToe.SOUTH |
TicTacToe.NORTH << 9 | TicTacToe.MID << 9 | TicTacToe.SOUTH << 9;
System.
out.
println("Board: " + board
); System.
out.
println("Won: Player " + TicTacToe.
checkWin(board
));
board &= TicTacToe.BOARD;
System.
out.
println("Board: " + board
); System.
out.
println("Won: Player " + TicTacToe.
checkWin(board
));
board = 0;
System.
out.
println("Board: " + board
); System.
out.
println("Won: Player " + TicTacToe.
checkWin(board
));
board = 0;
board = TicTacToe.setBoard(board, 1, TicTacToe.NORTH_WEST);
board = TicTacToe.setBoard(board, 1, TicTacToe.MID);
board = TicTacToe.setBoard(board, 1, TicTacToe.SOUTH_EAST);
System.
out.
println("Board: " + board
); System.
out.
println("Board: " + (board
&TicTacToe.
BOARD)); System.
out.
println("Won: Player " + TicTacToe.
checkWin(board
));
System.
out.
println("The one below here doesn't work ---------------------------"); board = 0;
board = TicTacToe.setBoard(board, 0, TicTacToe.NORTH_WEST);
board = TicTacToe.setBoard(board, 0, TicTacToe.MID);
board = TicTacToe.setBoard(board, 0, TicTacToe.SOUTH_EAST);
System.
out.
println("Board: " + board
); System.
out.
println("Board: " + (board
&TicTacToe.
BOARD)); System.
out.
println("Won: Player " + TicTacToe.
checkWin(board
)); }
}
class TicTacToe{
/*
* The board looks like this
*
* 1 2 3
* 4 5 6
* 7 8 9
*
*/
public final static int NORTH_WEST = 1 << 0;
public final static int NORTH = 1 << 1;
public final static int NORTH_EAST = 1 << 2;
public final static int MID_WEST = 1 << 3;
public final static int MID = 1 << 4;
public final static int MID_EAST = 1 << 5;
public final static int SOUTH_EAST = 1 << 6;
public final static int SOUTH = 1 << 7;
public final static int SOUTH_WEST = 1 << 8;
public final static int WIN_DIAGONAL_NW_SE = NORTH_EAST | MID | SOUTH_EAST;
public final static int WIN_DIAGONAL_NE_SW = NORTH_WEST | MID | SOUTH_WEST;
public final static int WIN_HORIZONTAL_1 = NORTH_WEST | NORTH | NORTH_EAST;
public final static int WIN_HORIZONTAL_2 = MID_WEST | MID | MID_EAST;
public final static int WIN_HORIZONTAL_3 = SOUTH_WEST | SOUTH | SOUTH_EAST;
public final static int WIN_VERTICAL_1 = NORTH_WEST | MID_WEST | SOUTH_WEST;
public final static int WIN_VERTICAL_2 = NORTH | MID | SOUTH;
public final static int WIN_VERTICAL_3 = NORTH_EAST | MID_EAST | SOUTH_EAST;
public final static int BOARD =
NORTH_WEST | NORTH | NORTH_EAST |
MID_WEST | MID | MID_EAST |
SOUTH_WEST | SOUTH | SOUTH_EAST;
public final static int[] POSSIBLE_WINS = {
WIN_DIAGONAL_NE_SW, WIN_DIAGONAL_NW_SE,
WIN_HORIZONTAL_1, WIN_HORIZONTAL_2, WIN_HORIZONTAL_3,
WIN_VERTICAL_1, WIN_VERTICAL_2, WIN_VERTICAL_3
};
/**
*
* @param board
* @return -1 for no win, 0 or 1 for the player that won
*/
public static int checkWin(int board){
int state = board & BOARD;
for(int x : POSSIBLE_WINS){
if((x & state) == x) {
return (board
& (1 << (Integer.
lowestOneBit(x
) + 8))) > 0 ? 1 : 0; }
}
return -1;
}
/**
*
* @param player player 1 or 0
* @param place the spot in the board to take
* @return the new board state
*/
public static int setBoard(int board, int player, int place){
if(!(player == 0 || player == 1))
if((place & BOARD) != place || place == 0)
throw new RuntimeException("Invalid Place. Player " + player
+ " is attempting to steal multiple spots");
board |= place;
if(player == 1)
board
|= (1 << (Integer.
lowestOneBit(place
) + 8)); return board;
}
}