-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelAndUser.java
More file actions
290 lines (263 loc) · 10.5 KB
/
LevelAndUser.java
File metadata and controls
290 lines (263 loc) · 10.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//To use arraylists
import java.util.*;
//To use GUI items
import java.awt.*;
//To use action lsitener
import java.awt.event.*;
//Import items for file saving and loading
import java.io.*;
//class to hold level related variables
public class LevelAndUser implements ActionListener{
//Custom or main level?
boolean custom;
//The level number from a list of levels(not used if custom level)
int levelNo;
//The name of the level
String levelName;
//The enemy level up frequency
int enemyFrequency;
//The arraylist containing the amount of enemies per round
ArrayList<int[]> c = new ArrayList<int[]>();
//The arraylist containing allies
ArrayList<Integer> a = new ArrayList<Integer>();
//ally Level ups
ArrayList<int[]> lvlup = new ArrayList<int[]>();
//The current round and final round
int round, maxRound;
//The difficulty of the level
int difficulty;
//The user number in the list of records and the name
int userNo;
String username;
String customFilename, mainFilename;
//ITems to provide an intrrface for the user to ype his name in
TextField txtName = new TextField("No name");
Button back = new Button("Back");
Button next = new Button("Next");
public LevelAndUser(int widthLimit, int heightLimit, String customFilename2, String mainFilename2){
//Set the positions of items
txtName.setBounds(100, heightLimit/2, widthLimit-200, 30);
back.setBounds(3, heightLimit-40, 100, 40);
back.addActionListener(this);
next.setBounds(widthLimit-100, heightLimit-40, 100, 40);
next.addActionListener(this);
hideAll();
customFilename = customFilename2;
mainFilename = mainFilename2;
}
void showItems(){
txtName.setVisible(true);
back.setVisible(true);
next.setVisible(true);
}
void hideAll(){
txtName.setVisible(false);
back.setVisible(false);
next.setVisible(false);
}
//Print all the variales, for testing purposes
void printAll(){
System.out.println(custom);
System.out.println(levelNo);
System.out.println(levelName);
System.out.println(enemyFrequency);
System.out.println(round);
System.out.println(maxRound);
System.out.println(difficulty);
System.out.println(userNo);
System.out.println(username);
for(int i = 0; i < c.size(); i++){
for(int x = 0; x < c.get(i).length; x++){
System.out.println(c.get(i)[x]);
}
}
System.out.println("------");
for(int i = 0; i < a.size(); i++){
System.out.println(a.get(i));
}
System.out.println("-------");
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
System.out.println(lvlup.get(i7)[i6]);
}
}
}
void saveUser(){
//If the user is new or overwriting a previously saved user
boolean newUser = false;
try{
if(custom == false){
ArrayList<String> y = new ArrayList<String>();
BufferedReader f = new BufferedReader(new FileReader(mainFilename));
//read number of users
int z;
try{
z = Integer.parseInt(f.readLine());
}catch(Exception ex){
z = 0;
}
for(int i = 0; i < z; i++){
//read user name
y.add(f.readLine());
//Read difficulty
y.add(f.readLine());
//Read user round
y.add(f.readLine());
//Read Allies
for(int i2 = 0; i2 < 3; i2++){
y.add(f.readLine());
}
//Read abilities
for(int i2 = 0; i2 < 3; i2++){
for(int i6 = 0; i6 < 6; i6++){
y.add(f.readLine());
}
}
}
f.close();
int x = 0;
FileWriter f2 = new FileWriter(mainFilename);
if(userNo > z){ z++; newUser = true;}
f2.write(z +"\r\n");
for(int i = 0; i < z; i++){
if(userNo == i+1){
//write user name
f2.write(username +"\r\n");
//write difficulty
f2.write(difficulty +"\r\n");
//write user round
f2.write(round +"\r\n");
//write Allies
for(int i2 = 0; i2 < 3; i2++){
f2.write(a.get(i2) +"\r\n");
}
//Write abilities
for(int i2 = 0; i2 < 3; i2++){
for(int i6 = 0; i6 < 6; i6++){
f2.write(lvlup.get(i2)[i6] +"\r\n");
}
}
if(newUser == false){
x += 24;
}
}else{
//write user name
f2.write(y.get(x) +"\r\n"); x++;
//write difficulty
f2.write(y.get(x) +"\r\n"); x++;
//write user round
f2.write(y.get(x) +"\r\n"); x++;
//write Allies
for(int i2 = 0; i2 < 3; i2++){
f2.write(y.get(x) +"\r\n"); x++;
}
//Read abilities
for(int i2 = 0; i2 < 3; i2++){
for(int i6 = 0; i6 < 6; i6++){
f2.write(y.get(x) +"\r\n"); x++;
}
}
}
}
f2.close();
}else{
BufferedReader f = new BufferedReader(new FileReader(customFilename));
//The number of levels
int xyz;
try{
xyz = Integer.parseInt(f.readLine());
}catch(Exception ex){
xyz = 0;
}
//z is the number of users for each level
int z[] = new int[xyz];
//y contains user records
ArrayList<String> y = new ArrayList<String>();
for(int i = 0; i < xyz; i++){
try{
z[i] = Integer.parseInt(f.readLine());
}catch(Exception ex){
z[i] = 0;
}
for(int i2 = 0; i2 < z[i]; i2++){
//read user name
y.add(f.readLine());
//Read difficulty
y.add(f.readLine());
//Read user round
y.add(f.readLine());
//Read Allies
for(int i3 = 0; i3 < 3; i3++){
y.add(f.readLine());
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
y.add(f.readLine());
}
}
}
}
f.close();
FileWriter f2 = new FileWriter(customFilename);
f2.write(xyz +"\r\n");
int x = 0;
for(int i = 0; i < xyz; i++){
if(levelNo == i && userNo > z[i]){ z[i]++; newUser = true;}
f2.write(z[i] +"\r\n");
for(int i2 = 0; i2 < z[i]; i2++){
if(levelNo == i && userNo == i2+1){
//write user name
f2.write(username +"\r\n");
//write difficulty
f2.write(difficulty +"\r\n");
//write user round
f2.write(round +"\r\n");
//write Allies
for(int i3 = 0; i3 < 3; i3++){
f2.write(a.get(i3) +"\r\n");
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f2.write(lvlup.get(i7)[i6] +"\r\n");
}
}
if(newUser == false){
x += 24;
}
}else{
//write user name
f2.write(y.get(x) +"\r\n"); x++;
//write difficulty
f2.write(y.get(x) +"\r\n"); x++;
//write user round
f2.write(y.get(x) +"\r\n"); x++;
//write Allies
for(int i3 = 0; i3 < 3; i3++){
f2.write(y.get(x) +"\r\n"); x++;
}
//Write abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f2.write(y.get(x) +"\r\n"); x++;
}
}
}
}
}
f2.close();
}
}catch(Exception e){}
}
public void paint(Graphics g, int widthLimit, int heightLimit){
g.setFont(new Font("Times New Roman", Font.BOLD, 30));
g.drawString("Enter name here:", 100, heightLimit/2 - 20);
}
public void actionPerformed(ActionEvent e){
hideAll();
if(e.getSource() == next){
username = txtName.getText();
}
}
}