-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.java
260 lines (229 loc) · 7.28 KB
/
Robot.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
252
253
254
255
256
257
258
259
260
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 7, 2014
* File: Robot.java
*******************************************************************************/
import javax.swing.*;
import java.awt.*;
/**
* Robot.java implements methods for a computer player to make menu choices and
* decisions on scoring.
**/
public class Robot extends Player
{
/**
* Constructor creates a named player.
*
* @param name of the player
*
* @param app is the main Yahtzee class
*
* @param isUsingThisApp is a boolean whether the player being instantiated
* is controlling this applet
**/
public Robot(String name, Yahtzee app, boolean isUsingThisApp)
{
super(name, app, isUsingThisApp);
}
/**
* Decide which dice to hold.
*
* @param TheDice are needed by a computer player to make a choice
**/
public void getRollChoice(Dice TheDice)
{
int[] valueCount = new int[6];
//score a large straight
if(MyScore.getScore(10) == 0 &&
MyScore.calculateScore(10, TheDice.getDieValues()) == 40)
{
TheDice.holdAll();
return;
}
//have a small straight, check whether to look for a large straight
if(MyScore.calculateScore(9, TheDice.getDieValues()) == 30)
{
if(MyScore.getScore(10) == 0)
{ //able to score a large straight
holdForOutsideStraight(TheDice);
return;
}
else if(MyScore.getScore(9) == 0)
{ //able to score a small straight and already have large straight
TheDice.holdAll();
return;
}
}
//ensure valueCount indicies are initialized to 0
for(int idx = 0; idx < 6; idx++)
valueCount[idx] = 0;
//gather a count of each die value
for(int idx = 0; idx < 5; idx++)
{
valueCount[(TheDice.getNum(idx) - 1)]++;
}
for(int idx = 0; idx < 6; idx++)
{
//score dice for yahtzee
if(valueCount[idx] == 5)
{
TheDice.holdAll();
break;
}
//hold dice if a pair exists
if(valueCount[idx] > 1)
{
chooseHolds(TheDice);
break;
}
}
}
/**
* Call method that runs when the roll button is clicked.
**/
public void simulateRollButton()
{
app.doRollBtn();
}
/**
* Enter a value given by a player for a category.
**/
public void cheatCategory(int a, int b)
{
///Computer player doesn't cheat
}
/**
* Determine which category will maximize the player's score and enter
* the score into the scoresheet.
*
* @param DieValues is a string containing the five Die values
**/
public void setCategory(int index, String DieValues)
{
int bestIndex = 0;
int bestScore = 0;
int thisScore = 0;
for(int idx = 0; idx < 13; idx++)
{
thisScore = MyScore.calculateScore(idx, DieValues);
//if this category gets the most points and has not already
//been chosen
if(thisScore > bestScore && MyScore.getScore(idx) == 0)
{
bestIndex = idx;
bestScore = thisScore;
}
//have Yahtzee
if(thisScore == 50 && idx == 11)
{
bestIndex = idx;
bestScore = thisScore;
break;
}
}
//don't enter a score if at least one point cannot be earned
if(bestScore == 0)
{
bestIndex = 13;
}
else
{
if(MyScore.setScore(bestIndex, MyScore.calculateScore(
bestIndex, DieValues)))
{
addTurn();
}
}
//set category as used for non-"no score" and no yahtzee
if(bestScore != 0 && bestScore != 50)
MyScore.usedCategory(bestIndex);
lastCategory = bestIndex;
lastScore = bestScore;
//end turn
app.nextPlayer();
}
/**
* Choose which dice to hold during a turn.
*
* Precondition: this method should only be called when it is known that a
* pair exists within the Dice.
*
* @param TheDice is the array of Die objects
**/
public void chooseHolds(Dice TheDice)
{
int numToHold;
int[] valueCount = new int[6];
//ensure valueCount indicies are initialized to 0
for(int idx = 0; idx < 6; idx++)
valueCount[idx] = 0;
//gather a count of each die value
for(int idx = 0; idx < 5; idx++)
{
valueCount[(TheDice.getNum(idx) - 1)]++;
}
//hold any two/three/four of a kind
for(int idx = 0; idx < 6; idx++)
{
//found a two/three/four of a kind
if(valueCount[idx] > 1)
{
numToHold = idx + 1;
for(int dieIdx = 0; dieIdx < 5; dieIdx++)
{
//if a Die has the value of the two/three/four of a kind
//and is not already held, hold it
if(TheDice.getNum(dieIdx) == numToHold &&
!TheDice.isDieHeld(dieIdx))
{
TheDice.holdDie(dieIdx);
}
}
}
}
}
/**
* Hold any three or more dice in consecutive order.
*
* @param TheDice is the array of Die objects
**/
public void holdForOutsideStraight(Dice TheDice)
{
int[] numToHold = new int[3];
int[] valueCount = new int[6];
TheDice.resetHolds();
//ensure valueCount indicies are initialized to 0
for(int idx = 0; idx < 6; idx++)
valueCount[idx] = 0;
//gather a count of each die value
for(int idx = 0; idx < 5; idx++)
{
valueCount[(TheDice.getNum(idx) - 1)]++;
}
for(int idx = 0; idx < 4; idx++)
{
if(valueCount[idx] > 0 && valueCount[idx + 1] > 0 &&
valueCount[idx + 2] > 0)
{//hold any three dice in consecutive order
numToHold[0] = idx + 1;
numToHold[1] = idx + 2;
numToHold[2] = idx + 3;
for(int counter = 0; counter < 3; counter++)
{
for(int dieIdx = 0; dieIdx < 5; dieIdx++)
{
if(TheDice.getNum(dieIdx) == numToHold[counter])
{
if(!TheDice.isDieHeld(dieIdx))
TheDice.holdDie(dieIdx);
dieIdx = 5; //ensure that multiple dice with the
//same value are not held
}
}
}
}
}
}
}