-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanPlayer.java
More file actions
219 lines (190 loc) · 6.85 KB
/
HumanPlayer.java
File metadata and controls
219 lines (190 loc) · 6.85 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
package logic.utils.players;
import logic.utils.Card;
import networking.client.ClientGame;
import networking.protocol.Command;
import networking.protocol.Handler;
import java.io.*;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
public class HumanPlayer extends ClientPlayer {
private final PipedInputStream readingInput;
private final PrintWriter readingOutput;
private final AtomicBoolean needInput;
private CompletableFuture<?> inputRequest;
public HumanPlayer(String name) throws IOException {
super(name);
PipedOutputStream pipedOutputStream = new PipedOutputStream();
readingInput = new PipedInputStream(pipedOutputStream);
readingOutput = new PrintWriter(pipedOutputStream);
needInput = new AtomicBoolean(false);
}
/**
* Handles the input provided by the user.
*
* @param input The input provided by the user.
*/
public void handleInput(String input, Handler handler) {
if (needInput.get()) {
readingOutput.println(input);
readingOutput.flush();
return;
}
if (input.equalsIgnoreCase("nope") && hand.contains(Card.NOPE)) {
hand.remove(Card.NOPE);
handler.sendCommand(Command.MOVE, Card.NOPE.name());
}
}
/**
* Frees any ongoing input request by canceling it and setting the needInput flag to false.
* This method does nothing if there is no ongoing input request.
*/
private void freeRequest() {
if (inputRequest == null || inputRequest.isDone()) return;
inputRequest.cancel(true);
// free the lock created by the scanner
InputStream in = System.in;
System.setIn(new ByteArrayInputStream("\n".getBytes()));
System.setIn(in);
System.out.println();
needInput.set(false);
}
/**
* Retrieves the string representation of the player's hand of cards.
*
* @return The string representation of the player's hand of cards.
*/
public String getHandDisplay() {
StringBuilder message = new StringBuilder("Your hand:");
for (int i = 0; i < hand.size(); i++) {
message.append("\n").append(i+1).append(": ").append(hand.get(i));
}
return message.toString();
}
@Override
public CompletableFuture<Card> takeTurn() {
canPlay = true;
freeRequest();
System.out.println("Choose a card to play by number/name or \"draw\" a card");
inputRequest = CompletableFuture.supplyAsync(() -> {
needInput.set(true);
Scanner scanner = new Scanner(readingInput);
Card card = null;
while (card == null) {
System.out.print(">> ");
String input = scanner.nextLine();
if(input.equalsIgnoreCase("draw")) break;
card = getCardFromHand(input);
}
needInput.set(false);
return card;
});
return (CompletableFuture<Card>) inputRequest;
}
@Override
public CompletableFuture<Card> chooseCard() {
freeRequest();
System.out.println("Choose a card to give");
inputRequest = CompletableFuture.supplyAsync(() -> {
needInput.set(true);
Scanner scanner = new Scanner(readingInput);
Card card = null;
while (card == null) {
System.out.print(">> ");
String input = scanner.nextLine();
card = getCardFromHand(input);
}
needInput.set(false);
return card;
});
return (CompletableFuture<Card>) inputRequest;
}
/**
* Get a card from player hand either namely or by index
*
* @param input The input provided by the user, either the name of the card or the index of the card in the hand.
* @return The card if any matches null otherwise.
*/
private Card getCardFromHand(String input) {
// get card namely
String modifiedInput = input.toUpperCase().replaceAll(" ", "_");
if (
Stream.of(Card.values())
.map(Card::name)
.anyMatch(s -> s.equals(modifiedInput))
) {
Card card = Card.valueOf(modifiedInput);
if (!hand.contains(card)) return null;
return card;
}
// get card by index
try {
int index = Integer.parseInt(input) - 1;
if (hand.size() <= index || index < 0) return null;
return hand.get(index);
} catch (NumberFormatException ignore) {}
return null;
}
@Override
public CompletableFuture<String> choosePlayer(List<String> players) {
freeRequest();
System.out.println("Please choose a player that needs to make you a favor: " + String.join(", ", players));
inputRequest = CompletableFuture.supplyAsync(() -> {
needInput.set(true);
Scanner scanner = new Scanner(readingInput);
String player = null;
while (player == null) {
System.out.print(">> ");
String input = scanner.nextLine();
if (players.stream().noneMatch(p -> p.equalsIgnoreCase(input))) continue;
player = input;
}
needInput.set(false);
return player;
});
return (CompletableFuture<String>) inputRequest;
}
@Override
public CompletableFuture<Integer> choosePosition() {
freeRequest();
System.out.println("You draw an exploding kitten but you automatically defused it");
System.out.println(
"Choose a number to place the "
+ Card.EXPLODING_KITTEN.name()
+ " back at; (bottom is 0 top is "
+ ClientGame.pileSize
+ ")"
);
inputRequest = CompletableFuture.supplyAsync(() -> {
needInput.set(true);
Scanner scanner = new Scanner(readingInput);
int index = -1;
while (index == -1) {
System.out.print(">> ");
String input = scanner.nextLine();
try {
index = Integer.parseInt(input);
if (index < 0 || index > ClientGame.pileSize) index = -1;
} catch (NumberFormatException ignore) {}
}
needInput.set(false);
return index;
});
return (CompletableFuture<Integer>) inputRequest;
}
@Override
public void endTurn() {
freeRequest();
super.endTurn();
}
@Override
public void confirmMove() {
freeRequest();
}
@Override
public void stop() {
System.out.println("The game is over for you at least");
}
}