Hybeee
Hybeee

Reputation: 33

How can I make this program work properly?

Let me start off by quickly summarizing what this program does. It's a really basic form of a clicker game(here: space/any button on your keyboard/). I have 2 buildings(manor and booster). Manor makes gold based on its level, whilst booster boosts the amount of gold made per click(press).

However when I try to run the program itself I bump into a problem. Basically(as it'll be visible on the code I'll copy-paste here) when I run the first function of the program(making gold) it'll produce and save the gold properly(your gold balance is x now), but when I try to upgrade my building/look at my balance it says: insufficient gold/0.

I've tried printing out the steps seperately(without the while function) and it worked, I could even upgrade my building but then after that, it started to produce the gold weirdly(so instead of producing the amoung at level 2/press, it produced something else, once even a negative number came out as a result).

Now in the code below I don't have the full while function(I've tried to fix it and since I only needed the producing and one upgrading function for test purposes I didn't complete the function with 4 options(A)produce B) upgrade c)upgrade D)show balance) but only the first two.)

Any help is appreciated and have a nice day!

using System;
using System.Collections.Generic;
using System.Linq;

namespace Clicker_Game
{
  class Program
  {
    class Buildings
    {
      // two buildings: manor, helper(multiplier) + gold balance // 
      int manorlevel = 0;
      int boosterlevel = 0;
      int goldbalance = 0;

      // level cost of the buildings and their properties
      // on each level index - 1 = level's bonus // 
      int[] mproduce = new int[10] { 1, 5, 7, 10, 15, 20, 25, 30, 40, 50 };
      int[] mlevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };
      int[] bvalue = new int[10] { 1, 1, 1, 2, 2, 2, 2, 3, 4, 6 };
      int[] blevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };

      public int ManorUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your manor from " + manorlevel + 
                          " to " + ( manorlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= mlevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your manor to level" + 
                              ( manorlevel + 1 ) + "!");
            manorlevel += 1;
            goldbalance -= mlevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return manorlevel;
      }
      public int BoosterUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your booster from " + boosterlevel + 
                          " to " + ( boosterlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= blevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your booster to level" + 
                              ( boosterlevel + 1 ) + "!");
            boosterlevel += 1;
            goldbalance -= blevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return boosterlevel;
      }

      public int Clicker()
      {
        Console.WriteLine("Here you can click to produce gold! Can we start?");
        string answer = Console.ReadLine();
        Console.WriteLine("If you want to stop just say no!");

        if ( answer == "yes" )
        {
          while ( true == true )
          {
            string a = Console.ReadLine();
            if ( a == "no" )
            {
              Console.WriteLine(goldbalance);
              break;
            }
            goldbalance += mproduce[manorlevel - 1] * bvalue[boosterlevel - 1];
          }
        }

        return goldbalance;
      }
      public int Balance()
      {
        Console.WriteLine("You wanna see your balance?");
        string a = Console.ReadLine();
        if ( a == "yes" )
        {
          Console.WriteLine(goldbalance);
        }
        return goldbalance;
      }
    }

    static void Main(string[] args)
    {
      {
        string answer = "yes";
        while ( answer != "exit" )
        {
          Console.WriteLine("baba");
          answer = Console.ReadLine();
          Buildings app = new Buildings();

          if ( answer == "a" )
          {
            app.Clicker();
          }
          else if ( answer == "b" )
          {
            app.ManorUpgrader();
          }

          Console.ReadKey();
        }
      }
    }
  }
}

Upvotes: 1

Views: 73

Answers (1)

Markiian Benovskyi
Markiian Benovskyi

Reputation: 2161

Basically you just mistyped the while loop. What you need to do is to move your line:

Buildings app = new Buildings();

out of the while loop. So your Main method should look like:

static void Main(string[] args)
{
    // You need to create instance this class once
    // earlier you were creating it every time the loop started
    // so your values were reset to default
    var app = new Buildings();

    var answer = "yes";
    while (answer != "exit")
    {
        Console.WriteLine("Start loop");
        answer = Console.ReadLine();

        if (answer == "a")
        {
            app.Clicker();
        }
        else if (answer == "b")
        {
            app.ManorUpgrader();
        }

        Console.ReadKey();
    }
}

Upvotes: 1

Related Questions