-
Notifications
You must be signed in to change notification settings - Fork 0
/
Announcer.java
executable file
·100 lines (88 loc) · 2.54 KB
/
Announcer.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
import java.util.Random;
public class Announcer implements Runnable
{
Random randomNum = new Random(System.currentTimeMillis());
Thread myThread;
public static long time = System.currentTimeMillis();
static long startTime = System.currentTimeMillis();
protected static boolean doneSelecting = false;
private static Contestant selectedContestants[] = new Contestant[3];
//Constructor
public Announcer(String threadName)
{
myThread = new Thread(this, threadName);
myThread.start();
}
public void run()
{
Contestant contestantArray[] = new Contestant[7];
//create 7 contestants
for(int i = 0; i < 7; i++)
{
contestantArray[i] = new Contestant("contestant" + i);
}
while(doneSelecting == false)
{
//when the contestants are done selecting numbers
if(Contestant.getCounter() == 7)
{
int max = 0;
int index = 0;
//pick the 3 winners
for(int i = 0; i < 3; i++)
{
max = 0;
for(int j = 0; j < 7; j++)
{
//find the max number guessed. makes sure you don't look in a null element of the array
if(contestantArray[j] != null && contestantArray[j].getNumberGuessed() >= max)
{
index = j;
max = contestantArray[j].getNumberGuessed();
}
}
//put the contestant with the highest number into the selected contestants array
selectedContestants[i] = contestantArray[index];
selectedContestants[i].setSelected(true);
//remove the selected contestant from the contestantArray.
contestantArray[index] = null;
this.msg(selectedContestants[i].getName() + " has been selected.");
}
for(int i = 0; i < 7; i++)
if(contestantArray[i] != null)
this.msg(contestantArray[i].getName() + " has been eliminated.");
doneSelecting = true;
}
}
//start the show
this.msg("Announcer: Ok contestants, it's time to intoduce yourselves!");
//create the Host
Host theHost = new Host("theHost");
Contestant.setIntroduce(true);
while(true)
{
if(Contestant.getIntroCounter() == 3)
{
this.msg("Announcer: Ok, now it's time to play JEOPARDY!");
theHost.interrupt();
break;
}
myThread.yield();
}
}
public void msg(String m) {
System.out.println(myThread.getName()+" ["+(System.currentTimeMillis()-time)+"] "+": "); System.out.println(m);
}
public static Contestant[] getSelectedContestants()
{
return selectedContestants;
}
protected static final long age()
{
return System.currentTimeMillis() - startTime;
}
public static boolean getDoneSelecting()
{
return doneSelecting;
}
}