Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace BlackJack
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. bool gameInProgress = true, gameWon = false;
  15. Console.WriteLine("What would you like your name to be?");
  16. Player player = new Player(Console.ReadLine().Trim());
  17. Deck deck = new Deck();
  18. Game game = new Game(player, deck);
  19. game.dealer.DrawCards(player, deck, game);
  20. game.dealer.DrawCards(player, deck, game);
  21. game.dealer.DrawCards(game.dealer, deck, game);
  22. game.dealer.DrawCards(game.dealer, deck, game);
  23. game.dealer.shuffle(deck);
  24.  
  25. do
  26. {
  27. while (!gameWon)
  28. {
  29. bool stay = false;
  30. if (!game.checkWinner(player, game.dealer, stay))
  31. {
  32. if (game.promptUser(player))
  33. game.nextTurn(player, deck, game);
  34. else
  35. stay = true;
  36. if (stay)
  37. {
  38. game.checkWinner(player, game.dealer, stay);
  39. gameWon = true;
  40. }
  41. }
  42. else
  43. gameWon = true;
  44. }
  45. Console.WriteLine("The winner is {0} because {1}", game.winner, game.wonBy);
  46. Console.WriteLine("You have won {0} out of {1}. Would you like to play again?", player.gamesWon, player.gamesWon + player.gamesLost);
  47. bool response = false;
  48. while (!response)
  49. {
  50. switch (Console.ReadLine().Trim().ToLower())
  51. {
  52. case "yes":
  53. response = true;
  54. gameWon = false;
  55. player.score = 0;
  56. game = new Game(player, deck);
  57. continue;
  58. case "no":
  59. gameInProgress = false;
  60. response = true;
  61. continue;
  62. default:
  63. Console.WriteLine("I'm sorry, I can't interperate your response. Try again.");
  64. continue;
  65. }
  66. }
  67. } while (gameInProgress);
  68. }
  69. }
  70. public class Game
  71. {
  72. public Dealer dealer;
  73. public string winner, wonBy;
  74. public Game(Player player, Deck deck)
  75. {
  76. player.score = 0;
  77. dealer = new Dealer();
  78. player.hand.Clear();
  79. }
  80. public void nextTurn(Player player, Deck deck, Game game)
  81. {
  82. dealer.DrawCards(player, deck, game);
  83. dealer.DrawCards(dealer, deck, game);
  84. }
  85. public bool checkWinner(Player player, Dealer dealer, bool stay)
  86. {
  87. if (player.score > 21)
  88. {
  89. winner = "the dealer";
  90. wonBy = "the player busted.";
  91. player.gamesLost++;
  92. return true;
  93. }
  94. else if (dealer.score > 21)
  95. {
  96. winner = player.playerName;
  97. wonBy = "the dealer busted.";
  98. player.gamesWon++;
  99. return true;
  100. }
  101. else if (player.hand.Count == 2 && player.score == 21)
  102. {
  103. winner = player.playerName;
  104. wonBy = "you obtained a blackjack.";
  105. player.gamesWon++;
  106. return true;
  107. }
  108. else if (dealer.cardsDealt == 12)
  109. {
  110. winner = player.playerName;
  111. wonBy = "you drew six cards without having a score above or equal to 21";
  112. player.gamesWon++;
  113. return true;
  114. }
  115. else if (dealer.score > 15)
  116. stay = true;
  117. else if (player.score > dealer.score && stay)
  118. {
  119. winner = player.playerName;
  120. wonBy = "a stay was played, and you're closer to 21 than the dealer.";
  121. player.gamesWon++;
  122. return true;
  123. }
  124. else if (dealer.score > player.score && stay)
  125. {
  126. winner = "the dealer";
  127. wonBy = "a stay was played, and your score is further away to 21 than the dealers.";
  128. player.gamesLost++;
  129. return true;
  130. }
  131. else if (dealer.score == player.score)
  132. {
  133. winner = "nobody";
  134. wonBy = "a tie was achieved.";
  135. return true;
  136. }
  137. return false;
  138. }
  139. public bool promptUser(Player player)
  140. {
  141. bool response = false;
  142. Console.WriteLine("The dealers showing card is: {0}", dealer.hand[0].key);
  143. Console.WriteLine("Your score is {0}. Would you like to hit or stay?", player.score);
  144. foreach (var card in player.hand)
  145. {
  146. Console.WriteLine(card.key);
  147. }
  148. while (!response)
  149. {
  150. switch (Console.ReadLine().Trim().ToLower())
  151. {
  152. case "hit":
  153. case "i would like to hit":
  154. case "i'd like to hit":
  155. response = true;
  156. return true;
  157. case "stay":
  158. case "i would like to stay":
  159. case "i'd like to stay":
  160. response = true;
  161. return false;
  162. default:
  163. Console.WriteLine("I'm sorry, I can't interperate your input. Try again.");
  164. continue;
  165. }
  166. }
  167. return false;
  168. }
  169.  
  170. public class Dealer : Participants
  171. {
  172. Random random = new Random();
  173. public int cardsDealt = 0;
  174.  
  175. public void shuffle(Deck deck)
  176. {
  177. Console.WriteLine("Shuffling...");
  178. cardsDealt = 0;
  179. for (int j = 0; j < 5; j++)
  180. {
  181. int[] indexes = Enumerable.Repeat(0, deck.cards.Count).Select(i => random.Next(0, deck.cards.Count)).ToArray();
  182. for (int i = 0; i < deck.cards.Count; i++)
  183. {
  184. Card placeHolder;
  185. placeHolder = deck.cards[indexes[i]];
  186. deck.cards[indexes[i]] = deck.cards[i];
  187. deck.cards[i] = placeHolder;
  188. }
  189. }
  190. }
  191.  
  192.  
  193. public void DrawCards<T>(T participant, Deck deck, Game game) where T : Participants
  194. {
  195. cardsDealt++;
  196. if (cardsDealt != deck.cards.Count)
  197. {
  198. participant.hand.Add(deck.cards[cardsDealt]);
  199. participant.score += deck.cards[cardsDealt].value;
  200. }
  201. else
  202. {
  203. deck = deck.newDeck(game);
  204. participant.hand.Add(deck.cards[cardsDealt]);
  205. participant.score += deck.cards[cardsDealt].value;
  206. cardsDealt = 1;
  207. }
  208. }
  209. }
  210.  
  211. public abstract class Participants
  212. {
  213. public List<Card> hand = new List<Card>();
  214. public int score = 0;
  215. }
  216. }
  217. public class Player : Game.Participants
  218. {
  219. public string playerName;
  220. public int gamesWon = 0, gamesLost = 0;
  221. public Player(string playerName)
  222. {
  223. this.playerName = playerName;
  224. }
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement