Example #1
0
 public void UpdateGui(Gameboard gameboard)
 {
     foreach (HexdameButton button in playButtons)
     {
         button.Text    = gameboard.GetCell(button.FieldPosition).Content.ToString();
         button.Checked = false;
         if (gameboard.GetCell(button.FieldPosition).ContainsWhite)
         {
             button.BackColor = Color.White;
         }
         else if (gameboard.GetCell(button.FieldPosition).ContainsRed)
         {
             button.BackColor = Color.Red;
         }
         else
         {
             button.BackColor = Color.Transparent;
         }
     }
 }
Example #2
0
        /// <summary>
        /// Applies the given move, if possible. Switches the current player on success.
        /// </summary>
        public bool ApplyMove(Move move)
        {
            // Check if valid
            if (!IsValidMove(move))
            {
                return(false);
            }

            // Start at first position in move
            Cell startingCell = gameboard.GetCell(move.GetStartingPosition());

            Position currentPosition = move.GetStartingPosition();
            Position nextPosition;

            int indexNextPosition = 1;

            // Iterate through all positions in the move, make capures if necessary
            while (indexNextPosition < move.GetNumberOfPositions())
            {
                nextPosition = move.GetPosition(indexNextPosition);

                Cell nextCell    = gameboard.GetCell(nextPosition);
                Cell currentCell = gameboard.GetCell(currentPosition);

                int deltaNumber    = nextPosition.Number - currentPosition.Number;
                int deltaCharacter = nextPosition.Character - currentPosition.Character;

                // Capture move?
                Position?capturePosition = CheckForCapture(currentPosition, nextPosition);
                if (capturePosition != null)
                {
                    // yes
                    Cell captureCell = gameboard.GetCell((Position)capturePosition);
                    captureCell.Content = Cell.Occupancy.Empty;
                }

                // Move player
                Cell.Occupancy stoneToMove = currentCell.Content;
                currentCell.Content = Cell.Occupancy.Empty;
                nextCell.Content    = stoneToMove;

                indexNextPosition++;
                currentPosition = nextPosition;
            }

            // Check for promotion
            Position lastPosition = move.GetPosition(move.GetNumberOfPositions() - 1);
            int      kingRow      = gameboard.CurrentPlayer == Game.Player.White ? 9 : 1;

            if (lastPosition.Number == kingRow || lastPosition.Character == kingRow)
            {
                if (!gameboard.GetCell(lastPosition).ContainsKing)
                {
                    gameboard.GetCell(lastPosition).PromoteToKing();
                }
            }

            // Switch Player
            gameboard.CurrentPlayer = GetNextPlayer(gameboard.CurrentPlayer);

            return(true);
        }