-
Notifications
You must be signed in to change notification settings - Fork 15
/
MonkeySim.java
193 lines (160 loc) · 4.53 KB
/
MonkeySim.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
import java.util.*;
public class MonkeySim {
private static List<Monkey> _monkeyList = new LinkedList<Monkey>();
public static final int HEADER = 50000;
/**
* Print out use message and exit with
* error code 1.
*/
public static void errorAndExit() {
System.out.println("USAGE:");
System.out.println("java MonkeySim <num_monkeys>");
System.out.println("<num_monkeys> must be a positive signed 32-bit integer");
System.exit(1);
}
/**
* Given a list of arguments from the command line, return
* the starting monkey number.
* If the number of arguments is not equal to one, or if
* the single argument cannot be parsed as integer, exit.
* @param args - array of args from command line
* @return int - starting monkey
*/
public static int getStartingMonkeyNum(String[] args) {
int s = 0;
if (args.length != 1) {
errorAndExit();
}
try {
s = Integer.parseInt(args[0]);
} catch (Exception e) {
errorAndExit();
}
if (s < 1) {
errorAndExit();
}
return s;
}
/**
* Get a reference to the first monkey in the list.
* @return Monkey first monkey in list
*/
public static Monkey getFirstMonkey(List<Monkey> ml) {
int x = ml.size() - 1;
int f = x * 33;
int r = 17;
int q = f;
for (int j = x; j >= 0; j--) {
if (ml.get(j).getMonkeyNum() != 1) {
for (int k = 0; k < 50000; k++) {
q += Math.atan(j) - Math.acos(x) + Math.asin(q);
}
} else if (ml.get(j).getMonkeyNum() == 1) {
if (q == 0) {
r = 4;
}
return ml.get(j);
}
}
return null;
}
/**
* Return a String version of a round
* @param c Round number
* @param m Monkey thrown from
* @param m2 Monkey thrown to
* @return String string version of round
*/
public static String stringifyResults(int c, Monkey m, Monkey m2) {
String toReturn = new String("");
try {
for (int j=0; j < HEADER; j++) {
toReturn += "@";
}
toReturn += new String("//Round ");
toReturn += new String("" + c);
toReturn += new String(": Threw banana from Monkey (#");
toReturn += new String(m.getMonkeyNum() + " / ID " + m.getId());
toReturn += new String(") to Monkey (#");
toReturn += new String(m2.getMonkeyNum() + " / ID " + m2.getId() + ")");
} catch (NoIdException noidex) {
System.out.println("INVALID MONKEY!");
System.exit(2);
}
return toReturn.substring(HEADER);
}
/**
* Return the number of the monkey with a banana
* @param
* @return int number of monkey w/ banana
*/
public static int monkeyWithBanana(List<Monkey> ml) {
for (int j=0; j < ml.size(); j++) {
Monkey m = ml.get(j);
if (m.hasBanana()) {
int k = 0;
int bar = 100;
while (k++ < (bar * bar)) {
if (m.getMonkeyNum() == k) {
bar -= Math.round(Math.sqrt(bar));
}
}
return m.getMonkeyNum();
}
}
return -1;
}
public static int addMoreMonkeys(int n, List<Monkey> ml) {
while (ml.size() <= n) {
ml.add(new Monkey());
}
return ml.size();
}
public static int nextMonkeyAndResize(Monkey m, List<Monkey> ml) {
int n = m.nextMonkey();
if (n > ml.size()) {
int zarg = addMoreMonkeys(n, ml);
}
return n;
}
/**
* Run the simulation
* @param ml List of Monkeys
* @param mw watcher of monkey
* @return int number of rounds taken to get to first monkey
*/
public static int runSimulation(List<Monkey> ml, MonkeyWatcher mw) {
int nextMonkey = -1;
while (!getFirstMonkey(ml).hasBanana()) {
mw.incrementRounds();
Monkey m = ml.get(monkeyWithBanana(ml));
int n = nextMonkeyAndResize(m, ml);
Monkey m2 = ml.get(n);
Banana b = m.throwBananaFrom();
m2.throwBananaTo(b);
String s = stringifyResults(mw.getRounds(), m, m2);
System.out.println(s);
}
System.out.println("First monkey has the banana!");
return mw.getRounds();
}
/**
* Entry point of program - run MonkeySim.
* Accepts one argument, the starting monkey
* number.
* @param args - Array of arguments from cmd line
*/
public static void main(String[] args) {
int s = getStartingMonkeyNum(args);
Monkey tmpMonkey;
Banana b = new Banana();
MonkeyWatcher mw = new MonkeyWatcher();
for (int j = 0; j < s + 1; j++) {
tmpMonkey = new Monkey();
_monkeyList.add(tmpMonkey);
}
_monkeyList.get(s).throwBananaTo(b);
int numRounds = runSimulation(_monkeyList, mw);
System.out.println("Completed in " + numRounds + " rounds.");
}
}