0

I'm into a project of a resource-game simulation. What I have to do is...

"If there are enough resources available, the request will be satisfied, and the requested quantities will be subtracted from the available quantities. If there are not enough resources, the animat consumes the available quantities and wait for more resources to be available."

The question is how do I make this possible... To reduce the resource and then hold somewhere what is still needed and reduce it when there are available resources(like a loan)...

Example:

... 100 GOLD NEEDED.... but ... 50 GOLD AVAILABLE...

... REDUCE GOLD BY 50... and wait untill gold>=50 and then

... REDUCE GOLD BY 50...

etc...

Sample Code...

    public void feedArmy(){
    if(food>=100){
        food=food-100;
        System.out.println("*Feed Soldiers  (-100 Food)");
        System.out.println(toString());
    }
    else{
        System.out.println("*Feed Soldiers  (-100 Food)");
        System.out.println("-Not Enough Food!"); //get loan instead
    }
}

(After Edit) BEST SOLUTION FOR NOW:

Actually ... I thought of just reducing the wanted value from the wood and then if the number goes negative I keep the negative value turn it to positive with

Math.abs();

so if the wood is 30 and I want 100... I do 30-100=-70; then loan=-70; ...

then I Math.abs(loan); so that loan=70;

and then I do an if(wood>loan){ wood=wood-loan} //i might need to put a sleep untill wood is refreshed again... and thats it... I still have no idea what is that Producer/Consumer stuff...

user2375278
  • 17
  • 1
  • 4
  • The statement "wait until resources are available" implies you have producer threads and consumer threads. A detailed explanation of producer-consumer synchronization is far beyond the scope of SO, which is for specific programming questions. Search the web for tutorials on multi-threaded programming and specifically "producer-consumer". Also, please read the [FAQ] and [Ask] to better understand how to write good questions. – Jim Garrison May 13 '13 at 18:35
  • @JimGarrison I dont disagree that this is beyond SO scope but this sort of game could be achieved without multithreading with a simple game loop – Richard Tingle May 13 '13 at 18:40
  • Beyond SO scope?? Whats that? – user2375278 May 13 '13 at 18:45
  • Technically SO (stack overflow) is for questions and answers that are of general interest, not just the question asker. (Although that rule is often overlooked for a polite, well writen question). See the FAQs for more details – Richard Tingle May 13 '13 at 18:48
  • It's not only for me ... my classmates are looking for this information too... if this counts – user2375278 May 13 '13 at 18:50
  • I updated my question with a the best answer I could find... – user2375278 May 13 '13 at 19:53

2 Answers2

1

So the solution you are looking for is well known and much studied in computer science. The problem itself is called the Producer Consumer problem. Do a search for this and you will find numerous examples and code to solve the problem.

Here is a stackoverflow question about it, Producer/Consumer threads using a Queue.

Wikipedia page explaining the problem in detail.

Community
  • 1
  • 1
greedybuddha
  • 7,488
  • 3
  • 36
  • 50
0

Consider creating an unfulfilled request object; just resource type and outstanding amount. Then add one to an arraylist every time one of these debts needs to be raised. Then every game loop (how ever you are implementing that in your game) check through the unfulfilled list and debt the resources if you can

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77