/// <summary> /// Executes a valid move of the active player. /// </summary> /// <param name="player1">Player 1</param> /// <param name="player2">Player 2</param> /// <param name="_checker">Selected checker to be moved</param> /// <param name="_move">Selected move (according to one of the dice values)</param> /// <returns>Success (true) if the active player has won the game, otherwise false</returns> public static bool ExecuteMove(ref NetworkBackgammonPlayer player1, ref NetworkBackgammonPlayer player2, NetworkBackgammonChecker _checker, NetworkBackgammonDice _move) { // Determine the active player NetworkBackgammonPlayer activePlayer = player1.Active ? player1 : player2; NetworkBackgammonPlayer waitingPlayer = player1.Active ? player2 : player1; bool bActivePlayerWon = true; // Find active player's checker that has been selected to move foreach (NetworkBackgammonChecker checkerAP in activePlayer.Checkers) { // Found checker, now find checker's move that has been selected if (checkerAP == _checker) { foreach (NetworkBackgammonDice move in checkerAP.PossibleMoves) { // Found move if (move == _move) { checkerAP.MoveChecker(_move); if (checkerAP.CurrentPosition.CurrentPosition >= NetworkBackgammonPosition.GameBoardPosition.NORMAL_START && checkerAP.CurrentPosition.CurrentPosition <= NetworkBackgammonPosition.GameBoardPosition.NORMAL_END) { // Check whether active player kicked checker of the waiting player onto the bar foreach (NetworkBackgammonChecker checkerWP in waitingPlayer.Checkers) { if (checkerWP.CurrentPosition.GetOppositePosition().CurrentPosition == checkerAP.CurrentPosition.CurrentPosition) { checkerWP.CurrentPosition.Reset(); } } } break; } } break; } } // Check whether active player has won the game foreach (NetworkBackgammonChecker checker in activePlayer.Checkers) { if (checker.CurrentPosition.CurrentPosition != NetworkBackgammonPosition.GameBoardPosition.OFFBOARD) { bActivePlayerWon = false; break; } } return(bActivePlayerWon); }
public NetworkBackgammonChip(CHIP_TYPE chipType, NetworkBackgammonChecker checker) { // Load the icon directly from the manifest resource Icon blackChipIcon = new Icon(this.GetType(), (chipType == CHIP_TYPE.OPPONENT_1 ? "Resources.BlackChip.ico" : "Resources.WhipChip.ico")); // Set the chip bitmap m_chipBitmap = new Bitmap(blackChipIcon.ToBitmap()); // Set the chip size based on the image size ChipSize = new Size(blackChipIcon.ToBitmap().Width, blackChipIcon.ToBitmap().Height); m_checker = checker; }
private void listBoxCheckers_SelectedIndexChanged(object sender, EventArgs e) { listBoxMoves.Items.Clear(); try { NetworkBackgammonChecker selectedChecker = (NetworkBackgammonChecker)listBoxCheckers.SelectedItem; foreach (NetworkBackgammonDice diceValue in selectedChecker.PossibleMoves) { listBoxMoves.Items.Add(diceValue); } } catch (Exception ex) { listBoxLog.Items.Add(ex.Message); } }
/// <summary> /// Verifies a list of checkers and possible moves against a verified list of checkers and possible moves /// </summary> /// <param name="checkersToVerify">List of checkers and possible moves to be verified</param> /// <param name="checkersVerified">List of verified checkers and possible moves</param> /// <param name="returnMessage">Additional detail information if verification fails</param> /// <returns>"True" if both lists (checkers and possible moves) match, otherwise "false"</returns> private bool VerifyCheckersAndPossibleMoves(List <NetworkBackgammonChecker> checkersToVerify, List <NetworkBackgammonChecker> checkersVerified, ref string returnMessage) { bool bRetVal = false; // Does the number of checkers match? bRetVal = checkersVerified.Count == checkersToVerify.Count; if (bRetVal) { List <NetworkBackgammonChecker> checkersCheckOffList = new List <NetworkBackgammonChecker>(); // Build a list of checkers that need to be verified (and checked off once verified) foreach (NetworkBackgammonChecker checker in checkersVerified) { checkersCheckOffList.Add(checker); } // Loop through the list of checkers to be verified foreach (NetworkBackgammonChecker checkerToVerify in checkersToVerify) { NetworkBackgammonChecker checkerToCheckoff = null; // Loop through the list of verified checkers to find the matching one foreach (NetworkBackgammonChecker checkerVerified in checkersCheckOffList) { // Found matching checker? if (checkerVerified.CurrentPosition == checkerToVerify.CurrentPosition) { // Number of possible moves matching? if (checkerVerified.PossibleMoves.Count == checkerToVerify.PossibleMoves.Count) { // We can potentially check this one off, unless we find the actual possible moves list not matching checkerToCheckoff = checkerVerified; // Sort possible moves checkerVerified.PossibleMoves.Sort(); checkerToVerify.PossibleMoves.Sort(); // Verify whether the list of actual possible moves matches for (int i = 0; i < checkerVerified.PossibleMoves.Count; i++) { // Found one non-matching item if (checkerVerified.PossibleMoves[i].CurrentValue != checkerToVerify.PossibleMoves[i].CurrentValue) { // So, we cannot check it off checkerToCheckoff = null; returnMessage += "Not all possible moves of verified checkers and checkers to be verified match for position " + checkerVerified.CurrentPosition.ToString() + ". Verified checker has possible move " + checkerVerified.PossibleMoves[i].CurrentValue.ToString() + " at this position. However, checker to be verified has possible move " + checkerToVerify.PossibleMoves[i].CurrentValue.ToString() + "."; bRetVal = false; break; } } } else { returnMessage += "Number of possible moves (" + checkerToVerify.PossibleMoves.Count() + ") for checker to be verified doesn't match number of possible moves (" + checkerVerified.PossibleMoves.Count() + ") for verified checker at position " + checkerVerified.CurrentPosition.ToString() + "."; bRetVal = false; } break; } } if (checkerToCheckoff != null) { checkersCheckOffList.Remove(checkerToCheckoff); checkerToCheckoff = null; } else { bRetVal = false; break; } } } else { returnMessage += "Number of checkers to be verified (" + checkersToVerify.Count + ") doesn't match number of verified checkers (" + checkersVerified.Count + ")"; } return(bRetVal); }