fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int board = TicTacToe.NORTH | TicTacToe.MID | TicTacToe.SOUTH |
  13. TicTacToe.NORTH << 9 | TicTacToe.MID << 9 | TicTacToe.SOUTH << 9;
  14. System.out.println("Board: " + board);
  15. System.out.println("Won: Player " + TicTacToe.checkWin(board));
  16. System.out.println("------");
  17.  
  18. board &= TicTacToe.BOARD;
  19. System.out.println("Board: " + board);
  20. System.out.println("Won: Player " + TicTacToe.checkWin(board));
  21. System.out.println("------");
  22.  
  23. board = 0;
  24. System.out.println("Board: " + board);
  25. System.out.println("Won: Player " + TicTacToe.checkWin(board));
  26. System.out.println("------");
  27.  
  28. board = 0;
  29. board = TicTacToe.setBoard(board, 1, TicTacToe.NORTH_WEST);
  30. board = TicTacToe.setBoard(board, 1, TicTacToe.MID);
  31. board = TicTacToe.setBoard(board, 1, TicTacToe.SOUTH_EAST);
  32. System.out.println("Board: " + board);
  33. System.out.println("Board: " + (board&TicTacToe.BOARD));
  34. System.out.println("Won: Player " + TicTacToe.checkWin(board));
  35. System.out.println("------");
  36.  
  37.  
  38. System.out.println("The one below here doesn't work ---------------------------");
  39. board = 0;
  40. board = TicTacToe.setBoard(board, 0, TicTacToe.NORTH_WEST);
  41. board = TicTacToe.setBoard(board, 0, TicTacToe.MID);
  42. board = TicTacToe.setBoard(board, 0, TicTacToe.SOUTH_EAST);
  43. System.out.println("Board: " + board);
  44. System.out.println("Board: " + (board&TicTacToe.BOARD));
  45. System.out.println("Won: Player " + TicTacToe.checkWin(board));
  46. System.out.println("------");
  47. }
  48. }
  49. class TicTacToe{
  50. /*
  51.   * The board looks like this
  52.   *
  53.   * 1 2 3
  54.   * 4 5 6
  55.   * 7 8 9
  56.   *
  57.   */
  58.  
  59. public final static int NORTH_WEST = 1 << 0;
  60. public final static int NORTH = 1 << 1;
  61. public final static int NORTH_EAST = 1 << 2;
  62. public final static int MID_WEST = 1 << 3;
  63. public final static int MID = 1 << 4;
  64. public final static int MID_EAST = 1 << 5;
  65. public final static int SOUTH_EAST = 1 << 6;
  66. public final static int SOUTH = 1 << 7;
  67. public final static int SOUTH_WEST = 1 << 8;
  68.  
  69. public final static int WIN_DIAGONAL_NW_SE = NORTH_EAST | MID | SOUTH_EAST;
  70. public final static int WIN_DIAGONAL_NE_SW = NORTH_WEST | MID | SOUTH_WEST;
  71. public final static int WIN_HORIZONTAL_1 = NORTH_WEST | NORTH | NORTH_EAST;
  72. public final static int WIN_HORIZONTAL_2 = MID_WEST | MID | MID_EAST;
  73. public final static int WIN_HORIZONTAL_3 = SOUTH_WEST | SOUTH | SOUTH_EAST;
  74. public final static int WIN_VERTICAL_1 = NORTH_WEST | MID_WEST | SOUTH_WEST;
  75. public final static int WIN_VERTICAL_2 = NORTH | MID | SOUTH;
  76. public final static int WIN_VERTICAL_3 = NORTH_EAST | MID_EAST | SOUTH_EAST;
  77.  
  78. public final static int BOARD =
  79. NORTH_WEST | NORTH | NORTH_EAST |
  80. MID_WEST | MID | MID_EAST |
  81. SOUTH_WEST | SOUTH | SOUTH_EAST;
  82.  
  83. public final static int[] POSSIBLE_WINS = {
  84. WIN_DIAGONAL_NE_SW, WIN_DIAGONAL_NW_SE,
  85. WIN_HORIZONTAL_1, WIN_HORIZONTAL_2, WIN_HORIZONTAL_3,
  86. WIN_VERTICAL_1, WIN_VERTICAL_2, WIN_VERTICAL_3
  87. };
  88.  
  89. /**
  90.   *
  91.   * @param board
  92.   * @return -1 for no win, 0 or 1 for the player that won
  93.   */
  94. public static int checkWin(int board){
  95. int state = board & BOARD;
  96. for(int x : POSSIBLE_WINS){
  97. if((x & state) == x) {
  98. return (board & (1 << (Integer.lowestOneBit(x) + 8))) > 0 ? 1 : 0;
  99. }
  100. }
  101. return -1;
  102. }
  103.  
  104. /**
  105.   *
  106.   * @param player player 1 or 0
  107.   * @param place the spot in the board to take
  108.   * @return the new board state
  109.   */
  110. public static int setBoard(int board, int player, int place){
  111. if(!(player == 0 || player == 1))
  112. throw new RuntimeException("Invalid Player. Must be 0 or 1.");
  113. if((place & BOARD) != place || place == 0)
  114. throw new RuntimeException("Invalid Place " + place);
  115. if(Integer.highestOneBit(place) != Integer.lowestOneBit(place))
  116. throw new RuntimeException("Invalid Place. Player " + player + " is attempting to steal multiple spots");
  117.  
  118. board |= place;
  119. if(player == 1)
  120. board |= (1 << (Integer.lowestOneBit(place) + 8));
  121. return board;
  122. }
  123. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Board: 74898
Won: Player 1
------
Board: 146
Won: Player 0
------
Board: 0
Won: Player -1
------
Board: 16778065
Board: 337
Won: Player 1
------
The one below here doesn't work ---------------------------
Board: 81
Board: 81
Won: Player -1
------