-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContestant.java
More file actions
executable file
·273 lines (239 loc) · 6.36 KB
/
Copy pathContestant.java
File metadata and controls
executable file
·273 lines (239 loc) · 6.36 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
import java.util.Random;
public class Contestant implements Runnable
{
Random randomNum1 = new Random(System.currentTimeMillis());
Random randomNum = new Random(randomNum1.nextInt());
Thread myThread;
public static long time = System.currentTimeMillis();
static long startTime = System.currentTimeMillis();
protected int numberGuessed = 0;
protected boolean selected = false;
//keeps track of the amount of contestants that have selected a number
protected static int counter = 0;
//lets the contestants know if it's time for them to introduce themselves
protected static boolean introduce = false;
//keeps track of how many of the contestants have introduced themselves
protected static int introCounter = 0;
private boolean isAnswering = false;
private int score = 0;
private int wager = 0;
//keeps track of which number thread this is to wake up after answering the final jeopardy question
private static int wokeNumber = 0;
//keeps track of the number of contestants that didn't make it to final jeopardy
private static int leftNumber = 0;
//a flag for the first contestant who wakes up from the final jeopardy
private boolean first = false;
//a flag for the second contestant who wakes up from the final jeopardy
private boolean second = false;
//keeps track of how many contestants are ready for the final jeopardy question
private static int contestantsReady = 0;
//Constructor
public Contestant(String threadName)
{
myThread = new Thread(this, threadName);
myThread.start();
}
public void run()
{
//picks a random number up to 500 and announces it
numberGuessed = randomNum.nextInt(501);
//System.out.println(myThread.getName() + ": picked " + numberGuessed);
counted();
//System.out.println(myThread.getName() + " has picked the number " + numberGuessed);
//busy waits until the announcer selects the winners
while(!Announcer.getDoneSelecting())
{
boolean garbageBusyWaitVariable = true;
myThread.yield();
}
if(selected == true)
{
//sleep random time
try
{
myThread.sleep(randomNum.nextInt(5000));
} catch (InterruptedException e) {}
//busy wait
while(introduce == false)
{
boolean garbageBusyWaitVariable = true;
myThread.yield();
}
//introduce yourself
myThread.setPriority(10);
this.msg(": Hi, my name is " + myThread.getId());
introCounted();
myThread.setPriority(5);
myThread.yield();
//regular jeopardy
while(!Host.getFinalJeopardy())
{
//Busy wait until Host is done asking the question
while(!isAnswering && !Host.getFinalJeopardy())
{
boolean garbageBusyWaitVariable = true;
myThread.yield();
}
if(isAnswering)
{
this.msg("Here is my answer.");
isAnswering = false;
}
}
//final jeopardy
if(this.score > 0)
{
//increment the amount of ready contestants
incContestantsReady();
wager = randomNum.nextInt(score+1);
while(!Host.getFinalQuestion())
{
boolean garbageBusyWaitVariable = true;
myThread.yield();
}
try {
myThread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//what the contestants do upon waking after the final jeopardy question
Contestant contestants[] = Host.getContestants();
incWoke(this);
if(this.first == true)
{
for(int i = 0; i < 3; i++)
{
//loop through the contestants, if it's not you, and its alive, join with it
if(contestants[i] != this && contestants[i].getThread().isAlive())
try {
contestants[i].getThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Host.setFinalQuestionAnswered(true);
}
else if(this.second == true)
{
for(int i = 0; i < 3; i++)
{
//loop through the contestants, if it's not you, and its alive, join with it
if(contestants[i] != this && contestants[i].getThread().isAlive() && contestants[i].first == false)
try {
contestants[i].getThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//increment the score
if(randomNum.nextInt(10) < 5)
this.score+=wager;
else
this.score-=wager;
}
this.msg("Good-bye");
//keeps track of how many contestants did not participate in final jeopardy
leftNumber++;
}
}
//keeps track of how many contestants have counted
public synchronized static void counted()
{
counter++;
}
//keeps track of how many of the contestants have introduced themselves
public synchronized static void introCounted()
{
introCounter++;
}
//keeps track of which number this contestant is to wake up from the final jeopardy question
public synchronized static void incWoke(Contestant me)
{
if(getWokeNumber() == 0)
{
wokeNumber++;
me.first = true;
}
else if(getWokeNumber() == 1)
{
wokeNumber++;
me.second = true;
}
}
public synchronized static int getWokeNumber()
{
return wokeNumber;
}
public synchronized static void incContestantsReady()
{
contestantsReady++;
}
public synchronized static int getContestantsReady()
{
return contestantsReady;
}
public static int getCounter()
{
return counter;
}
public synchronized static int getIntroCounter()
{
return introCounter;
}
public int getNumberGuessed()
{
return numberGuessed;
}
public String getName()
{
return myThread.getName();
}
//sets if this contestant was selected
public void setSelected(boolean value)
{
this.selected = value;
}
public static void setIntroduce(boolean value)
{
introduce = value;
}
//decides if this contestant is answering the question
public void setIsAnswering(boolean value)
{
this.isAnswering = value;
}
public boolean getIsAnswering()
{
return isAnswering;
}
public void incrementScore(int value)
{
score+=value;
}
public void decrementScore(int value)
{
score-=value;
}
public int getScore()
{
return score;
}
//allow access to the thread
public Thread getThread()
{
return myThread;
}
//keeps track of how many contestants have left
public static int getLeftNumber()
{
return leftNumber;
}
public void msg(String m) {
System.out.println(myThread.getName()+" ["+(System.currentTimeMillis()-time)+"] "+": "); System.out.println(m);
}
protected final long age()
{
return System.currentTimeMillis() - startTime;
}
}