Procházet zdrojové kódy

seperated casino and console

Jonathan Hinds před 6 roky
rodič
revize
c368a19d71

+ 110
- 0
src/main/java/io/zipcoder/casino/Casino.java Zobrazit soubor

@@ -1,6 +1,116 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 
4
+import java.util.ArrayList;
5
+import java.util.InputMismatchException;
6
+
4 7
 public class Casino {
8
+    public boolean running = true;
9
+    private ArrayList<String> gameLib = new ArrayList<>();
10
+    private ArrayList<Player> players = new ArrayList<>();
11
+    private Console console = new Console();
12
+    private Game game = null;
13
+    private Player player;
14
+
15
+    public Casino(){
16
+        gameLib.add("yahtzee");
17
+        gameLib.add("war");
18
+        gameLib.add("stud");
19
+        gameLib.add("slot");
20
+        gameLib.add("quit");
21
+    }
22
+
23
+    public static void main(String[] args){
24
+        Casino casino = new Casino();
25
+        //get a player
26
+        casino.setPlayer(casino.getConsole().createAccount());
27
+        //player picks game
28
+        casino.chooseGame();
29
+    }
30
+
31
+    public String getGameListing(){
32
+        int i=1;
33
+        String list = "";
34
+        for (String s: gameLib) {
35
+            list += "Enter "+i+ " for : " + s + "\n";
36
+            i++;
37
+        }
38
+        return list;
39
+    }
40
+
41
+    public void chooseGame()
42
+    {
43
+        while(running) {
44
+
45
+            Printer.pickGameMsg();
46
+            Printer.printMessage(getGameListing());
47
+
48
+            int command = console.getIntFromUser("Please choose a game to play by entering the number next to the game.");
49
+
50
+            switch (command) {
51
+
52
+                case 2:
53
+                    Game war = new War(10);
54
+                    ((War) war).addPlayers(player);
55
+                    ((War) war).addNpc();
56
+                    war.startGame();
57
+                    break;
58
+
59
+                case 3:
60
+                    Game stud = new Stud(10);
61
+                    ((Stud) stud).addPlayers(player);
62
+                    ((Stud) stud).addNpc();
63
+                    stud.startGame();
64
+                    break;
65
+
66
+                case 4:
67
+                    int slotBet1= console.getIntFromUser("Enter the amount you want to bet on Slot");
68
+                    Game slot= new SlotMachine(slotBet1);
69
+                    slot.startGame();
70
+                    ((SlotMachine) slot).payout();
71
+                    break;
72
+
73
+                case 1:
74
+                    Game yahtzee = new Yahtzee(player);
75
+                    yahtzee.startGame();
76
+                    break;
77
+
78
+                case 5:
79
+                    running = false;
80
+                    Printer.closeGameMsg();
81
+                    break;
82
+
83
+                default:
84
+                    Printer.noMatchingGameName(gameLib);
85
+                    break;
86
+            }
87
+        }
88
+    }
89
+
90
+    public Console getConsole() {
91
+        return console;
92
+    }
93
+
94
+    public int getStudPlayers() {
95
+        int numOfStudPlayers = 0;
96
+
97
+        while (numOfStudPlayers <= 1) {
98
+
99
+            numOfStudPlayers = console.getIntFromUser("How many players are there?");
100
+        }
101
+        return numOfStudPlayers;
102
+    }
103
+
104
+    public void addStudPlayers(Game game){
105
+        int numPlayers = getStudPlayers();
106
+
107
+        for(int i = 1; i < numPlayers; i ++) {
108
+            console.createAccount();
109
+            ((Stud) game).addPlayers(players.get(i));
110
+        }
111
+    }
5 112
 
113
+    public void setPlayer(Player player) {
114
+        this.player = player;
115
+    }
6 116
 }

+ 16
- 162
src/main/java/io/zipcoder/casino/Console.java Zobrazit soubor

@@ -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
 }

+ 18
- 0
src/main/java/io/zipcoder/casino/Printer.java Zobrazit soubor

@@ -35,4 +35,22 @@ public class Printer {
35 35
     public static void showCard(Player player, Card card){
36 36
         System.out.println(player.getName() + " shows a " + card.getName());
37 37
     }
38
+
39
+    public static void pickGameMsg(){
40
+        System.out.println("Please choose a game to play!");
41
+    }
42
+
43
+    public static void closeGameMsg(){
44
+        System.out.println("Thanks for your money chump!");
45
+    }
46
+
47
+    public static void printMessage(String string) {
48
+        System.out.println(string);
49
+    }
50
+
51
+    public static void pleaseEnterNum(){
52
+        System.out.println("Please enter a number");
53
+    }
54
+
55
+
38 56
 }

+ 0
- 6
src/main/java/io/zipcoder/casino/test.java Zobrazit soubor

@@ -2,10 +2,4 @@ package io.zipcoder.casino;
2 2
 
3 3
 public class test {
4 4
 
5
-    public static void main(String[] args){
6
-        Console console = new Console();
7
-        console.createAccount();
8
-        console.chooseGame();
9
-    }
10
-
11 5
 }

+ 0
- 2
src/test/java/io/zipcoder/casino/YahtzeeTest.java Zobrazit soubor

@@ -30,8 +30,6 @@ public class YahtzeeTest {
30 30
 
31 31
         //Then
32 32
         Assert.assertEquals(expected, actual);
33
-
34
-
35 33
     }
36 34
 
37 35
     @Test