Reputation: 107
Im trying to make a Pacman, so im using an array[8][8], in which i have a PacMan, 2 ghosts, walls, and points that the Pacman can eat. as the pacman eats points you start to get empty spaces.
So i decidad to make a function random that ramdonly assign Fruits ONLY to the empty spaces, but the problem is that as more empty spaces you get the fruits start going crazy and appear all over my array. I would like to have always the same chances of getting fruits despite of the empty spaces i have.
Here are the methods im using, help please.
public void AppearFruit()
{
for(int i= 0; i<_world.length; i++){
for(int j= 0; j<_world.length; j++){
Random as = new Random();
int fru = as.nextInt(20);
if(fru==10){
if(_world[i][j] instanceof Empty){
_world[i][j] = new Fruit(i,j);
}
}
}
}
}
public void DisappearFruit()
{
for(int i= 0; i<_world.length; i++){
for(int j= 0; j<_world.length; j++){
if (_world[i][j] instanceof Fruit){
if(contfru>=3) {
_mundo[i][j] = new Empty(i,j);
contfru=0;
}
}
}
}
}
Upvotes: 0
Views: 121
Reputation: 103797
It depends what you mean by "the same chance of getting fruits".
What you have now will independently give each empty space a 1 in 20 chance of getting a fruit. So if you have a large grid with 2-300 empty spaces, you'd expect to get 10-15 fruits every time AppearFruit
runs.
I don't know what the contfru
variable is in DisappearFruit
, but I would imagine it's not getting rid of the fruits at anywhere near this rate - so that over time, more and more fruits will appear.
Edit: After your update, you're asking for
the same chances of getting fruits despite of the empty spaces i have
By definition, this means that your current approach can't work at all, because you give each space an equal chance of getting fruit. Doubling the number of empty spaces will double the number of fruits you get (on average).
It sounds like you want something more like a method to add one fruit, which scans the array of empty spaces and assigns the fruit to one of the spaces at random. Then you can tweak how often you call this method - maybe it happens exactly every 50 turns, maybe it happens every 30 + rnd(40)
turns, maybe every 60 seconds, etc. You can tweak how much fruit appears (and how quickly) through the frequency of the calls.
Upvotes: 1
Reputation: 114777
Right now the probabilty for a fruit to appear is 5% for each empty field and for each move. Like you have one empty field and move the pacman 10 times, the probability for this field is
P_fruit = 1 - (19/20)^10
= 40.1%
I guess, the algorithm is not bad, you may call it far to often.
Edit
You could tweak the behaviour by using a far lower probability, but in the end, it would be the same: if you need more fruit, simply keep the pacman moving.
I'd disconnect the fruit generation from movement. Think about a timer instead - like every 30 seconds an event is triggered that generates fruit on empty spaces with a low probability. You could even say: I define a probability p that one fruit is generated on the board and then randomly choose one of the current empty spaces: (pseudocode)
placeFruit(board)
if (random() > 5%) return; // no fruit this time
List fields :== getAllEmptyFields(board);
fruitField :== random(fields.size());
fruitField.add(new Fruit());
Upvotes: 0
Reputation: 47608
Ok I think I get why you get many fruits. In your method AppearFruit
, when you assign a Fruit, you don't increment countfru, making you method DisappearFruit
never work like intended
Upvotes: 0