Package reversi

Class BitBoard

java.lang.Object
reversi.BitBoard
All Implemented Interfaces:
GameBoard

public class BitBoard
extends java.lang.Object
implements GameBoard
BitBoard Implementation of a Reversi GameBoard including useful helper functions.
See Also:
Arena
  • Field Summary

    Fields inherited from interface reversi.GameBoard

    EMPTY, GREEN, RED
  • Constructor Summary

    Constructors 
    Constructor Description
    BitBoard()
    Creates a new instance of BitBoard, initialised with the usual Reversi starting position with 4 stones.
    BitBoard​(boolean othelloStartPosition)
    Creates a new Instance of BitBoard with the usual Reversi/Othello starting position.
    BitBoard​(long gbRed, long gbGreen)
    Creates a new Instance of BitBoard
    BitBoard​(BitBoard gb)
    Creates a new Instance of BitBoard, which represents an exact copy of the game position of gb.
    BitBoard​(GameBoard gb)
    Creates a new Instance of BitBoard, which represents an exact copy of the game position of gb.
  • Method Summary

    Modifier and Type Method Description
    boolean checkMove​(int player, Coordinates c)
    Checks if player is allowed to place a stone at the position coord.
    GameBoard clone()
    Creates a copy of the game board This is useful to evaluate hypothetical moves.
    int countStones​(int player)
    Counts the number of stones for a given player.
    long getLegalInDirection​(long gbAttack, long gbDefend, int direction)
    Gets all legal Moves for a player in a given direction.
    long getLegalMoves​(int player)
    Gets a bit board with all the legal moves for a specific player.
    int getOccupation​(int row, int col)
    Returns the Occupation of a position on the game board.
    int getOccupation​(Coordinates c)
    Returns the Occupation of a position on the game board.
    int getSize()
    Returns the size of the game board.
    boolean isFull()
    Checks if the game board is full.
    boolean isMoveAvailable​(int player)
    Returns true, if a valid move exists for player
    void makeMove​(int player, Coordinates c)
    This method places a stone for the given player on the game board.
    int mobility​(int player)
    Returns the number of legal moves for a specific player.
    long moveBoard​(long board, int directionIndex)
    Helper function for checkMove.
    java.lang.String toString()
    Creates a multi-line String, which contains a text-representation of the game board.
    void updateBoard​(long gbRed, long gbGreen)  
    void updateBoard​(BitBoard gb)
    Clones the gamestate of a BitBoard including the cached legal moves and count of red and green stones.
    void updateBoard​(GameBoard gb)
    Clones the gamestate of a generic GameBoard
    void updateLegalMoves​(long gbAttack, long gbDefend, int playerIndex)
    Updates the Legal Moves for a specific Player.
    boolean validCoordinates​(Coordinates c)
    Checks if some given Coordinates are valid

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BitBoard

      public BitBoard()
      Creates a new instance of BitBoard, initialised with the usual Reversi starting position with 4 stones.
    • BitBoard

      public BitBoard​(GameBoard gb)
      Creates a new Instance of BitBoard, which represents an exact copy of the game position of gb.
      Parameters:
      gb - a given game position
    • BitBoard

      public BitBoard​(BitBoard gb)
      Creates a new Instance of BitBoard, which represents an exact copy of the game position of gb.
      Parameters:
      gb - a given game position
    • BitBoard

      public BitBoard​(long gbRed, long gbGreen)
      Creates a new Instance of BitBoard
      Parameters:
      redStones - positions of red stones
      greenStones - position s of green stones
    • BitBoard

      public BitBoard​(boolean othelloStartPosition)
      Creates a new Instance of BitBoard with the usual Reversi/Othello starting position.
      Parameters:
      othelloStartPosition - True: othello start position (diagonal) False: reversi start position (parallel)
  • Method Details

    • clone

      public GameBoard clone()
      Creates a copy of the game board This is useful to evaluate hypothetical moves.
      Specified by:
      clone in interface GameBoard
      Returns:
      eine Kopie des Spielbretts.
    • updateBoard

      public void updateBoard​(GameBoard gb)
      Clones the gamestate of a generic GameBoard
      Parameters:
      gb - The GameBoard we want to copy
    • updateBoard

      public void updateBoard​(BitBoard gb)
      Clones the gamestate of a BitBoard including the cached legal moves and count of red and green stones.
      Parameters:
      gb - The BitBoard we want to copy
    • updateBoard

      public void updateBoard​(long gbRed, long gbGreen)
    • getSize

      public int getSize()
      Returns the size of the game board. The game board is 2-dimensional square, therefore the size is equal for both directions.
      Specified by:
      getSize in interface GameBoard
      Returns:
      the side length of the game board
    • validCoordinates

      public boolean validCoordinates​(Coordinates c)
      Checks if some given Coordinates are valid
      Specified by:
      validCoordinates in interface GameBoard
      Parameters:
      coord - the Coordinates in question
      Returns:
      true if row and column of the coordinates are between 1 and GameBoard.getSize(), false otherwise.
    • getOccupation

      public int getOccupation​(Coordinates c) throws OutOfBoundsException
      Returns the Occupation of a position on the game board.
      Specified by:
      getOccupation in interface GameBoard
      Parameters:
      coord - The Coordinates, which specify the position on the game board.
      Returns:
      One of the constants GameBoard.RED, GameBoard.GREEN or GameBoard.EMPTY.
      Throws:
      OutOfBoundsException - Gets thrown if coord describes an invalid position. Valid positions are: 1<= row, col <= GameBoard.getSize().
    • getOccupation

      public int getOccupation​(int row, int col)
      Returns the Occupation of a position on the game board.
      Parameters:
      row - The row of the position.
      col - The column of the position.
      Returns:
      One of the constants GameBoard.RED, GameBoard.GREEN or GameBoard.EMPTY.
    • isFull

      public boolean isFull()
      Checks if the game board is full.
      Specified by:
      isFull in interface GameBoard
      Returns:
      true if it's full, false otherwise.
    • isMoveAvailable

      public boolean isMoveAvailable​(int player)
      Returns true, if a valid move exists for player
      Specified by:
      isMoveAvailable in interface GameBoard
      Parameters:
      player - Specifies the player (either GameBoard.RED or GameBoard.GREEN), for which we want to check.
      Returns:
      true if a valid move exists, false otherwise.
    • checkMove

      public boolean checkMove​(int player, Coordinates c)
      Checks if player is allowed to place a stone at the position coord.
      Specified by:
      checkMove in interface GameBoard
      Parameters:
      player - Specifies the player (either GameBoard.RED or GameBoard.GREEN)
      coord - The coordinates that are to be checked.
      Returns:
      true, if the move is valid, false otherwise.
    • getLegalMoves

      public long getLegalMoves​(int player)
      Gets a bit board with all the legal moves for a specific player. If the legal Moves for this player has already been calculated, it will return the cached bit board
      Parameters:
      player - Specifies the player (either GameBoard.RED or GameBoard.GREEN)
      Returns:
      bit board containing all legal move positions for that player
    • mobility

      public int mobility​(int player)
      Returns the number of legal moves for a specific player.
      Specified by:
      mobility in interface GameBoard
      Parameters:
      player - Specifies the player (either GameBoard.RED or GameBoard.GREEN)
      Returns:
      The number of legal moves for that player
    • updateLegalMoves

      public void updateLegalMoves​(long gbAttack, long gbDefend, int playerIndex)
      Updates the Legal Moves for a specific Player. All the legal Moves will be stored in legalMoves[playerIndex] and the legal Moves in specific directions will be stored in legalMovesDir[playerIndex][direction]
      Parameters:
      gbAttack - The bit board of the attacker.
      gbDefend - The bit board of the defendant.
      playerIndex - The index of the player (RED => 0, GREEN => 1)
    • getLegalInDirection

      public long getLegalInDirection​(long gbAttack, long gbDefend, int direction)
      Gets all legal Moves for a player in a given direction.
      Parameters:
      gbAttack - The bit board of the attacker.
      gbDefend - The bit board of the defendant.
      direction - The index for the direction (N => 0, NE => 1, SE => 2, ... , NW => 7)
      Returns:
    • moveBoard

      public long moveBoard​(long board, int directionIndex)
      Helper function for checkMove. Moves Board in a specific direction
      Parameters:
      board - The board we want to move
      amount - How far to move it
      Returns:
      The moved board
    • makeMove

      public void makeMove​(int player, Coordinates c)
      This method places a stone for the given player on the game board.
      Specified by:
      makeMove in interface GameBoard
      Parameters:
      player - Specifies the player (either GameBoard.RED or GameBoard.GREEN)
      coord - The Coordinates of the position we want to place the stone at.
      Throws:
      java.lang.IllegalArgumentException - if the player can't play at the given position.
    • countStones

      public int countStones​(int player)
      Counts the number of stones for a given player.
      Specified by:
      countStones in interface GameBoard
      Parameters:
      player - Specifies the player (either GameBoard.RED, GameBoard.GREEN or GameBoard.EMPTY)
      Returns:
      the number of stones of the given player on the game board.
    • toString

      public java.lang.String toString()
      Creates a multi-line String, which contains a text-representation of the game board.
      Overrides:
      toString in class java.lang.Object