예제 #1
0
        private BlackjackGame InitializeGameForTesting()
        {
            var game = new BlackjackGame(
                minWager: 1,
                maxWager: 100,
                maxPlayers: _numberOfPlayers);

            for (int i = 0; i < _numberOfPlayers; i++)
            {
                var player = new TestPlayer(name: "p" + i);
                game.AddPlayer(player.Account, player.Name);
            }

            return(game);
        }
예제 #2
0
        public IActionResult PlayBlackjack(string bet)
        {
            /* Get the current logged in user. */
            ViewBag.userId = _userManager.GetUserId(HttpContext.User);
            ApplicationUser user = _userManager.FindByIdAsync(ViewBag.userId).Result;

            ViewBag.User = user;

            // set up game + player objects
            BlackjackGame   blackjack = new BlackjackGame(50, 5000, 6);
            BlackjackPlayer player    = new BlackjackPlayer(user);

            blackjack.AddPlayer(player);

            /* Validation */
            ViewBag.Errors = new List <string>();
            if (!int.TryParse(bet, out int i))
            {
                ViewBag.Errors.Add("The bet must be an integer.");  // checks if bet is an integer
            }
            /* If any errors got added to error list we'll return the view now */
            if (ViewBag.Errors.Count >= 1)
            {
                return(View("Coinflip"));
            }

            // take players bet
            blackjack.Player.Bet(i); // i == bet after validation.

            // display hand values
            ViewBag.PlayerHandValue = blackjack.Player.Hand.GetValue();
            ViewBag.DealerHandValue = blackjack.Dealer.Hand.GetValue();

            // check if player got a blackjack
            if (ViewBag.PlayerHand.Blackjack)
            {
                blackjack.Dealer.Payout(blackjack.Player); // we need an optional blackjack parameter on this method right?
                ViewBag.Results = "You got a Blackjack!";
            }
            else
            {
                ViewBag.CanHit = ViewBag.PlayerHand.CanHit();
            }

            return(View("Blackjack"));
        }
예제 #3
0
        static void Main(string[] args)
        {
            var decider = new PlayerActionDecider();
            var wager   = 20;

            decider.Initialize();
            const double start   = 5000;
            double       balance = start;
            var          rounds  = 0;
            var          game    = new BlackjackGame(10, 100, 2);
            var          account = new PlayerAccount(
                id: Guid.NewGuid().ToString(),
                startingBalance: balance);
            var alias = "P1";

            game.AddPlayer(account, alias);
            var           player = game.Players.First(p => p.Alias == alias);
            StringBuilder round  = new StringBuilder();

            while (balance > 0 && balance < start * 10)
            {
                round.AppendLine("-----------------NEW ROUND-----------------");
                rounds++;
                // Console.WriteLine("-----NEW ROUND-----");

                round.AppendLine("Wagering");
                player.SetWager(wager);
                round.AppendLine("refreshshoeifneeded");
                game.RefreshShoeIfNeeded();
                // Console.WriteLine("Start Round");
                game.StartRound();
                if (game.RoundClosedBecauseOfDealer21)
                {
                    round.AppendLine("dealer has 21");
                    //Console.WriteLine("Dealer has 21");
                    round.AppendLine("endround");
                    game.EndRound();
                    balance = player.Account.Balance;
                    Console.WriteLine(player.Account.Balance);
                    continue;
                }
                //Console.WriteLine(player.Hand.Cards.Count());
                ShowHand(player);

                var done = false;
                while (!done)
                {
                    if (player.Hand.IsBusted)
                    {
                        //  Console.WriteLine("Busted");
                        round.AppendLine("Busted!");
                        done = true;
                    }
                    else if (player.Hand.IsBlackjack || player.Hand.ScoreHighLow.High == 21)
                    {
                        //  Console.WriteLine($"standing with score {player.Hand.ScoreHighLow.High}");
                        round.AppendLine("player has blackjack");
                        done = true;
                        round.AppendLine("standing with bj");
                        player.Stand();
                    }
                    else
                    {
                        var action = decider.DecideAction(player.Hand, game.DealerHand.Cards.First());
                        switch (action)
                        {
                        case PlayerAction.Stand:
                            round.AppendLine("standing");
                            player.Stand();
                            done = true;
                            break;

                        case PlayerAction.Hit:
                            round.AppendLine("hit");
                            player.Hit();
                            break;

                        case PlayerAction.DoubleHit:
                            if (player.Hand.Cards.Count() == 2)
                            {
                                round.AppendLine("doubledown");
                                DoubleDown(wager, player);
                                done = true;
                            }
                            else
                            {
                                player.Hit();
                            }
                            break;

                        case PlayerAction.DoubleStand:
                            if (player.Hand.Cards.Count() == 2)
                            {
                                round.AppendLine("doubledown2");
                                DoubleDown(wager, player);
                            }
                            else
                            {
                                player.Stand();
                            }
                            done = true;
                            break;

                        case PlayerAction.Split:
                            round.AppendLine("splithit");
                            player.Hit();
                            break;

                        case PlayerAction.SplitIfDAS:
                            round.AppendLine("splitdas");
                            player.Hit();
                            break;

                        case PlayerAction.DontSplit:
                            round.AppendLine("don'tsplit");
                            player.Hit();
                            break;

                        default:
                            break;
                        }
                    }
                }
                round.AppendLine("settling hand");
                game.SettlePlayerHand(player);

                round.AppendLine("endround");
                game.EndRound();
                balance = player.Account.Balance;
                if (balance > start)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                Console.WriteLine(player.Account.Balance);
            }
            Console.WriteLine($"{(balance <= 0 ? "Bankrupt" : "Rich")} after {rounds} rounds");
            Console.ReadKey();
        }
예제 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting game");
            BlackjackGame game = new BlackjackGame();

            game.AddPlayer("Bill", 10000);
            game.AddPlayer("Bobby", 10000);
            game.AddPlayer("Lisa", 15000);
            game.AddPlayer("Mabel", 20000);
            int roundCount = 1;

            while (1 == 1)
            {
                Console.WriteLine("Round: " + roundCount);
                foreach (Player player in game.PlayersWithChoices)
                {
                    Console.WriteLine(player.Name + " Last Round Result " + player.LastRoundResult.ToString() + " Current Bet " + player.LastBetAmount + " Winnings: " + player.MoneyWon + " Money On Hand: " + player.MoneyOnHand + " Money lost: " + player.MoneyLost);
                    if (roundCount == 1)
                    {
                        player.PlaceBet(100);
                    }
                    else
                    {
                        if (player.LastRoundResult == LastRoundResult.Lost)
                        {
                            player.PlaceBet(player.LastBetAmount * 2);
                        }
                        else
                        {
                            player.PlaceBet(100);
                        }
                    }
                }
                game.StartRound();
                roundCount++;
                while (game.PlayersWithChoices.Count() > 0)
                {
                    foreach (Player player in game.PlayersWithChoices)
                    {
                        if (game.DealerUpCards.First().Values[0] > 6 || game.DealerUpCards.First().CardName == CardName.Ace)
                        {
                            if (player.CardsOnHand.BestBlackJackValue() >= 12)
                            {
                                player.ChangeState(PlayerState.Stand);
                            }
                            else
                            {
                                player.ChangeState(PlayerState.Hit);
                            }
                        }
                        else
                        {
                            if (player.CardsOnHand.BestBlackJackValue() <= 11)
                            {
                                player.ChangeState(PlayerState.Hit);
                            }
                            else
                            {
                                player.ChangeState(PlayerState.Stand);
                            }
                        }
                    }
                    game.HitPlayers();
                }
                game.EndRound();
                if (game.players.Count() == 0)
                {
                    Console.WriteLine("Game Over");
                    Thread.Sleep(1000000);
                    break;
                }
            }
        }