Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
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...
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.
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
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm developing this for Bukkit, but I am open to porting a java-only solution.
Now, I have a character in a room with an opening exposed to the outside for radiation to seep in like this:
(sorry for terrible paint skills) In this picture the algorithm should return false, however if that white bit was covered, it would return true.
I need a way of detecting whether the character is inside a room or not. Please remember that a room can be any size, and does not have to be a cuboid.
The way I'd like it to work is that I'd have an arbitrary position above the roof, and if this maze algorithm would be able to reach this position, I'd know that the character is not completely covered.
EDIT: Actually if the exit was 20+ units away, I wouldn't mind the algorithm timing out.
This is not an answer per se, but it may be a good point to move on from.
The definition for room:
a part or division of a building enclosed by walls, floor, and
ceiling.
The definition for building:
a structure with a roof and walls
The definition of a structure:
a building or other object constructed from several parts
Can you see how poorly defined these are? (The definitions for building and structure are recursive!) Until you can come up with a clear cut, black and white definition for what you consider to be a room, you (and us) have very little hope of coming up with a good solution.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a requirement where I am writing a small utility to test apis(ofcourse there are existing tools but it has been decided to write one). I am required to bombard the api, for the same api call, with say 100 threads, around say 100,000 times.
I am using 'PoolingHttpClientConnectionManager' for the making the calls. I am using something as mentioned in the below link:
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
My question is:
(1) How can I run the above code for 100,000 iterations? Using that many number of threads is obviously a bad idea. Initially thought of using ExecutorService for maintaining thread count and number of jobs to be submitted but it felt redundant.
(2)I read about 'setMaxTotal'(max connections) and 'setDefaultMaxPerRoute'(concurrent connections) but I dont think it will help achieve(1) though I will obviously be required to increase the values.
Please advise. Thanks in advance.
You could use a threadpool and push the workerfunction the required number of times. Then you could even vary the number of workerthreads executing the functions to simulate different loadsituations.
Threadpool tutorial:
https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
Why don't you use Jmeter for such performance/load testing?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
You're dealt 17 cards from a 52-card deck. On average, what is the longest straight flush you will have in your hand?
A straight flush is a set of cards that are consecutive, and also of the same suit. 2 is low, Ace is high, and you cannot wrap around. Do not solve this mathematically — create a program that approximates this.
I'm not able to think about the correct approach for this type of question. Is there any type of algorithm I have to apply?
This is an example of what's called a "Monte Carlo" program, after the casino resort. The idea is to just try a random process a whole bunch of times and look at the statistics.
Basically, you should write a program that deals 17 cards from the deck and counts the longest straight flush. Then you call that a lot of times (maybe 10,000 or 100,000) and take the average.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I had been reading two books on JAVA and while covering data structures, I started to do some online research with regards to "QUEUE" implementation. I have an extensive background in FLEX, however ACTIONSCRIPT isn't comparable to advance languages.
Lets say if I was on a job interview and asked to implement a Queue of Object, how should I pursue it as? I am not looking for code help here, I would like to what would you quick answer be? I have been to Java online docs and do understand there are 13 known implementing classes, and "LinkedList" is one of them.
Google search has return more results with "LinkedList" implementation code than any other.
My apologies if you find this question to be rubbish or pointless in anyway.
Oracle's Java online doc ref:
Do you know what the concept of a queue is and how it differs from a stack (closely related data structure)? If so, you should be able to think of multiple ways to implement it.
Which is best depends on the exact requirements of the task it's being used to address.
So the right response to that interview question is not to start coding but to ask them for more information about the requirements your implementation has to address. Performance? Memory size? Multitasking? Any limits on maximum queue depth, eg to guard against things like a DOS attack? What's being enqueued -- objects, primitives, other? Specific kinds thereof? Parameterized type? Are there any values which should be discarded (maybe null shouldn't be enqueued)?
Knowing the requirements, you should be able to judge which answer is appropriate. Starting coding without asking the requirements is immediately going to earn you a demerit.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This is kind of unusual question for developers but for some reason i want to post it here and hope to get adequate answer.
Here is a simple example:
I wrote a java function that calculates distance between two geo points. The function is not more than 50 lines of code. I decided to download a source code from ibm that does the same thing but when i opened it i saw that it looks very complicated and is almost thousand lines of code.
What kind of people write such source code? Are they just very good programmers? Should i use their source code or my own?
I have noticed this kind of thing lots of times and i from time to time i start to wonder if it is just me who do not know how exactly to program or maybe i am wrong?
Do you guys have the same kind of feeling when you browse throught some other peoples source code?
The code you found, does it do the exact same calculation? Perhaps it takes into account some edge cases you didn't think of, or uses an algorithm that has better numerical stability, lower asymptotic complexity, or is written to take advantage of branch prediction or CPU caches. Or it could be just over-engineered.
Remember the saying: "For every complex problem there is a solution that is simple, elegant, and wrong." If you are dealing with numerical software, even the most basic problems like adding a bunch of numbers can turn out to be surprisingly complex.