/// <summary> /// Update the card controls in the panels /// </summary> private void panelUpdate() { CardBox.CardBox aCardBox; foreach (Card card in currentGame.Players[0].Hand) { // Create a new CardBox control based on the card drawn aCardBox = new CardBox.CardBox(card) { FaceUp = false }; aCardBox.FaceUp = true; if (toolStripSuggest.SelectedIndex > 0 && card.Playable(currentGame)) { aCardBox.Size = new Size(regularSize.Width + POP, regularSize.Height + POP); // Enlarge the card for visual effect aCardBox.Top = 0; // move the card to the top edge of the panel. } // Wire events handlers to the new control: aCardBox.Click += CardBox_Click; // wire CardBox_Click pnlPlayerArea.Controls.Add(aCardBox); // Add the new control to the appropriate panel RealignCards(pnlPlayerArea); // Realign the controls in the panel so they appear correctly. } foreach (Card card in currentGame.Players[1].Hand) { aCardBox = new CardBox.CardBox(card) { FaceUp = tscbCheats.SelectedIndex <= 0 }; aCardBox.FaceUp = tscbCheats.SelectedIndex > 0; //------change to false for production ------------------------------------------------------------ pnlComputerArea.Controls.Add(aCardBox); // Add the new control to the appropriate panel RealignCards(pnlComputerArea); // Realign the controls in the panel so they appear correctly. } }
/// <summary> /// DrawCard method, /// draw card into player panel and hand /// </summary> /// <param name="panel">panel to draw card into</param> private void DrawCard(Panel panel) { // check which panel to draw card into if (panel == pnlHumanHand) // if its human { // add to human hand HumanPlayer.PlayHand.Add(playDeck.GetCard(currentCard)); // create new cardbox object CardBox.CardBox aCardBox = new CardBox.CardBox(playDeck.GetCard(currentCard), true); // Event for the cardbox aCardBox.Click += CardBox_Click; aCardBox.MouseEnter += CardBox_MouseEnter; // wire CardBox_MouseEnter for the "POP" visual effect aCardBox.MouseLeave += CardBox_MouseLeave; // wire CardBox_MouseLeave for the regular visual effect // add cardbox object to the human panel pnlHumanHand.Controls.Add(aCardBox); currentCard++; // increment current hand } if (panel == pnlComputerHand) // if its computer { // add to computer hand ComputerPlayer.PlayHand.Add(playDeck.GetCard(currentCard)); // create new cardbox object CardBox.CardBox aCardBox = new CardBox.CardBox(playDeck.GetCard(currentCard), false); // add cardbox object to computer panel pnlComputerHand.Controls.Add(aCardBox); currentCard++; // increment current hand } // update the card remaining text txtDeckCardsRemaining.Text = (playDeck.CardsRemaining - currentCard).ToString(); }
/// <summary> /// DisplayPlayerTwoCards method, /// is to set all card on the pnlComputerHand to face up /// *It's for testing purposes /// </summary> public void DisplayPlayerTwoCards() { foreach (Control control in pnlComputerHand.Controls) { CardBox.CardBox card = control as CardBox.CardBox; card.FaceUp = true; } }
/// <summary> /// DisplayPlayerOneCards method, /// is to set all card on the pnlHumanHand to face up /// *It's for testing purposes /// </summary> public void DisplayPlayerOneCards() { foreach (Control control in pnlHumanHand.Controls) { CardBox.CardBox card = control as CardBox.CardBox; card.FaceUp = true; } }
/// <summary> /// DisplayDiscardCards method, /// is to set all card on the pnlDiscardedCard to face up /// *It's for testing purposes /// </summary> public void DisplayDiscardCards() { foreach (Control control in pnlDiscardPile.Controls) { CardBox.CardBox card = control as CardBox.CardBox; card.FaceUp = true; } }
/// <summary> /// DisplayRiverCards method, /// is to set all card on the flowRiver to face up /// *It's for testing purposes /// </summary> public void DisplayRiverCards() { foreach (Control control in flowRiver.Controls) { CardBox.CardBox card = control as CardBox.CardBox; card.FaceUp = true; } }
/// <summary> /// ComputerAttack method, /// if the deck still has a card remaing the computer will attack with it's lowest card /// and if the card is empty the computer will attack with it's highest cards first /// or if cards have been played the computer will attack with a valid card /// </summary> public void ComputerAttack() { // check if the computer first attack if (flowRiver.Controls.Count == 0) { // check if card remaining is more than 0 if (playDeck.CardsRemaining > 0) { // get the lowest attackCard to initlize the round CardBox.CardBox attackCard = pnlComputerHand.Controls.OfType <CardBox.CardBox>().First(); foreach (CardBox.CardBox card in pnlComputerHand.Controls) { if (card.Rank < attackCard.Rank) { attackCard = card; } } ComputerPlaysCard(attackCard); System.Diagnostics.Debug.Write("Computer Lowest Attack."); } else { // get the highest attackCard to initilize the round CardBox.CardBox attackCard = pnlComputerHand.Controls.OfType <CardBox.CardBox>().First(); foreach (CardBox.CardBox card in pnlComputerHand.Controls) { if (card.Rank > attackCard.Rank) { attackCard = card; } } ComputerPlaysCard(attackCard); System.Diagnostics.Debug.Write("Computer Highest Attack."); } } else { // continues attack after the first attack foreach (CardBox.CardBox fieldCard in flowRiver.Controls) { foreach (CardBox.CardBox attackCard in pnlComputerHand.Controls) { if (fieldCard.Card.Rank == attackCard.Card.Rank) // validating the attack { ComputerPlaysCard(attackCard); // computer play the card System.Diagnostics.Debug.Write("Computer Attacked."); return; } } } EndTurn(); } }
/// <summary> /// CardBox control shrinks to regular size when the mouse leaves. /// </summary> void CardBox_MouseLeave(object sender, EventArgs e) { // Convert sender to a CardBox CardBox.CardBox aCardBox = sender as CardBox.CardBox; // If the conversion worked if (aCardBox != null) { aCardBox.Size = regularSize; // resize the card back to regular size aCardBox.Top = POP; // move the card down to accommodate for the smaller size. } }
/// <summary> /// CardBox controls grow in size when the mouse is over it. /// </summary> void CardBox_MouseEnter(object sender, EventArgs e) { // Convert sender to a CardBox CardBox.CardBox aCardBox = sender as CardBox.CardBox; // If the conversion worked if (aCardBox != null) { aCardBox.Size = new Size(regularSize.Width + POP, regularSize.Height + POP); // Enlarge the card for visual effect aCardBox.Top = 0; // move the card to the top edge of the panel. } }
/// <summary> /// CardBox Click event, /// </summary> void CardBox_Click(object sender, EventArgs e) { // Convert sender to a CardBox CardBox.CardBox aCardBox = sender as CardBox.CardBox; // If the conversion worked if (aCardBox != null) { // if the card is in the home panel... if (aCardBox.Parent == pnlHumanHand) { if (HumanPlayer.IsAttacking) { if (ValidAttack(aCardBox.Card)) // check if the attack is valid { pnlHumanHand.Controls.Remove(aCardBox); // Remove the card from the home panel aCardBox.Enabled = false; // disable the card box onFieldCards.Add(aCardBox.Card); // add cardbox to the field flowRiver.Controls.Add(aCardBox); // Add the control to the play panel System.Diagnostics.Debug.Write("Human Attacked With " + aCardBox.ToString() + "\n"); ComputerDefence(aCardBox); // call ComputerDefence method } btnCeaseAttack.Enabled = true; // enable btnCeaseAttack } else { if (ValidDefend(aCardBox.Card)) // check if the defend is valid { pnlHumanHand.Controls.Remove(aCardBox); // Remove the card from the home panel aCardBox.Enabled = false; // disable the cardbox onFieldCards.Add(aCardBox.Card); // add cardbox to the field flowRiver.Controls.Add(aCardBox); // Add the control to the play panel System.Diagnostics.Debug.Write("Human Defended With " + aCardBox.ToString() + "\n"); ComputerAttack(); // call ComputerAttack method } btnCeaseAttack.Enabled = false; // disable btnCeaseAttack } } // update river card text counter txtRiverCardsRemaning.Text = flowRiver.Controls.Count.ToString(); // Realign the cards RealignCards(pnlHumanHand); } if (!cardRemaining) // check if there is any card remaining to draw { CheckWinner(); // call CheckWinner method } }
/// <summary> /// ComputerPlaysCard method, /// Takes the attack or defened card the computer has chosen and places it on the river after removing it from the computers hand /// </summary> /// <param name="aCardBox"></param> public void ComputerPlaysCard(CardBox.CardBox aCardBox) { pnlComputerHand.Controls.Remove(aCardBox); // remove the card from the panel aCardBox.Enabled = false; // disable the card aCardBox.FaceUp = true; // set the FaceUp property to true onFieldCards.Add(aCardBox.Card); // add the card to on field flowRiver.Controls.Add(aCardBox); // add cardbox to the field RealignCards(pnlComputerHand); // realign computer panel System.Diagnostics.Debug.Write("Computer Played " + aCardBox.ToString() + " "); if (!cardRemaining) // check if there is card remaning { CheckWinner(); // check the winner } }
/// <summary> /// DisplayTrumpCards method, /// is to draw trump card, set the image of trump card and set it as the last card. /// </summary> public void DisplayTrumpCards() { /*The reason of number 12, is because the program is designed to accomodate 2 players. * Which makes the trump card is always been on 12th position.*/ //create a new cardbox object CardBox.CardBox aCardBox = new CardBox.CardBox(playDeck.GetCard(12), true); trumpCard = playDeck.GetCard(12); // add the cardbox to flowTrumpCard flowTrumpCard.Controls.Add(aCardBox); // set the trumpSuit to the 12th card suit playDeck.GetCard(12).TrumpSuit = playDeck.GetCard(12).Suit; // move the 12th card to last card on the deck playDeck.ChangePosition(12, playDeck.GetCard(12)); // set the trump card }
/// <summary> /// ComputerDefence method, /// Computer checks to see if it has a card that can be used to defend the attack card /// and chooses the lowest card possible in the deck to denfend with /// </summary> /// <param name="attackCard"></param> public void ComputerDefence(CardBox.CardBox attackCard) { // set valid defend to false bool validDefend = false; // create cardbox object CardBox.CardBox defendCard = new CardBox.CardBox(); foreach (CardBox.CardBox aCardBox in pnlComputerHand.Controls) { // check if card suit is the same with attack card suit if (aCardBox.Card.Suit == attackCard.Card.Suit) { // check if the card rank is higher than attack card suit if (aCardBox.Card.Rank > attackCard.Card.Rank) { // if true set the defendCard defendCard = aCardBox; // find the lowest possible defend card if (aCardBox.Rank < defendCard.Rank) { // set the defendCard defendCard = aCardBox; } // set validDefend to true validDefend = true; } } else if (aCardBox.Card.Suit == trumpCard.Suit && attackCard.Card.Suit != trumpCard.Suit) // check if the defend card suit is trump { defendCard = aCardBox; validDefend = true; } } // check if there is valid defend if (validDefend) { // if true, play the defend card ComputerPlaysCard(defendCard); } else { // if false, pick up the card on the field PickUpRiver(pnlComputerHand); } }
/// <summary> /// Display the Cardboxes for the deck /// </summary> private void SetDeck() { //Instantialize the deck and trump cardboxs deckBox = new CardBox.CardBox(); trumpBox = new CardBox.CardBox(talon.getTrumpCard()); //Set the orientation of the trump card and make it face up trumpBox.CardOrientation = Orientation.Horizontal; trumpBox.FaceUp = true; //Set the locations of the cards deckBox.Location = new Point(4, 200); trumpBox.Location = new Point(4, 215); //Add the controls to the form this.Controls.Add(deckBox); this.Controls.Add(trumpBox); }
/// <summary> /// Update card controls on the form /// </summary> private void UpdateCardsView() { pnlPlayerArea.Controls.Clear(); pnlPlayArea.Controls.Clear(); pnlComputerArea.Controls.Clear(); btnSurrender.Enabled = currentGame.Players[0].Defender; btnCease.Enabled = currentGame.Players[0].Attacker; //cbTrump.Visible = currentGame.HandEquilibrium > 0; // TrumpCard CardBox visibility, currently wanted to stay visible panelUpdate(); foreach (Card card in (currentGame as GameLibrary.Durak).PlayArea) { // Create a new CardBox control based on the card drawn CardBox.CardBox areaCardBox = new CardBox.CardBox(card) { FaceUp = false }; areaCardBox.FaceUp = true; pnlPlayArea.Controls.Add(areaCardBox); // Add the new control to the appropriate panel } RealignCards(pnlPlayArea); // Realign the controls in the panel so they appear correctly. UpdateStatus(); }
/// <summary> /// Insert a given card into a given panel /// </summary> /// <param name="card"> The card to add </param> /// <param name="addPanel"> The panel to add the card to </param> private void InsertCardIntoPanel(Card card, Panel addPanel) { // Create a new CardBox control based on the card to be inserted CardBox.CardBox aCardBox = new CardBox.CardBox(card); //Set the new cardbox's size aCardBox.Size = regularSize; //Determine if the inserted card should be face up if (addPanel != pnlCPU) { aCardBox.FaceUp = true; } else { aCardBox.FaceUp = false; } //Update the card's image aCardBox.UpdateCardImage(); // Add the new control to the appropriate panel addPanel.Controls.Add(aCardBox); }
/// <summary> /// The event that will be wired to playable Cardboxes /// </summary> /// <param name="sender"> The object that the event is fired from </param> /// <param name="e"> The EventArgs object </param> private void Cardbox_Click(object sender, EventArgs e) { //Initialize the sender as a Cardbox CardBox.CardBox box = sender as CardBox.CardBox; //If the human player is the attacker if (attacker == humanPlayer) { //Add the card to the river river.Add(box.Card); //Remove the card from the player's hand humanPlayer.GetHand().Remove(box.Card); //If the player's hand is empty and the deck is empty then the human player wins if (humanPlayer.GetHand().Count == 0 && talon.Count == 0) { //Show the Winner form this.Hide(); frmWinner win = new frmWinner(); win.ShowDialog(); this.Close(); } else { try { //Draw a card from the computer's hand Card cpuCard = cpuPlayer.selectCard(river); //If the computer's hand is empty and the deck is empty then the human player loses if (defender.GetHand().Count == 0 && talon.Count == 0) { //Show the Loser form this.Hide(); frmLoser lose = new frmLoser(); lose.ShowDialog(); this.Close(); } //Add the computer's card to the river river.Add(cpuCard); //If the number of cards in the river equals 12 if (river.Count == 12) { //The defender was successful DefenceSuccess(); } } catch (OperationCanceledException) { //Set the human player as the cardAdder cardAdder = humanPlayer; attacker = null; //Move the played card to the adder adder.Add(box.Card); //Set the rank of the adder adderRank = box.Card.rank; //Remove the card from the river river.Remove(box.Card); } } } //If the human player is the defender else if (defender == humanPlayer) { //Add the card to the river river.Add(box.Card); //Remove the card from the player hand humanPlayer.GetHand().Remove(box.Card); //If the human player's hand is empty and the deck is empty then the human player wins if (humanPlayer.GetHand().Count == 0 && talon.Count == 0) { //Show the Winner form this.Hide(); frmWinner win = new frmWinner(); win.ShowDialog(); this.Close(); } //if the river count equals 12 if (river.Count == 12) { //The defender was successful DefenceSuccess(); } else { try { //Draw a card from the computer's hand Card cpuCard = cpuPlayer.selectCard(river); //If the computer's hand is empty and the deck is empty then the human player loses if (attacker.GetHand().Count == 0 && talon.Count == 0) { //Show the Loser form this.Hide(); frmLoser lose = new frmLoser(); lose.ShowDialog(); this.Close(); } //Add the computer's card to the river river.Add(cpuCard); } catch (OperationCanceledException) { //The defender was successful DefenceSuccess(); } } } //If the human player is the cardAdder else if (cardAdder == humanPlayer) { //Add the card to the adder adder.Add(box.Card); //Remove the card from the player hand humanPlayer.GetHand().Remove(box.Card); //If the human player's hand is empty and the deck is empty then the human player wins if (humanPlayer.GetHand().Count == 0 && talon.Count == 0) { //Show the Winner form this.Hide(); frmWinner win = new frmWinner(); win.ShowDialog(); this.Close(); } //If the human player has played 6 cards if (river.Count / 2 + adder.Count == 6) { //The attacker was successful AttackSuccess(); } } //Redraw the form Redraw(); }
/// <summary> /// PickUpRiver method, /// is to move every card on the field into hand /// </summary> /// <param name="panel"></param> public void PickUpRiver(Panel panel) { // Check if the defense is sucessful by getting the control count modulus by 2 // since the card come in pair (1 attack, 1 defense), if the result is 0 meaning the attack is repelled if (flowRiver.Controls.Count % 2 != 0) { successfulDefense = false; // set successfulDefense to false } else { successfulDefense = true; // set successfulDefense to true } for (int i = flowRiver.Controls.Count - 1; i >= 0; i--) { CardBox.CardBox card = flowRiver.Controls[i] as CardBox.CardBox; // move the card to the panel panel.Controls.Add(card); // remove from the field flowRiver.Controls.Remove(card); if (panel == pnlComputerHand) { card.FaceUp = false; // set FaceUp property to false card.Enabled = false; // disable card } if (panel == pnlHumanHand) { // wire the event to the card box card.Enabled = true; card.Click += CardBox_Click; card.MouseEnter += CardBox_MouseEnter; card.MouseLeave += CardBox_MouseLeave; } // remove the card onFieldCards.Remove(card.Card); System.Diagnostics.Debug.Write(panel.Name + " Picked Up " + card.ToString() + "\n"); } //realign both panel RealignCards(panel); RealignCards(pnlDiscardPile); if (successfulDefense) // check if its a successful defense { EndTurn(); // call EndTurn method } else { roundNumber++; // increment roundNumber // update text txtRoundNumber.Text = roundNumber.ToString(); txtRiverCardsRemaning.Text = flowRiver.Controls.Count.ToString(); DealHands(cardRemaining); // deal hand RemoveRiverCard(); // remove river card if (!HumanPlayer.IsAttacking) // check who's turn to attack { ComputerAttack(); } } }