Random randomnumber = new Random();
int dice1 = randomnumber.Next(1, 7);
int score = Convert.ToInt32(dice1.ToString());
lblDice.Text = dice1.ToString();
while (dice1 <= 1)
{
MessageBox.Show("Your Turn is over");
score = 0;
}
Dice Game
Page 1 of 13 Replies - 1945 Views - Last Post: 14 November 2010 - 11:49 PM
#1
Dice Game
Posted 13 November 2010 - 04:44 PM
Hi i am new to C Sharp and run into a problem.. my aim is to create a dice game where 2 players roll a dice . when the player rolls between 2 and 6 the number is added to the player total and if player rolls 1 the total gets resetted and becomes player2 turn. my problem is that i have generated a random number generator where i generate and roll between 1 and 6 but i cant add and keep scores of the dice...
Replies To: Dice Game
#2
Re: Dice Game
Posted 13 November 2010 - 05:03 PM
To do that, you can just do:
int dice = 1
int turn = 1;
int score = 0;
int dice2 = 0;
int score2 = 0;
int turn2 = 0;
if(turn == 1)
{
while (dice1 <= 1)
{
MessageBox.Show("Your Turn is over");
score = 0;
turn = 0;
turn2 = 1;
}
}
else if(turn2 == 1)
{
while (dice2 <= 1)
{
MessageBox.Show("Your Turn is over");
score2 = 0;
turn = 1;
turn2 = 0;
}
}
This post has been edited by CreaturGames: 13 November 2010 - 05:05 PM
#3
Re: Dice Game
Posted 13 November 2010 - 05:25 PM
The problem you're having is that you are never accumulating the score of your dice.
//Roll.
Random randomnumber = new Random();
int roll = randomnumber.Next(1, 7);
if (roll != 1)
{
playerOneScore += roll;
}
else
{
playerOneScore = 0;
MessageBox.Show("You went overboard!");
}
#4
Re: Dice Game
Posted 14 November 2010 - 11:49 PM
Whenever you see something like this:
Your next thought should be "how can I turn this into a class?".
Now elsewhere you can have:
Make sense?
int dice = 1 int turn = 1; int score = 0; int dice2 = 0; int score2 = 0; int turn2 = 0;
Your next thought should be "how can I turn this into a class?".
public class Player
{
public int Dice { get; set; }
public int Turn { get; set; }
public int Score { get; set; }
}
Now elsewhere you can have:
Player player1 = new Player() { Dice = 1, Turn = 1, Score = 0 };
Player player2 = new Player() { Dice = 0, Turn = 0, Score = 0 };
Make sense?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|