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
-
Constructor Summary
Constructors Constructor Description BitBoard()
Creates a new instance ofBitBoard
, initialised with the usual Reversi starting position with 4 stones.BitBoard(boolean othelloStartPosition)
Creates a new Instance ofBitBoard
with the usual Reversi/Othello starting position.BitBoard(long gbRed, long gbGreen)
Creates a new Instance ofBitBoard
BitBoard(BitBoard gb)
Creates a new Instance ofBitBoard
, which represents an exact copy of the game position ofgb
.BitBoard(GameBoard gb)
Creates a new Instance ofBitBoard
, which represents an exact copy of the game position ofgb
. -
Method Summary
Modifier and Type Method Description boolean
checkMove(int player, Coordinates c)
Checks ifplayer
is allowed to place a stone at the positioncoord
.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)
Returnstrue
, if a valid move exists forplayer
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-lineString
, which contains a text-representation of the game board.void
updateBoard(long gbRed, long gbGreen)
void
updateBoard(BitBoard gb)
Clones the gamestate of aBitBoard
including the cached legal moves and count of red and green stones.void
updateBoard(GameBoard gb)
Clones the gamestate of a genericGameBoard
void
updateLegalMoves(long gbAttack, long gbDefend, int playerIndex)
Updates the Legal Moves for a specific Player.boolean
validCoordinates(Coordinates c)
Checks if some givenCoordinates
are valid
-
Constructor Details
-
BitBoard
public BitBoard()Creates a new instance ofBitBoard
, initialised with the usual Reversi starting position with 4 stones. -
BitBoard
Creates a new Instance ofBitBoard
, which represents an exact copy of the game position ofgb
.- Parameters:
gb
- a given game position
-
BitBoard
Creates a new Instance ofBitBoard
, which represents an exact copy of the game position ofgb
.- Parameters:
gb
- a given game position
-
BitBoard
public BitBoard(long gbRed, long gbGreen)Creates a new Instance ofBitBoard
- Parameters:
redStones
- positions of red stonesgreenStones
- position s of green stones
-
BitBoard
public BitBoard(boolean othelloStartPosition)Creates a new Instance ofBitBoard
with the usual Reversi/Othello starting position.- Parameters:
othelloStartPosition
- True: othello start position (diagonal) False: reversi start position (parallel)
-
-
Method Details
-
clone
Creates a copy of the game board This is useful to evaluate hypothetical moves. -
updateBoard
Clones the gamestate of a genericGameBoard
- Parameters:
gb
- TheGameBoard
we want to copy
-
updateBoard
Clones the gamestate of aBitBoard
including the cached legal moves and count of red and green stones.- Parameters:
gb
- TheBitBoard
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. -
validCoordinates
Checks if some givenCoordinates
are valid- Specified by:
validCoordinates
in interfaceGameBoard
- Parameters:
coord
- theCoordinates
in question- Returns:
true
if row and column of the coordinates are between 1 andGameBoard.getSize()
,false
otherwise.
-
getOccupation
Returns the Occupation of a position on the game board.- Specified by:
getOccupation
in interfaceGameBoard
- Parameters:
coord
- TheCoordinates
, which specify the position on the game board.- Returns:
- One of the constants
GameBoard.RED
,GameBoard.GREEN
orGameBoard.EMPTY
. - Throws:
OutOfBoundsException
- Gets thrown ifcoord
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
orGameBoard.EMPTY
.
-
isFull
public boolean isFull()Checks if the game board is full. -
isMoveAvailable
public boolean isMoveAvailable(int player)Returnstrue
, if a valid move exists forplayer
- Specified by:
isMoveAvailable
in interfaceGameBoard
- Parameters:
player
- Specifies the player (eitherGameBoard.RED
orGameBoard.GREEN
), for which we want to check.- Returns:
true
if a valid move exists,false
otherwise.
-
checkMove
Checks ifplayer
is allowed to place a stone at the positioncoord
.- Specified by:
checkMove
in interfaceGameBoard
- Parameters:
player
- Specifies the player (eitherGameBoard.RED
orGameBoard.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 (eitherGameBoard.RED
orGameBoard.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 interfaceGameBoard
- Parameters:
player
- Specifies the player (eitherGameBoard.RED
orGameBoard.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 moveamount
- How far to move it- Returns:
- The moved board
-
makeMove
This method places a stone for the given player on the game board.- Specified by:
makeMove
in interfaceGameBoard
- Parameters:
player
- Specifies the player (eitherGameBoard.RED
orGameBoard.GREEN
)coord
- TheCoordinates
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 interfaceGameBoard
- Parameters:
player
- Specifies the player (eitherGameBoard.RED
,GameBoard.GREEN
orGameBoard.EMPTY
)- Returns:
- the number of stones of the given player on the game board.
-
toString
public java.lang.String toString()Creates a multi-lineString
, which contains a text-representation of the game board.- Overrides:
toString
in classjava.lang.Object
-