-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.java
More file actions
executable file
·150 lines (130 loc) · 3.77 KB
/
Copy pathHost.java
File metadata and controls
executable file
·150 lines (130 loc) · 3.77 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
import java.util.Random;
public class Host implements Runnable
{
Random randomNum = new Random(System.currentTimeMillis());
Thread myThread;
public static long time = System.currentTimeMillis();
private int numRounds = 3;
private int numQuestions = 6;
private int questionValues = 200;
private double rightPercent = 0.70;
private int currentRound = 1;
private int currentQuestion = 1;
private static Contestant[] contestants;
private static boolean finalJeopardy = false;
private static boolean finalQuestion = false;
private static boolean finalQuestionAnswered = false;
public Host(String threadName)
{
//takes the array of selected contestants from the announcer
contestants = Announcer.getSelectedContestants();
myThread = new Thread(this, threadName);
myThread.start();
}
public void run()
{
int answeringContestant = 0;
//sleep
try {
myThread.sleep(1000000000);
} catch (InterruptedException e) {}
while(currentRound <= numRounds)
{
this.msg("Lets begin round " + currentRound);
currentQuestion = 1;
while(currentQuestion <= numQuestions)
{
this.msg("And now here is your question!");
try {
myThread.sleep(randomNum.nextInt(5000));
} catch (InterruptedException e) {
e.printStackTrace();
}
answeringContestant = randomNum.nextInt(3);
contestants[answeringContestant].setIsAnswering(true);
//Busy Wait until the contestant finishes answering
while(contestants[answeringContestant].getIsAnswering())
{
boolean garbageBusyWaitingVariable = true;
myThread.yield();
}
//decide if the answer was correct
if(randomNum.nextInt(100) > (rightPercent * 100))
{
this.msg(contestants[answeringContestant].getName() + ", that's correct!");
contestants[answeringContestant].incrementScore(questionValues);
}
else
{
this.msg("I'm sorry, " + contestants[answeringContestant].getName() + ", that's incorrect...");
contestants[answeringContestant].decrementScore(questionValues);
}
currentQuestion++;
}
currentRound++;
}
//final Jeopardy
finalJeopardy = true;
//busy wait until all the contestants are either out or have decided on how much to wager
while(Contestant.getContestantsReady() + Contestant.getLeftNumber() != 3)
{
boolean garbageBusyWaitingVariable = true;
myThread.yield();
}
if(Contestant.getContestantsReady() > 0)
{
this.msg("Here is the final jeopardy question!");
finalQuestion = true;
while(!finalQuestionAnswered)
{
boolean garbageBusyWaitingVariable= true;
myThread.yield();
}
}
//print the scores
this.msg("The scores are in");
for(int i = 0; i < 3; i++)
{
this.msg(contestants[i].getName() + " has a score of " + contestants[i].getScore());
}
//determine the winner
int maxScore = contestants[0].getScore();
int winnerIndex = 0;
for(int i = 1; i < 3; i++)
{
if(contestants[i].getScore() >= maxScore)
{
maxScore = contestants[i].getScore();
winnerIndex = i;
}
}
this.msg("And the winner is: " + contestants[winnerIndex].getName());
myThread.yield();
}
public void msg(String m) {
System.out.println(myThread.getName()+" ["+(System.currentTimeMillis()-time)+"] "+": "); System.out.println(m);
}
//checks if it's final Jeopardy
public static boolean getFinalJeopardy()
{
return finalJeopardy;
}
//checks to see if the final question has been asked
public static boolean getFinalQuestion()
{
return finalQuestion;
}
//lets the host know that the final jeopardy question has been answered
public static void setFinalQuestionAnswered(boolean value)
{
finalQuestionAnswered = value;
}
public static Contestant[] getContestants()
{
return contestants;
}
public void interrupt()
{
myThread.interrupt();
}
}