forked from fjricci/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.java
More file actions
105 lines (88 loc) · 2.34 KB
/
Input.java
File metadata and controls
105 lines (88 loc) · 2.34 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
package monopoly;
import java.util.Scanner;
/**
* Created by fjricci on 4/7/2015.
* Gather player inputs
*/
class Input {
private final Scanner scanner;
public Input() {
scanner = new Scanner(System.in);
}
public String inputString() {
return scanner.nextLine();
}
public boolean inputBool() {
return inputDecision(new String[]{"Yes", "No"}) == 0;
}
public int inputInt() {
while (true) {
int val;
try {
val = Integer.parseInt(inputString());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
continue;
}
return val;
}
}
public int inputDecision(String[] choices) {
while (true) {
String input = inputString();
for (int i = 0; i < choices.length; i++) {
if (input.equalsIgnoreCase(choices[i]) || input.equalsIgnoreCase(choices[i].substring(0, 1)))
return i;
}
System.out.println("Please enter a valid decision.");
}
}
public Player inputPlayer(Iterable<Player> players, Player notAllowed) {
Player player = null;
do {
String name = inputString();
for (Player p : players) {
if (name.equals(p.name()))
player = p;
}
if (player == null)
System.out.println("Invalid player, please enter another name.");
else if (notAllowed != null && player.name().equals(notAllowed.name())) {
System.out.println("You may not select this player. Choose another.");
player = null;
}
} while (player == null);
return player;
}
public Dice.Roll inputRoll() {
Dice.Roll roll = new Dice.Roll();
while (true) {
try {
roll.val = Integer.parseInt(inputString());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
continue;
}
if (roll.val < 1 || roll.val > 6)
System.out.println("Please enter a valid die value.");
else
break;
}
while (true) {
int second_val;
try {
second_val = Integer.parseInt(inputString());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
continue;
}
if (second_val < 1 || second_val > 6) {
System.out.println("Please enter a valid die value.");
continue;
}
roll.is_double = (second_val == roll.val);
roll.val += second_val;
return roll;
}
}
}