/// <summary> /// Realign human and computer panels on screen /// </summary> private void RealignAll() { PlayerHand humanHand = table.GetHumanHand(); pnlHuman.Controls.Clear(); foreach (Card c in humanHand.GetCards()) { c.FaceUp = true; TheCardBox.CardBox cardBox = new TheCardBox.CardBox(c); cardBox.Click += CardBox_Click; pnlHuman.Controls.Add(cardBox); RealignHand(pnlHuman); } PlayerHand machineHand = table.GetMachineHand(); pnlMachine.Controls.Clear(); foreach (Card c in machineHand.GetCards()) { c.FaceUp = false; TheCardBox.CardBox cardBox = new TheCardBox.CardBox(c); pnlMachine.Controls.Add(cardBox); RealignHand(pnlMachine, 0); } }
private void CardBox_Click(object sender, EventArgs e) { // Convert sender to a CardBox TheCardBox.CardBox aCardBox = sender as TheCardBox.CardBox; // If the conversion worked if (aCardBox != null) { ProcessTurn(aCardBox); } }
/// <summary> /// Repositions the cards in the table to indicate an attack / defense correctly /// </summary> private void AddCardToTable(TheCardBox.CardBox aCardBox, bool isAttack) { pnlTable.Controls.Add(aCardBox); // Determine the number of cards/controls in the panel, after creating the last control int myCount = pnlTable.Controls.Count; if (isAttack) { pnlTable.Controls[myCount - 1].Top = 0; // Attack cards are at the top of the panel pnlTable.Controls[myCount - 1].Left = countAttacks * 130; // 176 is the magical number to allow a good visual position countAttacks++; } else // It is a defense { pnlTable.Controls[myCount - 1].Top = 60; // 80 is the vertical position of the defense card pnlTable.Controls[myCount - 1].Left = 16 + countDefenses * 130; // 176 is the magical number to allow a good visual position and 16 is the good initial horizontal position pnlTable.Controls[myCount - 1].BringToFront(); countDefenses++; } }
/// <summary> /// This process the human and the machine turn, in this order /// </summary> /// <param name="aCardBox"></param> private void ProcessTurn(TheCardBox.CardBox aCardBox) { // if the card is in the human panel... if (aCardBox.Parent == pnlHuman) { // Identify if it is attack or defense bool isAttack; if (table.GameStage == GameStage.HumanAttack) { isAttack = true; if (table.MakeAttack(table.Human, aCardBox.Card)) { // Change to machine defense if the attack was a valid card, otherwise keep waiting for card table.GameStage = GameStage.MachineDefense; // Add the control to the table panel AddCardToTable(aCardBox, isAttack); // Remove the card from the human panel pnlHuman.Controls.Remove(aCardBox); // Realign the cards RealignHand(pnlHuman); } } else // table.GameStage == GameStage.HumanDefense { isAttack = false; if (table.MakeDefense(table.Human, aCardBox.Card)) { // Change to machine attack if the defense was a valid card, otherwise keep waiting for card table.GameStage = GameStage.MachineAttack; // Add the control to the table panel AddCardToTable(aCardBox, isAttack); // Remove the card from the human panel pnlHuman.Controls.Remove(aCardBox); // Realign the cards RealignHand(pnlHuman); } } } // If it is Machine turn if (table.GameStage == GameStage.MachineAttack) { try { // Select next attack card and move it to the table Card machineCard = table.MachineAttack(); foreach (Control control in pnlMachine.Controls) { TheCardBox.CardBox cardBox = (TheCardBox.CardBox)control; if (cardBox != null) // which means the control is a cardBox { if (cardBox.Card == machineCard) // Look for which cardBox has the selected card { // Flip this card to show its value cardBox.FaceUp = true; // Add this cardbox to the table panel AddCardToTable(cardBox, true); // Remove the card from the computer panel pnlMachine.Controls.Remove(cardBox); // Realign the cards RealignHand(pnlMachine); } } } // Allow the human to defend it table.GameStage = GameStage.HumanDefense; } catch (NoOKCardException) // If the machine player has no more good cards to play { // End turn EndTurn(true); // true because in this case, defense won because the machine do not have more good cards to attack RealignAll(); // REalign all cards on the screen } } if (table.GameStage == GameStage.MachineDefense) { try { // Select the defense card and move it to the table Card machineCard = table.MachineDefense(aCardBox.Card); foreach (Control control in pnlMachine.Controls) { TheCardBox.CardBox cardBox = (TheCardBox.CardBox)control; if (cardBox != null) // which means the control is a cardBox { if (cardBox.Card == machineCard) // Look for which cardBox has the selected card { // Flip this card to show its value cardBox.FaceUp = true; // Add this cardbox to the table panel AddCardToTable(cardBox, false); // Remove the card from the computer panel pnlMachine.Controls.Remove(cardBox); // Realign the cards RealignHand(pnlMachine); } } } // Allow the human to defend it table.GameStage = GameStage.HumanAttack; } catch (NoOKCardException) // If the machine player has no more good cards to play { // End turn EndTurn(false); // false because in this case, defense lose because the machine do not have more good cards to defend RealignAll(); // Realign all cards on the screen } } }