-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanStrategy.java
More file actions
204 lines (179 loc) · 7.28 KB
/
HumanStrategy.java
File metadata and controls
204 lines (179 loc) · 7.28 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
/*HumanStrategy is the strategy class that the user can directly control. It is different to the other strategies
in that it has no decision making of its own, that is all done by the user's own input. Due to the fact
there is no computerised decision-making the majority of this code is input validation
*/
import java.util.*;
public class HumanStrategy extends BasicStrategy
implements Strategy {
//initialising keyboard input scanner to be used throughout the class
Scanner input = new Scanner(System.in);
@Override
public boolean cheat(Bid b, Hand h)
{
//checks if hand is unplayable when not cheating before prompting
//if unplayable automatically evaluates to true
if(!h.canPlayHand(b, h))
{
System.out.println("Hand unplayable, you must cheat");
return true;
}
System.out.println("Will you cheat this turn? (Y/N) ");
return promptYN();
}
@Override
public Bid chooseBid(Bid b, Hand h, boolean cheat)
{
boolean valid = false;
boolean finished;
ArrayList<Card> playArr = new ArrayList<>();
Card.Rank playRank;
//local cheat attribute in case the user changes their mind
boolean localCheat = cheat;
while(!valid)
{
//setting validity to true by default. Invalid entries will falsify it
valid = true;
//resetting finished in case of invalid entry so flow can break out of while loop
finished = false;
System.out.println("The previous bid was " + b.toString());
System.out.println("Cheating? " + localCheat);
System.out.println("Your hand: ");
for(int i = 0; i < h.size(); i++)
{
System.out.println("Enter " + i + " to play " + h.getHand().get(i).toString());
}
System.out.println("Enter Q to stop adding to hand");
//reset the playHand if an invalid input is detected
playArr = new ArrayList<>();
//adding cards to play hand and validating them
while(!finished)
{
char entry = input.next().charAt(0);
//set play rank - default setting is for not cheating
playRank = h.getHand().get(0).getRank();
//checks if user wanted to stop adding to hand
//checks as char type
if(entry == 'Q' || entry == 'q')
{
if(playArr.isEmpty())
{
System.out.println("Cannot make an empty bid. Please add cards to your bid");
}
else
{
break;
}
}
//validating input to see if it is a valid card
//tries converting to int that can be used as index
try
{
int entryIndex = Character.getNumericValue(entry);
h.getHand().get(entryIndex);
}
catch(Exception ex)
{
System.out.println("Invalid input: please enter a given number");
valid = false;
break;
}
//if deemed valid, initialise entryIndex so the card can be accessed in hand
int entryIndex = Character.getNumericValue(entry);
//validating input to see if it violates the rules of the game
//checking to see if all cards in play hand are the same rank
for(int i = 0; i < playArr.size(); i++)
{
Card curCard = playArr.get(i);
Card enteredCard = h.getHand().get(entryIndex);
if(curCard.getRank() != enteredCard.getRank())
{
System.out.println("Invalid input: all cards to be played must be the same rank");
valid = false;
finished = true;
}
}
//validating input to see if it's permitted by if they're cheating or not
if(localCheat)
{
Card.Rank curBid = b.getRank();
Card.Rank enteredBid = h.getHand().get(entryIndex).getRank();
if(curBid == enteredBid || curBid.next() == enteredBid)
{
//in case of invalid entry, gives the user the option to change bid
System.out.println("Invalid entry while cheating: Card rank matches current bid");
localCheat = cheat(b, h);
System.out.println("Cheating status updated. Please re-enter bid");
valid = false;
finished = true;
}
}
else
{
Card.Rank curBid = b.getRank();
Card.Rank enteredBid = h.getHand().get(entryIndex).getRank();
if(curBid != enteredBid && curBid.next() != enteredBid)
{
//in case of invalid entry, gives the user the option to change bid
System.out.println("Invalid entry when not cheating: Card rank doesnt match current bid");
localCheat = cheat(b, h);
System.out.println("Cheating status updated. Please re-enter bid");
valid = false;
finished = true;
}
}
//if still deemed valid add to playHand
if(valid)
{
System.out.println("added to hand");
playArr.add(h.getHand().get(entryIndex));
}
System.out.println(playArr.toString());
}
}
//setting playRank based on cheat
if(localCheat)
{
Card.Rank posRankLower = b.getRank();
Card.Rank posRankHigher = b.getRank().next();
playRank = getCheatRank(posRankLower, posRankHigher);
}
else
{
playRank = playArr.get(0).getRank();
}
//if deemed valid return new Bid
Hand playHand = new Hand();
playHand.addCardCollection(playArr);
return new Bid(playHand, playRank);
}
@Override
public boolean callCheat(Hand h, Bid b)
{
System.out.println("Will you call cheat? (Y/N) ");
return promptYN();
}
//new method reduces redundant code
private boolean promptYN()
{
boolean valid = false;
//boolean used to contain program in a loop until valid input entered
while(!valid)
{
//first character entered to console evaluated
char entry = input.next().charAt(0);
if(entry == 'Y' || entry == 'y')
{
valid = true;
}
else if(entry == 'N' || entry == 'n')
{
return false;
}
else
{
System.out.println("Invalid input. Please enter using Y or N");
}
}
return true;
}
}