Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SoloLearn
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // number of games
  14. int playCount = 100;
  15.  
  16. Game game = new Game();
  17. int wins = 0;
  18. for (int i = 0; i < playCount; i++)
  19. {
  20. if (game.playGame())
  21. {
  22. wins++;
  23. }
  24. }
  25. Console.Write("You got " + wins + " blackjacks");
  26. }
  27. }
  28. public class Game
  29. {
  30. Deck deck;
  31. int result;
  32. List<Card> hand;
  33. int acesInHand;
  34.  
  35. public bool playGame() {
  36. bool isBlackJack = false;
  37. acesInHand = 0;
  38. deck = new Deck();
  39. deck.shuffle();
  40. result = 0;
  41. int cardNumber = 0;
  42. hand = new List<Card>();
  43.  
  44. while (result < 21) {
  45. if (deck.getCards().ElementAt(cardNumber).getSecondCount() > 0) {
  46. acesInHand++;
  47. }
  48. hand.Add(deck.getCards().ElementAt(cardNumber));
  49. result = calculateResult(hand);
  50. cardNumber++;
  51. }
  52. if (result == 21) {
  53. isBlackJack = true;
  54. }
  55. return isBlackJack;
  56. }
  57.  
  58. public int calculateResult(List<Card> cards) {
  59. int sum = 0;
  60. for (int j = acesInHand; j >= 0; j--) {
  61. sum = 0;
  62. for (int i = 0; i < cards.Count; i++) {
  63. int smallAcecInHand = acesInHand;
  64. if (cards.ElementAt(i).getSecondCount() > 0 && smallAcecInHand > 0) {
  65. sum = sum + cards.ElementAt(i).getSecondCount();
  66. smallAcecInHand--;
  67. }
  68. else
  69. {
  70. sum = sum + cards.ElementAt(i).getCount();
  71. }
  72. }
  73. if (sum == 21) {
  74. return sum;
  75. }
  76. }
  77. return sum;
  78. }
  79. }
  80.  
  81. public class Card
  82. {
  83. int secondCount = -1;
  84. int count;
  85. string name;
  86. int[] counts = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
  87. string[] names = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
  88.  
  89. public Card(int num) {
  90. this.name = names[num];
  91. this.count = counts[num];
  92. }
  93. public Card(int num1, int num2) {
  94. this.name = names[num1];
  95. this.count = counts[num1];
  96. this.secondCount = counts[num2];
  97. }
  98. public string getName() {
  99. return name;
  100. }
  101. public int getCount() {
  102. return count;
  103. }
  104. public int getSecondCount() {
  105. return secondCount;
  106. }
  107. }
  108.  
  109. public class Deck
  110. {
  111. List<Card> cards;
  112. static Random rnd = new Random();
  113.  
  114. public Deck() {
  115. cards = new List<Card>();
  116. for (short a = 0; a <= 3; a++) {
  117. for (short b = 0; b <= 12; b++) {
  118. if (b == 0) {
  119. cards.Add(new Card(b, 13));
  120. } else {
  121. cards.Add(new Card(b));
  122. }
  123. }
  124. }
  125. }
  126. public void shuffle() {
  127. int n = cards.Count;
  128. while (n > 1)
  129. {
  130. n--;
  131. int k = rnd.Next(n + 1);
  132. Card value = cards[k];
  133. cards[k] = cards[n];
  134. cards[n] = value;
  135. }
  136. }
  137. public List<Card> getCards() {
  138. return cards;
  139. }
  140. public Card getCard(int i) {
  141. return cards.ElementAt(i);
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement