Looking over Code

Alivegamer

Member
Joined
Jul 20, 2022
Messages
10
Programming Experience
1-3
I am starting to learn C# and I've created a very simple game you can say and want to get feedback and tips on how to make my code more efficient or something I could do differently and make my code better or tips on some habits I should pick up.
I also don't know if this is a good place to post this.
C#:
using System;

namespace Game
{
    class Menu
    {
        static void Main(string[] args)
        {
            bool loop = true;
            string Input;
            while (loop == true)
            {
                Console.Clear();
                Console.WriteLine("Colony Simulator");
                Console.WriteLine("Start");
                Console.WriteLine("End");
                Input = Console.ReadLine().ToLower();
                if (Input == "start")
                {
                    GamePlay Game = new GamePlay();
                    Game.Start();

                }
                if (Input == "end")
                {
                    loop = false;
                }
            }
        }
    }
    public class GamePlay
    {
        private int Wood;
        private int People;
        private int Rock;
        private int House;
        private int Lumberjack;
        private int Mines;
        private int Miners;
        private int WoodCutters;
        private int Farms;
        private int Farmers;
        private int Food;
        private string Input;

        public GamePlay()
        {
            House = Lumberjack = Mines = Miners = WoodCutters = 0;
            Wood = Rock = Food =  50;
            People = 10;
        }
        public void Job()
        {
            int Working;
            bool loop = true;
            string Input;
            while (loop == true)
            {
                Working = Farmers+Miners+Lumberjack;
                Console.Clear();
                Console.WriteLine("JOBS:\nFarmers: " + Convert.ToString(Farmers) + "\nMiners: " + Convert.ToString(Miners) + "\nLumberjacks: " + Convert.ToString(Lumberjack));
                Console.WriteLine("BACK");
                Console.WriteLine("What job do you want to use?");
                Input = Console.ReadLine().ToLower();
                if (Input == "farmers")
                {
                    Console.Clear();
                    Console.WriteLine("How many farmers?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("Plus or minus?");
                    Input = Console.ReadLine().ToLower();
                    if (Input == "plus")
                    {
                        if (amount + Farmers <= Farms * 2 && amount <= People - Working)
                        {
                            Farmers += amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have enough people or farms\nENTER");
                            Console.ReadLine();
                        }
                    }
                    if (Input == "minus")
                    {
                        if (amount <= Farmers)
                        {
                            Farmers -= amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have that much Farmer\nENTER");
                            Console.ReadLine();
                        }
                    }
                }
                if (Input == "miner" || Input == "miners")
                {
                    Console.Clear();
                    Console.WriteLine("How many miners?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("Plus or minus?");
                    Input = Console.ReadLine().ToLower();
                    if (Input == "plus")
                    {
                        if (amount + Miners <= Mines * 2 && amount <= People - Working)
                        {
                            Miners += amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have enough people or mines\nENTER");
                            Console.ReadLine();
                        }
                    }
                    if (Input == "minus")
                    {
                        if (amount <= Miners)
                        {
                            Miners -= amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have that much Miners\nENTER");
                            Console.ReadLine();
                        }
                    }
                }
                if (Input == "lumberjack")
                {
                    Console.Clear();
                    Console.WriteLine("How many Lumberjacks?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("Plus or minus?");
                    Input = Console.ReadLine().ToLower();
                    if (Input == "plus")
                    {
                        if (amount + Lumberjack <= WoodCutters * 2 && amount <= People - Working)
                        {
                            Lumberjack += amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have enough WoodCutters or people\nENTER");
                            Console.ReadLine();
                        }
                    }
                    if (Input == "minus")
                    {
                        if (amount <= Lumberjack)
                        {
                            Lumberjack -= amount;
                        }
                        else
                        {
                            Console.Clear();
                            Console.WriteLine("You Don't have that much Lumberjacks\nENTER");
                            Console.ReadLine();
                        }
                    }
                }
                if (Input == "back")
                {
                    loop = false;
                }
            } 
        }
        public void Build()
        {
            bool loop = true;
            string Input;
            while (loop == true)
            {
                Console.Clear();
                Console.WriteLine("You Have:\nHouse: " + Convert.ToString(House) + "\nMines: " + Convert.ToString(Mines) + "\nWoodCutters: " + Convert.ToString(WoodCutters));
                Console.WriteLine("Farms: " + Convert.ToString(Farms));
                Console.WriteLine("What do you want to build?");
                Console.WriteLine("House\nMine\nLumberMill\nFarm\n\nBACK");
                Input = Console.ReadLine().ToLower();
                if (Input == "house")
                {
                    Console.Clear();
                    Console.WriteLine("House Cost 5 Wood and 5 Rock\nHow many do you want to build?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    if (amount*5 <= Wood && amount*5 <= Rock)
                    {
                        Wood -= amount * 5;
                        Rock -= amount *5;
                        House += amount;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("You Don't have " + Convert.ToString(amount*5)+ " Wood and/or Rock\nENTER");
                        Console.ReadLine();
                    }
                }
                if (Input == "mine" || Input == "mines")
                {
                    Console.Clear();
                    Console.WriteLine("Mine Cost 10 Rock and 5 Wood\nHow many do you want to build?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    if (amount * 5 <= Wood && amount * 10 <= Rock)
                    {
                        Rock -= amount * 10;
                        Wood -= amount * 5;
                        Mines += amount;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("You dont have " + Convert.ToString(amount*5) + " Wood or " + Convert.ToString(amount*10) + " Rock\nENTER");
                        Console.ReadLine();
                    }
                }
                if (Input == "woodcutter")
                {
                    Console.Clear();
                    Console.WriteLine("WoodCutter Cost 5 Wood and 10 Rock\nHow many do you want to build?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    if (amount * 5 <= Wood && amount * 10 <= Rock)
                    {
                        Wood -= amount*5;
                        Rock -= amount*10;
                        WoodCutters += amount;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("You Don't have " + Convert.ToString(amount * 5) + " Wood or " + Convert.ToString(amount*10) + " Rock\nENTER");
                        Console.ReadLine();
                    }
                }
                if (Input == "farm")
                { 
                    Console.Clear();
                    Console.WriteLine("Farm Cost 10 Wood\nHow many do you want to build?");
                    int amount = Convert.ToInt32(Console.ReadLine());
                    if (amount*10 <=Wood)
                    {
                        Wood -= amount * 10;
                        Farms += amount;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("You Dont have " + Convert.ToString(amount * 10) + " Wood\nENTER");
                        Console.ReadLine();
                    }
                }
                if (Input == "back")
                {
                    loop = false;
                }
            }
        }
        public void Start()
        {
            bool loop = true;
            while (loop == true)
            {
                Console.Clear();
                Console.WriteLine("Food " + Convert.ToString(Food) + " " + Convert.ToString((Farmers*5)-People*2) + "\nPeople " + Convert.ToString(People) + "\nWood " + Convert.ToString(Wood) + "\nRock " + Convert.ToString(Rock));
                Console.WriteLine("CAN DO:"); 
                Console.Write("Next Day\nBuild\nJobs\nQuit");
                Input = Console.ReadLine().ToLower();
                if (Input == "next" || Input == "next day")
                {
                    Wood += Lumberjack * 2;
                    Rock += Miners * 2;
                    Food += Farmers * 5;
                    Food -= People * 2;
                    if (Food > 0 && House * 5 > People)
                    {
                        People *= 2;
                        if (People > House * 5)
                        {
                            People = House * 5;
                        }
                    }
                }
                if (Input == "build")
                {
                    Build();
                }
                if (Input == "job")
                {
                    Job();
                }
                if (Input == "quit")
                {
                    loop = false;
                }
                if (Food <= 0)
                {
                    Console.Clear();
                    Console.WriteLine("Your colony starved\nENTER");
                    Console.ReadLine();
                    loop = false;
                }
            }
        }
    }
}
 
Move repeated code into a helper method. For example you have a recurring pattern this kind of code:
C#:
Console.Clear();
Console.WriteLine("How many farmers?");
int amount = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Console.WriteLine("Plus or minus?");
Input = Console.ReadLine().ToLower();
if (Input == "plus")
{
    if (amount + Farmers <= Farms * 2 && amount <= People - Working)
    {
        Farmers += amount;
    }
    else
    {
        Console.Clear();
        Console.WriteLine("You Don't have enough people or farms\nENTER");
        Console.ReadLine();
    }
}
if (Input == "minus")
{
    if (amount <= Farmers)
    {
        Farmers -= amount;
    }
    else
    {
        Console.Clear();
        Console.WriteLine("You Don't have that much Farmer\nENTER");
        Console.ReadLine();
    }
}

where the only thing that really changes is farmer, miner, or lumberjack.

You could create a method that looks like:
C#:
void DoJob(string workerName, string resourceName, ref int workers, int resource)
{
    Console.Clear();
    Console.WriteLine($"How many {workerName}?");
    int amount = Convert.ToInt32(Console.ReadLine());
    Console.Clear();
    Console.WriteLine("Plus or minus?");
    string input = Console.ReadLine().ToLower();
    if (input == "plus")
    {
        if (amount + workers <= resource * 2 && amount <= People - Working)
        {
            workers += amount;
        }
        else
        {
            Console.Clear();
            Console.WriteLine($"You Don't have enough people or ${resourceName}\nENTER");
            Console.ReadLine();
        }
    }
    if (input == "minus")
    {
        if (amount <= workers)
        {
            workers -= amount;
        }
        else
        {
            Console.Clear();
            Console.WriteLine($"You Don't have that many {workerName}}\nENTER");
            Console.ReadLine();
        }
    }
}

Then:
lines 67-98 can be replaced with DoJob("farmers", "farms", ref Farmers, Farms);
lines 102-133 can be replaced with DoJob("miners", "mines", ref Miners, Mines);
lines 137-168 can be replaced with DoJob("lumberjacks", "woodcutters", ref Lumberjack, WoodCutters);
 
Last edited:
Class variables should be camel cased.

So for example,
C#:
private int Farms;
private int WoodCutters;
should be:
C#:
private int farms;
private int woodCutters;
 
When getting input for values, use TryParse() instead of Convert.ToSomeType().
 
Use string interpolation.

Instead of:
C#:
Console.WriteLine("Food " + Convert.ToString(Food) + " " + Convert.ToString((Farmers*5)-People*2) + "\nPeople " + Convert.ToString(People) + "\nWood " + Convert.ToString(Wood) + "\nRock " + Convert.ToString(Rock));

Do:
C#:
Console.WriteLine($"Food {Food} {(Farmers*5)-People*2}\nPeople {People}\nWood {Wood}\nRock {Rock}");
 
Move repeated code into a helper method. For example you have a recurring pattern this kind of code:
C#:
Console.Clear();
Console.WriteLine("How many farmers?");
int amount = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Console.WriteLine("Plus or minus?");
Input = Console.ReadLine().ToLower();
if (Input == "plus")
{
    if (amount + Farmers <= Farms * 2 && amount <= People - Working)
    {
        Farmers += amount;
    }
    else
    {
        Console.Clear();
        Console.WriteLine("You Don't have enough people or farms\nENTER");
        Console.ReadLine();
    }
}
if (Input == "minus")
{
    if (amount <= Farmers)
    {
        Farmers -= amount;
    }
    else
    {
        Console.Clear();
        Console.WriteLine("You Don't have that much Farmer\nENTER");
        Console.ReadLine();
    }
}

where the only thing that really changes is farmer, miner, or lumberjack.

You could create a method that looks like:
C#:
void DoJob(string workerName, string resourceName, ref int workers, int resource)
{
    Console.Clear();
    Console.WriteLine($"How many {workerName}?");
    int amount = Convert.ToInt32(Console.ReadLine());
    Console.Clear();
    Console.WriteLine("Plus or minus?");
    string input = Console.ReadLine().ToLower();
    if (input == "plus")
    {
        if (amount + workers <= resource * 2 && amount <= People - Working)
        {
            workers += amount;
        }
        else
        {
            Console.Clear();
            Console.WriteLine($"You Don't have enough people or ${resourceName}\nENTER");
            Console.ReadLine();
        }
    }
    if (input == "minus")
    {
        if (amount <= workers)
        {
            workers -= amount;
        }
        else
        {
            Console.Clear();
            Console.WriteLine($"You Don't have that many {workerName}}\nENTER");
            Console.ReadLine();
        }
    }
}

Then:
lines 67-98 can be replaced with DoJob("farmers", "farms", ref Farmers, Farms);
lines 102-133 can be replaced with DoJob("miners", "mines", ref Miners, Mines);
lines 137-168 can be replaced with DoJob("lumberjacks", "woodcutters", ref Lumberjack, WoodCutters);
So in the New function you wrote why do you have to put ref for the int varabales but not the rest?
And thank you for all of your input I'm just trying to understand the reasoning now.
 
Can you explain on why you do that? Does it like see if a input is a string and if it isn't it doesn't do the code?
All input will be a string because that's what Console.ReadLine and the like return. If you want to see how those methods behave differently then you should read the documentation for each and see what they do. As that documentation will tell you, TryParse will return a bool to indicate success or failure while the methods of the Convert class will throw an exception if the input isn't valid. TryParse also provides more flexibility as far as input format validation is concerned.
 
So in the New function you wrote why do you have to put ref for the int varabales but not the rest?
As pretty much any beginner tutorial will explain - i.e. you should already learned this on your own - the ref keyword is applied when a parameter is used for output as well as input. Most parameters are only used for input. If the parameter is to be used for output only, you apply the out keyword.
 
As pretty much any beginner tutorial will explain - i.e. you should already learned this on your own - the ref keyword is applied when a parameter is used for output as well as input. Most parameters are only used for input. If the parameter is to be used for output only, you apply the out keyword.
Thank you for clarifying. I like to just asking what I am looking for then watching videos.
 
I like to just asking what I am looking for then watching videos.
Then you're taking advantage of us. Sites like this should be for things that you can't work out for yourself, not for thing that you can't be bothered to work out for yourself. I know I'm not alone when I say that I contribute to sites like this because I want to help people become better developers but if you're asking us to provide information just so you don't have to make the effort to find it then you're not really doing your part to become a better developer. Being able to find information for yourself is a critical skill that you should try to improve. If it's too much trouble for you, you'll soon find that it's too much trouble for us too, and then you won't get help when you actually need it.
 
Then you're taking advantage of us. Sites like this should be for things that you can't work out for yourself, not for thing that you can't be bothered to work out for yourself. I know I'm not alone when I say that I contribute to sites like this because I want to help people become better developers but if you're asking us to provide information just so you don't have to make the effort to find it then you're not really doing your part to become a better developer. Being able to find information for yourself is a critical skill that you should try to improve. If it's too much trouble for you, you'll soon find that it's too much trouble for us too, and then you won't get help when you actually need it.
I understand. Sorry for the hassle and the trouble. Thank you for your help and time though.
 
Last edited:
There's lots of Clear/WriteLine/ReadLine combinations, here you can write a helper method to replace these with a single line call, again "move repeated code into a helper method".
For example write an AskString method, then an AskInt method utilizing int.TryParse and AskString.

The Build method also has a repeating pattern for each case; where you inform the requirements, ask for amount, check requirements and "return" amount built.
You could turn that pattern into a method with this signature: public int AskBuild(string reqInfo, int reqWood, int reqRock)
 
Use else if when evaluating different conditions for same variable, instead of multiple separate if statements.
switch statement can also be used.
 
Back
Top Bottom