// parameters: card, and an int: 0 = mycard1, 1 = mycard2, 2 = mycard3, 3 = mycard4. // 4 = housecard1, 5 = housecard2, 6 = housecard3, 7 = housecard4 public Card aceTest(Card card, int which) { Console.Out.WriteLine ("Ace test: which: " + which + ". " + card.getRank () + " of " + card.getSuit ()); // first some error handling if(which < 0 || which > 7) { Console.Out.WriteLine("AceTest: Not a real card."); return card; } // now set it to an 11 and update the count card.setNumericalRank (11); updateCount (); // see if an 11 will bust if(which == 0 || which == 1 || which == 2 || which == 3) { if(checkBust()) { card.setNumericalRank (1); } } else if(which == 4 || which == 5 || which == 6 || which == 7) { if(checkHouseBust()) { card.setNumericalRank (1); } } Console.Out.WriteLine("ace test final result: which: " + which + ". " + card.getRank () + " of " + card.getSuit ()); return card; }
/// <summary> /// calcualtes the value and checks that it doesnt go over 21 /// </summary> /// <param name="dealerCards"></param> private void calculateDealerHand(Card dealerCards) { dealerValue += Convert.ToInt32(dealerCards.Value); if(isBusted(dealerValue)) { bustedDealer = true; } }
/// <summary> /// calculate the size of the players hand , checks that the hand isnt to big /// </summary> /// <param name="playerCards"></param> private void calculatePlayerHand(Card playerCards) { playerValue += Convert.ToInt32(playerCards.Value); if (isBusted(playerValue)) { bustedPlayer = true; } }
/// <summary> /// Initializes a new deck of 52 cards /// in shuffled, random order /// </summary> public Deck() { for (var suit = 1; suit < 5; suit++) { for (var rank = 1; rank < 14; rank++) { Card card = new Card(suit, rank); _deck.Add(card); } } shuffleDeck(_deck); }
private void button1_Click(object sender, EventArgs e) { Card newCard = new Card(); infoLabel.Text = "Drawing Card!"; cardLabel.Text += newCard.Value + " of " + newCard.Color + ". The value of this card is: " + newCard.ValuePoints + "\n" ; int _p = int.Parse(pointCount.Text) + newCard.ValuePoints; //sum current point score + value of new card pointCount.Text = _p.ToString(); //update point Label text. if (_p == 21) { bustLabel.Text = "BLACKJACXX whoa that's awesome"; deal.Enabled = false; } if (_p > 21) { deal.Enabled = false; bustLabel.Text = "YOU'RE BUST BUDDY!"; } }
public virtual int CalculateHandValue(Card[] hand, int nOC) { if (hand == null) return 0; else { int total = 0; int aces = 0; for (int count = 0; count < nOC; count++) { switch (hand[count].Face) { case "Ace": aces += 1; break; case "Two": total += 2; break; case "Three": total += 3; break; case "Four": total += 4; break; case "Five": total += 5; break; case "Six": total += 6; break; case "Seven": total += 7; break; case "Eight": total += 8; break; case "Nine": total += 9; break; case "Ten": case "Jack": case "Queen": case "King": total += 10; break; default: // throw new Exception("\nNot a recognized card Suit.\n"); } } // allows user to decide aces value after other cards have been totalled if (aces > 0) { Console.WriteLine("\nCurrent total is: {0}\nYou have {1} ace(s).\n", total, aces); int num; for (int count = 0; count < aces; count++) { num = 0; while (num != 1 && num != 11) { try { Console.WriteLine("\nIs the Ace a 1 or 11?: "); num = Convert.ToInt32(Console.ReadLine()); } catch (FormatException e) { Console.WriteLine(e.Message + "\nInvalid Input, try again.\n"); } catch (Exception e) { Console.WriteLine(e.Message + "\nUnknown Error.\n"); } } total += num; } } return total; } }
public override int CalculateHandValue(Card[] hand, int nOC) { if (hand == null) return 0; else { int total = 0; int aces = 0; for (int count = 0; count < nOC; count++) { switch (hand[count].Face) { case "Ace": aces += 1; break; case "Two": total += 2; break; case "Three": total += 3; break; case "Four": total += 4; break; case "Five": total += 5; break; case "Six": total += 6; break; case "Seven": total += 7; break; case "Eight": total += 8; break; case "Nine": total += 9; break; case "Ten": case "Jack": case "Queen": case "King": total += 10; break; default: // throw new Exception("\nNot a recognized card Suit.\n"); } } // logic to decide what ace should be for dealer if (aces > 0) { while (aces > 0) { aces--; if (total + 11 + aces > 21) // if 11 plus number of aces left cannot be added total += 1; // then 11 is too much, so add 1 else total += 11; } } return total; } }
public async void hitMe() { if(mycard3 != null && mycard4 != null) { var alert = UIAlertController.Create("Whoa Whoa Whoa.", "You can't hit now.", UIAlertControllerStyle.Alert); // add buttons alert.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Default, null)); // actually show the thing PresentViewController(alert, true, null); } else { if(mycard3 == null) { myCard3Label.Hidden = false; myImage3.Image = UIImage.FromBundle ("back.png"); await Task.Delay (2500); mycard3 = dealDeck.dealCard (); aceTest (mycard1, 0); aceTest (mycard2, 1); aceTest (mycard3, 2); Console.Out.WriteLine ("mycard3: " + mycard3.getRank () + " of " + mycard3.getSuit ()); myCard3Label.Text = mycard3.getRank () + " of " + mycard3.getSuit (); myImage3.Image = UIImage.FromBundle (mycard3.getCardString ()); if(checkBust()) { var alert = UIAlertController.Create("Whoa Whoa Whoa.", "You busted! \n" + "On a " + mycard3.getRank() + " of " + mycard3.getSuit() + "\n Better hope the dealer busts. \n" + "He's dealing now. Hurry!", UIAlertControllerStyle.Alert); // add buttons alert.AddAction(UIAlertAction.Create("Okay :(", UIAlertActionStyle.Default, null)); // actually show the thing PresentViewController(alert, true, null); hitButton.Hidden = true; doneButton.Hidden = true; await Task.Delay (2000); hitDealer (); } } else if(mycard4 == null) { myCard4Label.Hidden = false; myImage4.Image = UIImage.FromBundle ("back.png"); await Task.Delay (2500); mycard4 = dealDeck.dealCard (); aceTest (mycard1, 0); aceTest (mycard2, 1); aceTest (mycard3, 2); aceTest (mycard4, 3); myCard4Label.Text = mycard4.getRank () + " of " + mycard4.getSuit (); myImage4.Image = UIImage.FromBundle (mycard4.getCardString ()); // could refactor into its own function if(checkBust()) { var alert = UIAlertController.Create("Whoa Whoa Whoa.", "You busted! \n" + "On a " + mycard4.getRank() + " of " + mycard4.getSuit() + "\n Better hope the dealer busts.\n" + "He's dealing now. Hurry!", UIAlertControllerStyle.Alert); // add buttons alert.AddAction(UIAlertAction.Create("Okay :(", UIAlertActionStyle.Default, null)); // actually show the thing PresentViewController(alert, true, null); hitButton.Hidden = true; doneButton.Hidden = true; await Task.Delay (2000); hitDealer (); } } } }
// remove a card from the deck (in the case of being dealt) public void removeCard(Card card) { if(card.getSuit() == "spades") { switch(card.getRank()) { case("ace"): this.aceSpades = null; break; case("two"): this.twoSpades = null; break; case("three"): this.threeSpades = null; break; case("four"): this.fourSpades = null; break; case("five"): this.fiveSpades = null; break; case("six"): this.sixSpades = null; break; case("seven"): this.sevenSpades = null; break; case("eight"): this.eightSpades = null; break; case("nine"): this.nineSpades = null; break; case("ten"): this.tenSpades = null; break; case("jack"): this.jackSpades = null; break; case("queen"): this.queenSpades = null; break; case("king"): this.kingSpades = null; break; default: Console.Out.WriteLine ("Remove Card Error: " + card.getRank () + " " + card.getSuit ()); break; } } else if(card.getSuit() == "clubs") { switch(card.getRank()) { case("ace"): this.aceClubs = null; break; case("two"): this.twoClubs = null; break; case("three"): this.threeClubs = null; break; case("four"): this.fourClubs = null; break; case("five"): this.fiveClubs = null; break; case("six"): this.sixClubs = null; break; case("seven"): this.sevenClubs = null; break; case("eight"): this.eightClubs = null; break; case("nine"): this.nineClubs = null; break; case("ten"): this.tenClubs = null; break; case("jack"): this.jackClubs = null; break; case("queen"): this.queenClubs = null; break; case("king"): this.kingClubs = null; break; default: Console.Out.WriteLine ("Remove Card Error: " + card.getRank () + " " + card.getSuit ()); break; } } else if(card.getSuit() == "diamonds") { switch(card.getRank()) { case("ace"): this.aceDiamonds = null; break; case("two"): this.twoDiamonds = null; break; case("three"): this.threeDiamonds = null; break; case("four"): this.fourDiamonds = null; break; case("five"): this.fiveDiamonds = null; break; case("six"): this.sixDiamonds = null; break; case("seven"): this.sevenDiamonds = null; break; case("eight"): this.eightDiamonds = null; break; case("nine"): this.nineDiamonds = null; break; case("ten"): this.tenDiamonds = null; break; case("jack"): this.jackDiamonds = null; break; case("queen"): this.queenDiamonds = null; break; case("king"): this.kingDiamonds = null; break; default: Console.Out.WriteLine ("Remove Card Error: " + card.getRank () + " " + card.getSuit ()); break; } } else if(card.getSuit() == "hearts") { switch(card.getRank()) { case("ace"): this.aceHearts = null; break; case("two"): this.twoHearts = null; break; case("three"): this.threeHearts = null; break; case("four"): this.fourHearts = null; break; case("five"): this.fiveHearts = null; break; case("six"): this.sixHearts = null; break; case("seven"): this.sevenHearts = null; break; case("eight"): this.eightHearts = null; break; case("nine"): this.nineHearts = null; break; case("ten"): this.tenHearts = null; break; case("jack"): this.jackHearts = null; break; case("queen"): this.queenHearts = null; break; case("king"): this.kingHearts = null; break; default: Console.Out.WriteLine ("Rank Remove Card Error: " + card.getRank () + " " + card.getSuit ()); break; } } else { Console.Out.WriteLine ("Suit Remove Card Error: " + card.getRank() + " " + card.getSuit ()); } this.isFull = false; this.remaining--; }
// function to pick a random card and deal it // eventually this should return a card public Card dealCard() { Random random = new Random (); int randCardRank = random.Next(52); // random number between 0 and 51 to use when picking a random card //Console.Out.WriteLine ("random number: " + randCardRank); Card returnCard = new Card("the angel's card", "king of heaven", 777); try { // now pick a card in the deck and make it null // this is gonna be a nightmare switch(randCardRank) { case 0: this.aceSpades = null; returnCard = new Card ("spades", "ace", 0); break; case 1: this.twoSpades = null; returnCard = new Card ("spades", "two", 1); break; case 2: this.threeSpades = null; returnCard = new Card ("spades", "three", 2); break; case 3: this.fourSpades = null; returnCard = new Card ("spades", "four", 3); break; case 4: this.fiveSpades = null; returnCard = new Card ("spades", "five", 4); break; case 5: this.sixSpades = null; returnCard = new Card ("spades", "six", 5); break; case 6: this.sevenSpades = null; returnCard = new Card ("spades", "seven", 6); break; case 7: this.eightSpades = null; returnCard = new Card ("spades", "eight", 7); break; case 8: this.nineSpades = null; returnCard = new Card ("spades", "nine", 8); break; case 9: this.tenSpades = null; returnCard = new Card ("spades", "ten", 9); break; case 10: this.jackSpades = null; returnCard = new Card ("spades", "jack", 10); break; case 11: this.queenSpades = null; returnCard = new Card ("spades", "queen", 11); break; case 12: this.kingSpades = null; returnCard = new Card ("spades", "king", 12); break; case 13: this.aceClubs = null; returnCard = new Card ("clubs", "ace", 13); break; case 14: this.twoClubs = null; returnCard = new Card ("clubs", "two", 14); break; case 15: this.threeClubs = null; returnCard = new Card ("clubs", "three", 15); break; case 16: this.fourClubs = null; returnCard = new Card ("clubs", "four", 16); break; case 17: this.fiveClubs = null; returnCard = new Card ("clubs", "five", 17); break; case 18: this.sixClubs = null; returnCard = new Card ("clubs", "six", 18); break; case 19: this.sevenClubs = null; returnCard = new Card ("clubs", "seven", 19); break; case 20: this.eightClubs = null; returnCard = new Card ("clubs", "eight", 20); break; case 21: this.nineClubs = null; returnCard = new Card ("clubs", "nine", 21); break; case 22: this.tenClubs = null; returnCard = new Card ("clubs", "ten", 22); break; case 23: this.jackClubs = null; returnCard = new Card ("clubs", "jack", 23); break; case 24: this.queenClubs = null; returnCard = new Card ("clubs", "queen", 24); break; case 25: this.kingClubs = null; returnCard = new Card ("clubs", "king", 25); break; case 26: this.aceDiamonds = null; returnCard = new Card ("diamonds", "ace", 26); break; case 27: this.twoDiamonds = null; returnCard = new Card ("diamonds", "two", 27); break; case 28: this.threeDiamonds = null; returnCard = new Card ("diamonds", "three", 28); break; case 29: this.fourDiamonds = null; returnCard = new Card ("diamonds", "four", 29); break; case 30: this.fiveDiamonds = null; returnCard = new Card ("diamonds", "five", 30); break; case 31: this.sixDiamonds = null; returnCard = new Card ("diamonds", "six", 31); break; case 32: this.sevenDiamonds = null; returnCard = new Card ("diamonds", "seven", 32); break; case 33: this.eightDiamonds = null; returnCard = new Card ("diamonds", "eight", 33); break; case 34: this.nineDiamonds = null; returnCard = new Card ("diamonds", "nine", 34); break; case 35: this.tenDiamonds = null; returnCard = new Card ("diamonds", "ten", 35); break; case 36: this.jackDiamonds = null; returnCard = new Card ("diamonds", "jack", 36); break; case 37: this.queenDiamonds = null; returnCard = new Card ("diamonds", "queen", 37); break; case 38: this.kingDiamonds = null; returnCard = new Card ("diamonds", "king", 38); break; case 39: this.aceHearts = null; returnCard = new Card ("hearts", "ace", 39); break; case 40: this.twoHearts = null; returnCard = new Card ("hearts", "two", 40); break; case 41: this.threeHearts = null; returnCard = new Card ("hearts", "three", 41); break; case 42: this.fourHearts = null; returnCard = new Card ("hearts", "four", 42); break; case 43: this.fiveHearts = null; returnCard = new Card ("hearts", "five", 43); break; case 44: this.sixHearts = null; returnCard = new Card ("hearts", "six", 44); break; case 45: this.sevenHearts = null; returnCard = new Card ("hearts", "seven", 45); break; case 46: this.eightHearts = null; returnCard = new Card ("hearts", "eight", 46); break; case 47: this.nineHearts = null; returnCard = new Card ("hearts", "nine", 47); break; case 48: this.tenHearts = null; returnCard = new Card ("hearts", "ten", 48); break; case 49: this.jackHearts = null; returnCard = new Card ("hearts", "jack", 49); break; case 50: this.queenHearts = null; returnCard = new Card ("hearts", "queen", 50); break; case 51: this.kingHearts = null; returnCard = new Card ("hearts", "king", 51); break; default: Console.Out.WriteLine ("Random Card Error."); returnCard = new Card ("the devil's card", "king of hell", 666); break; } Console.Out.WriteLine("after switch: " + returnCard.getRank() + " " + returnCard.getSuit()); } catch(Exception ex) { Console.Out.WriteLine ("Random card error. Card is already null"); Console.Out.WriteLine ("Error: " + ex.ToString ()); dealCard (); } return returnCard; }
public void AddCard(Card card, bool pocket) { card.pocket = pocket; cards.Add(card); }
public async void firstDeal() { // alright, deal me in. mycard1 = dealDeck.dealCard (); myCard1Label.Text = mycard1.getRank () + " of " + mycard1.getSuit (); myImage1.Image = UIImage.FromBundle (mycard1.getCardString ()); myImage2.Image = UIImage.FromBundle ("back.png"); houseImage1.Image = UIImage.FromBundle ("back.png"); houseImage2.Image = UIImage.FromBundle ("back.png"); await Task.Delay (2500); mycard2 = dealDeck.dealCard (); myCard2Label.Text = mycard2.getRank () + " of " + mycard2.getSuit (); myImage2.Image = UIImage.FromBundle (mycard2.getCardString ()); await Task.Delay (2500); aceTest (mycard1, 0); aceTest (mycard2, 1); // deal the dealer in housecard1 = dealDeck.dealCard (); houseCard1Label.Text = housecard1.getRank () + " of " + housecard1.getSuit (); houseImage1.Image = UIImage.FromBundle (housecard1.getCardString ()); await Task.Delay (2500); housecard2 = dealDeck.dealCard (); houseCard2Label.Text = housecard2.getRank () + " of " + housecard2.getSuit (); houseImage2.Image = UIImage.FromBundle (housecard2.getCardString ()); aceTest (housecard1, 4); aceTest (housecard2, 5); updateCount (); hitButton.Hidden = false; doneButton.Hidden = false; }
public void clearUI() { // add all the cards back to the deck dealDeck = null; dealDeck = new Deck (); // make all the cards null. why aren't there 1's and 2's here? mycard3 = null; mycard4 = null; housecard3 = null; housecard4 = null; // hide the labels myCard3Label.Hidden = true; myCard4Label.Hidden = true; houseCard3Label.Hidden = true; houseCard4Label.Hidden = true; hitButton.Hidden = true; doneButton.Hidden = true; // hide the images myImage1.Image = UIImage.FromBundle (" "); myImage2.Image = UIImage.FromBundle (" "); myImage3.Image = UIImage.FromBundle (" "); myImage4.Image = UIImage.FromBundle (" "); houseImage1.Image = UIImage.FromBundle (" "); houseImage2.Image = UIImage.FromBundle (" "); houseImage3.Image = UIImage.FromBundle (" "); houseImage4.Image = UIImage.FromBundle (" "); myCard1Label.Text = "Card One"; myCard2Label.Text = "Card Two"; houseCard1Label.Text = "Card One"; houseCard2Label.Text = "Card Two"; myCardsLabel.Text = "Your Cards"; houseCardsLabel.Text = "Dealer's Cards"; playButton.Hidden = false; }
public void add(Card card) { hand.Add(card); }
// constructor public Deck() { this.remaining = 52; this.isFull = true; // initialize the entire deck... // start with the spades as per usual this.aceSpades = new Card ("spades", "ace", 0); this.twoSpades = new Card ("spades", "two", 1); this.threeSpades = new Card ("spades", "three", 2); this.fourSpades = new Card ("spades", "four", 3); this.fiveSpades = new Card ("spades", "five", 4); this.sixSpades = new Card ("spades", "six", 5); this.sevenSpades = new Card ("spades", "seven", 6); this.eightSpades = new Card ("spades", "eight", 7); this.nineSpades = new Card ("spades", "nine", 8); this.tenSpades = new Card ("spades", "ten", 9); this.jackSpades = new Card ("spades", "jack", 10); this.queenSpades = new Card ("spades", "queen", 11); this.kingSpades = new Card ("spades", "king", 12); // initialize the clubs this.aceClubs = new Card ("clubs", "ace", 13); this.twoClubs = new Card ("clubs", "two", 14); this.threeClubs = new Card ("clubs", "three", 15); this.fourClubs = new Card ("clubs", "four", 16); this.fiveClubs = new Card ("clubs", "five", 17); this.sixClubs = new Card ("clubs", "six", 18); this.sevenClubs = new Card ("clubs", "seven", 19); this.eightClubs = new Card ("clubs", "eight", 20); this.nineClubs = new Card ("clubs", "nine", 21); this.tenClubs = new Card ("clubs", "ten", 22); this.jackClubs = new Card ("clubs", "jack", 23); this.queenClubs = new Card ("clubs", "queen", 24); this.kingClubs = new Card ("clubs", "king", 25); // then the diamonds this.aceDiamonds = new Card ("diamonds", "ace", 26); this.twoDiamonds = new Card ("diamonds", "two", 27); this.threeDiamonds = new Card ("diamonds", "three", 28); this.fourDiamonds = new Card ("diamonds", "four", 29); this.fiveDiamonds = new Card ("diamonds", "five", 30); this.sixDiamonds = new Card ("diamonds", "six", 31); this.sevenDiamonds = new Card ("diamonds", "seven", 32); this.eightDiamonds = new Card ("diamonds", "eight", 33); this.nineDiamonds = new Card ("diamonds", "nine", 34); this.tenDiamonds = new Card ("diamonds", "ten", 35); this.jackDiamonds = new Card ("diamonds", "jack", 36); this.queenDiamonds = new Card ("diamonds", "queen", 37); this.kingDiamonds = new Card ("diamonds", "king", 38); // finally the hearts this.aceHearts = new Card ("hearts", "ace", 39); this.twoHearts = new Card ("hearts", "two", 40); this.threeHearts = new Card ("hearts", "three", 41); this.fourHearts = new Card ("hearts", "four", 42); this.fiveHearts = new Card ("hearts", "five", 43); this.sixHearts = new Card ("hearts", "six", 44); this.sevenHearts = new Card ("hearts", "seven", 45); this.eightHearts = new Card ("hearts", "eight", 46); this.nineHearts = new Card ("hearts", "nine", 47); this.tenHearts = new Card ("hearts", "ten", 48); this.jackHearts = new Card ("hearts", "jack", 49); this.queenHearts = new Card ("hearts", "queen", 50); this.kingHearts = new Card ("hearts", "king", 51); }
// methods private void PopulateDeck() { string[] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; string[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" }; foreach (string suit in suits) foreach (string face in faces) { deck[CurrentCard] = new Card(face, suit); CurrentCard++; } }
public async void hitDealer() { updateCount (); if(housesum < 17) { if(housecard3 == null) { houseCard3Label.Hidden = false; houseImage3.Image = UIImage.FromBundle ("back.png"); await Task.Delay (2500); housecard3 = dealDeck.dealCard (); aceTest (housecard1, 4); aceTest (housecard2, 5); aceTest (housecard3, 6); Console.Out.WriteLine ("housecard3: " + housecard3.getRank () + " of " + housecard3.getSuit ()); houseImage3.Image = UIImage.FromBundle (housecard3.getCardString ()); await Task.Delay (1000); if(checkHouseBust()) { /*var alert = UIAlertController.Create("Whoa Whoa Whoa.", "The house busted! \n On a " + housecard3.getRank() + " of " + housecard3.getSuit(), UIAlertControllerStyle.Alert); // add buttons alert.AddAction(UIAlertAction.Create("Okay :)", UIAlertActionStyle.Default, null)); // actually show the thing PresentViewController(alert, true, null);*/ } } updateCount (); Console.Out.WriteLine ("housesum: " + housesum + ". mysum: " + mysum); if(housecard4 == null && housesum < 17) { houseCard4Label.Hidden = false; houseImage4.Image = UIImage.FromBundle ("back.png"); await Task.Delay (2500); housecard4 = dealDeck.dealCard (); aceTest (housecard1, 4); aceTest (housecard2, 5); aceTest (housecard3, 6); aceTest (housecard4, 7); Console.Out.WriteLine ("housecard4: " + housecard4.getRank () + " of " + housecard4.getSuit ()); houseCard4Label.Text = housecard4.getRank () + " of " + housecard4.getSuit (); houseImage4.Image = UIImage.FromBundle (housecard4.getCardString ()); await Task.Delay (1000); if(checkHouseBust()) { /*var alert = UIAlertController.Create("Whoa Whoa Whoa.", "The house busted! \n On a " + housecard4.getRank() + " of " + housecard4.getSuit(), UIAlertControllerStyle.Alert); // add buttons alert.AddAction(UIAlertAction.Create("Okay :)", UIAlertActionStyle.Default, null)); // actually show the thing PresentViewController(alert, true, null);*/ } } } updateCount (); checkWinner (); }