user3185653
user3185653

Reputation: 39

Loading form before while loop in game

I am developing a game in C# .But it has some infinite loop that prevents form from being loaded. I have tried many solutions available on internet, But that doesn't seems to work.

My problem is :"How to load the form before execution of while loop."?

My Program.cs Source

namespace SumSwamp
{
static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var window = new Form1();
        window.Show();

    }
}
}

Form1.cs :

namespace SumSwamp
{
public partial class Form1 : Form
{
    public static int DieLargeNum = 0;
    public static int DieSmallNum = 0;
    public static int DieOperator = 0;
    public static int DieTotal = 0;
    public static int TotalSpaces = 42;
    public static int CompSum = 0;
    public static int PlayerSum = 0;
    public static Boolean PlayersRoll = true;
    public static Boolean WaitForRoll = true;
    public static int Turn = 0;

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(this.Form1_Load);
        while(Turn == 0) //INFINITE LOOP
        {
            if (WaitForRoll==false)
            {
                DieTotal=DieLargeNum;
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                if (DieTotal>DieLargeNum)
                {
                    Turn = 1;
                    labelStatus.Text = "Player 1's Turn";
                    WaitForRoll=true;
                }
                else
                {
                    Turn = 2;
                    labelStatus.Text = "Player 2's Turn";
                    WaitForRoll = false;
                }
            }
        }

        while ((CompSum < TotalSpaces) & (PlayerSum < TotalSpaces))//INFINITE LOOP
        {
            while (Turn == 1)
            {
                if (WaitForRoll == false)
                {
                    if (DieOperator == 1)
                    {
                        DieTotal = DieLargeNum + DieSmallNum;
                    }
                    else
                    {
                        if (DieLargeNum > DieSmallNum)
                        {
                            DieTotal = DieLargeNum - DieSmallNum;
                        }
                        else
                        {
                            DieTotal = DieSmallNum - DieLargeNum;
                        }

                    }
                    PlayerSum = PlayerSum + DieTotal;
                    Turn = 2;
                    PlayersRoll = false;
                    labelStatus.Text = "Player 2's Turn";
                }
            }

            while (Turn == 2)
            {
                Random rnd1 = new Random();
                DieLargeNum = rnd1.Next(1, 7);
                Random rnd2 = new Random();
                DieSmallNum = rnd2.Next(1, 7);
                Random rnd3 = new Random();
                DieOperator = rnd3.Next(1, 3);
                labelDieLargeNum.Text = DieLargeNum.ToString();
                labelDieSmallNum.Text = DieSmallNum.ToString();
                if (DieOperator == 1)
                {
                    labelDieOperator.Text = "+";
                }
                else
                {
                    labelDieOperator.Text = "-";

                }

                if (DieOperator == 1)
                {
                    DieTotal = DieLargeNum + DieSmallNum;
                }
                else
                {
                    if (DieLargeNum > DieSmallNum)
                    {
                        DieTotal = DieLargeNum - DieSmallNum;
                    }
                    else
                    {
                        DieTotal = DieSmallNum - DieLargeNum;
                    }
                }
                CompSum = CompSum + DieTotal; 
                Turn = 1;
                PlayersRoll = true;
                labelStatus.Text = "Player 1's Turn";                    
            }

        }
        if (CompSum>=TotalSpaces)
        {
            labelResult.Text = "CPU Player has won!";
        }
        else
        {
            labelResult.Text = "You win!";
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonRoll_Click(object sender, EventArgs e)
    {
        if (PlayersRoll == true)
        {
            Random rnd1 = new Random();
            DieLargeNum = rnd1.Next(1, 7);
            Random rnd2 = new Random();
            DieSmallNum = rnd2.Next(1, 7);
            Random rnd3 = new Random();
            DieOperator = rnd3.Next(1, 3);
            WaitForRoll = false;
            labelDieLargeNum.Text = DieLargeNum.ToString();
            labelDieSmallNum.Text = DieSmallNum.ToString();
            if(DieOperator == 1)
            {
                labelDieOperator.Text = "+";
            }
            else
            {
                labelDieOperator.Text = "-";

            }

        }
    }

}
}

Upvotes: 0

Views: 273

Answers (1)

Rufus L
Rufus L

Reputation: 37020

If you put your infinite loop in the Form.Enter() event, the form will be loaded first and then run your loop. But the UI will be unresponsive if you're really in an infinite loop.

Usually UI games are driven by user interaction with controls. So, instead of an infinite loop where you have "WaitForRoll", you just have a button that says "Roll", and when they click it you run your code for that particular event (of rolling the dice).

If you do have to run a loop while the user isn't interacting with controls, it should be done in a separate thread, like a BackgroundWorker or Task.

Upvotes: 1

Related Questions