-
Notifications
You must be signed in to change notification settings - Fork 1
/
WhistController.java
82 lines (71 loc) · 2.35 KB
/
WhistController.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
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.text.Format;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
/**
* Created by mattmorgan on 2/12/16.
*/
public class WhistController implements IWhistController{
final Readable rd;
final Appendable ap;
/**
* constructor for a WhistController
* @param rd the readable input the controller takes in
* @param ap the appendable outpu the controller removes.
*/
public WhistController(Readable rd, Appendable ap) {
this.rd = rd;
this.ap = ap;
}
@Override
public void startGame(CardGameModel game, int numPlayers){
List<Card> Cards = game.getDeck();
Collections.shuffle(Cards);
startGame(game, numPlayers, Cards);
}
void startGame(CardGameModel game, int numPlayers, List<Card> deck) {
Objects.requireNonNull(game);
Scanner scan = new Scanner(this.rd);
while (!game.isGameOver() && scan.hasNext()) {
int card_index = scan.nextInt();
try {
ap.append(game.getGameState() + "\n");
game.play(game.getCurrentPlayer(), card_index);
}
catch (IllegalArgumentException e) {
try {
ap.append("Try again, that was invalid input: " + card_index +"\n");
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
ap.append(game.getGameState());
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(this.ap.toString());
}
//Collections.Shuffle(deck) <-- to shuffle a deck in tests
public void testRun(WhistModel model, Interaction... interactions) {
StringBuilder fakeUserInput = new StringBuilder();
StringBuilder expectedOutput = new StringBuilder();
for (Interaction interaction : interactions) {
interaction.apply(fakeUserInput, expectedOutput);
}
StringReader input = new StringReader(fakeUserInput.toString());
StringBuilder actualOutput = new StringBuilder();
WhistController controller = new WhistController(input, actualOutput);
controller.startGame(new WhistModel(), 2);
assertEquals(expectedOutput.toString(), actualOutput.toString());
}
}