Exemple #1
0
        private void cmdNewGame_Click(object sender, EventArgs e)
        {
            Deck player = new Deck();

            pictureBox1.Image = (Image)player.DisplayCard(player.DrawCard());
            pictureBox2.Image = (Image)player.DisplayCard(player.DrawCard());
        }
Exemple #2
0
        static void Main(string[] args)
        {
            int dealerTotal = 0, playerTotal = 0;

            Deck d1 = new Deck();

            d1.Shuffle();
            List <Card> dealerHand = d1.DrawCard(2);
            List <Card> playerHand = d1.DrawCard(2);

            dealerHand[0].FaceUp = false;

            Console.Write("Dealer".PadRight(10));
            Console.WriteLine("Player");

            for (int i = 0; i < 2; i++)
            {
                if (dealerHand[i].FaceUp)
                {
                    Console.Write($"{dealerHand[i].Value}{dealerHand[i].Suit.ToString().PadRight(10)}");
                }
                else
                {
                    Console.Write("..".PadRight(11));
                }
                Console.WriteLine($"{playerHand[i].Value}{playerHand[i].Suit}");
                dealerTotal += dealerHand[i].NumberValue;
                playerTotal += playerHand[i].NumberValue;
            }

            Console.ReadLine();
        }
Exemple #3
0
        private void FirstDeal()
        {
            int dealerCards = 0;

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < players.Count; j++)
                {
                    deckOfCards.DrawCard(player: players[j]);
                    Console.ReadKey();
                }

                if (dealerCards < 1)
                {
                    deckOfCards.DrawCard(dealer: dealer);
                    Console.ReadKey();
                    dealerCards++;
                }
                else
                {
                    continue;
                }
            }

            hiddenNumber = deckOfCards.DeckOfCards[0];
            deckOfCards.DeckOfCards.Remove(deckOfCards.DeckOfCards[0]);

            Console.Clear();
            foreach (Player player in players)
            {
                Console.WriteLine(player);
            }

            Console.Write(dealer);
            Console.Write(" and a hidden card!");
        }
Exemple #4
0
        public void Hit(Deck deck)
        {
            Console.Write($"{this.Name} hits. ");
            Utility.Sleep();

            // Take a card from the deck and put into player's Hand.
            //Card card = new Card(Suit.Hearts, Face.Ace); //deck.DrawCard();
            Card card = deck.DrawCard(this);

            // If there is any Ace in the Hand, change all the Ace's value to 1.
            // if (this.GetHandValue() + card.Value > 21 && card.Face == Face.Ace)
            //     card.Value = 1;

            //Hand.Add(card); // Background
            card.PrintCardColor(); // UI
            Utility.Sleep();
        }
Exemple #5
0
        /// <summary>
        /// deals the Dealer a card and puts it into the hand property of the dealer
        /// </summary>
        /// <param name="faceUp"></param>
        public void DealDealerCard(bool faceUp)
        {
            int index = 0;

            while (Dealer.Hand[index] != null)
            {
                if (index < 4)
                {
                    index++;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            Dealer.Hand[index]          = Deck.DrawCard();
            Dealer.Hand[index].IsFaceUp = faceUp;
        }
Exemple #6
0
        /// <summary>
        /// deals the player a card and puts it into the hand Property of the player
        /// </summary>
        public void DealPlayerCard()
        {
            int index = 0;

            while (Player.Hand[index] != null)
            {
                if (index < 4)
                {
                    index++;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            Player.Hand[index]          = Deck.DrawCard();
            Player.Hand[index].IsFaceUp = true;
        }
Exemple #7
0
        static void Main(string[] args)
        {
            Utility.SetupWindow("♣♥ BlackJack ♦♠", 70, 30, false);


            while (Gameover() == false)
            {
                //New Game \ Reset
                deck.CreateDeck();
                hand.ResetHand();
                dealer.ResetHand();

                //Beginning hand
                hand.addCardtoHand(deck.DrawCard());
                hand.addCardtoHand(deck.DrawCard());
                Cards card = deck.DrawCard();
                card.Faceup = false;
                dealer.addCardtoHand(card); //Needs to be faced down
                dealer.addCardtoHand(deck.DrawCard());
                Console.Clear();
                Render();

                //Bets
                int amt;
                do
                {
                    Console.Write("Bet ammount?");
                    amt = Utility.ReadInt();
                    Console.Clear();
                    Render();
                } while (Utility.IsReadGood() && player.getMoney() < amt || amt <= 0);
                player.Bet(amt);
                Console.Clear();
                Render();

                //Draws
                while (hand.Hit() == true)
                {
                    Console.Clear();
                    hand.addCardtoHand(deck.DrawCard());
                    Render();
                    if (hand.handValue() > 21)
                    {
                        break;
                    }
                }
                Console.Clear();
                Render();

                //Final hand
                if (hand.handValue() > 21)
                {
                    Console.Write("Bust!");
                }

                Console.WriteLine("Your total is " + hand.handValue());
                Console.WriteLine("----------------------------------------------------------------------");

                while (dealer.dealerHit())
                {
                    if (hand.handValue() > 21)
                    {
                        break;
                    }
                    dealer.addCardtoHand(deck.DrawCard());
                    Render();
                }

                //Determine winner & money distribution
                if (hand.handValue() < dealer.getHandValue() && dealer.getHandValue() <= 21 || hand.handValue() > 21)
                {
                    dealer.setMoney(dealer.getMoney() + amt);
                    Console.SetCursorPosition(0, 5);
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                    Console.WriteLine("You Lost $" + amt);
                    Console.ResetColor();
                    Console.SetCursorPosition(35, 3);
                    Console.Write("Dealer had: " + dealer.getHandValue());
                }
                else if (hand.handValue() > dealer.getHandValue() && hand.handValue() <= 21 || dealer.getHandValue() > 21)
                {
                    dealer.setMoney(dealer.getMoney() - amt);
                    player.setMoney(player.getMoney() + amt + amt);
                    Console.SetCursorPosition(0, 5);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("You won $" + amt);
                    Console.ResetColor();
                    Console.SetCursorPosition(35, 3);
                    Console.Write("Dealer had: " + dealer.getHandValue());
                }
                else
                {
                    Console.SetCursorPosition(35, 3);
                    Console.Write("Dealer had: " + dealer.getHandValue());
                    Console.SetCursorPosition(0, 5);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("A tie occured, money returned");
                    Console.ResetColor();
                    player.setMoney(player.getMoney() + amt);
                }
                if (dealer.getHandValue() > 21)
                {
                    Console.SetCursorPosition(35, 3);
                    Console.Write("The Dealer busted");
                }
                card.Faceup = true;
                Render();
                Console.SetCursorPosition(0, 6);

                /* Optional question
                 * Console.ForegroundColor = ConsoleColor.DarkGray;
                 * Console.Write("Press ENTER to play the next round...");
                 * Console.ResetColor();
                 */
                Console.Read();

                //End screen
                if (player.getMoney() == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Utility.WriteCentered("You lost to the dealer. You are now homeless");
                    break;
                }
                if (dealer.getMoney() <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Utility.WriteCentered("You win! Now the Cassino kicks you out for \"cheating\"!");
                    break;
                }
                Console.Clear();
                Render();
            }
            Console.ResetColor();
            Console.SetCursorPosition(0, Console.WindowHeight - 1);
            Console.Write("Press ENTER to exit...");
            Console.Read();
            Console.ReadLine();
        }
Exemple #8
0
        //if start button clicked
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            //clears card value text
            txtBlCardValue.Text = "";
            //check if the game has been restarted to 0
            if (gameRestarted == true && gameStarted == false)
            {
                //check if nothing is in text box
                if (String.IsNullOrEmpty(txtBxEnterName.Text))
                {
                    txtBlNameFound.Text = "Enter Name First";
                    DialogHost.IsOpen   = true;
                }
                else
                {
                    //else start the game thus turing the game started bool too true
                    gameStarted = true;

                    playerFound = false;

                    deck = new Deck();

                    #region PlayerHand
                    //creates the players hand
                    Hand playerHand = new Hand(deck);

                    //draws 2 cards for player hand
                    Card firstCard  = deck.DrawCard(playerHand);
                    Card secondCard = deck.DrawCard(playerHand);

                    //gets the sums of these cards
                    int firstCardNum  = playerHand.AddValue(firstCard, playerSum);
                    int secondCardNum = playerHand.AddValue(secondCard, playerSum);

                    //equal them too overall player sum
                    playerSum = firstCardNum + secondCardNum;

                    //display sum
                    playerSumString = playerSum.ToString();

                    txtBlPlayerTotal.Text = playerSumString;

                    //display the image of the first 2 cards
                    BitmapImage userFirstbitmapImage = Convert(firstCard.ReturnImage());

                    ImgUserFirstCard.Source = userFirstbitmapImage;

                    BitmapImage userSecondbitmapImage = Convert(secondCard.ReturnImage());

                    ImgUserSecondCard.Source = userSecondbitmapImage;

                    #endregion PlayerHand


                    #region DealerHand
                    //creates the dealers hand
                    Hand dealerHand = new Hand(deck);

                    //draws dealers first card
                    Card dealerCard = deck.DrawCard(dealerHand);

                    //gets card value and equal it too dealer sum
                    dealerSum = dealerHand.AddValue(dealerCard, dealerSum);


                    //display dealer sum
                    dealerSumString = dealerSum.ToString();

                    txtBlDealerTotal.Text = dealerSumString;

                    //display dealer card image
                    BitmapImage dealerbitmapImage = Convert(dealerCard.ReturnImage());

                    ImgDealerFirstCard.Source = dealerbitmapImage;

                    #endregion DealerHand



                    //check if player is a returning one
                    foreach (Player returningPlayer in players)
                    {
                        //check if player name can be found in player list if so turn returning player too true and display message
                        if (returningPlayer.PlayerName == txtBxEnterName.Text)
                        {
                            playerReturned = true;

                            txtBlNameFound.Text = "Returning Player";
                            DialogHost.IsOpen   = true;
                        }
                    }

                    //if player returning is true
                    if (playerReturned == true)
                    {
                        //display there name
                        foreach (Player returningPlayer in players)
                        {
                            if (returningPlayer.PlayerName == txtBxEnterName.Text)
                            {
                                returningPlayer.PlayerName = txtBxEnterName.Text;

                                txtBlCurrentPlayer.Text = returningPlayer.PlayerName;
                            }
                        }

                        //get whats turned into the txt box turn it into a string
                        string x = txtBxEnterName.Text;

                        gameInProgress = false;

                        //compare the string too the list and see which matches
                        foreach (Player currentPlayer in players)
                        {
                            //if the string matches the players name
                            if (x == currentPlayer.PlayerName)
                            {
                                //if the player num is equal too 21 they win
                                if (playerSum == 21)
                                {
                                    Win();
                                    return;
                                }
                            }
                        }
                    }
                    else
                    {
                        //clears all wins losses and drwas for new player
                        txtBlWin.Text    = "0";
                        txtBlLosses.Text = "0";
                        txtBlDraws.Text  = "0";
                        //if player is not a returning player create a new player
                        Player newPlayer = new Player();

                        players.Add(newPlayer);


                        newPlayer.PlayerName = txtBxEnterName.Text;

                        txtBlCurrentPlayer.Text = newPlayer.PlayerName;



                        string x = txtBxEnterName.Text;

                        gameInProgress = false;

                        foreach (Player currentPlayer in players)
                        {
                            if (x == currentPlayer.PlayerName)
                            {
                                if (playerSum == 21)
                                {
                                    Win();
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            //if game has not been restarted display message
            else
            {
                txtBlNameFound.Text = "Restart Game and press start";
                DialogHost.IsOpen   = true;
            }
        }
Exemple #9
0
        //if btn has been clicked
        private void btnHitMe_Click(object sender, RoutedEventArgs e)
        {
            //if game has been restarted and the game has been started
            if (gameRestarted == true && gameInProgress == false && gameStarted == true)
            {
                //name has to be entred to use this button
                if (String.IsNullOrEmpty(txtBxEnterName.Text))
                {
                    txtBlNameFound.Text = "Enter Name First";
                    DialogHost.IsOpen   = true;
                }
                else
                {
                    //set player found too false

                    playerFound = false;
                    //get the name in the textbox
                    string x = txtBxEnterName.Text;

                    //check in the list if that name matches with the player that is playing
                    foreach (Player newPlayer in players)
                    {
                        if (newPlayer.PlayerName == x)
                        {
                            //if player found set too true
                            playerFound = true;

                            ifHit = true;
                            //get random card between 1 and 10 and add it too your total
                            playerHand = new Hand(deck);

                            Card HitCard = deck.DrawCard(playerHand);

                            int HitCardNum = playerHand.AddValue(HitCard, playerSum);


                            playerSum = HitCardNum;

                            playerSumString = playerSum.ToString();

                            txtBlPlayerTotal.Text = playerSumString;

                            BitmapImage userHitbitmapImage = Convert(HitCard.ReturnImage());

                            if (ImgUserThirdCard.Source == null)
                            {
                                ImgUserThirdCard.Source = userHitbitmapImage;
                            }

                            else if (ImgUserFourthCard.Source == null)
                            {
                                ImgUserFourthCard.Source = userHitbitmapImage;
                            }

                            else if (ImgUserFifthCard.Source == null)
                            {
                                ImgUserFifthCard.Source = userHitbitmapImage;
                            }

                            else
                            {
                                ImgUserFirstCard.Source = userHitbitmapImage;
                            }


                            //if player gets more then 21 they lose or if player gets exactly 21 they win
                            if (playerSum > 21)
                            {
                                Loss();
                                return;
                            }
                            else if (playerSum == 21)
                            {
                                Win();
                                return;
                            }
                        }
                    }
                    //if player cant be found as the same player in the txtbx get a warning
                    if (playerFound == false)
                    {
                        txtBlNameFound.Text = "Player changed, please press start";
                        DialogHost.IsOpen   = true;
                    }
                }
            }
            else
            {
                //warning too restart the game
                txtBlNameFound.Text = "Press Restart and then press start game";
                DialogHost.IsOpen   = true;
            }
        }
Exemple #10
0
        public void Play()
        {
            bool continuePlay = true;

            Screen.SplashScreen();
            Screen.PromptPlayerName();

            var player = new Player(Console.ReadLine());

            var dealerComputer = new Player();

            deck = new Deck();

            while (continuePlay)
            {
                // Initialize screen and reset player and dealer's certain property
                // for the new round.
                Console.Clear();
                player.ResetPlayerHand();
                dealerComputer.ResetPlayerHand();

                // Create a new deck if remaining cards are less than 20
                if (deck.GetRemainingDeckCount() < 20)
                {
                    deck = new Deck();
                }

                deck.ShowRemainingDeckCount();

                // Show player bank roll
                Console.WriteLine($"{player.Name} Chips Balance: {player.ChipsOnHand}");

                if (player.ChipsOnHand <= 10)
                {
                    Utility.WriteLineInColor("Insufficient chips in your account.", ConsoleColor.Red);
                    Utility.WriteLineInColor("Please reload your chips from the counter to continue to play.\n", ConsoleColor.Red);

                    continuePlay = false;
                    break;
                }

                // Get bet amount from player
                Console.Write("Enter chips: ");
                player.ChipsOnBet = Convert.ToInt16(Console.ReadLine());
                // for brevity, no input validation here.

                // Deal first two cards to player (Background)
                deck.DrawCard(player);
                deck.DrawCard(player);

                // Show player's hand (UI)
                player.ShowUpCards();
                Utility.Sleep();

                Utility.Line();

                // Deal first two cards to dealer (Background)
                deck.DrawCard(dealerComputer);
                deck.DrawCard(dealerComputer);

                // Show dealer's hand (UI)
                dealerComputer.ShowUpCards(true);
                Utility.Sleep();

                Utility.Line();

                // Check natural black jack
                if (CheckNaturalBlackJack(player, dealerComputer) == false)
                {
                    // If both also don't have natural black jack,
                    // then player's turn to continue.
                    // After player's turn, it will be dealer's turn.
                    TakeAction(player);
                    TakeAction(dealerComputer, player.IsBusted);

                    AnnounceWinnerForTheRound(player, dealerComputer);
                }

                Console.WriteLine("This round is over.");

                Console.Write("\nPlay again? Y or N? ");

                continuePlay = Console.ReadLine().ToUpper() == "Y" ? true : false;
                // for brevity, no input validation
            }

            PrintEndGame(player, dealerComputer);
        }