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 = "-";
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire