public GameBoardState(int[] mainBoard, int whiteCheckersOnBar, int whiteCheckersOnTarget, int blackCheckersOnBar, int blackCheckersOnTarget) { //Checking that both players have exactly 15 checkers on the board int numberOfWhiteCheckers = mainBoard .Where(i => i > 0) //Filtering out all positions that have negative numbers, or black checkers, on them .Sum() + whiteCheckersOnBar + whiteCheckersOnTarget; int numberOfBlackCheckers = mainBoard .Where(i => i < 0) //Filtering out all positions that have positive numbers / white checkers .Sum() * -1 //Making the black checkers count as positive + blackCheckersOnBar + blackCheckersOnTarget; if (numberOfWhiteCheckers != NUMBER_OF_CHECKERS_PER_PLAYER || numberOfBlackCheckers != NUMBER_OF_CHECKERS_PER_PLAYER) { throw new InvalidOperationException("There is not the expected number of checkers. There are " + numberOfWhiteCheckers + " white checkers and " + numberOfBlackCheckers + " black checkers"); } //Adding all the supplied information to the dictionary gameBoard for (int i = 0; i < mainBoard.Length; i++) { gameBoard.Add(i + 1, mainBoard[i]); } gameBoard.Add(White.GetBar(), whiteCheckersOnBar); gameBoard.Add(Black.GetBar(), blackCheckersOnBar); gameBoard.Add(White.BearOffPositionID(), whiteCheckersOnTarget); gameBoard.Add(Black.BearOffPositionID(), blackCheckersOnTarget); }
//Given a position and a color, assumes the position is with regards to the other color and //converts it to the perspective of the given color private static int convertTo(CheckerColor color, int i) { if (color == BLACK) { if (i == WHITE.GetBar()) { return(BLACK.GetBar()); } if (i == WHITE.BearOffPositionID()) { return(BLACK.BearOffPositionID()); } return(25 - i); } if (i == color.OppositeColor().GetBar()) { return(color.GetBar()); } if (i == color.OppositeColor().BearOffPositionID()) { return(color.BearOffPositionID()); } return(25 - i); }
internal static IEnumerable <int> GetMoveableCheckers(GameBoardState state, CheckerColor color, List <int> moves) { HashSet <int> output = new HashSet <int>(); if (new MovesCalculator(state, color, color.GetBar(), moves).GetReachablePositions().Count() > 0) { output.Add(color.GetBar()); } for (int i = 1; i <= 24; i++) { if (new MovesCalculator(state, color, i, moves).GetReachablePositions().Count() > 0) { output.Add(i); } } return(output); }
internal void HighlightChecker(CheckerColor color, int i) { if (i == color.GetBar()) { HighlightImage(GetOnBarPoint(color).GetTopChecker()); } else { HighlightImage(Points[i].GetTopChecker()); } }
//Performs a move and returns the resulting state. If the move is illegal, null will be returned instead. internal static GameBoardState Move(GameBoardState state, CheckerColor color, int initialPosition, int distance) { //Since moving a checker from the black player is identical to moving a checker from the white player, if the white player's //situation was the same as the black player, the game board and input is inverted so this is the case, and the code for //moving a white checker is reused if (color == BLACK) { //Transforming the input state = state.InvertColor(); initialPosition = convertTo(WHITE, initialPosition); state = Move(state, WHITE, initialPosition, distance); //Returns null if the move is invalid, else transforms the move //back into the perspective of the black player and returns it. return(state == null ? null : state.InvertColor()); } //The position of the white bar relative to the board is 25, as 24 is the first position //for white when moving from the bar int initialPositionRelativeToBoard = initialPosition == WHITE.GetBar() ? 25 : initialPosition; int targetPosition = initialPositionRelativeToBoard - distance; if (IsLegalMove(state, initialPosition, targetPosition)) { //The previous definition of the target position is useful for determining whether or not //A move is legal. However, we risk getting negative values, for example when //the white player bears off a checker on position 4 using a die that shows 5 //Therefore the /actual/ target position is found using the below method call targetPosition = GetPositionAfterMove(color, initialPosition, distance); return(state.MoveChecker(initialPosition, targetPosition)); } else { return(null); } }
public int getCheckersOnBar(CheckerColor color) { return(GetCheckersOnPosition(color.GetBar())); }
internal static Move CapturingMove(CheckerColor color, int from) { return(new Move(color, from, color.GetBar())); }