public Solitare_Form() { InitializeComponent(); Solitare_Game.SetupGame(); PopulateTableau(); DrawCardForDiscardPile(); }
/// <summary> /// Draw a card from the Draw Pile and place it in the Discard Pile. /// </summary> private void DrawCardForDiscardPile() { Hand drawPile = Solitare_Game.GetDrawPile(); if (drawPile.GetCount() > 1) { Card card = Solitare_Game.DrawCard(); Solitare_Game.AddCardToDiscardPile(card); DisplayDrawPile(card); } else if (drawPile.GetCount() == 1) { Card card = Solitare_Game.DrawCard(); Solitare_Game.AddCardToDiscardPile(card); DisplayDrawPile(card); drawPilePictureBox.Image = null; } else if (drawPile.GetCount() == 0) { Solitare_Game.ResetDrawDiscardPiles(); Card card = Solitare_Game.DrawCard(); Solitare_Game.AddCardToDiscardPile(card); DisplayDrawPile(card); } currentlyPlayingCard = false; }
/// <summary> /// Display all cards in Tableau. /// </summary> private void PopulateTableau() { TableLayoutPanel[] tableauLayoutPanels = ReturnTableLayoutPanels(); Hand[] tableaus = Solitare_Game.GetTableaus(); for (int i = 0; i < tableauLayoutPanels.Length; i++) { DisplayGuiHand(tableaus[i], tableauLayoutPanels[i]); } }
/// <summary> /// Removes a card from the Tableau /// </summary> /// <param name="clickedCard"></param> private void RemoveCardsFromTableau(Card clickedCard) { TableLayoutPanel[] tableLayoutPanels = ReturnTableLayoutPanels(); Hand[] tableaus = Solitare_Game.GetTableaus(); for (int i = 0; i < tableLayoutPanels.Length; i++) { foreach (Card card in tableaus[i]) { if (card == clickedCard) { tableaus[i].Remove(clickedCard); DisplayGuiHand(tableaus[i], tableLayoutPanels[i]); break; } } } }
/// <summary> /// Checks if the player has won. UNABLE TO CHECK FOR STALEMATE. /// </summary> private void CheckIfWon() { Hand[] suitPiles = Solitare_Game.GetSuitPiles(); bool hasWon = false; foreach (Hand hand in suitPiles) { if (hand.GetCount() == 13) { hasWon = true; } else { hasWon = false; break; } } if (hasWon) { MessageBox.Show("Congratulations! You've won the game of Solitare. Reselect the Soliate Game in the Card Games menu to replay."); this.Close(); } }
/// <summary> /// Move vards on the Tableau in respect to the current move the player is making or has made. /// </summary> /// <param name="clickedCard"></param> /// <param name="source"></param> private void SortCardsOnTableau(Card clickedCard, PictureBox source) { TableLayoutPanel[] tableLayoutPanels = ReturnTableLayoutPanels(); Hand[] tableaus = Solitare_Game.GetTableaus(); int indexOne = -1; int indexTwo = -1; for (int i = 0; i < tableaus.Length; i++) { foreach (Card card in tableaus[i]) { if (currentlySelectedCard == card) { indexOne = i; DisplayGuiHand(tableaus[i], tableLayoutPanels[i]); currentlyPlayingCard = false; } else if (clickedCard == card) { indexTwo = i; DisplayGuiHand(tableaus[i], tableLayoutPanels[i]); currentlyPlayingCard = false; } } } if (indexTwo != -1 && !(source.Name.Contains("suitPile"))) { tableaus[indexTwo].Add(currentlySelectedCard); DisplayGuiHand(tableaus[indexTwo], tableLayoutPanels[indexTwo]); } else if (indexOne != -1) { tableaus[indexOne].Remove(currentlySelectedCard); DisplayGuiHand(tableaus[indexOne], tableLayoutPanels[indexOne]); } }
/// <summary> /// Determine the conditions of the game and try to move the card appropriately. /// </summary> /// <param name="clickedCard"></param> /// <param name="source"></param> private void TryToPlayCard(Card clickedCard, PictureBox source) { FaceValue facevalueOfClickedCard = clickedCard.GetFaceValue(); Hand[] suitPiles = Solitare_Game.GetSuitPiles(); PictureBox[] suitPilePictureBoxes = ReturnSuitPilePictureBoxes(); Hand discardPile = Solitare_Game.GetDiscardPile(); Hand drawPile = Solitare_Game.GetDrawPile(); if (facevalueOfClickedCard == FaceValue.Ace) { EvaluatePlayingAce(clickedCard, source, suitPiles, suitPilePictureBoxes, discardPile, drawPile); } else if (facevalueOfClickedCard == FaceValue.King) { EvaluatePlayingKing(clickedCard, source); } else { EvaluatePlayingRegularCard(clickedCard, source, discardPile); } // Add code to do something with the clicked card. }
/// <summary> /// Try to play an Ace while checking for the special conditions that an Ace may be of. /// </summary> /// <param name="clickedCard"></param> /// <param name="source"></param> /// <param name="suitPiles"></param> /// <param name="suitPilePictureBoxes"></param> /// <param name="discardPile"></param> /// <param name="drawPile"></param> private void EvaluatePlayingAce(Card clickedCard, PictureBox source, Hand[] suitPiles, PictureBox[] suitPilePictureBoxes, Hand discardPile, Hand drawPile) { currentlyPlayingAceCard = true; for (int i = 0; i < suitPiles.Length; i++) { //If Ace is in DiscardPile if (source.Name.Contains("suitPile")) { int currentlySelectedCardIntegerValue = ReturnIntegerValueOfCard(currentlySelectedCard); int clickedCardIntegerValue = ReturnIntegerValueOfCard(clickedCard); if (((currentlySelectedCardIntegerValue == (clickedCardIntegerValue + 1)) && (currentlySelectedCard.GetSuit() == clickedCard.GetSuit()))) { if (suitPiles[i] != null) { foreach (Card card in suitPiles[i]) { if (card == clickedCard) { suitPiles[i].Add(currentlySelectedCard); suitPilePictureBoxes[i].Image = Images.GetCardImage(currentlySelectedCard); suitPilePictureBoxes[i].Tag = currentlySelectedCard; if (discardPile.Contains(currentlySelectedCard)) { discardPile.Remove(clickedCard); drawPile.Remove(clickedCard); DrawCardForDiscardPile(); } else { SortCardsOnTableau(clickedCard, source); } break; } } SortCardsOnTableau(clickedCard, source); CheckIfWon(); } } else { CannotPlay(); break; } } else if (suitPilePictureBoxes[i].Image == null && !currentlyPlayingCard && !(source.Name.Contains("suitPile"))) { Solitare_Game.AddCardToSuitPile(i, clickedCard); suitPilePictureBoxes[i].Image = Images.GetCardImage(clickedCard); suitPilePictureBoxes[i].Tag = clickedCard; currentlyPlayingAceCard = false; if (source.Name == "discardPilePictureBox") { discardPile.Remove(clickedCard); drawPile.Remove(clickedCard); DrawCardForDiscardPile(); } else if (!(source.Name.Contains("suitPile"))) { RemoveCardsFromTableau(clickedCard); SortCardsOnTableau(clickedCard, source); } break; } } }
/// <summary> /// Try to play a King while checking for the special conditions that a King may be of. /// </summary> /// <param name="clickedCard"></param> /// <param name="source"></param> private void EvaluatePlayingKing(Card clickedCard, PictureBox source) { TableLayoutPanel[] tableLayoutPanels = ReturnTableLayoutPanels(); Hand[] tablaus = Solitare_Game.GetTableaus(); int currentlySelectedCardIntegerValue = ReturnIntegerValueOfCard(currentlySelectedCard); int clickedCardIntegerValue = ReturnIntegerValueOfCard(clickedCard); Hand discardPile = Solitare_Game.GetDiscardPile(); Hand[] suitPiles = Solitare_Game.GetSuitPiles(); Hand drawPile = Solitare_Game.GetDrawPile(); PictureBox[] suitPilePictureBoxes = ReturnSuitPilePictureBoxes(); for (int i = 0; i < tableLayoutPanels.Length; i++) { if (tableLayoutPanels[i].Controls.Count == 0 && !currentlyPlayingCard) { tablaus[i].Add(clickedCard); DisplayGuiHand(tablaus[i], tableLayoutPanels[i]); DrawCardForDiscardPile(); break; } else if (source.Name.Contains("suitPile")) { for (int ii = 0; ii < suitPiles.Length; i++) { if (((currentlySelectedCardIntegerValue == (clickedCardIntegerValue + 1)) && (currentlySelectedCard.GetSuit() == clickedCard.GetSuit()))) { if (suitPiles[ii] != null) { foreach (Card card in suitPiles[i]) { if (card == clickedCard) { suitPiles[ii].Add(currentlySelectedCard); suitPilePictureBoxes[ii].Image = Images.GetCardImage(currentlySelectedCard); suitPilePictureBoxes[ii].Tag = currentlySelectedCard; if (discardPile.Contains(currentlySelectedCard)) { discardPile.Remove(clickedCard); drawPile.Remove(clickedCard); DrawCardForDiscardPile(); } else { SortCardsOnTableau(clickedCard, source); } break; } } SortCardsOnTableau(clickedCard, source); CheckIfWon(); } } else { CannotPlay(); break; } } } else if (((currentlySelectedCardIntegerValue == (clickedCardIntegerValue - 1)) && (currentlySelectedCard.GetColour() != clickedCard.GetColour()))) { if (discardPile.Contains(currentlySelectedCard)) { discardPile.Remove(currentlySelectedCard); DrawCardForDiscardPile(); SortCardsOnTableau(clickedCard, source); } else { SortCardsOnTableau(clickedCard, source); } } else { CannotPlay(); } } }
/// <summary> /// Try to play a Regular Playing Card (not a King or an Ace) /// </summary> /// <param name="clickedCard"></param> /// <param name="source"></param> /// <param name="discardPile"></param> private void EvaluatePlayingRegularCard(Card clickedCard, PictureBox source, Hand discardPile) { if (source.Name.Contains("suitPile")) { Hand[] suitPiles = Solitare_Game.GetSuitPiles(); Hand drawPile = Solitare_Game.GetDrawPile(); PictureBox[] suitPilePictureBoxes = ReturnSuitPilePictureBoxes(); for (int i = 0; i < suitPiles.Length; i++) { int currentlySelectedCardIntegerValue = ReturnIntegerValueOfCard(currentlySelectedCard); int clickedCardIntegerValue = ReturnIntegerValueOfCard(clickedCard); if (((currentlySelectedCardIntegerValue == (clickedCardIntegerValue + 1)) && (currentlySelectedCard.GetSuit() == clickedCard.GetSuit()))) { if (suitPiles[i] != null) { foreach (Card card in suitPiles[i]) { if (card == clickedCard) { suitPiles[i].Add(currentlySelectedCard); suitPilePictureBoxes[i].Image = Images.GetCardImage(currentlySelectedCard); suitPilePictureBoxes[i].Tag = currentlySelectedCard; if (discardPile.Contains(currentlySelectedCard)) { discardPile.Remove(clickedCard); drawPile.Remove(clickedCard); DrawCardForDiscardPile(); } else { SortCardsOnTableau(clickedCard, source); } break; } } SortCardsOnTableau(clickedCard, source); CheckIfWon(); } } else { CannotPlay(); break; } } } else if (currentlyPlayingCard && source.Name == "discardPilePictureBox") { CannotPlay(); } else if (!currentlyPlayingCard) { currentlyPlayingCard = true; currentlySelectedCard = clickedCard; } else if (currentlyPlayingCard) { int currentlySelectedCardIntegerValue = ReturnIntegerValueOfCard(currentlySelectedCard); int clickedCardIntegerValue = ReturnIntegerValueOfCard(clickedCard); if (((currentlySelectedCardIntegerValue == (clickedCardIntegerValue - 1)) && (currentlySelectedCard.GetColour() != clickedCard.GetColour()))) { if (discardPile.Contains(currentlySelectedCard)) { discardPile.Remove(currentlySelectedCard); DrawCardForDiscardPile(); SortCardsOnTableau(clickedCard, source); } else { SortCardsOnTableau(clickedCard, source); } } else { CannotPlay(); } } }