Jump to content

Passing a random Value

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I have a separate class which creates a random object and a implements a dice class, I need to pass the roll value into the main form class which I have done, however I need to add each of the values, each time Im doing it its just returning the value which has just been rolled :cursing:

Code:

Form:

private void btnRoll_Click(object sender, EventArgs e)

        {

            if (turn % 2 == 0) //if turn is even

            {


                player one = new player();

                one.diceLoad();

                lblRollValue.Text = Convert.ToString(one.rollValue);


                one.getPosition();

                lblPlayerOnePositionValue.Text = Convert.ToString(one.position);

             }

          }

playerClass:

Random randomGenerator;

        dice gameDice;

        public int rollValue,

            position;


        public void diceLoad()

        {

            randomGenerator = new Random();

            gameDice = new dice(randomGenerator);

            rollValue = gameDice.Roll();

        }


        public void getPosition()

        {

            position += rollValue;

        }


#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
is this a game program , if Yes :)
Semprance where are you ? :loading:, someone here needs your help man :amr:

#3
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Yep it is a game program and yes I do need help! :D

#4
terroare

terroare

    Newbie

  • Members
  • PipPip
  • 16 posts
You are using this event handler
private void btnRoll_Click(object sender, EventArgs e)
, to just roll the dice, right ?
Why are you creating every time a player object
player one = new player();
?
Try to declare player one, outside any method.

#5
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Did you ask semprance for any help ? he is the man when it comes to Gaming

#6
terroare

terroare

    Newbie

  • Members
  • PipPip
  • 16 posts

gokuajmes said:

Did you ask semprance for any help ? he is the man when it comes to Gaming

Man, it doesn't matter if its a game or a financial program, its programing :)

#7
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
The random, is not that random as it seems, usually the computer mixes the numbers of the current time to get a random number.
It happened to me to before, I recommend creating another random.
I might have your answer here: xkcd: Random Number
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
lobo521

lobo521

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Problem is with your Random object, wich is based on time. You have constant time between creating random object and getting next value from it. Random object should be created once. This should work. It's still bad code:

        dice gameDice = new dice(new Random());
        public int rollValue,
            position;

        public void diceLoad()
        {
            rollValue = gameDice.Roll();
        }

        // 1. avoid using get/set methods. Use properties.
        // 2. geting  value from object shouldn't change its state. What will happen if you get value twice??
        public void getPosition()
        {
            position += rollValue;
        }

It should look like that:

public class Player{
    private Dice dice = new Dice();
    private int position;

    public void Move()
    {
        position += dice.Roll();
    }

    public int Position { get { return positnion; } }

}

public class Dice{
    private Random random = new Random();

    public int Roll(){
        return random.Next(1, 7);
    }
}