-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.java
186 lines (168 loc) · 5.01 KB
/
World.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.io.BufferedWriter;
/**
*
* @author Stephanie Engelhardt
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
/**
* @author Stephanie Engelhardt
* The world is represented as a square grid of size width X width.
*
*/
public class World {
private int width; // grid size: width X width
public Living[][] grid;
/**
* Default constructor reads from a file
*/
public World(String inputFileName) throws FileNotFoundException{
try {
File file = new File(inputFileName);
Scanner input = new Scanner(file);
int x=0;
while (input.hasNextLine()) {
int y=0;
String s = input.nextLine();
Scanner scan=new Scanner(s);
scan.useDelimiter("");
if(x==0 && y==0){
width=s.length()/3;
grid= new Living[width][width];
}
for(int z=0; z<grid.length; z++){
String c=scan.next();
if(c.equals("G")){
grid[x][y]= new Grass(this, x, y);
scan.next();
}
else if(c.equals("E")){
grid[x][y]= new Empty(this, x, y);
scan.next();
}
else if(c.equals("F"))
grid[x][y]= new Fox(this, x, y, scan.nextInt());
else if(c.equals("R"))
grid[x][y]= new Rabbit(this, x, y, scan.nextInt());
else if(c.equals("B"))
grid[x][y]= new Badger(this, x, y, scan.nextInt());
scan.next();
y++;
}
x++;
scan.close();
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Constructor that builds a w X w grid without initializing it.
* @param width the grid
*/
public World(int w){
width=w;
grid= new Living[width][width];
}
public int getWidth(){
return width;
}
/**
* Initialize the world by randomly assigning to every square of the grid
* one of BADGER, FOX, RABBIT, GRASS, or EMPTY.
*
* Every animal starts at age 0.
*/
public void randomInit(){
Random generator = new Random();
for (int x= 0; x< grid.length; x++ ) {
for ( int y= 0; y< grid[0].length; y++ ){
int next=generator.nextInt(5);
if(next==0)
grid[x][y]=new Badger(this, x, y, 0);
else if(next==1)
grid[x][y]= new Empty(this, x, y);
else if(next==2)
grid[x][y]=new Fox(this, x, y, 0);
else if(next==3)
grid[x][y]= new Grass(this, x, y);
else
grid[x][y]=new Rabbit(this, x, y, 0);
}
}
}
/**
* Output the world grid. For each square, output the first letter of the living form
* occupying the square. If the living form is an animal, then output the age of the animal
* followed by a blank space; otherwise, output two blanks.
*/
public String toString(){
String result= new String();
for (int x= 0; x< grid.length; x++ ) {
for ( int y= 0; y< grid[0].length; y++ ){
if(grid[x][y].who()==State.BADGER){
Badger b= (Badger) grid[x][y];
result+=("B"+b.myAge()+ " ");
}
else if(grid[x][y].who()==State.RABBIT){
Rabbit r= (Rabbit) grid[x][y];
result+=("R"+r.myAge()+ " ");
}
else if(grid[x][y].who()==State.FOX){
Fox f= (Fox) grid[x][y];
result+=("F"+f.myAge()+ " ");
}
else if(grid[x][y].who()==State.GRASS){
result+=("G ");
}
else{
result+=("E ");
}
}
result= result+ "\n";
}
return result;
}
/**
* Write the world grid to an output file. Also useful for saving a randomly
* generated world for debugging purpose.
* @throws FileNotFoundException
*/
public void write(String outputFileName) throws FileNotFoundException{
BufferedWriter writer = null;
try {
File file = new File(outputFileName);
writer = new BufferedWriter(new FileWriter(file));
for(int x=0; x<this.grid.length; x++){
for(int y=0; y<this.grid[0].length; y++){
if(this.grid[x][y].who()==State.BADGER)
writer.write("B"+ ((Badger) this.grid[x][y]).myAge()+ " ");
else if(this.grid[x][y].who()==State.FOX)
writer.write("F"+ ((Fox) this.grid[x][y]).myAge()+ " ");
else if(this.grid[x][y].who()==State.RABBIT)
writer.write("R"+ ((Rabbit) this.grid[x][y]).myAge()+ " ");
else if(this.grid[x][y].who()==State.EMPTY)
writer.write("E"+ " ");
else if(this.grid[x][y].who()==State.GRASS)
writer.write("G"+ " ");
}
writer.newLine();
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}