Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BlackJack
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool gameInProgress = true, gameWon = false;
- Console.WriteLine("What would you like your name to be?");
- Player player = new Player(Console.ReadLine().Trim());
- Deck deck = new Deck();
- Game game = new Game(player, deck);
- game.dealer.DrawCards(player, deck, game);
- game.dealer.DrawCards(player, deck, game);
- game.dealer.DrawCards(game.dealer, deck, game);
- game.dealer.DrawCards(game.dealer, deck, game);
- game.dealer.shuffle(deck);
- do
- {
- while (!gameWon)
- {
- bool stay = false;
- if (!game.checkWinner(player, game.dealer, stay))
- {
- if (game.promptUser(player))
- game.nextTurn(player, deck, game);
- else
- stay = true;
- if (stay)
- {
- game.checkWinner(player, game.dealer, stay);
- gameWon = true;
- }
- }
- else
- gameWon = true;
- }
- Console.WriteLine("The winner is {0} because {1}", game.winner, game.wonBy);
- Console.WriteLine("You have won {0} out of {1}. Would you like to play again?", player.gamesWon, player.gamesWon + player.gamesLost);
- bool response = false;
- while (!response)
- {
- switch (Console.ReadLine().Trim().ToLower())
- {
- case "yes":
- response = true;
- gameWon = false;
- player.score = 0;
- game = new Game(player, deck);
- continue;
- case "no":
- gameInProgress = false;
- response = true;
- continue;
- default:
- Console.WriteLine("I'm sorry, I can't interperate your response. Try again.");
- continue;
- }
- }
- } while (gameInProgress);
- }
- }
- public class Game
- {
- public Dealer dealer;
- public string winner, wonBy;
- public Game(Player player, Deck deck)
- {
- player.score = 0;
- dealer = new Dealer();
- player.hand.Clear();
- }
- public void nextTurn(Player player, Deck deck, Game game)
- {
- dealer.DrawCards(player, deck, game);
- dealer.DrawCards(dealer, deck, game);
- }
- public bool checkWinner(Player player, Dealer dealer, bool stay)
- {
- if (player.score > 21)
- {
- winner = "the dealer";
- wonBy = "the player busted.";
- player.gamesLost++;
- return true;
- }
- else if (dealer.score > 21)
- {
- winner = player.playerName;
- wonBy = "the dealer busted.";
- player.gamesWon++;
- return true;
- }
- else if (player.hand.Count == 2 && player.score == 21)
- {
- winner = player.playerName;
- wonBy = "you obtained a blackjack.";
- player.gamesWon++;
- return true;
- }
- else if (dealer.cardsDealt == 12)
- {
- winner = player.playerName;
- wonBy = "you drew six cards without having a score above or equal to 21";
- player.gamesWon++;
- return true;
- }
- else if (dealer.score > 15)
- stay = true;
- else if (player.score > dealer.score && stay)
- {
- winner = player.playerName;
- wonBy = "a stay was played, and you're closer to 21 than the dealer.";
- player.gamesWon++;
- return true;
- }
- else if (dealer.score > player.score && stay)
- {
- winner = "the dealer";
- wonBy = "a stay was played, and your score is further away to 21 than the dealers.";
- player.gamesLost++;
- return true;
- }
- else if (dealer.score == player.score)
- {
- winner = "nobody";
- wonBy = "a tie was achieved.";
- return true;
- }
- return false;
- }
- public bool promptUser(Player player)
- {
- bool response = false;
- Console.WriteLine("The dealers showing card is: {0}", dealer.hand[0].key);
- Console.WriteLine("Your score is {0}. Would you like to hit or stay?", player.score);
- foreach (var card in player.hand)
- {
- Console.WriteLine(card.key);
- }
- while (!response)
- {
- switch (Console.ReadLine().Trim().ToLower())
- {
- case "hit":
- case "i would like to hit":
- case "i'd like to hit":
- response = true;
- return true;
- case "stay":
- case "i would like to stay":
- case "i'd like to stay":
- response = true;
- return false;
- default:
- Console.WriteLine("I'm sorry, I can't interperate your input. Try again.");
- continue;
- }
- }
- return false;
- }
- public class Dealer : Participants
- {
- Random random = new Random();
- public int cardsDealt = 0;
- public void shuffle(Deck deck)
- {
- Console.WriteLine("Shuffling...");
- cardsDealt = 0;
- for (int j = 0; j < 5; j++)
- {
- int[] indexes = Enumerable.Repeat(0, deck.cards.Count).Select(i => random.Next(0, deck.cards.Count)).ToArray();
- for (int i = 0; i < deck.cards.Count; i++)
- {
- Card placeHolder;
- placeHolder = deck.cards[indexes[i]];
- deck.cards[indexes[i]] = deck.cards[i];
- deck.cards[i] = placeHolder;
- }
- }
- }
- public void DrawCards<T>(T participant, Deck deck, Game game) where T : Participants
- {
- cardsDealt++;
- if (cardsDealt != deck.cards.Count)
- {
- participant.hand.Add(deck.cards[cardsDealt]);
- participant.score += deck.cards[cardsDealt].value;
- }
- else
- {
- deck = deck.newDeck(game);
- participant.hand.Add(deck.cards[cardsDealt]);
- participant.score += deck.cards[cardsDealt].value;
- cardsDealt = 1;
- }
- }
- }
- public abstract class Participants
- {
- public List<Card> hand = new List<Card>();
- public int score = 0;
- }
- }
- public class Player : Game.Participants
- {
- public string playerName;
- public int gamesWon = 0, gamesLost = 0;
- public Player(string playerName)
- {
- this.playerName = playerName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement