Преглед изворни кода

generified some classes, sorry

NedRedmond пре 6 година
родитељ
комит
da85bc4443

+ 29
- 18
src/main/java/io/zipcoder/casino/Casino.java Прегледај датотеку

@@ -31,15 +31,24 @@ public class Casino {
31 31
     }
32 32
 
33 33
     public void enterPlayers() {
34
-      int numberOfPlayers = console.getIntegerInput("WELCOME TO EPSILON CASINO, CALLED \"ALMOST A CASINO\" ON YELP\n" +
35
-              "PLEASE ENTER A NUMBER OF PLAYERS.");
36
-      String playerNames = "";
37
-      for (int i = 1; i <= numberOfPlayers; i++) {
38
-        String nameOfPlayer = console.getStringInput("PLAYER " + i + ": WHAT IS YOUR NAME?\n");
39
-        players.addPlayer(new Player(nameOfPlayer));
40
-        playerNames += nameOfPlayer + ", ";
41
-      }
42
-      console.println(playerNames + "THANK YOU FOR JOINING US");
34
+        int numberOfPlayers = getNumberOfPlayers();
35
+        String playerNames = "";
36
+        playerNames = getPlayerNames(numberOfPlayers, playerNames);
37
+        console.println(playerNames + "THANK YOU FOR JOINING US");
38
+    }
39
+
40
+    private String getPlayerNames(int numberOfPlayers, String playerNames) {
41
+        for (int i = 1; i <= numberOfPlayers; i++) {
42
+          String nameOfPlayer = console.getStringInput("PLAYER " + i + ": WHAT IS YOUR NAME?\n");
43
+          players.addPlayer(new Player(nameOfPlayer));
44
+          playerNames += nameOfPlayer + ", ";
45
+        }
46
+        return playerNames;
47
+    }
48
+
49
+    private int getNumberOfPlayers() {
50
+        return console.getIntegerInput("WELCOME TO EPSILON CASINO, CALLED \"ALMOST A CASINO\" ON YELP\n" +
51
+                  "PLEASE ENTER A NUMBER OF PLAYERS.");
43 52
     }
44 53
 
45 54
     public void chooseTable() {
@@ -52,20 +61,22 @@ public class Casino {
52 61
       Integer number=console.getIntegerInput("") ;
53 62
        switch(number){
54 63
          case 1:
55
-              console.println("WELCOME TO CRAPS\n") ;
56
-              game = new Craps();
57
-              break;
64
+             runGame(new Craps());
65
+             break;
58 66
         case 2:
59
-              console.println("WELCOME TO BLACK JACK\n") ;
60
-              game = new BlackJack();
61
-              break;
67
+             runGame(new BlackJack());
68
+             break;
62 69
         case 3:
63
-              console.println("WELCOME TO GO FISH\n");
64
-              game = new GoFish();
65
-              break;
70
+             runGame(new GoFish());
71
+             break;
66 72
        }        
67 73
      }
68 74
 
75
+    private void runGame(Game game) {
76
+        console.println("WELCOME TO %s", game.getClassName());
77
+        this.game = game;
78
+    }
79
+
69 80
     public void confirmPlayers() {
70 81
       boolean isConfirmed = false;
71 82
       while(!isConfirmed){

+ 12
- 6
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java Прегледај датотеку

@@ -22,7 +22,7 @@ public class BlackJack extends CardGame implements Gamble {
22 22
     public BlackJack(){
23 23
         readyPlayers();
24 24
         dealCards(2);
25
-        run();
25
+        runGame();
26 26
     }
27 27
 
28 28
     public void readyPlayers() {
@@ -31,7 +31,8 @@ public class BlackJack extends CardGame implements Gamble {
31 31
         }
32 32
     }
33 33
 
34
-    public void run(){
34
+    @Override
35
+    public void runGame(){
35 36
         for(int i = 0; i < blackJackPlayers.size(); i++){
36 37
             play(blackJackPlayers.get(i),blackJackPlayers.get(i).getBet());
37 38
         }
@@ -71,7 +72,7 @@ public class BlackJack extends CardGame implements Gamble {
71 72
 
72 73
             while (getSum(currentPlayer.getHand()) < 21) {
73 74
                 if (hitOrStand.equalsIgnoreCase("H")) {
74
-                    dealCard(currentPlayer,1);
75
+                    dealCards(currentPlayer,1);
75 76
                 } else if (hitOrStand.equalsIgnoreCase("S")) {
76 77
                     break;
77 78
                 } else {
@@ -114,7 +115,7 @@ public class BlackJack extends CardGame implements Gamble {
114 115
         }
115 116
 
116 117
 }
117
-
118
+    @Override
118 119
     public void dealCards(int numberOfCards) {
119 120
 
120 121
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
@@ -125,10 +126,12 @@ public class BlackJack extends CardGame implements Gamble {
125 126
         }
126 127
     }
127 128
 
128
-    public void dealCard(BlackJackPlayer p, int numberOfCards) {
129
+    @Override
130
+    public void dealCards(Player p, int numberOfCards) {
131
+        BlackJackPlayer bjp = (BlackJackPlayer) p;
129 132
         for (int i = 0; i < numberOfCards; i++) {
130 133
             Card card = deck.removeFirst();
131
-            p.getHand().add(card);
134
+            bjp.getHand().add(card);
132 135
         }
133 136
     }
134 137
 
@@ -176,5 +179,8 @@ public class BlackJack extends CardGame implements Gamble {
176 179
         dealerHand.add(newCard);
177 180
     }
178 181
 
182
+    @Override
183
+    public void promptContinue(){};
184
+
179 185
 
180 186
 }

+ 5
- 4
src/main/java/io/zipcoder/casino/cardgames/CardGame.java Прегледај датотеку

@@ -1,21 +1,22 @@
1 1
 package io.zipcoder.casino.cardgames;
2 2
 
3
+import io.zipcoder.casino.player.BlackJackPlayer;
3 4
 import io.zipcoder.casino.utilities.containers.Deck;
4 5
 import io.zipcoder.casino.utilities.Game;
5 6
 import io.zipcoder.casino.player.Player;
6 7
 
7
-public class CardGame extends Game {
8
+public abstract class CardGame extends Game {
8 9
 
9 10
     Deck deck = new Deck();
10 11
 
11
-    void dealCards(Player player, int numberOfCards) {
12
-
13
-    }
12
+    public abstract void dealCards(Player player, int numberOfCards);
14 13
 
15 14
     public void playCard() {
16 15
 
17 16
     }
18 17
 
18
+    public abstract void dealCards(int numberOfCards);
19
+
19 20
     void revealCard() {
20 21
 
21 22
     }

+ 13
- 2
src/main/java/io/zipcoder/casino/cardgames/GoFish.java Прегледај датотеку

@@ -21,6 +21,11 @@ public class GoFish extends CardGame {
21 21
         runGame();
22 22
     }
23 23
 
24
+    @Override
25
+    public void runGame(){
26
+
27
+    }
28
+
24 29
     public void readyPlayers() {
25 30
         for (Player player : Players.getInstance().getPlayers()) {
26 31
             goFishPlayers.add(new GoFishPlayer(player));
@@ -38,11 +43,17 @@ public class GoFish extends CardGame {
38 43
     }
39 44
 
40 45
     @Override
41
-    void dealCards(Player player, int numberOfCards) {
42
-        super.dealCards(player, numberOfCards);
46
+    public void dealCards(Player player, int numberOfCards) {
43 47
         // dealPile = dealPile - numberOfCards;
44 48
     }
45 49
 
50
+    @Override
51
+    public void dealCards(int numberOfCards) {
52
+
53
+    }
54
+
55
+    @Override
56
+    public void promptContinue(){};
46 57
     //
47 58
     public void updateDrawPile() {
48 59
 

+ 7
- 3
src/main/java/io/zipcoder/casino/dicegames/Craps.java Прегледај датотеку

@@ -15,12 +15,12 @@ public class Craps extends DiceGame implements Gamble {
15 15
 
16 16
     public Craps() {
17 17
         readyPlayers();
18
-        run();
18
+        runGame();
19 19
     }
20 20
     //for test purposes
21 21
     public Craps(ArrayList<CrapsPlayer> playerz){
22 22
         crapsPlayers.addAll(playerz);
23
-        run();
23
+        runGame();
24 24
     }
25 25
 
26 26
     public void readyPlayers() {
@@ -29,7 +29,8 @@ public class Craps extends DiceGame implements Gamble {
29 29
         }
30 30
     }
31 31
 
32
-    public void run(){
32
+    @Override
33
+    public void runGame(){
33 34
         for(int i = 0; i < crapsPlayers.size(); i++){
34 35
             greetPlayer(crapsPlayers.get(i).getP());
35 36
             play(crapsPlayers.get(i));
@@ -107,4 +108,7 @@ public class Craps extends DiceGame implements Gamble {
107 108
     public void promptEnterKey(String str){
108 109
         String input = console.getStringInput("\nPress \"ENTER\" to " + str);
109 110
     }
111
+
112
+    @Override
113
+    public void promptContinue(){};
110 114
 }

+ 1
- 3
src/main/java/io/zipcoder/casino/dicegames/DiceGame.java Прегледај датотеку

@@ -3,12 +3,10 @@ package io.zipcoder.casino.dicegames;
3 3
 import io.zipcoder.casino.utilities.Dice;
4 4
 import io.zipcoder.casino.utilities.Game;
5 5
 
6
-public class DiceGame extends Game {
6
+public abstract class DiceGame extends Game {
7 7
 
8 8
     private Dice dice = new Dice(1);
9 9
 
10
-    public void rollDie() {
11
-    }
12 10
     public int rollDie(int num) {
13 11
         Dice dice = new Dice(num);
14 12
         int sum = dice.tossAndSum();

+ 1
- 1
src/main/java/io/zipcoder/casino/player/BlackJackPlayer.java Прегледај датотеку

@@ -2,7 +2,7 @@ package io.zipcoder.casino.player;
2 2
 
3 3
 import io.zipcoder.casino.utilities.containers.CardHand;
4 4
 
5
-public class BlackJackPlayer{
5
+public class BlackJackPlayer extends Player{
6 6
     private CardHand hand;
7 7
     Player blackJackPlayer;
8 8
     private long bet;

+ 1
- 1
src/main/java/io/zipcoder/casino/player/CrapsPlayer.java Прегледај датотеку

@@ -1,6 +1,6 @@
1 1
 package io.zipcoder.casino.player;
2 2
 
3
-public class CrapsPlayer  {
3
+public class CrapsPlayer extends Player {
4 4
     Player crapsPlayer;
5 5
     long bet;
6 6
 

+ 1
- 1
src/main/java/io/zipcoder/casino/player/GoFishPlayer.java Прегледај датотеку

@@ -2,7 +2,7 @@ package io.zipcoder.casino.player;
2 2
 
3 3
 import io.zipcoder.casino.utilities.containers.CardHand;
4 4
 
5
-public class GoFishPlayer {
5
+public class GoFishPlayer extends Player{
6 6
     private CardHand hand;
7 7
 
8 8
     Player goFishPlayer;

+ 2
- 0
src/main/java/io/zipcoder/casino/utilities/Console.java Прегледај датотеку

@@ -136,6 +136,8 @@ public class Console {
136 136
         double userInput = scanner.nextDouble() ;
137 137
         return userInput;
138 138
     }
139
+
140
+
139 141
 //
140 142
 //    public Integer getPlayersInput(String prompt){
141 143
 //        Scanner scanner = new Scanner(System.in);

+ 6
- 4
src/main/java/io/zipcoder/casino/utilities/Game.java Прегледај датотеку

@@ -1,11 +1,13 @@
1 1
 package io.zipcoder.casino.utilities;
2 2
 
3 3
 public abstract class Game {
4
-    abstract void promptContinue();
4
+    String className = this.getClass().getName();
5 5
 
6
-    abstract void runGame();
7
-
8
-    abstract void endGame();
6
+    public abstract void promptContinue();
9 7
 
8
+    public abstract void runGame();
10 9
 
10
+    public String getClassName() {
11
+        return className;
12
+    }
11 13
 }