1

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();
        }
      }
    }
  }
}
Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
Hybeee
  • 33
  • 4
  • You start with building's levels `0`? Or am I missing something? – Markiian Benovskyi Nov 11 '19 at 19:03
  • Desktop Display Technologies like WindowsForms and WPF are not the right tool for game programming. Console? Even less so. For game development you need something with a GameLoop. | Making "Building" a nested class, also makes reading this unessesarily hard. It hsould be a brother/peer to programm, not a subpart. It is to easy to mess up value access this way. – Christopher Nov 11 '19 at 19:03
  • @MarkiianBenovskyi no, you were right. I changed it to 1(as the basic level), however I still face the original problem. – Hybeee Nov 11 '19 at 21:07
  • @Christopher. Thank you for the clarification. I know that game making I'd need something else as you mentioned(I didn't know what, but now I do). Basically what I wanted to do with this program is to make the basic logic/algorithm behind this clicker game. – Hybeee Nov 11 '19 at 21:10
  • It unfrotunately does not work that way. Games and WebApplications each have a very unique structure, that dictates how you do things. So do console applications. So does WinForms/WPF and other message pump/event queue based things. | That being said, of all the unsuited tools on console it might be the easiest to simulate/make a game loop. The big issue is getting a non-blocking way to read user input, but I think I found some elses solution: https://stackoverflow.com/a/5620647/3346583 That is pretty much how you deal with input in game programming too. – Christopher Nov 11 '19 at 21:18
  • The basic structure is this (https://en.wikipedia.org/wiki/Game_programming#Game_structure). However in practice it tends to be limited to 1 Update and 1 Draw function. Update does the most from that list. Draw only the last 2 parts (with most of the work being handed off to GPU and Sound Hardware) | As for the proper ways: https://dotnet.microsoft.com/apps/gaming | There is the somewhat data XNA too, but the stuff on the link is current and worth learning. – Christopher Nov 11 '19 at 21:21
  • @Christopher Appreciate it! – Hybeee Nov 11 '19 at 21:28

1 Answers1

1

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();
    }
}
Markiian Benovskyi
  • 2,137
  • 22
  • 29