First C program with two Classes Confused

0 votes

This is the first time I've used two classes in a programme (in two seperate class files). All of the code will be included below. It's an application that allows you to play Craps, a dice game. My assignment is to write a method in the CrapsGame class that allows me to play the game till I opt to stop. But I'm having difficulties figuring out how to invoke the manyPlay() method appropriately. I have no idea what I'm doing. When you call myCraps.play(), the software will play the game once and then stop. Any assistance would be greatly appreciated.

using System;

namespace Task4_7
{
    public class CrapsGame
    {
        string replay;
        private Craps myCraps;
        private CrapsGame newGame;


        public static void Main()
        {
            CrapsGame newGame = new CrapsGame();
            Craps myCraps = new Craps ();
            myCraps.play ();
            newGame.manyPlay ();
        }
        public void manyPlay() {
            string input; // declare local variable
            do {
                myCraps.play();
                replay:
                Console.Write("Would you like to play again? y/n");
                input = Console.ReadLine();
                if (input == "y") {
                    replay = input;
                }
                else if (input == "n") {
                    replay = "n";
                }
                else {
                    Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
                    goto replay;
                }
            }
            while(replay != "n");
        } 
    }
}





using System;

namespace Task4_7
{
    public class Craps
    {
        private Random randy;               // define randy as a Random class

        public Craps() {

            this.randy = new Random ();
        }

        public int oneThrow() {
            return randy.Next(6) + 1;        // pick a number from 1 to 6 and return this
        }
        public int throw2Dice() {
            int a, b, c;
            a = oneThrow ();
            b = oneThrow ();
            c = a + b;
            Console.WriteLine ("You threw a " + a + " and a " + b + " making " + c);
            return c;
        }
        public void play() {
            int result = throw2Dice ();
            switch (result) {
                case 2:
                    Console.WriteLine ("You lose! End of game!");
                    break;
                case 3:
                    Console.WriteLine ("You lose! End of game!");
                    break;
                case 12:
                    Console.WriteLine ("You lose! End of game!");
                    break;
                case 7:
                    Console.WriteLine ("You win! End of game!");
                    break;
                case 11:
                    Console.WriteLine ("You win! End of game!");
                    break;
                case 4:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
                case 5:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
                case 6:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
                case 8:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
                case 9:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
                default:
                    Console.WriteLine ("Your point! Rolling again!");
                    throwPoint (result);
                    break;
            }
        }
        public void throwPoint(int result) {
            Throw:
            int a = throw2Dice();
            if (a == result) {
                Console.WriteLine ("You rolled the same score! You win!");
            } else if (a == 7) {
                Console.WriteLine ("You rolled a 7! You loose!");
            } else {
                Console.WriteLine ("You rolled a " + a + ". Rolling again!");
                goto Throw;
            }
        }
    }
}
Jun 11 in C# by pranav
• 2,580 points
16 views

1 answer to this question.

0 votes

I figured out where your problem is

Craps myCraps = new Craps();

You're using a local variable in the Main method to hide a variable from the CrapsGame class. Simply replace it with

myCraps = new Craps();

Now it should be working.

answered Jun 21 by jyoti
• 1,240 points

Related Questions In C#

0 votes
1 answer

How can I compile and run c# program without using visual studio

If you have.NET Framework v4 installed, then C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
30 views
0 votes
0 answers

Monaco Editor (react) with extended C# intellisense

To load certain code-files, I'm using the ...READ MORE

Jun 11 in C# by krishna
• 2,540 points
77 views
0 votes
0 answers

How to add a gridview to a C# winforms project with a list of objects as the datasource

In Visual Studio 2015 community, I'm creating ...READ MORE

Jun 11 in C# by krishna
• 2,540 points
12 views
0 votes
0 answers

Can a C# program be cross-platform

I'm a complete beginner when it comes ...READ MORE

Jun 11 in C# by pranav
• 2,580 points
10 views
0 votes
1 answer

SEO asp.net description spelling order

https://neilpatel.com/blog/simple-guide-to-seo/ here you can see step by ...READ MORE

answered Mar 1 in Digital Marketing by narikkadan
• 11,280 points
35 views
0 votes
1 answer

OOP real world example

This is what I believe. OOP does not ...READ MORE

answered Jul 4 in C# by krishna
• 2,540 points
17 views
0 votes
1 answer

C# class without main method

Not every single class need the main ...READ MORE

answered Jul 7 in C# by krishna
• 2,540 points
17 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,120 points
4,511 views
0 votes
1 answer

I don't understand inheritance with base classes

If you just want to keep a ...READ MORE

answered Jun 17 in C# by jyoti
• 1,240 points
18 views
0 votes
1 answer

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17 in C# by jyoti
• 1,240 points
18 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP