/// <summary> /// /// </summary> private void ComputerAttacks() { Random rand = new Random(); //initialize a random object int computerChoiceIndex = rand.Next(pnlComputerCards.Controls.Count); //generates a random number between 0 and the number of cards the computer has ReenableAllCards(); if (computerChoiceIndex >= 0) { CardBox.CardBox computerCard = pnlComputerCards.Controls[computerChoiceIndex] as CardBox.CardBox; //create a copy of the card object computerCard.FaceUp = true; logs.WriteLine("Computer Plays " + computerCard.ToString() + " as an attack"); pnlComputerCards.Controls.Remove(computerCard); //remove the card from the computers hand pnlActiveCards.Controls.Add(computerCard); //place the card into the active play panel if (playerAttacking == false) { ReenableAllCards(); DisableInvalidPlayerDefenseChoices(computerCard); } } RealignAllCards(); UpdateDefendedAndDiscardPanelControls(); }
/// <summary> /// Computers successive attack movements after initial attack is successfully defended /// </summary> /// <param name="card1"></param> /// <param name="card2"></param> private void ComputerSuccessiveAttacks(CardBox.CardBox card1, CardBox.CardBox card2) { Dictionary <int, CardBox.CardBox> validCards = new Dictionary <int, CardBox.CardBox>(); int cardIndex = 0; for (int i = 1; i < pnlComputerCards.Controls.Count; i++) { CardBox.CardBox tempCard = (CardBox.CardBox)pnlComputerCards.Controls[i]; if (tempCard.Rank == card1.Rank || tempCard.Rank == card2.Rank) { validCards.Add(i, tempCard); cardIndex = i; } } if (validCards.Count == 0) { logs.WriteLine("Computer can no longer attacks. Switching turns."); MoveCards(pnlDefended, pnlDiscard); RoundDeal(); txtComputerAttacker.Visible = false; playerAttacking = true; ReenableAllCards(); } //generates a random number between 0 and the number of cards the computer has if (playerAttacking == false) { CardBox.CardBox computerCard = validCards[cardIndex]; logs.WriteLine("Computer attacks with " + computerCard.ToString()); pnlComputerCards.Controls.Remove(computerCard); //remove the card from the computers hand pnlActiveCards.Controls.Add(computerCard); //place the card into the active play panel Wait(1500); DisableInvalidPlayerDefenseChoices(computerCard); } UpdateDefendedAndDiscardPanelControls(); DisableInvalidCardsInHands(); }
/// <summary> /// Move a card/control when it is dropped from one Panel to another. /// </summary> private void Panel_DragDrop(object sender, DragEventArgs e) { if (dragCard != null) { Panel thisPanel = sender as Panel; Panel fromPanel = dragCard.Parent as Panel; CardBox.CardBox aCardBox = new CardBox.CardBox(); if (thisPanel != null && fromPanel != null) { if (thisPanel != fromPanel) { fromPanel.Controls.Remove(dragCard); thisPanel.Controls.Add(dragCard); logs.WriteLine("Players plays " + dragCard.ToString()); RealignCards(thisPanel); RealignCards(fromPanel); /************************ATTACKING PLAYER LOGIC**************************************/ // Check if the player is attacking or defending, then trigger the appropriate events if (playerAttacking) { ComputerDefends(); } /************************DEFENDING PLAYER LOGIC**************************************/ else if (playerAttacking == false) { MoveCards(pnlActiveCards, pnlDefended); CardBox.CardBox attackCard = (CardBox.CardBox)pnlDefended.Controls[0]; CardBox.CardBox defenseCard = (CardBox.CardBox)pnlDefended.Controls[1]; ComputerSuccessiveAttacks(attackCard, defenseCard); } } } RealignAllCards(); UpdateDefendedAndDiscardPanelControls(); } }
/// <summary> /// initialDeal - deals players 6 cards to start /// </summary> private void InitialDeal() { cbxDeck.FaceUp = false; PlayingCard playersCard; PlayingCard AIsCard; PlayingCard lowestCard = new PlayingCard(PlayingCard.trumpSuit, CardRank.Ace); // start as the highest possible card // setting the first card cbxDeck.Card = mainDeck.GetCard(0); mainDeck.DrawCard(); logs.WriteLine("Initial Deal Starting: \n"); for (int i = 0; i < 6; i++) { PlayingCard card = cbxDeck.Card; if (card != null) //if card isn't null { card.FaceUp = true; //Make it a cardbox for the player CardBox.CardBox playerCardBox = new CardBox.CardBox(card); playersCard = card; playerCardBox.Size = normalCardSize; //Wire events WireCardBoxEventHandlers(playerCardBox); //playerCardBox.Click += CardBox_Click; //When the player clicks a card in their hand //Add cardbox to panel pnlPlayerCards.Controls.Add(playerCardBox); logs.WriteLine("Player draws " + playerCardBox.ToString()); cbxDeck.Card = mainDeck.DrawCard(); card = cbxDeck.Card; CardBox.CardBox computerCardBox = new CardBox.CardBox(card); AIsCard = card; computerCardBox.Size = normalCardSize; //Make a cardbox for the computer pnlComputerCards.Controls.Add(computerCardBox); PlayingCard cardToLog = computerCardBox.Card; cardToLog.FaceUp = true; logs.WriteLine("Computer draws " + cardToLog.ToString()); cbxDeck.Card = mainDeck.DrawCard(); // determine who has the lowest trump card // If no entity has a trump card then the player goes first if (AIsCard < lowestCard && AIsCard.Suit == PlayingCard.trumpSuit) { lowestCard = AIsCard; firstTurn = "the AI"; } if (playersCard < lowestCard && playersCard.Suit == PlayingCard.trumpSuit) { lowestCard = playersCard; firstTurn = "the player"; } } } ReenableAllCards(); RealignAllCards(); }
/// <summary> /// This Click method happens for the player's CardBox /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CardboxTest_Click(object sender, EventArgs e) { int cardClickedValue = 0; int lastCardDealtValue = 0; lblMessage.Text = ""; //Sets the player to become the defender and the AI being the Attacker if (cardsDealt.Count == 0) { players[0].isAttack = false; players[1].isAttack = true; btnClearCards.Text = TAKE_BUTTON; } //Clicked card is set up to check values CardBox.CardBox item = (CardBox.CardBox)sender; DurakLog("\tPlayer Clicked on " + item.ToString()); //Checks if playing cards has less than 6 cards if (cardsDealt.Count <= MAX_NUMBER_OF_PLAYING_CARDS) { //Checks if the there is any cards on the playing field if (cardsDealt.Count > 0) { //Sets the cards values cardClickedValue = (int)item.Rank; lastCardDealtValue = (int)cardsDealt.Last <Card>().rank; //Checks if the clicked object is a trump card if (item.Suit == trumpCard.Suit) { cardClickedValue += 10; } //Checks if the last card on the field, being a trump card if (cardsDealt.Last <Card>().suit == trumpCard.Suit) { lastCardDealtValue += 10; } //Checks if the clicked card value is equal or greater than the last card dealt if (cardClickedValue >= lastCardDealtValue) { DurakLog("\t\t(" + item.ToString() + ") beats last playing card (" + cardsDealt.Last <Card>().ToString() + ")"); Card clickedCard = new Card(item.Suit, item.Rank); cardsDealt.Add(clickedCard); players[0].PlayHand.Remove(clickedCard); //Checks if there is enough space for AI to play if (cardsDealt.Count < MAX_NUMBER_OF_PLAYING_CARDS) { //Lets the AI play next if (players[1].PlayHand.Count > 0) { playAiPlayer(); } //If AI makes the playing cards full, then it discards the cards. if (cardsDealt.Count == MAX_NUMBER_OF_PLAYING_CARDS) { DurakLog("\tAI Player made the cards full, discarded the cards in playing field"); cardsDealt = new Cards(); addMoreCardsToPlayerHands(); //Makes the player defend and AI attacks if (cardsDealt.Count == 0) { players[0].isAttack = false; players[1].isAttack = true; btnClearCards.Text = TAKE_BUTTON; } showWhoseAttackDefend(); } } else //AI has to discard, since the player played the last card to reach the maximum cards in play { cardsDealt = new Cards(); AiStartsNewPlayingCards(); } setNewCardField(); } else//Shows a message that it is not able to be played { lblMessage.Text = item.ToString() + " cannot beat " + cardsDealt.Last <Card>().ToString(); DurakLog("\t\t(" + item.ToString() + ") cannot beat last playing card (" + cardsDealt.Last <Card>().ToString() + ")"); } } else //Sets the first card for the playing field from the Player themself { Card setCard = new Card(item.Suit, item.Rank); cardsDealt.Add(setCard); players[0].PlayHand.Remove(setCard); DurakLog("\t\t(" + item.ToString() + ") starts the playing cards"); //checks if AI player has enough cards if (players[1].PlayHand.Count > 0) { playAiPlayer(); } setNewCardField(); } //If AI Player wins or Player Wins if (players[1].PlayHand.Count == 0 || players[0].PlayHand.Count == 0) { setWinner(); } } else { DurakLog("\t\t\tPlaying field is full"); lblWarning.Text = "Cannot have anymore cards"; } }