# Dice Game

Page 1 of 1

## 3 Replies - 3460 Views - Last Post: 14 November 2010 - 11:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=199986&amp;s=7f1331a575a625679536f3324f997ff2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 1
• Joined: 13-November 10

# 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...

```Random randomnumber = new Random();
int dice1 = randomnumber.Next(1, 7);
int score = Convert.ToInt32(dice1.ToString());
lblDice.Text = dice1.ToString();

while (dice1 <= 1)

{

score = 0;

}
```

Is This A Good Question/Topic? 0

## Replies To: Dice Game

### #2 CreaturGames

• D.I.C Regular

Reputation: 30
• Posts: 290
• Joined: 29-December 09

## 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)
{
score = 0;
turn = 0;
turn2 = 1;

}
}
else if(turn2 == 1)
{
while (dice2 <= 1)
{
score2 = 0;
turn = 1;
turn2 = 0;

}
}

```

This post has been edited by CreaturGames: 13 November 2010 - 05:05 PM

### #3 Sergio Tapia

• D.I.C Lover

Reputation: 1258
• Posts: 4,168
• Joined: 27-January 10

## 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 MentalFloss

• .

Reputation: 577
• Posts: 1,500
• Joined: 02-September 09

## Re: Dice Game

Posted 14 November 2010 - 11:49 PM

Whenever you see something like this:

```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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }