forked from ccase/Cowboys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFightClub.java
253 lines (203 loc) · 7.51 KB
/
FightClub.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
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
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class FightClub {
ArrayList<Shooter> cowboys = new ArrayList<Shooter>();
public static void main(String[] args){
AePlayWave player = new AePlayWave("./sound_effects/theme_song.wav");
player.start();
System.out.println();
System.out.println();
System.out.print(AsciiArt.pikachus);
Scanner scanner = new Scanner(System.in);
System.out.print("Duel or tournament? (d/t/q to quit) ");
Boolean done = false;
while (!done) {
String answer = scanner.nextLine();
if (answer.equals("d")) {
String cowboy1;
String cowboy2;
boolean show_moves;
boolean wait;
int number_of_duels;
System.out.print("Cowboy 1? ");
cowboy1 = scanner.nextLine();
System.out.print("Cowboy 2? ");
cowboy2 = scanner.nextLine();
System.out.print("How many duels? ");
number_of_duels = scanner.nextInt();
scanner.nextLine();
System.out.print("Show moves? (y/n) ");
if (scanner.nextLine().equals("y")) {
show_moves = true;
player.suspend();
} else {
show_moves = false;
}
FightClub duel = new FightClub(cowboy1, cowboy2);
for (int i=0; i<number_of_duels; i++) {
duel.fight(duel.cowboys.get(0), duel.cowboys.get(1), show_moves);
}
duel.printStandings();
if (show_moves) {
player = new AePlayWave("./sound_effects/theme_song.wav");
player.start();
}
System.out.println();
System.out.print("Duel or tournament? (d/t/q to quit) ");
} else if (answer.equals("t")) {
boolean should_restart_music = false;
boolean skip;
boolean show_moves;
int number_of_iterations = 1;
System.out.print("How many iterations? ");
number_of_iterations = scanner.nextInt();
scanner.nextLine();
System.out.print("Want to see the action? (y/n) ");
if (scanner.nextLine().equals("y")) {
skip = false;
show_moves = true;
} else {
skip = true;
show_moves = false;
}
FightClub fc = new FightClub("Tournament_Cowboys.txt");
int number_of_cowboys = fc.cowboys.size();
for (int n=0; n<number_of_iterations; n++) {
for (int i=0; i<number_of_cowboys; i++) {
for (int j=0; j<number_of_cowboys; j++) {
if (i != j) {
if (!skip) {
System.out.println(fc.cowboys.get(i).toS() + " vs " + fc.cowboys.get(j).toS());
System.out.println("Type f to fight! (s to skip, a to skip all) ");
boolean isDone = false;
while (!isDone) {
String fight_or_skip = scanner.nextLine();
if (fight_or_skip.equals("f")) {
player.suspend();
should_restart_music = true;
show_moves = true;
isDone = true;
} else if (fight_or_skip.equals("s")) {
show_moves = false;
isDone = true;
} else if (fight_or_skip.equals("a")) {
show_moves = false;
skip = true;
isDone = true;
}
}
isDone = false;
} else {
show_moves = false;
}
fc.fight(fc.cowboys.get(i),fc.cowboys.get(j),show_moves);
}
}
}
}
if (should_restart_music) {
player = new AePlayWave("./sound_effects/theme_song.wav");
player.start();
}
fc.printStandings();
System.out.print("Duel or tournament? (d/t/q to quit) ");
} else if (answer.equals("q")) {
done = true;
System.out.println();
System.out.println();
System.out.println(AsciiArt.owls);
player.stop();
} else {
System.out.print("? ");
}
}
}
public FightClub(String a, String b) {
try {
Class shooter1 = Class.forName(a);
Class shooter2 = Class.forName(b);
cowboys.add((Shooter) shooter1.newInstance());
cowboys.add((Shooter) shooter2.newInstance());
} catch (ClassNotFoundException ex) {
System.err.println(ex + " Interpreter class must be in class path.");
} catch(InstantiationException ex) {
System.err.println(ex + " Interpreter class must be concrete.");
} catch(IllegalAccessException ex) {
System.err.println(ex + " Interpreter class must have a no-arg constructor.");
}
}
public FightClub(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = reader.readLine()) != null) {
Class shooter = Class.forName(line);
cowboys.add((Shooter) shooter.newInstance());
}
} catch (FileNotFoundException ex) {
System.err.println(ex + " File not found.");
} catch (IOException ex) {
System.err.println(ex + " IO problem.");
} catch (ClassNotFoundException ex) {
System.err.println(ex + " Interpreter class must be in class path.");
} catch(InstantiationException ex) {
System.err.println(ex + " Interpreter class must be concrete.");
} catch(IllegalAccessException ex) {
System.err.println(ex + " Interpreter class must have a no-arg constructor.");
}
}
public void printStandings() {
int number_of_cowboys = cowboys.size();
ArrayList<Shooter> sortedCowboys = new ArrayList<Shooter>();
for (int i=0; i<number_of_cowboys; i++) {
int index = 0;
for (int j=0; j<sortedCowboys.size(); j++) {
if (cowboys.get(i).getPoints() < sortedCowboys.get(j).getPoints()) {
index = j+1;
}
}
sortedCowboys.add(index, cowboys.get(i));
}
System.out.printf("%15s %15s %15s %15s %15s %n", "Cowboy", "Wins", "Losses", "Ties", "Points");
for (int i=0; i<number_of_cowboys; i++) {
Shooter s = sortedCowboys.get(i);
System.out.printf("%15s %15s %15s %15s %15s %n",
s.toS(),
s.getWins(),
s.getLosses(),
s.getTies(),
s.getPoints());
}
System.out.println();
}
public void printFighters() {
int number_of_cowboys = cowboys.size();
for (int i=0; i<number_of_cowboys; i++) {
System.out.println(cowboys.get(i).toS());
}
}
public void fight(Shooter one, Shooter two, boolean show_moves) {
Duel d = new Duel(one, two);
String matchup = one.toS() + " vs " + two.toS() + ": ";
String outcome;
String result = d.run(show_moves);
if (result.equals("A")) {
outcome = one.toS() + " wins!!!";
one.setWins(one.getWins() + 1);
two.setLosses(two.getLosses() + 1);
} else if (result.equals("B")) {
outcome = two.toS() + " wins!!!";
one.setLosses(one.getLosses() + 1);
two.setWins(two.getWins() + 1);
} else {
outcome = "Tie";
one.setTies(one.getTies() + 1);
two.setTies(two.getTies() + 1);
}
one.setPoints(one.getWins()*3 + one.getTies());
two.setPoints(two.getWins()*3 + two.getTies());
System.out.printf("%-30.30s %-30.30s %-30.30s%n", matchup, outcome, "in " + d.rounds + " rounds");
System.out.println();
}
}