|
@@ -5,187 +5,41 @@ import java.util.Scanner;
|
5
|
5
|
|
6
|
6
|
public class Console {
|
7
|
7
|
private Scanner scanner = new Scanner(System.in);
|
8
|
|
- private ArrayList<String> gameLib = new ArrayList<>();
|
9
|
|
- private Game game = null;
|
10
|
|
- private Player player;
|
11
|
|
- public boolean running = true;
|
12
|
|
-
|
13
|
|
- private ArrayList<Player> players = new ArrayList<>();
|
14
|
|
-
|
15
|
|
-
|
16
|
8
|
|
17
|
9
|
Console(){
|
18
|
|
- gameLib.add("yahtzee");
|
19
|
|
- gameLib.add("war");
|
20
|
|
- gameLib.add("stud");
|
21
|
|
- gameLib.add("slot");
|
22
|
|
- gameLib.add("quit");
|
23
|
|
-
|
24
|
|
- }
|
25
|
|
-
|
26
|
|
- public void createAccount()
|
27
|
|
- {
|
28
|
|
- System.out.println("Hello, what is your name?");
|
29
|
|
- String name = scanner.next();
|
30
|
|
-
|
31
|
|
- System.out.println("How much money are you bringing to the table?");
|
32
|
|
- int balance = getIntFromUser();
|
33
|
10
|
|
34
|
|
- Player player = new Player(name, balance);
|
35
|
|
- players.add(player);
|
36
|
11
|
}
|
37
|
12
|
|
38
|
|
- /**
|
39
|
|
- * Currently required to make Stud a 2 player game, could later refactor to declare 'player' in createAccount method and pass to PlayerArray
|
40
|
|
- */
|
41
|
|
-
|
42
|
|
- public void chooseGame()
|
|
13
|
+ public Player createAccount()
|
43
|
14
|
{
|
44
|
|
-
|
45
|
|
-
|
46
|
|
- while(running) {
|
47
|
|
- System.out.println("Please choose a game to play!");
|
48
|
|
- getGameIndex();
|
49
|
|
-
|
50
|
|
- int command = getIntFromUser();
|
51
|
|
-
|
52
|
|
- switch (command) {
|
53
|
|
-
|
54
|
|
-
|
55
|
|
- case 2:
|
56
|
|
- int[] warMinMax = getMinMax();
|
57
|
|
- Game war = new War(10);
|
58
|
|
- ((War) war).addPlayers(player);
|
59
|
|
- ((War) war).addNpc();
|
60
|
|
- war.startGame();
|
61
|
|
- break;
|
62
|
|
-
|
63
|
|
-
|
64
|
|
- case 3:
|
65
|
|
- int[] studMinMax = getMinMax();
|
66
|
|
- Game stud = new Stud(10);
|
67
|
|
- ((Stud) stud).addPlayers(player);
|
68
|
|
- ((Stud) stud).addNpc();
|
69
|
|
- stud.startGame();
|
70
|
|
- break;
|
71
|
|
-
|
72
|
|
- case 4:
|
73
|
|
- //call the function to get the bet amount
|
74
|
|
- int slotBet1= getSlotBet();
|
75
|
|
- // call slot machine constructor ie. create object
|
76
|
|
- Game slot= new SlotMachine(slotBet1);
|
77
|
|
- slot.startGame();
|
78
|
|
- // start game
|
79
|
|
- ((SlotMachine) slot).payout();
|
80
|
|
- break;
|
81
|
|
-
|
82
|
|
- case 1:
|
83
|
|
- Game yahtzee = new Yahtzee(player);
|
84
|
|
- yahtzee.startGame();
|
85
|
|
- break;
|
86
|
|
-
|
87
|
|
- case 5:
|
88
|
|
- System.out.println("Thanks for your money chump!");
|
89
|
|
- running = false;
|
90
|
|
- break;
|
91
|
|
-
|
92
|
|
- default:
|
93
|
|
- Printer.noMatchingGameName(gameLib);
|
94
|
|
- break;
|
95
|
|
- }
|
96
|
|
- }
|
|
15
|
+ String name = getLineFromUser("Hello, what is your name?");
|
|
16
|
+ int balance = getIntFromUser("How much money are you bringing to the table?");
|
|
17
|
+ return new Player(name, balance);
|
97
|
18
|
}
|
98
|
19
|
|
99
|
|
- public int getIntFromUser(){
|
|
20
|
+ public int getIntFromUser(String message){
|
|
21
|
+ Printer.printMessage(message);
|
100
|
22
|
try{
|
101
|
23
|
int num = scanner.nextInt();
|
102
|
24
|
return num;
|
103
|
25
|
}catch(InputMismatchException err){
|
104
|
|
- System.out.println("Please enter a number");
|
|
26
|
+ Printer.pleaseEnterNum();
|
105
|
27
|
scanner.next();
|
106
|
28
|
}
|
107
|
29
|
return -1;
|
108
|
30
|
}
|
109
|
31
|
|
110
|
|
-
|
111
|
|
- public int[] getMinMax(){
|
112
|
|
- Printer.getBet("minimum bet");
|
113
|
|
- int min = 0;
|
114
|
|
- while(min <= 0){
|
115
|
|
- min = getIntFromUser();
|
116
|
|
- if(min < 0){
|
117
|
|
- Printer.unacceptableMinBet();
|
118
|
|
- }
|
119
|
|
- }
|
120
|
|
-
|
121
|
|
- Printer.getBet("maximum bet");
|
122
|
|
- int max = 0;
|
123
|
|
- while(max < min) {
|
124
|
|
- max = getIntFromUser();
|
125
|
|
- if(max < min){
|
126
|
|
- Printer.unacceptableMaxBet(min);
|
127
|
|
- }
|
128
|
|
- }
|
129
|
|
- int[] minMax = {min, max};
|
130
|
|
- return minMax;
|
131
|
|
- }
|
132
|
|
-
|
133
|
|
- //function to get the bet amount for slot game
|
134
|
|
- public int getSlotBet(){
|
135
|
|
- System.out.println("Enter the amount you want to bet on Slot");
|
136
|
|
- int slotBet= scanner.nextInt();
|
137
|
|
- return slotBet;
|
138
|
|
-
|
139
|
|
- }
|
140
|
|
-
|
141
|
|
-
|
142
|
|
- public String getCommand() {
|
143
|
|
- String command = "";
|
|
32
|
+ public String getCMDFromUser(String msg){
|
|
33
|
+ Printer.printMessage(msg);
|
144
|
34
|
String input = scanner.next();
|
145
|
|
- input = input.toLowerCase().trim();
|
146
|
|
-
|
147
|
|
- for(String name : gameLib){
|
148
|
|
- if(input.equals(name)){
|
149
|
|
- command = name;
|
150
|
|
- break;
|
151
|
|
- }
|
152
|
|
- }
|
153
|
|
- command = command.toLowerCase().trim();
|
154
|
|
- return command;
|
|
35
|
+ input.toLowerCase().trim();
|
|
36
|
+ return input;
|
155
|
37
|
}
|
156
|
38
|
|
157
|
|
- public int getStudPlayers() {
|
158
|
|
- int numOfStudPlayers = 0;
|
159
|
|
-
|
160
|
|
- while (numOfStudPlayers <= 1) {
|
161
|
|
-
|
162
|
|
- System.out.println("How many players are there?");
|
163
|
|
- try {
|
164
|
|
- numOfStudPlayers = scanner.nextInt();
|
165
|
|
- } catch (InputMismatchException e) {
|
166
|
|
- System.out.println("Please enter a number");
|
167
|
|
- }
|
168
|
|
- }
|
169
|
|
-
|
170
|
|
- return numOfStudPlayers;
|
171
|
|
- }
|
172
|
|
-
|
173
|
|
- public void addStudPlayers(Game game){
|
174
|
|
- int numPlayers = getStudPlayers();
|
175
|
|
-
|
176
|
|
- for(int i = 1; i < numPlayers; i ++) {
|
177
|
|
- createAccount();
|
178
|
|
- ((Stud) game).addPlayers(players.get(i));
|
179
|
|
- }
|
180
|
|
- }
|
181
|
|
-
|
182
|
|
- public void getGameIndex(){
|
183
|
|
- int i=1;
|
184
|
|
-
|
185
|
|
- for (String s: gameLib) {
|
186
|
|
- System.out.println("Enter "+i+ " for : " + s );
|
187
|
|
- i++;
|
188
|
|
-
|
189
|
|
- }
|
|
39
|
+ public String getLineFromUser(String msg){
|
|
40
|
+ Printer.printMessage(msg);
|
|
41
|
+ String input = scanner.nextLine();
|
|
42
|
+ input.toLowerCase().trim();
|
|
43
|
+ return input;
|
190
|
44
|
}
|
191
|
45
|
}
|