Esempio n. 1
0
 private void btnHand1_Click(object sender, EventArgs e)
 {
     try
     {
         isDealHand1Clicked = true;
         hand1 = aDeck.DealHand(3);
         ShowHand(Panel1, hand1);
         btnHand1.Enabled = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error");
     }
 }
        private void SetUp()
        {
            try
            {
                aDeck = new Deck();
                aDeck.Shuffle();

                hand1 = aDeck.DealHand(2);

                int firstValue = ((int)hand1.cards[0].FaceValue + 1);

                if (firstValue > 10)
                {
                    firstValue = 10;
                }

                tboScoreDealer.Text = firstValue.ToString();

                hand2 = aDeck.DealHand(2);
                tboScorePlayer.Text = HandSum(hand2).ToString();
                ShowHandDealer(Panel1, hand1);
                ShowHand(Panel2, hand2);

                if (HandSum(hand2) == 21)
                {
                    win.Play();
                    MessageBox.Show("Blackjack!", "You win!");
                    AskToRestart();
                }

                btnHit.Enabled   = true;
                btnStand.Enabled = true;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Spread cards to users, after turn decision
        /// Remove duplicated facevalue cards in player's hand
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtPlayerName.Text) || txtPlayerName.Text == "Enter your name")
                {
                    SystemMsg("Please enter your name", 1000);
                }

                else
                {
                    players[0].Name   = txtPlayerName.Text;
                    lbPlayerName.Text = players[0].Name;
                    lbUser1Name.Text  = players[1].Name;
                    lbUser2Name.Text  = players[2].Name;

                    panel_turnChoice.Controls.Clear();
                    panel_turnChoice.Visible = false;
                    lbFirstCard.Visible      = false;
                    lbSecondCard.Visible     = false;
                    lbThirdCard.Visible      = false;
                    btnStart.Visible         = false;
                    txtPlayerName.Visible    = false;
                    lbChooseCard.Visible     = false;
                    PanelPlayer.Visible      = true;
                    PanelUser1.Visible       = true;
                    PanelUser2.Visible       = true;
                    gameStart = true;

                    SystemMsg("Dealing...", 1000);

                    players[0].Hand = aDeck.DealHand(18);
                    players[1].Hand = aDeck.DealHand(18);
                    players[2].Hand = aDeck.DealHand(17);

                    foreach (Player p in players)
                    {
                        ShowCard(p.PanelName, p.Hand, p.CardSide, p.CardWidth, p.CardHeight, p.CardLeft);
                    }

                    lbUser1Name.Visible  = true;
                    lbUser2Name.Visible  = true;
                    lbPlayerName.Visible = true;
                    Delay(1000);

                    SystemMsg("Cards are sorting...", 2000);
                    foreach (Player p in players)
                    {
                        SortCards(p.Hand);
                    }

                    Turn_Order();
                    Delay(1000);

                    if (players[0].Hand.Count < 6)
                    {
                        SystemMsg("You are lucky! \nGame Start!", 2000);
                    }

                    else if (players[0].Hand.Count > 8)
                    {
                        SystemMsg("Oh, Good luck! \nGame Start!", 2000);
                    }

                    else
                    {
                        SystemMsg("Game Start!", 1000);
                    }

                    btnEndTurn.Visible = true;

                    Action();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
Esempio n. 4
0
        private void btnNewGame_Click(object sender, EventArgs e)
        {
            try
            {
                if (game.GameNumber == 1)
                {
                    if (cmbHeroes.SelectedIndex == -1)
                    {
                        MessageBox.Show("Please select your hero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    if (String.IsNullOrEmpty(txtPlayerAlias.Text))
                    {
                        MessageBox.Show("Please put in an alias", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    Hero hero = new Hero();
                    hero = heroes[cmbHeroes.SelectedIndex + 1];

                    //Set up player 1
                    player1.Name            = hero.Name;
                    player1.AvatarImg       = hero.AvatarImg;
                    player1.Number          = 1;
                    player1.PictureBox      = picP1;
                    player1.CardsPanel      = PanelHandP1;
                    player1.ComboCardPanels = new List <Panel>()
                    {
                        PanelCmbP1A,
                        PanelCmbP1B,
                        PanelCmbP1C,
                        PanelCmbP1D
                    };
                    player1.PlayerType = PlayerType.HumanPlayer;
                    picP1.Image        = Image.FromFile(player1.AvatarImg);
                    lbl1.Text          = player1.Name;
                    lblAlias.Text      = $"Alyas \"{txtPlayerAlias.Text}\"";

                    //Set up player 2

                    hero = heroes[r.Next(1, 4)];
                    while (hero.Name == player1.Name)
                    {
                        hero = heroes[r.Next(1, 4)];
                    }

                    player2.Name            = hero.Name;
                    player2.AvatarImg       = hero.AvatarImg;
                    player2.Number          = 1;
                    player2.PictureBox      = picP1;
                    player2.CardsPanel      = PanelHandP2;
                    player2.ComboCardPanels = new List <Panel>()
                    {
                        PanelCmbP2A,
                        PanelCmbP2B,
                        PanelCmbP2C,
                        PanelCmbP2D
                    };
                    player2.PlayerType = PlayerType.ComputerPlayer;
                    picP2.Image        = Image.FromFile(player2.AvatarImg);
                    lblP2.Text         = player2.Name;

                    //Set up player 3

                    hero = heroes[r.Next(1, 4)];
                    while (hero.Name == player1.Name || hero.Name == player2.Name)
                    {
                        hero = heroes[r.Next(1, 4)];
                    }

                    player3.Name            = hero.Name;
                    player3.AvatarImg       = hero.AvatarImg;
                    player3.Number          = 1;
                    player3.PictureBox      = picP1;
                    player3.CardsPanel      = PanelHandP3;
                    player3.ComboCardPanels = new List <Panel>()
                    {
                        PanelCmbP3A,
                        PanelCmbP3B,
                        PanelCmbP3C,
                        PanelCmbP3D
                    };
                    player3.PlayerType = PlayerType.ComputerPlayer;
                    picP3.Image        = Image.FromFile(player3.AvatarImg);
                    lblP3.Text         = player3.Name;
                    List <Player> players = new List <Player>()
                    {
                        player1,
                        player2,
                        player3
                    };
                    game.Players = players;
                }

                SetUp();

                //Setup timer info and game status info
                timeLeft = 25;
                timerP1.Start();
                game.IsActive = true;

                //Set up stat label
                lblGameNum.Text = game.GameNumber.ToString();
                lblP1Wins.Text  = player1Wins.ToString();

                //Clear  combo card panels and dump card panel
                player1.ComboCardPanels.ForEach(c => c.Controls.Clear());
                player2.ComboCardPanels.ForEach(c => c.Controls.Clear());
                player3.ComboCardPanels.ForEach(c => c.Controls.Clear());
                PanelDump.Controls.Clear();

                //Set list of cards to default

                groupCards = new List <Card>();
                dumpCards  = new List <Card>();

                //Make controls visible
                foreach (Control c in this.Controls)
                {
                    c.Visible = true;
                }

                PanelSelection.Visible = false;

                //Deal card to players
                player1.PlayHand = aDeck.DealHand(13);
                player2.PlayHand = aDeck.DealHand(13);
                player3.PlayHand = aDeck.DealHand(13);

                //Sort player's card to arrange cards with same suit and ascending facevalue
                player1.PlayHand.Sort();
                player2.PlayHand.Sort();
                player3.PlayHand.Sort();

                //Show cards of players in the panel
                ShowHandFront(player1.CardsPanel, player1.PlayHand);
                Hand.ShowHandBack(player2.CardsPanel, player2.PlayHand);
                Hand.ShowHandBack(player3.CardsPanel, player3.PlayHand);

                // Show player 1 score
                UpdatePlayer1Score();

                //Set up stock area
                lblStockCount.Text = $"({aDeck.Count.ToString()})";
                PictureBox stockPic = new PictureBox()
                {
                    Image  = Image.FromFile($@"images\cardback.png"),
                    Width  = 71,
                    Height = 100,
                    Cursor = System.Windows.Forms.Cursors.Hand
                };
                stockPic.Click += Stock_Click;
                panelStock.Controls.Add(stockPic);

                // Prompt Player 1 turn
                picP1.BackColor  = Color.FromArgb(255, 255, 128);
                lblTurnName.Text = $"{player1.Name}'s";
                timerP1.Enabled  = true;
                game.Turn        = player1;
                //timerP1.Start();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }