1 Replies - 1836 Views - Last Post: 07 October 2009 - 01:39 PM Rate Topic: -----

#1 Bullseye23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-September 09

Dice Game

Posted 07 October 2009 - 01:21 PM

I am making a virtual dice game. The code pretty much states it out clearly.
The dice is rolled and if the result its an even number it displays it throught the message box and so forth if it is odd. The part i m having problems is determing how to let the user know if he/she has rolled the same number twice in row and then displayed it through the same message box that states something like "its even or odd and you rolled the same number".

private void timer1_Tick(object sender, EventArgs e)
		{
			Random rmd = new Random();
			int num1;
			int num2;
	   
			{
			 
				num1 = rmd.Next(1, 7);
				label1.Text = num1.ToString();
				num2 = num1 % 2;
				if (num2 == 0)
				{
					MessageBox.Show("You rolled an even number");
				}
				else
					MessageBox.Show("Your rolled an odd number");

				if (num2 == num1)//when a number repeats display message saying it rolled twice
				{ MessageBox.Show("You rolled the same number twice"); }



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Dice Game

#2 StCroixSkipper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 121
  • Joined: 23-December 08

Re: Dice Game

Posted 07 October 2009 - 01:39 PM

Can't you simply declare a class variable to keep the previous number rolledthe previous roll?

// Somewhere in you class declare a private member variable.
// And initialize it to something it can never be...
private int previous = -9999;
private void timer1_Tick(object sender, EventArgs e)
{
	Random rmd = new Random();
	int num1;
	int num2;
	   
	num1 = rmd.Next(1, 7);
	label1.Text = num1.ToString();
	num2 = num1 % 2;
	if (num2 == 0)
	{
		MessageBox.Show("You rolled an even number");
	}
	else
	{
		MessageBox.Show("Your rolled an odd number");
	}

	if (previous == num1)// then the same number was rolled twice
	{ 
		MessageBox.Show("You rolled the same number twice"); 
	}
	// set previous to the number just rolled.
	previous = num1;
}



This post has been edited by StCroixSkipper: 07 October 2009 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1