瀏覽代碼

generified some classes, sorry

NedRedmond 6 年之前
父節點
當前提交
da85bc4443

+ 29
- 18
src/main/java/io/zipcoder/casino/Casino.java 查看文件

31
     }
31
     }
32
 
32
 
33
     public void enterPlayers() {
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
     public void chooseTable() {
54
     public void chooseTable() {
52
       Integer number=console.getIntegerInput("") ;
61
       Integer number=console.getIntegerInput("") ;
53
        switch(number){
62
        switch(number){
54
          case 1:
63
          case 1:
55
-              console.println("WELCOME TO CRAPS\n") ;
56
-              game = new Craps();
57
-              break;
64
+             runGame(new Craps());
65
+             break;
58
         case 2:
66
         case 2:
59
-              console.println("WELCOME TO BLACK JACK\n") ;
60
-              game = new BlackJack();
61
-              break;
67
+             runGame(new BlackJack());
68
+             break;
62
         case 3:
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
     public void confirmPlayers() {
80
     public void confirmPlayers() {
70
       boolean isConfirmed = false;
81
       boolean isConfirmed = false;
71
       while(!isConfirmed){
82
       while(!isConfirmed){

+ 12
- 6
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java 查看文件

22
     public BlackJack(){
22
     public BlackJack(){
23
         readyPlayers();
23
         readyPlayers();
24
         dealCards(2);
24
         dealCards(2);
25
-        run();
25
+        runGame();
26
     }
26
     }
27
 
27
 
28
     public void readyPlayers() {
28
     public void readyPlayers() {
31
         }
31
         }
32
     }
32
     }
33
 
33
 
34
-    public void run(){
34
+    @Override
35
+    public void runGame(){
35
         for(int i = 0; i < blackJackPlayers.size(); i++){
36
         for(int i = 0; i < blackJackPlayers.size(); i++){
36
             play(blackJackPlayers.get(i),blackJackPlayers.get(i).getBet());
37
             play(blackJackPlayers.get(i),blackJackPlayers.get(i).getBet());
37
         }
38
         }
71
 
72
 
72
             while (getSum(currentPlayer.getHand()) < 21) {
73
             while (getSum(currentPlayer.getHand()) < 21) {
73
                 if (hitOrStand.equalsIgnoreCase("H")) {
74
                 if (hitOrStand.equalsIgnoreCase("H")) {
74
-                    dealCard(currentPlayer,1);
75
+                    dealCards(currentPlayer,1);
75
                 } else if (hitOrStand.equalsIgnoreCase("S")) {
76
                 } else if (hitOrStand.equalsIgnoreCase("S")) {
76
                     break;
77
                     break;
77
                 } else {
78
                 } else {
114
         }
115
         }
115
 
116
 
116
 }
117
 }
117
-
118
+    @Override
118
     public void dealCards(int numberOfCards) {
119
     public void dealCards(int numberOfCards) {
119
 
120
 
120
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
121
         for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
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
         for (int i = 0; i < numberOfCards; i++) {
132
         for (int i = 0; i < numberOfCards; i++) {
130
             Card card = deck.removeFirst();
133
             Card card = deck.removeFirst();
131
-            p.getHand().add(card);
134
+            bjp.getHand().add(card);
132
         }
135
         }
133
     }
136
     }
134
 
137
 
176
         dealerHand.add(newCard);
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
 package io.zipcoder.casino.cardgames;
1
 package io.zipcoder.casino.cardgames;
2
 
2
 
3
+import io.zipcoder.casino.player.BlackJackPlayer;
3
 import io.zipcoder.casino.utilities.containers.Deck;
4
 import io.zipcoder.casino.utilities.containers.Deck;
4
 import io.zipcoder.casino.utilities.Game;
5
 import io.zipcoder.casino.utilities.Game;
5
 import io.zipcoder.casino.player.Player;
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
     Deck deck = new Deck();
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
     public void playCard() {
14
     public void playCard() {
16
 
15
 
17
     }
16
     }
18
 
17
 
18
+    public abstract void dealCards(int numberOfCards);
19
+
19
     void revealCard() {
20
     void revealCard() {
20
 
21
 
21
     }
22
     }

+ 13
- 2
src/main/java/io/zipcoder/casino/cardgames/GoFish.java 查看文件

21
         runGame();
21
         runGame();
22
     }
22
     }
23
 
23
 
24
+    @Override
25
+    public void runGame(){
26
+
27
+    }
28
+
24
     public void readyPlayers() {
29
     public void readyPlayers() {
25
         for (Player player : Players.getInstance().getPlayers()) {
30
         for (Player player : Players.getInstance().getPlayers()) {
26
             goFishPlayers.add(new GoFishPlayer(player));
31
             goFishPlayers.add(new GoFishPlayer(player));
38
     }
43
     }
39
 
44
 
40
     @Override
45
     @Override
41
-    void dealCards(Player player, int numberOfCards) {
42
-        super.dealCards(player, numberOfCards);
46
+    public void dealCards(Player player, int numberOfCards) {
43
         // dealPile = dealPile - numberOfCards;
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
     public void updateDrawPile() {
58
     public void updateDrawPile() {
48
 
59
 

+ 7
- 3
src/main/java/io/zipcoder/casino/dicegames/Craps.java 查看文件

15
 
15
 
16
     public Craps() {
16
     public Craps() {
17
         readyPlayers();
17
         readyPlayers();
18
-        run();
18
+        runGame();
19
     }
19
     }
20
     //for test purposes
20
     //for test purposes
21
     public Craps(ArrayList<CrapsPlayer> playerz){
21
     public Craps(ArrayList<CrapsPlayer> playerz){
22
         crapsPlayers.addAll(playerz);
22
         crapsPlayers.addAll(playerz);
23
-        run();
23
+        runGame();
24
     }
24
     }
25
 
25
 
26
     public void readyPlayers() {
26
     public void readyPlayers() {
29
         }
29
         }
30
     }
30
     }
31
 
31
 
32
-    public void run(){
32
+    @Override
33
+    public void runGame(){
33
         for(int i = 0; i < crapsPlayers.size(); i++){
34
         for(int i = 0; i < crapsPlayers.size(); i++){
34
             greetPlayer(crapsPlayers.get(i).getP());
35
             greetPlayer(crapsPlayers.get(i).getP());
35
             play(crapsPlayers.get(i));
36
             play(crapsPlayers.get(i));
107
     public void promptEnterKey(String str){
108
     public void promptEnterKey(String str){
108
         String input = console.getStringInput("\nPress \"ENTER\" to " + str);
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
 import io.zipcoder.casino.utilities.Dice;
3
 import io.zipcoder.casino.utilities.Dice;
4
 import io.zipcoder.casino.utilities.Game;
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
     private Dice dice = new Dice(1);
8
     private Dice dice = new Dice(1);
9
 
9
 
10
-    public void rollDie() {
11
-    }
12
     public int rollDie(int num) {
10
     public int rollDie(int num) {
13
         Dice dice = new Dice(num);
11
         Dice dice = new Dice(num);
14
         int sum = dice.tossAndSum();
12
         int sum = dice.tossAndSum();

+ 1
- 1
src/main/java/io/zipcoder/casino/player/BlackJackPlayer.java 查看文件

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

+ 1
- 1
src/main/java/io/zipcoder/casino/player/CrapsPlayer.java 查看文件

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

+ 1
- 1
src/main/java/io/zipcoder/casino/player/GoFishPlayer.java 查看文件

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

+ 2
- 0
src/main/java/io/zipcoder/casino/utilities/Console.java 查看文件

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

+ 6
- 4
src/main/java/io/zipcoder/casino/utilities/Game.java 查看文件

1
 package io.zipcoder.casino.utilities;
1
 package io.zipcoder.casino.utilities;
2
 
2
 
3
 public abstract class Game {
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
 }