using System; using System.Collections.Generic; using System.IO; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography; namespace GreenvilleRevenueGUI { internal class DeckofCards { Crazy8Card ACardBack; Random Random = new Random(); Crazy8Card[] AllCards = new Crazy8Card[52]; int tries = 0; int currentcardnumber = 0; public DeckofCards() { } public void shuffleCards() { int timetoshuffle = Random.Next(41 - 1001); for (int i = 0; i < timetoshuffle; i++) { int card1 = Random.Next(52); int card2 = Random.Next(52); Crazy8Card TempCard1 = AllCards[card1]; AllCards[card1] = AllCards[card2]; AllCards[card2] = TempCard1; } } public void LoadCards() { Crazy8Card ACard; String msg = ""; try { string[] list = Directory.GetFiles(@"cards", "*.gif"); Array.Sort(list); int suit = 0; for (int index = 0; index < 52; index++) { int value = GetNextCardValue(index); Image image = Image.FromFile(list[index]); ACard = new Crazy8Card(image, value, suit++); if (suit == 4) { suit = 0; } if (index > 31 && index < 36) { //this is an ace for later BJ ACard.SetCardToAce(); } AllCards[index] = ACard; } string[] list2 = Directory.GetFiles(@"cards", "Wfswbackcard*.gif"); Image Backimage = Image.FromFile(list2[0]); ACardBack = new Crazy8Card(Backimage, 0, 0); } catch (Exception e) { if (tries < 2) { msg = "Error Please make sure the card files in the Directory. \nWhen you put the cards in the Directory hit OK button.\n\n " + e.ToString(); MessageBox.Show(msg); tries++; LoadCards(); } else { Environment.Exit(1); } } } private int GetNextCardValue(int currentcardnumber) { int cardvalue = 10; if (currentcardnumber < 33) { cardvalue = (currentcardnumber / 4) + 2; } if (currentcardnumber > 31 && currentcardnumber < 36) cardvalue = 11;//aces if (currentcardnumber > 35 && currentcardnumber < 40) cardvalue = 12;//jack if (currentcardnumber > 39 && currentcardnumber < 44) cardvalue = 13;//kings if (currentcardnumber > 43 && currentcardnumber < 48) cardvalue = 14;//queen return cardvalue; } public Crazy8Card GetNextCard() { if (currentcardnumber >= 52) { currentcardnumber = 0; MessageBox.Show("All cards have been dealt, Game Over"); Environment.Exit(-1); } return AllCards[currentcardnumber++]; } public void ResetCardsDiscarded() { for (int index = 0; index < 52; index++) { AllCards[index].Setdiscarded(false); } } public void DealCrazy8stoBoth() { for (int index = 0; index < 52; index++) { Crazy8Card TempCard1 = AllCards[index]; if (TempCard1.GetCardValue() == 8) { if (index == 0 || index == 9 || index == 10) { //do nothing it is already in its place } else { put8inposition(index); } } } currentcardnumber = 0;//RWW } private void put8inposition(int index) { //recall 8s go in (index == 0 || index == 9 || index == 10 ) Boolean notlocatedthenew8 = true; int index8 = 0; Crazy8Card Card8 = AllCards[index]; while (notlocatedthenew8) { Card TempCard1 = AllCards[index8]; if (TempCard1.GetCardValue() == 8) { if (index8 == 10) return;//all done if (index8 == 9) index8 = 10; if (index8 == 0) index8 = 9;// 0 is already in its place go to the next pos } else { // its ok to swap them AllCards[aceindex] is not an ace Crazy8Card OriginalCard = AllCards[index8]; AllCards[index8] = Card8; AllCards[index] = OriginalCard; notlocatedthenew8 = false; } } } } }