Программа BlackJack зависает после запуска игры - PullRequest
1 голос
/ 19 марта 2020

Я создаю игру БлэкДжек в C#, все (я почти уверен) работает до того, как оно дойдет до моего Do-While l oop (где все в реальной игре происходит.) И когда это произойдет. делает, он либо просто зависает, либо ничего не происходит вообще. Я заметил, что когда я избавляюсь от своего Do-Пока l oop игра не зависает, поэтому я предполагаю, что она бесконечно l oop или что-то в этом роде, но я не уверен, что здесь делать.

namespace Blackjack
{

    public partial class Form1 : Form
    {
        public Boolean standClicked = false;
        public Boolean hitClicked = false;
        public Boolean over;
        public Cards[] cardSet = new Cards[5];
        public PictureBox[] picArray = new PictureBox[5];
        public int points;
        public int i;

        public Form1()
        {
            InitializeComponent();

            //Makes all card images hidden 
            Card1.Hide();
            Card2.Hide();
            Card3.Hide();
            Card4.Hide();
            Card5.Hide();

            //Makes the win-lose label blank
            winOrLoseLabel.Text = " ";

            buttonHit.Hide();
            buttonStand.Hide();


        }


        private void buttonExit_Click(object sender, EventArgs e)
        {
            //Closes the application
            this.Close();
        }

        private Image SetImage(int cardValue)
        {

            Image _ = null;

            if (cardValue >= 0)
            {
                for (int check = 0; check >= 50; check++)
                {

                    if (cardValue == check)
                    {
                        _ = imageList1.Images[check];
                    }

                }

            }

            if (_ == null)
            {
                _ = imageList1.Images[0];
            }
            return _;

        }

        public void buttonHit_Click(object sender, EventArgs e)
        {
            hitClicked = true;

            if (hitClicked == true)
            {

                picArray[i].Image = SetImage(cardSet[i].GetValue());
                points += cardSet[i].CardValue();
                pointCounter.Text = points.ToString();

            }

            points += Convert.ToInt32(pointCounter.Text);
            if (points >= 21)
                over = true;
            else
                over = false;

            i++;

        }

        private void buttonStand_Click(object sender, EventArgs e)
        {
            standClicked = true;
            buttonHit.Enabled = false;
        }



        private void buttonPlay_Click_2(object sender, EventArgs e)
        {
            // Reveals all cards on the form
            Card1.Show();
            Card2.Show();
            Card3.Show();
            Card4.Show();
            Card5.Show();

            //Hides the logo as well as the play and exit buttons
            pictureBox3.Hide();
            buttonPlay.Hide();
            buttonExit.Hide();

            //Sets both hit and stand buttons to be enabled again
            buttonHit.Show();
            buttonStand.Show();
            buttonHit.Enabled = true;
            buttonStand.Enabled = true;


            //Makes the win-lose label blank
            winOrLoseLabel.Text = " ";


            //Sets the "standClicked boolean value to false - to not automatically end the game
            pointCounter.Text = "0";
            points = Convert.ToInt32(pointCounter.Text);

            //Makes the cardset values randomized
            cardSet[0] = new Cards(0);
            cardSet[0].Value = 0;
            cardSet[0].SetCards();

            cardSet[1] = new Cards(0);
            cardSet[1].Value = 0;
            cardSet[1].SetCards();

            cardSet[2] = new Cards(0);
            cardSet[2].Value = 0;
            cardSet[2].SetCards();

            cardSet[3] = new Cards(0);
            cardSet[3].Value = 0;
            cardSet[3].SetCards();

            cardSet[4] = new Cards(0);
            cardSet[4].Value = 0;
            cardSet[4].SetCards();



            // PictureBox array to allow changes to different PictureBoxes
            PictureBox[] picArray = new PictureBox[5];

            // Card thing
            for (int x = 0; x > picArray.Length; x++)
            {
                picArray[x] = new PictureBox();
            }

            picArray[0] = Card1;
            picArray[1] = Card2;
            picArray[2] = Card3;
            picArray[3] = Card4;
            picArray[4] = Card5;


            // Will automatically show you your first card, while the rest will stay hidden until you press the Hit button.
            picArray[0].Image = SetImage(cardSet[0].GetValue());
            points += cardSet[0].CardValue();
            pointCounter.Text = points.ToString();
            i = 1;

            //Sets over boolean to false - to not automatically end the game
            over = false;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
        }

     private void Card2_Click(object sender, EventArgs e)
        {
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
        }

        private void buttonPlay_Click(object sender, EventArgs e)
        {
        }

        private void buttonPlay_Click_1(object sender, EventArgs e)
        {
        }
    }
}

И мой класс карт, если он вам тоже нужен

using System;

public class Cards
{


    Random rand = new Random();
    public int Value = 0;




    public Cards(int value)
    {
        this.Value = value;
    }

    public int GetValue()
    {
        return Value;
    }

    public int CardValue()
    {
        if (Value == 0 || Value == 1 || Value == 2 || Value == 3)
            return 2;
        else if (Value == 4 || Value == 5 || Value == 6 || Value == 7)
            return 3;
        else if (Value == 8 || Value == 9 || Value == 10 || Value == 11)
            return 4;
        else if (Value == 12 || Value == 13 || Value == 14 || Value == 15)
            return 5;
        else if (Value == 16 || Value == 17 || Value == 18 || Value == 19)
            return 6;
        else if (Value == 20 || Value == 21 || Value == 22 || Value == 23)
            return 7;
        else if (Value == 24 || Value == 25 || Value == 26 || Value == 27)
            return 8;
        else if (Value == 28 || Value == 29 || Value == 30 || Value == 31)
            return 9;
        else if (Value == 32 || Value == 33 || Value == 34 || Value == 35)
            return 10;
        else if (Value == 36 || Value == 37 || Value == 38 || Value == 39)
            return 1;
        else if (Value == 40 || Value == 41 || Value == 42 || Value == 43 || Value == 44 || Value == 45 || Value == 46 || Value == 47)
            return 10;

        else
            return 2;
    }


    public void SetCards()
    {
        //Adds a random value to card
        int ranumber = rand.Next(0, 47);
        Value = ranumber;
    }


}

Я все еще новичок в программировании, и это мой первый настоящий проект. Любая помощь в том, как я могу улучшить свой код и исправить проблему, будет стоить мне миллиона.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...