2
"Mount an F matrix of size n x n representing a forest. Each element of this matrix can have 3 values:
0 - if empty 1 - contains a tree 2 - contains fire
With probability p, given by the user, put a tree in each position of F (use Math.random()
). Choose a tree to set on fire (change its value to 2).
Apply the following rule until there are no more items with a value of 2:
- If there is a tree in position (i,j) and there is a fire in some neighboring position, change tree to fire.
- If there is fire in a position (i,j) and there is no tree in the vicinity, change fire to void.
Determine the percentage of trees that survived."
I dare say I got it working. Here’s my code:
package aula10;
import java.util.*;
public class aula10_1 {
public static void main(String[] args) {
int n, i, j;
double p;
Scanner ent = new Scanner(System.in);
ent.useLocale(Locale.US);
System.out.println("Digite a dimensão da matriz (que é quadrada):");
n = ent.nextInt();
System.out.println("Digite a probabilidade de haver uma árvore em qualquer ponto da floresta:");
p = ent.nextDouble();
int [][] F = new int[n][n];
Random rnd = new Random();
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
F[i][j] = rnd.nextInt();
if (F[i][j] < p) {
F[i][j] = 1;
} else {
F[i][j] = 0;
}
System.out.print(F[i][j]+" ");
}
}
int k = rnd.nextInt(n-1);
int l = rnd.nextInt(n-1);
while (true) {
if (F[k][l] == 1) {
F[k][l] = 2;
break;
}
}
System.out.println(Arrays.deepToString(F));
}
}
I tested it several times and went into infinite looping some. With Debug I was able to identify and correct some errors, but even so sometimes gives problem with small probabilities. I don’t know exactly what happens... My knowledge in Java is very limited, so I was satisfied with this code. But I still accept suggestions for improvement.
But then in case I should do is (i=-1; i>=0; i++) and the same for for for j? Or does this instruction enter into any if?
– Cristiane Dos Santos Costa
Create a function that receives
x, y, valorASubstituir, valorNovo
. Inside it test if x, y come out of the forest area, if they come out you do nothing but change the item of position x, y of valueSubstitute to valueNew.– Piovezan
I have not yet learned functions in the course, so I do not know if the teacher would accept a resolution using a resource that we have not learned in class. My knowledge in java is very limited and the classes are very trivial, but the exercises are monstrous (the opinion of all the students in the class). Still, thanks for your help.
– Cristiane Dos Santos Costa
In this case the way is to do without functions even. Before trying to change a position next to the current position, test with
if
s if the neighbor’s coordinates leave the forest area.– Piovezan
I think I did, after days of working on it... it’s not perfect, much less practical, but I think it works. Feel free to check if it really worked and if the code can be improved. Thank you so much for your help! :)
– Cristiane Dos Santos Costa
Post the code to look. If the answer provided was satisfactory, do not forget to accept it as shown here.
– Piovezan
I changed the code on the question itself. It is updated.
– Cristiane Dos Santos Costa
The problem is that sometimes it goes into infinite looping for some reason...
– Cristiane Dos Santos Costa
I don’t think this is how the program should work. You started well, with a
for j
within afor i
to build the forest. But you need to run this doublefor
chained to the other two steps of the program, which are to spread the fire and test if the fire has gone out. And the choice of the initial tree that will catch fire should not be made within awhile
as you did (fixing this will also avoid the infinite loop, which is due to the condition of thebreak
never occur).– Piovezan