/// <summary> /// Draw a player's cube collection inside of a rectangle /// </summary> /// <param name="g">The graphics object to draw on</param> /// <param name="rekt">The rectangle to draw in</param> /// <param name="player">The player whose cubes we are to draw</param> private void DrawCubeCollection(Graphics g, Rectangle rekt, Player player) { int gap = 10; int x = rekt.X + gap; int y = rekt.Y + 2 * gap; foreach (Cube cube in player.Cubes) { if (x + cube.Width + gap > rekt.X + rekt.Width) { y += cube.Height + gap; x = rekt.X + gap; } cube.Move(x, y); cube.Draw(g); x += gap / 2 + cube.Width; } }
/// <summary> /// Handle all click events /// </summary> private void Form1_MouseClick(object sender, MouseEventArgs e) { if (!GameLocked) { // Try select a card from the player's hand for (int i = 0; i < human.Hand.Cards.Count; i++) { if (human.Hand.Cards[i].MouseOn(e.X, e.Y)) { if (selectedCard != null) selectedCard.Selected = false; // Unselect current selected card selectedCard = human.Hand.Cards[i]; // Set selectedCard to the card that was clicked human.Hand.Cards[i].Selected = true; // Set the card's selected property this.Invalidate(); // Redraw everything } } // A card has already been selected if (selectedCard != null) { // If we're over a slot if (CursorIsOnSlot(e.X, e.Y)) { // Get that slot CardSlot selectedSlot = GetSlotAt(e.X, e.Y); if (WhoseTurn == human) { Output("Human played " + selectedCard + " to " + selectedSlot); if (Play(new Move(selectedCard, selectedSlot), human)) // Play the move { Output("Robot's turn.."); WhoseTurn = robot; // It's the robot's turn if the play was valid } } if (WhoseTurn == robot) { // Play the robot's turn Move robotMove = robot.GetMove(tiles, deck, discardPile); Output("Robot played " + robotMove.CardToPlay + " to " + robotMove.SlotToPlay); if (Play(robotMove, robot)) // Play the move { Output("Human's turn.."); WhoseTurn = human; // It's the human's turn if the play was valid } else throw new Exception("The robot tried to make an invalid move"); } selectedCard.Selected = false; // Remove selected colours selectedCard = null; // Card is no longer selected this.Invalidate(); return; } } } // Select a card in the hand if we're over one // If not, deselect any selected cards if (!CheckCardInHandForClick(human.Hand, e.X, e.Y) && selectedCard != null) { selectedCard.Selected = false; selectedCard = null; // No card was selected this time, unselect anything currently selected this.Invalidate(); } }
/// <summary> /// Play a move and check for victory /// </summary> /// <param name="move">The move to be played</param> /// <param name="player">The player to play the move</param> /// <returns>False if play was invalid</returns> private bool Play(Move move, Player player) { Contract.Requires(move != null, "move must not be null"); // Check that the move is valid if (!move.SlotToPlay.OwnerTile.HasEmptySlotWithMatchingCube(move.CardToPlay, move.SlotToPlay)) { Output("That card doesn't match a cube."); return false; } else { move.SlotToPlay.SetCard(move.CardToPlay); // Put the card in the slot player.Hand.RemoveCard(move.CardToPlay); // Remove it from their hand player.DealHand(ref deck, ref discardPile); // Add appropriate amount of cards // Score the tiles for (int i = 0; i < tiles.Count; i++) // Tile is full, score it if (tiles[i].Full) { // Find the winner CardSlot.Position result = tiles[i].Score(); // Above or below Player winner; // If the side above had the best score, robot wins if (result == CardSlot.Position.Above) { winner = robot; } // If the side above had the best score, human wins else if (result == CardSlot.Position.Below) { winner = human; } // If both scores were the same, the person to play that card wins else { winner = WhoseTurn; } this.Invalidate(); // Draw the card played by the winner InformationBox(winner + " won " + tiles[i] + "!", "Tile has been won"); // Let 'em know! // Reset the tile tiles[i].Flip(); winner.AddCubes(tiles[i].Cubes); tiles[i].RemoveCubes(); // Refill the tile // If we have run out of cubes, remove the tile. if (!FillTile(tiles[i])) tiles.Remove(tiles[i]); // Discard the cards List<Card> discardedCards = tiles[i].DiscardCards(); // Add them to the discard pile foreach (Card c in discardedCards) discardPile.Add(c); // Give the winner a trophy if they deserve it List<Trophy> trophiesWon = winner.ChanceAtVictory(); if (trophiesWon.Count > 0) RemoveAvailableTrophies(trophiesWon); // End game? if (winner.Trophies.Count >= 3) { InformationBox(winner + " wins the game!", "Game over!"); LockGame(); } } return true; } }
public void Initialise() { deck.Load(); // Create a new deck deck.Shuffle(); // Shuffle the deck FillBag(); // Add some cubes to the bag LoadResources();// Draw everything WhoseTurn = human; }