Ejemplo n.º 1
0
        private void ResolvePlayerHand(Hand hand)
        {
            OnRoundTurnStart(new OnRoundTurnStartArgs()
            {
                Player = _player,
                Hand   = hand
            });

            bool isDouble = false;

            if (hand.Value <= 11)
            {
                DoubleAction doubleAction = OnRoundDouble(new OnRoundDoubleArgs()
                {
                    Player = _player
                });

                if (doubleAction == DoubleAction.Yes)
                {
                    OnRoundIfDouble(new OnRoundIfDoubleArgs()
                    {
                        Player = _player,
                        Hand   = hand
                    });

                    isDouble = true;
                    hand.AddCard(_deck.GetNextCard());
                    OnRoundDeal(new OnRoundDealArgs()
                    {
                        Player = _player,
                        Hand   = hand
                    });
                }
            }

            while (!hand.IsBust && !isDouble)
            {
                TurnAction turnAction = OnRoundTurnDecision(new OnRoundTurnDecisionArgs()
                {
                    Player = _player
                });

                if (turnAction == TurnAction.Hit)
                {
                    hand.AddCard(_deck.GetNextCard());
                    OnRoundDeal(new OnRoundDealArgs()
                    {
                        Player = _player,
                        Hand   = hand
                    });
                }
                else if (turnAction == TurnAction.Stay)

                {
                    OnRoundStay(new OnRoundStayArgs()
                    {
                        Player = _player,
                        Hand   = hand
                    });
                    break;
                }
                else
                {
                    throw new InvalidOperationException("Invalid TurnAction");
                }
            }
        }
Ejemplo n.º 2
0
        public static void GameStart(GameManagement game)
        {
            //List<Card> playerCards = new List<Card>();
            Hand playerHand = new Hand();
            Hand dealerHand = new Hand();

            bool       start = true;
            ConsoleKey answer;


            //GameLogic();
            Console.WriteLine("Dealer Limit is 18\nYour 2 cards are: ");
            playerHand.AddCard(game.GetCard());
            playerHand.AddCard(game.GetCard());

            //color for the foreground to match the card types and printing it
            Console.ForegroundColor = GetForeColor(playerHand.GetCard(0));
            Console.WriteLine($"Value : {playerHand.GetCard(0).GetCardValue().PadRight(10)} Suit: {playerHand.GetCard(0).GetSuit().PadRight(10)}");

            Console.ForegroundColor = GetForeColor(playerHand.GetCard(1));
            Console.WriteLine($"Value : {playerHand.GetCard(1).GetCardValue().PadRight(10)} Suit: {playerHand.GetCard(1).GetSuit().PadRight(10)}");

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine($"Hand value: {GetValue(playerHand.GetHand())}");

            //DEALER TEST
            Console.WriteLine("Dealer Has: ");
            dealerHand.AddCard(game.GetCard());

            //color for the foreground to match the card types and printing it
            Console.ForegroundColor = GetForeColor(dealerHand.GetCard(0));
            Console.WriteLine($"Value : {dealerHand.GetCard(0).GetCardValue().PadRight(10)} Suit: {dealerHand.GetCard(0).GetSuit().PadRight(10)}");

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine($"Dealer Hand value: {GetValue(dealerHand.GetHand())}");

            Console.WriteLine("Do you want more cards? \nH for hit, S to stop, Esc to end");

            //game loop
            while (start)
            {
                do
                {
                    answer = Console.ReadKey(true).Key; //to not show the key in the console
                } while (!answer.Equals(ConsoleKey.H) && !answer.Equals(ConsoleKey.Escape) && !answer.Equals(ConsoleKey.S));

                //conditional for the Hit key and the respective values
                if (answer.Equals(ConsoleKey.H))
                {
                    playerHand.AddCard(game.GetCard());

                    Console.ForegroundColor = GetForeColor(playerHand.GetCard(playerHand.GetHandSize() - 1));

                    Console.WriteLine($"Value : {playerHand.GetCard(playerHand.GetHandSize() - 1).GetCardValue().PadRight(10)}" +
                                      $" Suit: {playerHand.GetCard(playerHand.GetHandSize() - 1).GetSuit().PadRight(10)}");



                    Console.ForegroundColor = ConsoleColor.White;
                    if (GetValue(playerHand.GetHand()) < 21)
                    {
                        Console.WriteLine($"Hand value: {GetValue(playerHand.GetHand())}");
                    }
                    else if (GetValue(playerHand.GetHand()) == 21 && playerHand.GetHandSize() > 2)
                    {
                        Console.Write("Hand value is 21! Maximum Value!");
                    }
                    else if (GetValue(playerHand.GetHand()) == 21 && playerHand.GetHandSize() == 2)
                    {
                        Console.Write("Hand value is 21! Blackjack!");
                    }
                    else
                    {
                        Console.WriteLine($"You blew up with: {GetValue(playerHand.GetHand())}");
                        start = false;
                    }
                }
                else if (answer.Equals(ConsoleKey.S)) //player turn ended, activate dealer turn
                {
                    Console.WriteLine("Dealer turn.");
                }
                else if (answer.Equals(ConsoleKey.Escape)) //gameEnd
                {
                    Console.WriteLine("Thank you for not playing!");
                    start = false;
                }
            }
        }
Ejemplo n.º 3
0
        public static void Play()
        {
            Deck deck = new Deck();

            Hand player = new Hand();

            player.AddCard(deck.Draw());
            player.AddCard(deck.Draw());
            Console.WriteLine($"Player has drawn {player} worth {player.PointValue} points.");

            if (player.IsBlackJack)
            {
                Console.WriteLine("Black Jack! You Win!");
                return;
            }

            bool keepDrawing;

            do
            {
                Console.WriteLine("Do you want to draw another card?");
                var input = Console.ReadLine().ToLower();
                keepDrawing = input == "yes" || input == "hit me";
                if (keepDrawing)
                {
                    player.AddCard(deck.Draw());
                    Console.WriteLine($"You drew the {player.LastCard.FullName} for a new total of {player.PointValue} points.");
                }
            } while (keepDrawing && !player.IsBusted);

            if (player.IsBusted)
            {
                Console.WriteLine("Busted!  You lose.");
                return;
            }

            Hand computer = new Hand();

            computer.AddCard(deck.Draw());
            computer.AddCard(deck.Draw());
            Console.WriteLine($"Computer has drawn {computer} worth {computer.PointValue} points.");

            if (computer.IsBlackJack)
            {
                Console.WriteLine("Black Jack! You Lose.");
                return;
            }

            while (computer.PointValue < player.PointValue && computer.PointValue <= 21)
            {
                computer.AddCard(deck.Draw());
                Console.WriteLine($"Computer has drawn the {computer.LastCard.FullName} for a new total of {computer.PointValue} points.");
            }

            if (computer.IsBusted)
            {
                Console.WriteLine("Busted!  You win!");
                return;
            }

            if (computer.PointValue >= player.PointValue)
            {
                Console.WriteLine($"The computer won with {computer.PointValue} compared to your {player.PointValue}.");
            }
            if (computer.PointValue == player.PointValue)
            {
                Console.WriteLine("Ties always goes to the house.");
            }

            if (computer.PointValue < player.PointValue)
            {
                Console.WriteLine($"You won with {player.PointValue} compared to its {computer.PointValue}.");
            }

            return;
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var keepPlaying = true;

            while (keepPlaying)
            {
                Deck _deck = new Deck();

                Player _player = new Player();
                Dealer _dealer = new Dealer();


                Hand playerHand = new Hand();

                Hand dealerHand = new Hand();

                playerHand.AddCard(_deck.DealCard());
                dealerHand.AddCard(_deck.DealCard());
                playerHand.AddCard(_deck.DealCard());
                dealerHand.AddCard(_deck.DealCard());


                _player.AddHand(playerHand);
                _dealer.AddHand(dealerHand);

                while (playerHand.TotalValue() <= 21)
                {
                    Console.WriteLine();
                    Console.WriteLine("---------------------");
                    Console.WriteLine($"Hand Value: {playerHand.TotalValue()}");
                    playerHand.ShowHand();
                    Console.WriteLine("---------------------");
                    Console.WriteLine();

                    Console.Write("(H)it or (S)tand: ");
                    var answer = Console.ReadLine();

                    if (answer == "H")
                    {
                        playerHand.AddCard(_deck.DealCard());
                    }
                    else
                    {
                        break;
                    }
                }
                Console.WriteLine();
                Console.WriteLine("---------------------");
                Console.WriteLine($"Hand Value: {playerHand.TotalValue()}");
                playerHand.ShowHand();
                Console.WriteLine("---------------------");
                Console.WriteLine();

                while (dealerHand.TotalValue() < 17)
                {
                    dealerHand.AddCard(_deck.DealCard());
                }

                Console.WriteLine();
                Console.WriteLine("---------------------");
                Console.WriteLine($"Dealer's Hand Value: {dealerHand.TotalValue()}");
                dealerHand.ShowHand();
                Console.WriteLine("---------------------");
                Console.WriteLine();

                if (playerHand.TotalValue() > 21)
                {
                    Console.WriteLine("Dealer Wins!");
                }
                else
                if (dealerHand.TotalValue() > 21)
                {
                    Console.WriteLine("Player Wins!");
                }
                else if (dealerHand.TotalValue() >= playerHand.TotalValue())
                {
                    Console.WriteLine("Dealer Wins!");
                }
                else
                {
                    Console.WriteLine("Player Wins!");
                }

                Console.Write("Play again? (Y/N): ");
                var playAgainString = Console.ReadLine();
                keepPlaying = (playAgainString == "Y" || playAgainString == "y");
            }

            Console.WriteLine();
            Console.WriteLine("Thanks for playing");
        }