/// <summary> /// Adds a bew card to the panel /// </summary> /// <param name="cardBox">the card as a picture box</param> public void AddPlayingCard(CardPictureBox cardBox) { pnlHand.Controls.Add(cardBox); myPlayer.AddCard(cardBox.PlayingCard); lblNumberOfCards.Text = myPlayer.CardsRemaining().ToString(); SetLocation(); }
/// <summary> /// Removes the corresponding card from the panel and players hand /// </summary> /// <param name="card"></param> public void RemovePlayingCard(CardPictureBox card) { myPlayer.Remove(card.PlayingCard); lblNumberOfCards.Text = myPlayer.CardsRemaining().ToString(); pnlHand.Controls.Remove(card); SetLocation(); }
/// <summary> /// Gets called when anywhere in the control gets clicked. /// Determines if a card was clicked and if so, calls the corresponding click event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PlayerHandControl_Click(object sender, EventArgs e) { bool isClickedControl = false; // determines if the card was found // runs through each control in the panel for (int controlCount = 0; controlCount < pnlHand.Controls.Count && !isClickedControl; controlCount++) { // attemps to convert the control to a CardPictureBox CardPictureBox cbp = pnlHand.Controls[controlCount] as CardPictureBox; // if the control was converted if (cbp != null) { // if the mouse is within the card box location, // fire the given cards event and set isClickecControl to true if (MousePosition.X >= cbp.Location.X && MousePosition.X <= (cbp.Location.X + cbp.Width)) { if (MousePosition.Y >= cbp.Location.Y && MousePosition.Y <= (cbp.Location.Y + cbp.Height)) { Click(cbp, e); isClickedControl = true; } } } } }
public GraphicCard(CardSuite suite, CardFigure figure) : base(suite, figure) { Pb = new CardPictureBox(this) { SizeMode = PictureBoxSizeMode.Zoom, Image = IsOpened ? images[this] : faceDownImage }; }
private void Mix() { for (int i = 0; i < cards.Count; i++) { int rnd = RANDOM.Next(i); CardPictureBox temp = cards[rnd]; cards[rnd] = cards[i]; cards[i] = temp; } }
/// <summary> /// Gives the player all the cards in the panel /// </summary> /// <param name="pnl">the panel to remove the cards from</param> /// <param name="phc">the player hand control that will recieve the cards</param> private void RetrievePanelCards(Panel pnl, PlayerHand phc) { string playerInformation = phc.PlayerName.ToString(); // run through each card and move them to the player for (int cardCount = 0; cardCount < pnl.Controls.Count;) { CardPictureBox cpb = pnl.Controls[cardCount] as CardPictureBox; WriteToFile(log_file_path, "\t" + playerInformation + " has taken the card " + cpb.PlayingCard.ToString() + " from the table"); ChangeCardParameters(phc, ref cpb); phc.AddPlayingCard(cpb); } // clears the panel pnl.Controls.Clear(); }
private void InitDeck(List <int> figureSet, DurakGame durak) { cards = new List <CardPictureBox>(); foreach (var i in figureSet) { foreach (var j in SUITS) { cards.Add(new CardPictureBox(j, i, durak)); } } Mix(); TrumpCard = cards[cards.Count - 1]; }
/// <summary> /// Set the location of the cards in the panel /// </summary> /// <param name="ph"></param> private void SetLocation(Panel ph) { int numberOfControls = ph.Controls.Count; // saves the number of cards // run through each control for (int count = 0; count < numberOfControls; count++) { // attempt to convert it to a CardPictureBox CardPictureBox cpb = ph.Controls[count] as CardPictureBox; // if the control was converted, set the location if (cpb != null) { cpb.Location = new Point(count * CardPictureBox.cardSize.Width / 2, 0); } } }
/// <summary> /// Attempts to move the given CardPictureBox to the given panel /// </summary> /// <param name="phc">the PlayerHand control that the card should be in</param> /// <param name="pnl">the panel the CardPictureBox will be moved to</param> /// <param name="cpb">the CardPictureBox that will be moved</param> private void MovePlayingCard(PlayerHand phc, Panel pnl, CardPictureBox cpb) { // try to move the card to the panel try { phc.RemovePlayingCard(cpb); pnl.Controls.Add(cpb); SetLocation(pnl); WriteToFile(log_file_path, "\t" + phc.PlayerName + (pnl == pnlAttackCards ? " attacked " : " defended ") + "with " + cpb.PlayingCard.ToString()); } // if the card does not exist catch (CardDoesNotExistException ex) { MessageBox.Show(ex.Message + ". Please select another card!", "Invalid Operation"); } }
/// <summary> /// Changes the parameters on the CardPictureBox /// </summary> /// <param name="phc"></param> /// <param name="cpb"></param> private void ChangeCardParameters(PlayerHand phc, ref CardPictureBox cpb) { // removes the card click event (if it has one set) // NOTE: Done to remove the possibility of duplicate firing cpb.Click -= CardClick; // if the player is retiving the cards, add the click event if (phc == phcPlayer) { cpb.Click += CardClick; } // otherwise, make the card face down else { cpb.FaceUp = false; } }
/// <summary> /// Checks if the card's rank matches a previous one played /// </summary> /// <param name="pnl">the panel to check</param> /// <param name="card">the card to check</param> /// <returns>true if the card can be played</returns> private bool CheckPanelCards(Panel pnl, Card card) { bool returnValue = false; // determines if the card can be played. Defaulted to no // run through each card in the panel for (int cardCount = 0; cardCount < pnl.Controls.Count && !returnValue; cardCount++) { // save the current card as a picture box CardPictureBox currentCard = pnl.Controls[cardCount] as CardPictureBox; // if the ranks match, save the value to true if (card.Rank == currentCard.PlayingCard.Rank) { returnValue = true; } } return(returnValue); // return the result }
public CardPictureBox UseCard(CardPictureBox card) { cards.Remove(card); return(card); }
public GraphicCard(CardPictureBox pb, CardSuite suite, CardFigure figure) : base(suite, figure) { Pb = pb; Pb.SizeMode = PictureBoxSizeMode.Zoom; Pb.Image = IsOpened ? images[this] : faceDownImage; }
/// <summary> /// Dispenses the cards to each player /// </summary> private void DisperseCards() { // if the atttack and defend panels are empty if (pnlAttackCards.Controls.Count == 0 && pnlDefendCards.Controls.Count == 0) { // if cards are able to be used if (dealer.CurrentCardIndex < dealer.NumberOfCards) { WriteToFile(log_file_path, "\nPlayers are drawing cards"); bool cardsLeft = true; // determines if the player still has cards left in their hand // counts how many PlayerHandControls there are int numberOfPlayers = Controls.OfType <PlayerHand>().Count(); // saves each player hand control to an array PlayerHand[] panels = new PlayerHand[numberOfPlayers]; panels[0] = phcComputer; panels[1] = phcPlayer; // runs through each hand for (int playerCount = 0; playerCount < numberOfPlayers && cardsLeft; playerCount++) { PlayerHand currentPHC = panels[playerCount]; // gets the corresponding control // runs until the player reachers the required number of cards for (int count = currentPHC.PlayerInformation.CardsRemaining(); count < MINIMUM_NUMBER_OF_CARDS && cardsLeft; count++) { // draws the next card and adds it to the appropriate hand CardPictureBox cardBox = new CardPictureBox(dealer.GetNextCard()); cardBox.FaceUp = currentPHC.FaceUp; // sets the faceup orientation based on what the player has set // if its the player who is recieving the card, add the click event if (playerCount == 1) { cardBox.Click += CardClick; } // add the playing card to the correct control currentPHC.AddPlayingCard(cardBox); // if there are no cards left, set the bool to false if (dealer.CurrentCardIndex >= dealer.NumberOfCards) { cardsLeft = false; } WriteToFile(log_file_path, "\tPlayer " + currentPHC.PlayerName.ToString() + " has drawn the card " + cardBox.PlayingCard.ToString() + " from the deck"); } } // updates the cards remaining label lblCardsRemaining.Text = (dealer.NumberOfCards - dealer.CurrentCardIndex).ToString(); } // else if there are no cards remaining and the notification hasnt been sent else if (!noCardsRemainingNotification) { // display 0 cards and make the deck invisible lblCardsRemaining.Text = "0"; cpbDeck.Visible = false; // send the notification and set the boolean to true MessageBox.Show("There are no cards remaining", "No Cards"); noCardsRemainingNotification = true; } // otherwise check if a player has won else { // if the computer won if (phcComputer.PlayerInformation.CardsRemaining() == 0) { UpdatePlayerStats(WIN_OPTIONS[1]); GameOver(playerNames[0]); } // if the player won else if (phcPlayer.PlayerInformation.CardsRemaining() == 0) { UpdatePlayerStats(WIN_OPTIONS[0]); GameOver(playerNames[1]); } } } // otherwise the round is still in play else { MessageBox.Show("You cant draw as you are currently in the middle of a round!", "Invalid Operation"); } }
/// <summary> /// Event called when a CardPicture box is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CardClick(object sender, EventArgs e) { // if its the player's turn to go if (currentPlayer == 1) { // convert the object to a card box CardPictureBox cpb = sender as CardPictureBox; // if the conversion was successful if (cpb != null) { // set the size of the cardbox cpb.Size = new Size(CardPictureBox.cardSize.Width / 2, CardPictureBox.cardSize.Height / 2); // states if a player played successfully bool playedSuccessfully = false; // if the player is attacking if (playerAttacking) { // if the card can be played as an attack card if (CheckAttackCard(cpb.PlayingCard)) { MovePlayingCard(phcPlayer, pnlAttackCards, cpb); playedSuccessfully = true; } // otherwise advice the user the card is not a valid move else { MessageBox.Show(cpb.PlayingCard.ToString() + " cannot be played" + Environment.NewLine + "Card must match a previously selected rank.", "Invalid Move"); } } // otherwise the player is defending else { // if the card is a valid defence card if (CheckDefendCard(cpb.PlayingCard)) { MovePlayingCard(phcPlayer, pnlDefendCards, cpb); playedSuccessfully = true; } // otherwise, notify the user the card is not a valid move else { MessageBox.Show("The card cannot be played. Must be a higher card in the same suit or be within the trump suit.", "Invalid Move"); } } // if the player succesfully played a card if (playedSuccessfully) { // if the layer has no cards left if (phcPlayer.PlayerInformation.CardsRemaining() == 0) { // if dealer has no cards left, the player has won if (dealer.CurrentCardIndex >= dealer.NumberOfCards) { UpdatePlayerStats(WIN_OPTIONS[0]); GameOver(playerNames[1]); } // if they were attacking and the defn else if (playerAttacking) { WriteToFile(log_file_path, playerNames[1] + " has successfully defended"); MessageBox.Show("Defence wins as there are no more cards to defend with", "Defence Wins"); } else { AI_Logic(); } } else { AI_Logic(); } } /*// if the player successfully played * if (playedSuccessfully) * { * // if there are no cards left to play * if (phcPlayer.PlayerInformation.CardsRemaining() == 0 && dealer.CurrentCardIndex >= dealer.NumberOfCards) * { * UpdatePlayerStats(WIN_OPTIONS[0]); * GameOver(playerNames[1]); * } * // otherwise, let the ai play * else * AI_Logic(); * }*/ } } // other, the player cannot play else { MessageBox.Show("It is not your turn. Please wait until the other plays", "Unable to play"); } }
/// <summary> /// Adds a new card to the panel /// </summary> /// <param name="card">the playing card to add</param> public void AddPlayingCard(Card card) { CardPictureBox cardBox = new CardPictureBox(card); AddPlayingCard(cardBox); }
/// <summary> /// The AI (Computer) Logical thinking /// </summary> private void AI_Logic() { // the number of cards in teh computers hand int numberOfCards = phcComputer.PlayerInformation.CardsRemaining(); bool cardPlayed = false; // determines if the computer played a card. Defaulted to false CardPictureBox bestCard = null; // stores the best possible card to play // AI DEFEND LOGIC if (playerAttacking) { // run through each card in the hand for (int count = 0; count < numberOfCards; count++) { // stores the current card CardPictureBox currentCard = phcComputer.RetrieveCardBox(count); // if the card can be played as a defence if (CheckDefendCard(currentCard.PlayingCard)) { // if the best card has not been set yet, store this card as the best if (bestCard == null) { bestCard = currentCard; } // if the card is lower than the previously played card, store this card as the best else if (currentCard.PlayingCard < bestCard.PlayingCard) { bestCard = currentCard; } } } // if there was no card found if (bestCard == null) { // display the message to the log and messagebox WriteToFile(log_file_path, "\n" + phcPlayer.PlayerName.ToString() + " has won the round as the computer was not able to defend"); MessageBox.Show("Unable to Defend. Player wins round!", "Round Over"); // the computer retrieves all the cards and the next turn occurs RetrievePlayedCards(phcComputer); NextTurn(); } // otherwise, the card is played else { cardPlayed = true; bestCard.FaceUp = true; MovePlayingCard(phcComputer, pnlDefendCards, bestCard); } } // AI ATTACK LOGIC else { // run through each card for (int count = 0; count < numberOfCards; count++) { // save the card in the corresponding index CardPictureBox currentCard = phcComputer.RetrieveCardBox(count); // if the card can be used for an attack if (CheckAttackCard(currentCard.PlayingCard)) { // if the best card hasnt been set yet, make this card the best card if (bestCard == null) { bestCard = currentCard; } // if this card is higher else if (currentCard.PlayingCard > bestCard.PlayingCard) { // if this card is not trump, set it // otherwise, keep the trump card if (currentCard.PlayingCard.Suit != Card.TrumpSuit) { bestCard = currentCard; } } } } // if no card was selected if (bestCard == null) { // notify the player that the computer ended the round WriteToFile(log_file_path, "\nComputer decided to end the round without doing another attack"); MessageBox.Show("Computer is finishing the round", "Round Over"); // make the player attack and move to the next turn playerAttacking = true; NextTurn(); } // otherwise, play the card else { cardPlayed = true; bestCard.FaceUp = true; MovePlayingCard(phcComputer, pnlAttackCards, bestCard); } } // if there are no cards left and the deck is empty, they have won /* if (cardPlayed && phcComputer.PlayerInformation.CardsRemaining() == 0 && dealer.CurrentCardIndex >= dealer.NumberOfCards) * { * UpdatePlayerStats(WIN_OPTIONS[1]); * GameOver(playerNames[0]); * }*/ if (cardPlayed) { if (phcComputer.PlayerInformation.CardsRemaining() == 0) { if (dealer.CurrentCardIndex >= dealer.NumberOfCards - 1) { UpdatePlayerStats(WIN_OPTIONS[1]); GameOver(playerNames[0]); } else if (playerAttacking) { WriteToFile(log_file_path, playerNames[0] + " has successfully defended"); MessageBox.Show("Defence wins as there are no more cards to defend with", "Defence Wins"); } } } }