Class BitBoard

java.lang.Object
connectfour.core.BitBoard
All Implemented Interfaces:
Board

@Immutable public class BitBoard extends Object implements Board
An immutable implementation of the Board interface that uses bit-shifting to store the whole board state in two longs. It takes up very little memory and most of its operations are very fast.
Author:
Finn Voichick
  • Constructor Details

    • BitBoard

      public BitBoard()
      Constructs an empty BitBoard, with no pieces on the board.
    • BitBoard

      public BitBoard​(String moves)
      Constructs a BitBoard with the moves represented by the given string. The moves are represented as described by Pascal Pons here: http://blog.gamesolver.org/solving-connect-four/02-test-protocol/
      Parameters:
      moves - a string representing the moves that have been played so far
  • Method Details

    • get

      public Optional<Player> get​(BoardLocation location)
      Description copied from interface: Board
      Gets the player who has played at the given location. If nobody has played at the given location, this method returns empty.
      Specified by:
      get in interface Board
      Parameters:
      location - the location in the board to examine
      Returns:
      the player who played there, or empty
    • getCurrentPlayer

      public Player getCurrentPlayer()
      Description copied from interface: Board
      Gets the player who is currently up to play. This will be the opponent of the player who has just played.
      Specified by:
      getCurrentPlayer in interface Board
      Returns:
      the player whose turn it is
    • canPlay

      public boolean canPlay​(int column)
      Description copied from interface: Board
      Determines if it is legal for the current player to play a piece in the given column. A column is a legal play if it is not full.
      Specified by:
      canPlay in interface Board
      Parameters:
      column - the column that is questionably full
      Returns:
      true if the given column is not full, otherwise false
    • getValidPlays

      public Set<Integer> getValidPlays()
      Description copied from interface: Board
      Gets a set of columns that are valid plays for the current player. In other words, this set contains all integers for which the Board.canPlay(int) method would return true.
      Specified by:
      getValidPlays in interface Board
      Returns:
      the set of all valid moves for the current player
    • createNextBoard

      public Board createNextBoard​(int column)
      Description copied from interface: Board
      Creates a new Board object that contains one additional piece. The current player of this next board will be the opponent of the current board.
      Specified by:
      createNextBoard in interface Board
      Parameters:
      column - the column in which to play
      Returns:
      the new board
    • getWinner

      public Optional<Player> getWinner()
      Description copied from interface: Board
      Gets the player who has won the game. If nobody has won, empty is returned.
      Specified by:
      getWinner in interface Board
      Returns:
      the player who has won, or empty
    • getTurnsPlayed

      public int getTurnsPlayed()
      Description copied from interface: Board
      Gets the number of turns that have been played since the start of the game. For an empty board, this will return 0. If one player has played, this will return 1. In other words, this is equivalent to the number of pieces on the board.
      Specified by:
      getTurnsPlayed in interface Board
      Returns:
      the number of turns that have been played since the start of the game
    • isFull

      public boolean isFull()
      Description copied from interface: Board
      Determines whether or not this board is completely full. A board is full if each of its columns is full, and Board.getValidPlays() would return an empty set.
      Specified by:
      isFull in interface Board
      Returns:
      true if the board is completely filled in otherwise false
    • getKey

      public long getKey()
      Gets a key that uniquely identifies this board.
      Returns:
      a unique long representing this board
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object