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,320 points
7 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 5 days ago by jyoti
• 1,140 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
15 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
• 1,340 points
14 views
0 votes
1 answer

Convert VB.NET --> C# Projects

You may decompile whole Assemblies into Visual ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
18 views
0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
19 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
• 10,640 points
30 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,100 points
4,466 views
0 votes
1 answer

How to create a new R6 Class in R?

You have to first create an object ...READ MORE

answered Jul 5, 2018 in Data Analytics by DataKing99
• 8,240 points
682 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
499 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,140 points
8 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,140 points
9 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