title: Tic Tac Toe in Integer
body: <pre><code>public class TicTacToe{
      /*
       * The board looks like this
       * 
       * 1 2 3
       * 4 5 6
       * 7 8 9
       * 
       */
  
      public final static int NORTH_WEST   = 1 &lt;&lt; 0;
      public final static int NORTH        = 1 &lt;&lt; 1;
      public final static int NORTH_EAST   = 1 &lt;&lt; 2;
      public final static int MID_WEST     = 1 &lt;&lt; 3;
      public final static int MID          = 1 &lt;&lt; 4;
      public final static int MID_EAST     = 1 &lt;&lt; 5;
      public final static int SOUTH_EAST   = 1 &lt;&lt; 6;
      public final static int SOUTH        = 1 &lt;&lt; 7;
      public final static int SOUTH_WEST   = 1 &lt;&lt; 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 &amp; BOARD;
          for(int x : POSSIBLE_WINS){
              if((x &amp; state) == x) {
                  return (board &amp; (1 &lt;&lt; (Integer.lowestOneBit(x) + 8))) &gt; 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))
              throw new RuntimeException("Invalid Player. Must be 0 or 1.");
          if((place &amp; BOARD) != place || place == 0)
              throw new RuntimeException("Invalid Place " + place);
          if(Integer.highestOneBit(place) != Integer.lowestOneBit(place))
              throw new RuntimeException("Invalid Place. Player " + player + " is attempting to steal multiple spots");
  
  
          board |= place;
          if(player == 1)
              board |= place &lt;&lt; 9;
          else
              board &amp;= ~(place &lt;&lt; 9);
  
          return board;
      }
  }
  </code></pre>
  
  <p>main method</p>
  
  <pre><code>public static void main(String[] args) {
      int board = TicTacToe.NORTH      | TicTacToe.MID      | TicTacToe.SOUTH |
                  TicTacToe.NORTH &lt;&lt; 9 | TicTacToe.MID &lt;&lt; 9 | TicTacToe.SOUTH &lt;&lt; 9;
      System.out.println("Board: " + board);
      System.out.println("Won: Player " + TicTacToe.checkWin(board));
      System.out.println("------");
  
      board &amp;= TicTacToe.BOARD;
      System.out.println("Board: " + board);
      System.out.println("Won: Player " + TicTacToe.checkWin(board));
      System.out.println("------");
  
      board = 0;
      System.out.println("Board: " + board);
      System.out.println("Won: Player " + TicTacToe.checkWin(board));
      System.out.println("------");
  
      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&amp;TicTacToe.BOARD));
      System.out.println("Won: Player " + TicTacToe.checkWin(board));
      System.out.println("------");
  
  
      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&amp;TicTacToe.BOARD));
      System.out.println("Won: Player " + TicTacToe.checkWin(board));
      System.out.println("------");
  }
  </code></pre>
  
  <p>link to ideone <a href="https://ideone.com/nPsWGO" rel="nofollow">https://ideone.com/nPsWGO</a>
  this is what it prints out</p>
  
  <pre><code>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
          ------
  </code></pre>
  
  <p>You don't have to look at all of the code, just look at my <code>setBoard</code> func because it works for player 1 but not player 2. I know check win works because I tested it in the first the examples in the output above. 
  The goal of this is to store the entire game code in a single integer, which I will show below.</p>
  
  <pre><code>11111111111111111111111111111111
  [p1 s ][p2 s ][player ][board  ]
  [7 bits]p1 s   = player 1 score
  [7 bits]p2 s   = player 2 score
  [9 bits]player = the board shifted left 9 which player is holding the spot
  [9 bits]board  = the actual board
  </code></pre>