//Given a state, an initial position and a target position, returns true if the white player //can move a checker from the initial position to the target position, or false if not. private static bool IsLegalToMoveToPosition(GameBoardState state, int fromPosition, int toPosition) { //If the target position is between -5 and 0, then the white player is trying to bear off a checker if (toPosition <= 0 && toPosition >= -5) { return(IsLegalToBearOff(state, fromPosition, toPosition)); } //If not, make sure the position is on the board and that there are less than two enemy checkers there if (toPosition < 1 || toPosition > 24) { return(false); } return(state.getMainBoard()[toPosition - 1] > -2); }
internal static string CreateXmlForGameBoardState(GameBoardState state, string rootTag) { if (state == null) { throw new ArgumentNullException("state"); } if (rootTag == null) { throw new ArgumentNullException("rootTag"); } //Separating each element of the array with a space, and removes trailing spaces var board = state.getMainBoard().Select(i => i + " ").Aggregate((a, b) => a + b).Trim(); //Wrap the board with tags board = "<board>" + board + "</board>"; int whiteGoal = state.GetCheckersOnPosition(White.BearOffPositionID()); int whiteBar = state.GetCheckersOnPosition(White.GetBar()); //The agreed format is that black checkers on bar and target should be represented as negative int blackGoal = state.GetCheckersOnPosition(Black.BearOffPositionID()); blackGoal = Math.Min(blackGoal, blackGoal * -1); int blackBar = state.GetCheckersOnPosition(Black.GetBar()); blackBar = Math.Min(blackBar, blackBar * -1); //Wrap each of the above four values in their own tags var rest = String.Format("<whiteGoal>{0}</whiteGoal><whiteBar>{1}</whiteBar><blackGoal>{2}</blackGoal><blackBar>{3}</blackBar>", whiteBar, whiteGoal, blackBar, blackGoal); //Wrapping the entire message in the supplied root tags if (rootTag == "") { return(board + rest); } else { return(String.Format("<{0}>" + board + rest + "</{0}>", rootTag)); } }
//Given a state and a position, returns true if it is legal for the white player can move a checker from that position private static bool IsLegalToMoveFromPosition(GameBoardState state, int position) { //If the position is the bar, there has to be at least one checker on the bar if (position == WHITE.GetBar()) { return(state.getCheckersOnBar(WHITE) > 0); } //If not, there cannot be any checkers on the bar, the position has to be on the board, and there has to be at //least one checker on that position else { if (state.getCheckersOnBar(WHITE) > 0) { return(false); } if (position < 1 || position > 24) { return(false); } return(state.getMainBoard()[position - 1] > 0); } }