-2

I try to transition a Monopoly Banking python programming I made into C#.

I used the main module is where I declare all my global variables, however, I don't quite understand how to call a module in another module without all the required variables passed into it.

https://pastebin.com/bVdt5nrP

For example on line 50 i try running SetCustom module however i don't have all the parameters that are required passed into MainMenu, I really don't wanna pass every Parameter into every module cause that just ends in mess and confusion. Is there away i can work around this or do i have to do everything messy?

Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonopolyCSharpNonForm
{
    class Program
    {
        static void Main(string[] args)
        {

            //Primary Settings
            int StartingBallance = 2000;
            int PassGo = 200;
            int Bail = 50;
            int IncomeTax = 100;
            int SuperTax = 200;
            string TaxLocation = 1; // 1 goes to the bank

            //Players/Economy
            string Player1 = "Player1";
            string Player2 = "";
            string Player3 = "";
            string Player4 = "";
            int Ballance1 = 2000;
            int Ballance2 = 2000;
            int Ballance3 = 2000;
            int Ballance4 = 2000;
            int FreeParking = 0;
            string[] BankRupties = { };

            MainMenu(); // Calling Main Menu
        }

        static void MainMenu()
        {
            Console.WriteLine("Welcome to Monpoly Banker V4");
            Console.WriteLine("Would you to use the Default or Custom settings?"); //Ask for Default or Custom option
            string LocalOption = Console.ReadLine(); //Record the option as a local variable 'LocalOption'
            while (LocalOption != "Default" && LocalOption != "Custom")//Check if LocalOption is 'Default' or 'Custom'
            {
                Console.WriteLine("Niether option has been specified, type 'Default' or 'Custom'"); // If not state there is an Error.
                LocalOption = Console.ReadLine(); //Record the input again.
            }

            if (LocalOption == "Custom") // Check if local variable is equal to custom.
            {
                SetCustom(ref StartingBallance, ref PassGo, ref Bail, ref IncomeTax, ref SuperTax, ref TaxLocation); //If it is run the SetCustom module below with the required parameters
            }
            else //Anything else it runs GettingNames module with required parameters
            {
                GettingNames(ref Player1, ref Player2, ref Player3, ref Player4);
            }
        }

        static void SetCustom(ref int StartingBallance, ref int PassGo, ref int Bail, ref int IncomeTax, ref int SuperTax, ref string TaxLocation)
        {
            Console.WriteLine("\nHow much money is everyone starting with? "); // Ask for Starting Ballance
            StartingBallance = Convert.ToInt32(Console.ReadLine()); // Change the StartingBallance to entered integer
            Console.WriteLine("\nHow much money if you pass go? "); // Ask how much you get for passing go
            PassGo = Convert.ToInt32(Console.ReadLine()); // change PassGo to entered integer
            Console.WriteLine("\nHow much will bail cost? "); // Ash how much Bail is going to cost
            Bail = Convert.ToInt32(Console.ReadLine()); // change Bail to entered integer
            Console.WriteLine("\nHow much will income tax cost? "); // Ask how much Income Tax will cost
            IncomeTax = Convert.ToInt32(Console.ReadLine()); // Change IncomeTax to entered Integer
            Console.WriteLine("\nHow much will super tax cost? "); // Ask how much Super Tax will cost
            SuperTax = Convert.ToInt32(Console.ReadLine()); // Record the entered integer
            Console.WriteLine("\nDoes tax go to the bank for free parking? (1 = Bank | 2 = Free Parking)"); // Ask for where free parking will go (1/2 to make options easier)
            TaxLocation = Console.ReadLine(); // Change TaxLocation to entered String
            while (TaxLocation != "1" && TaxLocation != "2") // Check TaxLocation is either '1' or '2'
            {
                Console.WriteLine("Invalid Input. Try again."); // If not state its invalid input
                TaxLocation = Console.ReadLine(); // Record entered Integer
            }

            GettingNames(ref Player1, ref Player2, ref Player3, ref Player4); // When done continue on to module Getting Names passing required parameters

        }

        static void GettingNames(ref string Player1, ref string Player2, ref string Player3, ref string Player4)
        {
            Console.WriteLine("\nWhat is Player1 name? "); // Ask Player1 name
            Player1 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player2 name? "); // Ask Player2 name
            Player2 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player3 name? "); // Ask Player3 name
            Player3 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player4 name? "); // Ask Player4 name
            Player4 = Console.ReadLine(); // Set Player1 to entered string

            Console.WriteLine("All 4 player names have been entered."); // State all names entered and received
        }
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Ace Eca
  • 33
  • 1
  • 1
  • 5

1 Answers1

0

Personally, I think I would try breaking it down into individual classes first. For example, you could create a class called Game that had the settings for the game. The settings could be set through the constructor as default settings, and you can provide a custom constructor for setting individual things. Something like the following:

public class Game
{
    public int PassGo;
    public int Bail;

    public ClassName()
    {
        PassGo = 200;
        Bail = 50;
    }

    public ClassName(int customPassGo, int customBail)
    {
        PassGo = customPassGo;
        Bail = customBail;
    }
}

You could also set defaults for those parameters if you wanted by doing something like:

public ClassName(int customPassGo = 200, int customBail = 50)
{
    PassGo = customPassGo;
    Bail = customBail;
}

You can create objects out of the Players as well. Using objects can greatly simplify what you're trying to do here. Hope this helps!

Bacskay
  • 396
  • 4
  • 12
  • Okay so I've done what you have suggested and hit another stonewall when I call the main menu it gives the error: Error CS0120 An object reference is required for the non-static field, method, or property 'Program.MainMenu() https://pastebin.com/MDHNgfpg – Ace Eca Jan 25 '18 at 20:36
  • Correct, the Main method is a static method so MainMenu would need to be a static method as well, or you could make a class out of it and use it that way. In this case, I would probably make it a static method called like BuildMainMenu and then just put your code in there. You can read more about what static means here: https://stackoverflow.com/questions/9410688/static-keyword-in-c-sharp – Bacskay Jan 25 '18 at 20:45