Ship, Captain, Crew & Mate Dice Game

Term Project

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4347 Views - Last Post: 24 May 2010 - 04:47 PM Rate Topic: -----

#1 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 05:49 PM

Hi -

Typical C# 1 student which means I don't know squat; even worse, our class this term was cut from 2 1/2 hours to 1 1/2 so we just haven't had time. So please be patient with me.

Ok...Term Project is as named above. 2 Players, 5 Dice, 3 Rolls. Object is WITHIN 3 throws, to obtain a Ship (6), then a Captain (5), then a Mate (4) and finally your Crew (the total of the last two dice). The 6, 5 and 4 may not be rolled out of order so if the first throw contains a 6 and 4, but not a 5, only the 6 is put aside and the rest of the dice, including the 4, are rolled again. If a player fails to roll the 6, 5, and 4 after three rolls, they score nothing. The player with the highest 'crew' total, wins the game. In the case of a tie, game must be played again.

From a previous Java project, I have a 'Die' class (why reinvent the wheel), but am getting 2 'errors':

public class Die
{
   private final int MAX = 6;  

   private int faceValue;  

   public Die()
   {
      faceValue = 1;
   }

   public int roll()
   {
      faceValue = (int)(Math.random() * MAX) + 1;

      return faceValue;
   }

   public void setFaceValue (int value)
   {
      faceValue = value;
   }

   public int getFaceValue()
   {
      return faceValue;
   }

   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }
}


Error 1 - System.Math' does not contain a definition for 'random'
Error 2 - The name 'Integer' does not exist in the current context

I eliminated the secord error by rewriting it as: String result = Convert.ToString(faceValue); but can't test if it is actually correct until I solve Error 1.

Once I figure this out, I can move on to the next part of which I already have part of it written (from the same previous Java project, but it's going to get a lot more complicated and so I'm going to have a lot more questions.

I think you in advance your patience and assistance.

Geckko

Is This A Good Question/Topic? 0
  • +

Replies To: Ship, Captain, Crew & Mate Dice Game

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 06:09 PM

Hi there and welcome to DIC.

First things first, you can change this:
String result = Convert.ToString(faceValue);
to this:
string result = faceValue.ToString();

Much cleaner code to read and easier for you to write.

Next, to get a random number in C#, you'd do something like this:
        public int GetRandomNumber()
        {
            Random generator = new Random();
            int retreivedNumber = generator.Next(0, 10); // Get number from 0 to 10.
            return retreivedNumber;
        }


This post has been edited by stapia.gutierrez: 22 May 2010 - 06:13 PM

Was This Post Helpful? 1
  • +
  • -

#3 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 06:58 PM

Thank you.

I made the String change that you suggested:

String result = Convert.ToString(faceValue);
to this:
string result = faceValue.ToString();

The Random code seemed a little cumbersome since it was only one line that appeared to be effected. So what I did was add Random rmd = new Random(); under the second "Private" and then changed:
faceValue = (int)(Math.random() * MAX) + 1;
to
faceValue = (int)(rmd.Next() * MAX) + 1;

That elimated all the errors.

In order to test my 'Die' object, I had already started on 'RollingDice':

public class RollingDice
{
	public static void main(String[] args)
	{
		Die die1, die2, die3, die4, die5;
		
		die1 = new Die();
		die2 = new Die();
		die3 = new Die();
		die4 = new Die();
		die5 = new Die();
		
		die1.roll();
		die2.roll();
		die3.roll();
		die4.roll();
		die5.roll();
		
		Console.WriteLine("Die 1: {0}, Die 2: {1}, Die 3: {2}, Die 4: {3}, Die 5: {4})",
			die1, die2, die3, die4, die5);
	}
}


As is par, I am now getting an new Error: Term_Project.exe' does not contain a static 'Main' method suitable for an entry point

I have a feeling that I have taken on too complex a project given my great inexperience, I should have instead presented an old gambling game called '4,5,6' so I could have used a pre-written Craps game, but I have to learn sometime.

Again, thanks.

This post has been edited by Geckko: 22 May 2010 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 07:15 PM

OK, first thing is first; are you creating a Windows Form application or a Console Application?

I'd create something like this:

public class Die
{
    //Create a generator for 1 to 6 only.
    Random generator = new Random();

    public int RollDie()
    {
        return generator.Next(1,6);        
    }
}




Then in another class, like your form class where you actually use the Die:

public void MouseButtonclick_event
{
    Die die = new Die();
    MessageBox.Show(die.RollDie().ToString());
}

This post has been edited by stapia.gutierrez: 22 May 2010 - 07:16 PM

Was This Post Helpful? 1
  • +
  • -

#5 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 07:31 PM

Console Application...we haven't gotten to the fancy stuff yet (C# II).

I like your 'Random' suggestion and have incorportated it; thank you.

Still not sure where the other Error is coming from.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 07:35 PM

Every console application has to have a static void Main() method to use as an entry point.

My suggestion is to just create a brand new C# Console Application because Visual Studio will auto generate it for you. Once you are in there, just create the classes and other things with the Solution Explorer.
Was This Post Helpful? 1
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Ship, Captain, Crew & Mate Dice Game

Posted 22 May 2010 - 08:25 PM

View PostGeckko, on 22 May 2010 - 05:31 PM, said:

Still not sure where the other Error is coming from.

You have a main() method, not a Main() method. C# is case sensitive.
Was This Post Helpful? 1
  • +
  • -

#8 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 07:07 AM

You could also make a handler class for the dice (DiceCup?).
Then you could remove the roll on the die class, and put it into the DiceCup.

an example:
class DiceCup
{
    private readonly Random _random;
    public List<Die> Dice;

    public DiceCup()
    {
        _random = new Random();
        Dice = new List<Die>();
    }

    public void AddDice(int amount)
    {
        while (amount != 0)
        {
            Dice.Add(new Die());
            amount--;
        }
    }

    public void RemoveDice(Die die)
    {
            if (Dice.Contain(die))
                Dice.Remove(die);
    }

    public void Clear()
    {
        Dice.Clear();
    }

    public int Roll()
    {
        int total = 0;

        foreach (Die die in Dice)
        {
            die.Face = _random.Next(1, MAX + 1);
            total += die.Face;
        }

        return total;
    }
}



class Die
{
    public int Face;

    public Die()
    {
        Face = 0;
    }
}



Don't know if this should work for you :)
Now you just have to call 1 roll method to roll all the dice.
Then you have a total value, if needed, else you have your list of dice, you can go through to see the value of 'em.

example:
static void Main()
{
    DiceCup cup = new DiceCup();

    cup.AddDice(5);
    cup.Roll();

    int dieCounter = 1;

    foreach (Die die in cup.Dice)
    {
        Console.WriteLine("Die {0}: {1}", dieCounter, die.Face);
        dieCounter++;
    }
}



~Shivern

This post has been edited by Shivern: 23 May 2010 - 07:16 AM

Was This Post Helpful? 1
  • +
  • -

#9 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 09:26 AM

Ok...I'm thinking that part of my problem is that I still have Javanese on the brain, in that I'm trying to write a program in C# like I would write it in Java.

So...I'm going to make changes, see what happens, then come back.
Was This Post Helpful? 0
  • +
  • -

#10 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 11:01 AM

Because of the output I was getting, I decided to combine the Die class and the RollingDice class into just one file, but I'm still getting the same output...which is partially wrong. Mind you, the 'sum' is simply a test to see if code is working.

class Die
    {
        private int MAX = 6;
        private int faceValue;

        // Sets the initial face value.
        public Die()
        {
            faceValue = 1;
        }

        // Rolls the die and returns the result 
        Random rmd = new Random();

        public int RollDie()
        {
            faceValue = rmd.Next(1, 6);
            return faceValue;
        }

        // Face value mutator.
        public void setFaceValue(int value)
        {
            faceValue = value;
        }

        // Face value accessor.
        public int getFaceValue()
        {
            return faceValue;
        }

        // Returns a string representation of this die.
        public string ToString()
        {
            string result = Convert.ToString(faceValue);

            return result;
        }
    }
    class RollingDice
    {
        //Creates file Die objects and rolls them three times.
        public static void Main(String[] args)
        {
            Die die1, die2, die3, die4, die5;
            int sum;

            die1 = new Die();
            die2 = new Die();
            die3 = new Die();
            die4 = new Die();
            die5 = new Die();

            die1.RollDie();
            die2.RollDie();
            die3.RollDie();
            die4.RollDie();
            die5.RollDie();

            Console.WriteLine("Die1: {0} \nDie2: {1} \nDie3: {2} \nDie4: {3} \nDie5: {4}",
                die1, die2, die3, die4, die5);

            sum = die1.getFaceValue() + die2.getFaceValue() + die3.getFaceValue() + die4.getFaceValue() + die5.getFaceValue();

            Console.WriteLine("Sum: {0}", sum);

            Console.Read();
        }
    }
}


The output I am getting is:

Die1: Term_Project.Die
Die2: Term_Project.Die
Die3: Term_Project.Die
Die4: Term_Project.Die
Die5: Term_Project.Die
Sum: 25


Obviously, that is not the output I was expecting...except for sum. the "Die#:" should be showing the face value of the die.

HELP!!!
Was This Post Helpful? 0
  • +
  • -

#11 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 11:49 AM

Thats because you're writing die1 to 5 out ( a class, not a number ).
You want to write die1.getFaceValue() to die5.getFaceValue() :)

Or i can see you can call die1.ToString(), should give the same output.

~Shivern

This post has been edited by Shivern: 23 May 2010 - 11:55 AM

Was This Post Helpful? 1
  • +
  • -

#12 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 01:29 PM

Thank you Shivern, that solved that problem, but it appears that another one has cropped up and that is that every die is showing the name number (facevalue), which I guess means that the 'random' isn't working correctly (duh).

This is starting to really frustrate me which is bad since I haven't even gotten to the really tough stuff...like 'holding a die' and 'rolling all the dice but the one that is held' and so on and so forth.
Was This Post Helpful? 0
  • +
  • -

#13 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 01:36 PM

have you tried both with the ToString ang getFaceValue?

please post all your updated code, then i'll take a look again.

~Shivern
Was This Post Helpful? 1
  • +
  • -

#14 Geckko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 23 May 2010 - 02:11 PM

Yes...I tried both options.

Here is my code again:

#region DieClass Definition
    class Die
    {
        private int MAX = 6;
        private int faceValue;

        // Sets the initial face value.
        public Die()
        {
            faceValue = 1;
        }

        // Rolls the die and returns the result 
        Random rmd = new Random();
        public int RollDie()
        {
            faceValue = rmd.Next(1, 6);
            return faceValue;
        }

        // FaceValue set
        public void setFaceValue(int value)
        {
            faceValue = value;
        }

        // FaceValue get
        public int getFaceValue()
        {
            return faceValue;
        }

        // Returns a string representation of this die.
        public string ToString()
        {
            string result = faceValue.ToString();

            return result;
        }
    }
    #endregion

    #region RollingDice Definition
    class RollingDice
    {
        //Creates Die objects, rolls them once and adds sum
        public static void Main(String[] args)
        {
            Die die1, die2, die3, die4, die5;
            int sum;

            die1 = new Die();
            die2 = new Die();
            die3 = new Die();
            die4 = new Die();
            die5 = new Die();

            die1.RollDie();
            die2.RollDie();
            die3.RollDie();
            die4.RollDie();
            die5.RollDie();

            Console.WriteLine("Die1: {0} \nDie2: {1} \nDie3: {2} \nDie4: {3} \nDie5: {4}",
                die1.ToString(), die2.ToString(), die3.ToString(), die4.ToString(), die5.ToString());

            sum = die1.getFaceValue() + die2.getFaceValue() + die3.getFaceValue() + die4.getFaceValue() + die5.getFaceValue();

            Console.WriteLine("Sum: {0}", sum);

            Console.Read();
        }
    }
    #endregion

Was This Post Helpful? 0
  • +
  • -

#15 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

Re: Ship, Captain, Crew & Mate Dice Game

Posted 24 May 2010 - 12:17 AM

The reason this is happening, is because you're creating an Random object in every of you're Dice objects, and its get initialized in almost the exact same time, and then you use the Random.Next method, almost the exact same time, and that gives the same faceValue on all your Dice objects.

A way out of this, can be that you create a handler class, with only 1 Random object, that you call every time.
Another way is to create a logic class, with a static method, that you call the roll method on, on every Die object, to get the random number from.

example of logic class with a static method:
class Logic
{
	private static Random _random = new Random();

	public static int RollDie()
	{
		return _random.Next(1, 7); // 7 because 6 gives you 6 numbers to work with (0 - 5) the 7 gives you 7 numbers to work with (0 - 6)
	}
}



and a little change to your Die class, RollDie() method:
public int RollDie()
{
	faceValue = Logic.RollDie();

	return faceValue;
}



Now you can also remove the Random object in your Die class.

~Shivern
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2