-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTown.java
163 lines (135 loc) · 4.03 KB
/
Town.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
package isp;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
/**
* @author Ethan Gruening
*Object defining the town, filled with town cells (users) that can use power and change over time.
*/
public class Town {
private int length, width; //Row and col (first and second indices)
public TownCell[][] grid;
/**
* Constructor to be used when user wants to generate grid randomly, with the given seed.
* @param length
* The length of the town
* @param width
* The width of the town
*/
public Town(int length, int width) {
//assign variables
this.length = length;
this.width = width;
grid = new TownCell[length][width];
}
/**
* Constructor to be used when user wants to populate grid based on a file.
* @param inputFileName
* The file name for the template of the grid
* @throws FileNotFoundException
*/
public Town(String inputFileName) throws FileNotFoundException {
FileNotFoundException e = new FileNotFoundException();
//open the file for scanning
File f = new File(inputFileName);
Scanner scnr = null;
scnr = new Scanner(f);
if (!f.exists()) {
throw e;
}
//scan in the variables
length = scnr.nextInt();
width = scnr.nextInt();
grid = new TownCell[length][width];
//begin filling the grid
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
//create the new cell
TownCell newCell = null;
char c = scnr.next().charAt(0);
//scans in next word, converts to character, and makes a new cell
if (c == 'C') {
newCell = new Casual(this, i, j);
} else if (c == 'S') {
newCell = new Streamer(this, i, j);
} else if (c == 'R') {
newCell = new Reseller(this, i, j);
} else if (c == 'E') {
newCell = new Empty(this, i, j);
} else if (c == 'O') {
newCell = new Outage(this, i, j);
}
//adds cell to the grid
grid[i][j] = newCell;
}
}
//close scanner
scnr.close();
}
/**
* Returns width of the grid.
* @return
* Width of the grid
*/
public int getWidth() {
return width;
}
/**
* Returns length of the grid.
* @return
* Length of the grid
*/
public int getLength() {
return length;
}
/**
* Initialize the grid by randomly assigning cell with one of the following class object:
* Casual, Empty, Outage, Reseller OR Streamer
* @param seed
* The randomized seed used to fill the grid
*/
public void randomInit(int seed) {
Random rand = new Random(seed);
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
//make a cell and randomly assign it to a different type
TownCell newCell = null;
int randomInt = rand.nextInt(5);
if(randomInt == 0) {
newCell = new Casual(this, i, j);
} else if(randomInt == 1){
newCell = new Streamer(this, i, j);
} else if(randomInt == 2){
newCell = new Reseller(this, i, j);} else if(randomInt == 3) {
newCell = new Empty(this, i, j);
} else if(randomInt ==4) {
newCell = new Outage(this, i, j);
}
//Add it to the grid
grid[i][j] = newCell;
}
}
}
/**
* Output the town grid. For each square, output the first letter of the cell type.
* Each letter should be separated either by a single space or a tab.
* And each row should be in a new line. There should not be any extra line between
* the rows.
* @return
* Returns the town represented in a string
*/
@Override
public String toString() {
String s = "";
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
//Add a space if its not the first in the row, and if it is it will make a new line
if (j != 0) {s += ' ';} else if(i != 0){s += '\n';}
//Add the letter determined by the cell type
if(State.CASUAL == grid[i][j].who()){ s += 'C';} else if(State.EMPTY == grid[i][j].who()){s += 'E';} else if(State.OUTAGE == grid[i][j].who()){s += 'O';} else if(State.STREAMER == grid[i][j].who()){s += 'S';} else if(State.RESELLER == grid[i][j].who()){s += 'R';}
}
}
return s;
}
}