3 Replies - 2463 Views - Last Post: 14 November 2010 - 11:49 PM Rate Topic: -----

#1 zinadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)

            {    

                MessageBox.Show("Your Turn is over");
                score = 0;
                                 
            
            }


Is This A Good Question/Topic? 0
  • +

Replies To: Dice Game

#2 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • 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)
{
                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

Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • 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!");
}

Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • 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?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1