2

I am a new programmer and I am making a console/text strategy game in java.

my program works like you are in while (true) switch case and you got options if you press 1 you collect wood, 2 for stones and like that, I would like to make it that your wood, stones, food gather automatically and I have no idea how to make it will welcome any help,

simplified code looks like this

  while (true) {
        Scanner in = new Scanner(System.in);
        String volbačlověk = in.nextLine();

        switch (volbačlověk) {
            case "1":
                wood = wood + 1;
                break;
            case "2":
                stone = stone + 1;
                break;
            case "3":
                food = food + 1;
                break;
         }
     }

basically what I want to do is to make case 1,2,3 be done automatically every x seconds, so it works like you gather those materials automatically and in switch case there are building options.

also since I am a newbie in programing, please be specific I will not be ashamed I have to learn it somehow.

Jagger
  • 10,350
  • 9
  • 51
  • 93
  • _What_ will it gather automatically? Incidentally `wood = wood + 1` is more commonly written as `wood++`. – Boris the Spider May 07 '16 at 10:03
  • Use [Timer](https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) – Mrinal May 07 '16 at 10:05
  • and since this is going to be executed every second, how are you planning to stop the infinite loop??? – ΦXocę 웃 Пepeúpa ツ May 07 '16 at 10:06
  • I would recommend having variables that store the amount of each commodity that was collected by the player; then write methods that calculate the total amount of the commodity based on the amount of time that has passed since the "automatic gathering" began, and add in the "manually collected" amount. – Dawood ibn Kareem May 07 '16 at 10:35
  • actualy I use more difficult formula than wood++ I jsut simplified it to make it clear of what my code is like. – Václav Stýblo May 07 '16 at 10:43

3 Answers3

3

You can use the Timer Class to achieve a automatically increase of your resources:

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            // Increase value here...
        }
    }, 0, 1000); // 1000 = 1 Sek.

See also here: How to use Timer class to call a method, do something, reset timer, repeat?

Community
  • 1
  • 1
Marcinek
  • 2,144
  • 1
  • 19
  • 25
  • `Timer` is not really the commended approach anymore - hasn't been for a rather long time. Use `ScheduleExecutorService`. – Boris the Spider May 07 '16 at 10:20
  • I tried this one, found it somewhere on forum but my problem was I couldnt put my variables like wood, stones in the area, or maybe I was putting this timer in bad part of code? – Václav Stýblo May 07 '16 at 10:44
1

You don't need advanced stuff to do this. It is enough if you have:

  1. The quantity of the resources (wood, stone, etc) at a point in time t0
  2. The growing factor per resource, i.e. so many units of wood per time unit.
  3. The actual time difference to t0

Then you can compute the resources at that point in time.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • could you please tell me more about this? since Iam not really experienced with programming including time – Václav Stýblo May 07 '16 at 16:21
  • @VáclavStýblo Let's say wood grows by 2 units every time unit. Assume there were 3 units wood at some time. How many units are 5 time units later? It's 13, for sure. You don't need to increase the "wood" variable every other second to know this. – Ingo May 07 '16 at 17:58
0

Ok, so You want to run some random code from switch every x seconds without giving input, right?

Well, as you are a new programmer, so in your code, you could do that by generating some random number till your range of switch,

then just making the thread of your program sleep for some time, like:

while(true){
   Random random = new Random();
   switch(random.nextInt(3)){
     case 1:
          ////////blah blah
     break;
     ......... more alike
   }
   Thread.sleep(x seconds here);
}