Procházet zdrojové kódy

tests for printer and war

Jonathan Hinds před 6 roky
rodič
revize
9d0d2b76cd

+ 14
- 2
src/main/java/io/zipcoder/casino/Casino.java Zobrazit soubor

42
     {
42
     {
43
         while(running) {
43
         while(running) {
44
 
44
 
45
-            Printer.pickGameMsg();
45
+            Printer.printMessage("Please choose a game to play!");
46
             Printer.printMessage(getGameListing());
46
             Printer.printMessage(getGameListing());
47
 
47
 
48
             int command = console.getIntFromUser("Please choose a game to play by entering the number next to the game.");
48
             int command = console.getIntFromUser("Please choose a game to play by entering the number next to the game.");
53
                     Game war = new War(10);
53
                     Game war = new War(10);
54
                     ((War) war).addPlayers(player);
54
                     ((War) war).addPlayers(player);
55
                     ((War) war).addNpc();
55
                     ((War) war).addNpc();
56
+                    game = war;
56
                     war.startGame();
57
                     war.startGame();
57
                     break;
58
                     break;
58
 
59
 
60
                     Game stud = new Stud(10);
61
                     Game stud = new Stud(10);
61
                     ((Stud) stud).addPlayers(player);
62
                     ((Stud) stud).addPlayers(player);
62
                     ((Stud) stud).addNpc();
63
                     ((Stud) stud).addNpc();
64
+                    game = stud;
63
                     stud.startGame();
65
                     stud.startGame();
64
                     break;
66
                     break;
65
 
67
 
67
                     int slotBet1= console.getIntFromUser("Enter the amount you want to bet on Slot");
69
                     int slotBet1= console.getIntFromUser("Enter the amount you want to bet on Slot");
68
                     Game slot= new SlotMachine(slotBet1);
70
                     Game slot= new SlotMachine(slotBet1);
69
                     slot.startGame();
71
                     slot.startGame();
72
+                    game = slot;
70
                     ((SlotMachine) slot).payout();
73
                     ((SlotMachine) slot).payout();
71
                     break;
74
                     break;
72
 
75
 
76
                     int betAmount = console.getIntFromUser("How much would you like to bet on this game?");
79
                     int betAmount = console.getIntFromUser("How much would you like to bet on this game?");
77
                     ((Yahtzee) yahtzee).setBid(betAmount);
80
                     ((Yahtzee) yahtzee).setBid(betAmount);
78
                     ((Yahtzee) yahtzee).bet(betAmount);
81
                     ((Yahtzee) yahtzee).bet(betAmount);
82
+                    game = yahtzee;
79
                     yahtzee.startRound();
83
                     yahtzee.startRound();
80
                     Printer.printMessage("You scored " + ((Yahtzee) yahtzee).getDicePlayer().getScoreSheet().getTotalScore() + " points.");
84
                     Printer.printMessage("You scored " + ((Yahtzee) yahtzee).getDicePlayer().getScoreSheet().getTotalScore() + " points.");
81
                     ((Yahtzee) yahtzee).payout();
85
                     ((Yahtzee) yahtzee).payout();
84
 
88
 
85
                 case 5:
89
                 case 5:
86
                     running = false;
90
                     running = false;
87
-                    Printer.closeGameMsg();
91
+                    Printer.printMessage("Thanks for the money chump!");
88
                     break;
92
                     break;
89
 
93
 
90
                 default:
94
                 default:
120
     public void setPlayer(Player player) {
124
     public void setPlayer(Player player) {
121
         this.player = player;
125
         this.player = player;
122
     }
126
     }
127
+
128
+    public ArrayList<String> getGameLib() {
129
+        return gameLib;
130
+    }
131
+
132
+    public Game getGame() {
133
+        return game;
134
+    }
123
 }
135
 }

+ 10
- 1
src/main/java/io/zipcoder/casino/Console.java Zobrazit soubor

4
 import java.util.Scanner;
4
 import java.util.Scanner;
5
 
5
 
6
 public class Console {
6
 public class Console {
7
+
7
     private Scanner scanner = new Scanner(System.in);
8
     private Scanner scanner = new Scanner(System.in);
8
 
9
 
9
     Console(){
10
     Console(){
23
             int num = scanner.nextInt();
24
             int num = scanner.nextInt();
24
             return num;
25
             return num;
25
         }catch(InputMismatchException err){
26
         }catch(InputMismatchException err){
26
-            Printer.pleaseEnterNum();
27
+            Printer.printMessage("Please enter a number.");
27
             scanner.next();
28
             scanner.next();
28
         }
29
         }
29
         return -1;
30
         return -1;
42
         input.toLowerCase().trim();
43
         input.toLowerCase().trim();
43
         return input;
44
         return input;
44
     }
45
     }
46
+
47
+    public void setScanner(Scanner scanner) {
48
+        this.scanner = scanner;
49
+    }
50
+
51
+    public Scanner getScanner() {
52
+        return scanner;
53
+    }
45
 }
54
 }

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

19
 
19
 
20
         printMessage("Sorry, there is no game with that name, try one of: " + games);
20
         printMessage("Sorry, there is no game with that name, try one of: " + games);
21
     }
21
     }
22
-
23
-    public static void showCard(Player player, Card card){
24
-        printMessage(player.getName() + " shows a " + card.getName());
25
-    }
26
-
27
-    public static void pickGameMsg(){
28
-        printMessage("Please choose a game to play!");
29
-    }
30
-
31
-    public static void closeGameMsg(){
32
-        printMessage("Thanks for your money chump!");
33
-    }
34
-
35
-    public static void pleaseEnterNum(){
36
-        printMessage("Please enter a number");
37
-    }
38
-    
39
-    public static void welcomeTo(String gameName){
40
-        printMessage("Welcome to " + gameName + "!");
41
-    }
42
-
43
-    public static void printWarTurnResult(String name, String cardName, int handSize){
44
-        printMessage(name + " has played " + cardName + " and has " + handSize + " cards left.");
45
-    }
46
-
47
-    public static void playedFaceDown(String name){
48
-        printMessage(name + " has played a card face down.");
49
-    }
50
 }
22
 }

+ 1
- 1
src/main/java/io/zipcoder/casino/Stud.java Zobrazit soubor

13
     
13
     
14
     public void playCard(Player player, Card card) {
14
     public void playCard(Player player, Card card) {
15
         card.setVisibility(true);               //CARD isVISIBLE
15
         card.setVisibility(true);               //CARD isVISIBLE
16
-        Printer.showCard(player, card);         //PRINT card name to CONSOLE
16
+        Printer.printMessage(player.getName() + " shows a " + card.getName());         //PRINT card name to CONSOLE
17
     }
17
     }
18
 
18
 
19
 
19
 

+ 4
- 3
src/main/java/io/zipcoder/casino/War.java Zobrazit soubor

29
     public void playCardInHand(boolean cardFace){
29
     public void playCardInHand(boolean cardFace){
30
         Card card = getCardFromHand(cardFace);
30
         Card card = getCardFromHand(cardFace);
31
         if(cardFace) {
31
         if(cardFace) {
32
-            Printer.printWarTurnResult(super.getPlayersTurn().getPlayer().getName(), card.getName(), super.getPlayersTurn().getHand().size());
32
+            Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " has played a " + card.getName() + " and has " + super.getPlayersTurn().getHand().size() + " cards left.");
33
         } else {
33
         } else {
34
-            Printer.playedFaceDown(super.getPlayersTurn().getPlayer().getName());
34
+            Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " has played a card face down.");
35
         }
35
         }
36
     }
36
     }
37
 
37
 
114
         }
114
         }
115
     }
115
     }
116
 
116
 
117
-        Printer.welcomeTo("War");
117
+    public void startGame(){
118
+        Printer.printMessage("Welcome to War!");
118
         super.chooseStatingPlayer();
119
         super.chooseStatingPlayer();
119
         payAnte();
120
         payAnte();
120
         deal();
121
         deal();

+ 37
- 0
src/test/java/io/zipcoder/casino/ConsoleTest.java Zobrazit soubor

1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Test;
4
+
5
+import java.io.ByteArrayInputStream;
6
+import java.io.InputStream;
7
+import java.nio.ByteBuffer;
8
+import java.util.*;
9
+
10
+public class ConsoleTest {
11
+
12
+    @Test
13
+    public void testCreateAccont()
14
+    {
15
+        Console console = new Console();
16
+        String name = "Jon \n";
17
+        Integer balance = 100;
18
+        ByteBuffer b = ByteBuffer.allocate(balance);
19
+        byte[] result = b.array();
20
+
21
+
22
+        byte[] one = result;
23
+        byte[] two = name.getBytes();
24
+        byte[] combined = new byte[one.length + two.length];
25
+
26
+        System.arraycopy(one,0,combined,0,one.length);
27
+        System.arraycopy(two,0,combined,one.length,two.length);
28
+
29
+
30
+        String data = "Jon" + "\nA second line of user input.";
31
+        System.setIn(new ByteArrayInputStream(combined));
32
+
33
+        console.setScanner(new Scanner(new ByteArrayInputStream(combined)));
34
+
35
+    }
36
+
37
+}

+ 27
- 0
src/test/java/io/zipcoder/casino/PrinterTest.java Zobrazit soubor

1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.io.BufferedInputStream;
7
+import java.io.ByteArrayInputStream;
8
+import java.util.NoSuchElementException;
9
+import java.util.Scanner;
10
+
11
+public class PrinterTest {
12
+
13
+    @Test
14
+    public void testWrongGameEntered(){
15
+        Casino casino = new Casino();
16
+        Console console = new Console();
17
+
18
+        casino.getConsole().setScanner(new Scanner(new ByteArrayInputStream("6".getBytes())));
19
+        try {
20
+            casino.chooseGame();
21
+        }catch(NoSuchElementException e){
22
+
23
+        }
24
+
25
+        Assert.assertEquals(null, casino.getGame());
26
+    }
27
+}

+ 31
- 0
src/test/java/io/zipcoder/casino/WarTest.java Zobrazit soubor

1
 package io.zipcoder.casino;
1
 package io.zipcoder.casino;
2
 
2
 
3
+import org.junit.Assert;
3
 import org.junit.Test;
4
 import org.junit.Test;
4
 
5
 
5
 public class WarTest {
6
 public class WarTest {
6
 
7
 
7
     @Test
8
     @Test
8
     public void warTest01(){
9
     public void warTest01(){
10
+        War war = new War(10);
11
+        war.addNpc();
12
+        war.addNpc();
9
 
13
 
14
+        Assert.assertEquals(2, war.getPlayers().size());
15
+    }
16
+
17
+    @Test
18
+    public void playCardPileTest(){
19
+        Deck deck = new Deck();
20
+        War war = new War(10);
21
+        Player player = new Player("Jon", 100);
22
+        war.addPlayers(player);
23
+        war.chooseStatingPlayer();
24
+        war.deal();
25
+        war.getPlayers().get(0).setDiscard(deck);
26
+
27
+        war.playCardFromPile(false);
28
+
29
+        Assert.assertEquals(103, war.getPlayers().get(0).getHand().size());
30
+    }
31
+
32
+    @Test
33
+    public void playCardTest() {
34
+        War war = new War(10);
35
+        Player player = new Player("Jon", 100);
36
+        war.addPlayers(player);
37
+        war.chooseStatingPlayer();
38
+        war.playCard(false);
39
+
40
+        Assert.assertEquals(war.getLoser(), player);
10
     }
41
     }
11
 
42
 
12
 }
43
 }