Package info.kwarc.kalah
Class KalahState
- java.lang.Object
-
- info.kwarc.kalah.KalahState
-
public class KalahState extends Object
A Kalah board representation keeping track of turns. We are aware that one could also flip the board accordingly but went for the former since it's easier to use. Note that most methods are executed from the perspective of the player that is about to move. Moves are indexed from 0 to N-1 in sowing direction where N is the number of southern (northern) pits Note that this implementation is also ready for use with HashMaps.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKalahState.GameResultEnum for possible game outcomes.static classKalahState.PlayerThe players are North and South.
-
Constructor Summary
Constructors Constructor Description KalahState(int board_size, int seeds)Creates a board of @param board_size pits with @param seeds each.KalahState(KalahState state)Creates a copy of the given Board.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanUpOneSidedHouses()If either northern or southern houses are empty e.g. the game is over then the remaining seeds in southern houses are moved to the southern store and the remaining seeds in northern houses are moved to the northern store.voiddoMove(int move)Executes the given move by modifying the board.booleanequals(Object o)Returns true iff stores, houses and turn are perfectly equal e.g. same side to move, same stores, same houses.voidflip()Mirrors the board e.g. flips stores, houses and side to move.booleanflipIfNorthToMove()Flips the board if it's North's turn e.g. so that it's South's turn afterwards.voidflipIfWasFlipped(boolean wasFlipped)Useful helper function, so algorithms have to be implemented from souths point of view only.intgetBoardSize()Returns the number of southern pits.intgetHouse(KalahState.Player player, int index)intgetHouseSum()Returns the sum of seeds in all houses.intgetHouseSumNorth()Returns the sum of seeds in northern houses.intgetHouseSumSouth()Returns the sum of seeds in southern houses.ArrayList<Integer>getMoves()Returns an ArrayList containing the legal moves in sowing direction Moves are indexed from 0 to N-1 (in sowing direction).KalahState.PlayergetSideToMove()Returns the side to move.intgetStoreLead()Returns the difference between stores from the perspective of the player who is about to move e.g.intgetStoreNorth()Returns the number of seeds in northern store.intgetStoreSouth()Returns the number of seeds in southern store.inthashCode()Returns the hash code for board, not considering who is about to movebooleanisCaptureMove(int move)Returns true iff the move would be a capture.booleanisDoubleMove(int move)Returns true iff the last seed would end up in the store.booleanisLegalMove(int move)Return whether the given move is legal.intlowestLegalMove()Returns the leftmost legal move e.g. with the lowest index.intnumberOfMoves()Returns the number of legal moves.intrandomLegalMove()Returns a random legal move.KalahState.GameResultresult()Returns the GameResult from the player to move's perspective.voidsetHouse(KalahState.Player player, int index, int seeds)Sets the number of seeds in the given house.voidsetStoreNorth(int seeds)Sets the number of seeds in the northern store.voidsetStoreSouth(int seeds)Sets the number of seeds in the southern store.StringtoString()Returns a multiline representation of the board, including stores and side to move.inttotalSeeds()Returns the sum of all seeds, from both stores and all houses.
-
-
-
Constructor Detail
-
KalahState
public KalahState(int board_size, int seeds)Creates a board of @param board_size pits with @param seeds each.- Parameters:
board_size- Number of southern pits.seeds- Number of initial seeds in one pit.
-
KalahState
public KalahState(KalahState state)
Creates a copy of the given Board.- Parameters:
state- The board to copy.
-
-
Method Detail
-
flip
public void flip()
Mirrors the board e.g. flips stores, houses and side to move.
-
flipIfWasFlipped
public void flipIfWasFlipped(boolean wasFlipped)
Useful helper function, so algorithms have to be implemented from souths point of view only. Used together with flipIfNorthToMove().- Parameters:
wasFlipped- Whether the board should be flipped.
-
flipIfNorthToMove
public boolean flipIfNorthToMove()
Flips the board if it's North's turn e.g. so that it's South's turn afterwards. Useful helper function, so algorithms have to be implemented from souths point of view only. Used together with flipIfWasFlipped().- Returns:
- true if the board was flipped.
-
isLegalMove
public boolean isLegalMove(int move)
Return whether the given move is legal.- Parameters:
move- Move to check for legality.
-
lowestLegalMove
public int lowestLegalMove()
Returns the leftmost legal move e.g. with the lowest index. Moves are indexed from 0 to N-1 in sowing direction. Useful if you need any legal move.
-
randomLegalMove
public int randomLegalMove()
Returns a random legal move. Moves are indexed from 0 to N-1 in sowing direction.
-
numberOfMoves
public int numberOfMoves()
Returns the number of legal moves.
-
getMoves
public ArrayList<Integer> getMoves()
Returns an ArrayList containing the legal moves in sowing direction Moves are indexed from 0 to N-1 (in sowing direction).
-
isDoubleMove
public boolean isDoubleMove(int move)
Returns true iff the last seed would end up in the store.- Parameters:
move- The move to check. Moves are indexed from 0 to N-1 in sowing direction.
-
isCaptureMove
public boolean isCaptureMove(int move)
Returns true iff the move would be a capture.- Parameters:
move- The move to check. Moves are indexed from 0 to N-1 in sowing direction.
-
doMove
public void doMove(int move)
Executes the given move by modifying the board. Note that there is a constructor for copying from an existing board.- Parameters:
move- The move to execute. Moves are indexed from 0 to N-1 in sowing direction.
-
totalSeeds
public int totalSeeds()
Returns the sum of all seeds, from both stores and all houses.
-
result
public KalahState.GameResult result()
Returns the GameResult from the player to move's perspective.
-
cleanUpOneSidedHouses
public void cleanUpOneSidedHouses()
If either northern or southern houses are empty e.g. the game is over then the remaining seeds in southern houses are moved to the southern store and the remaining seeds in northern houses are moved to the northern store.
-
getStoreLead
public int getStoreLead()
Returns the difference between stores from the perspective of the player who is about to move e.g. positive if the player who is about to move has more seeds in their store.
-
getBoardSize
public int getBoardSize()
Returns the number of southern pits.
-
getSideToMove
public KalahState.Player getSideToMove()
Returns the side to move.
-
getStoreSouth
public int getStoreSouth()
Returns the number of seeds in southern store.
-
setStoreSouth
public void setStoreSouth(int seeds)
Sets the number of seeds in the southern store.- Parameters:
seeds- New number of seeds.
-
getStoreNorth
public int getStoreNorth()
Returns the number of seeds in northern store.
-
setStoreNorth
public void setStoreNorth(int seeds)
Sets the number of seeds in the northern store.- Parameters:
seeds- New number of seeds.
-
setHouse
public void setHouse(KalahState.Player player, int index, int seeds)
Sets the number of seeds in the given house.- Parameters:
index- Index of the pit (0 to N-1 in sowing direction).player- Which houses to set.seeds- Number of seeds to set.
-
getHouse
public int getHouse(KalahState.Player player, int index)
- Parameters:
index- Index of the pit (0 to N-1 in sowing direction).player- Which houses to set. Returns the number of seeds in this pit.
-
getHouseSumSouth
public int getHouseSumSouth()
Returns the sum of seeds in southern houses.
-
getHouseSumNorth
public int getHouseSumNorth()
Returns the sum of seeds in northern houses.
-
getHouseSum
public int getHouseSum()
Returns the sum of seeds in all houses.
-
hashCode
public int hashCode()
Returns the hash code for board, not considering who is about to move
-
equals
public boolean equals(Object o)
Returns true iff stores, houses and turn are perfectly equal e.g. same side to move, same stores, same houses.
-
-