Browse Source

Solitaire: logic to pull and place on foundation stacks works

Demetrius Murray 6 years ago
parent
commit
d37f94d421

+ 12
- 0
pom.xml View File

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>casino</artifactId>
8
     <artifactId>casino</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
 
22
 
11
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 24
- 2
src/main/java/io/zipcoder/casino/CardGame/Card.java View File

1
 package io.zipcoder.casino.CardGame;
1
 package io.zipcoder.casino.CardGame;
2
 
2
 
3
+import java.util.Objects;
4
+
3
 public class Card {
5
 public class Card {
4
 
6
 
5
     private Suit suit;
7
     private Suit suit;
10
     public Card(Suit suit, Face face){
12
     public Card(Suit suit, Face face){
11
         this.suit = suit;
13
         this.suit = suit;
12
         this.face = face;
14
         this.face = face;
15
+        setBlack();
16
+    }
17
+
18
+    public static Card toCard(String face, String suit){
19
+        return new Card(Suit.valueOf(suit.toUpperCase()), Face.valueOf(face.toUpperCase()));
13
     }
20
     }
14
 
21
 
15
     public Suit getSuit() {
22
     public Suit getSuit() {
24
         return isBlack;
31
         return isBlack;
25
     }
32
     }
26
 
33
 
27
-    public void setBlack(boolean black) {
28
-        isBlack = black;
34
+    private void setBlack(){
35
+        if(this.getSuit().toString().equals("HEARTS") || this.getSuit().toString().equals("DIAMOND")) isBlack = false;
36
+        else isBlack = true;
29
     }
37
     }
30
 
38
 
31
     public Face getFace() {
39
     public Face getFace() {
55
     public String toString(){
63
     public String toString(){
56
         return suit + "-" + face;
64
         return suit + "-" + face;
57
     }
65
     }
66
+
67
+    @Override
68
+    public boolean equals(Object o) {
69
+        if (this == o) return true;
70
+        if (o == null || getClass() != o.getClass()) return false;
71
+        Card card = (Card) o;
72
+        return suit == card.suit &&
73
+                face == card.face;
74
+    }
75
+
76
+    @Override
77
+    public int hashCode() {
78
+        return Objects.hash(suit, face);
79
+    }
58
 }
80
 }

+ 1
- 1
src/main/java/io/zipcoder/casino/CardGame/Deck.java View File

5
 
5
 
6
 public class Deck {
6
 public class Deck {
7
 
7
 
8
-    public Stack<Card> deckOfCards = new Stack<Card>();
8
+    public Stack<Card> deckOfCards = new Stack<>();
9
 
9
 
10
     public void shuffle(){
10
     public void shuffle(){
11
         Collections.shuffle(deckOfCards);
11
         Collections.shuffle(deckOfCards);

+ 4
- 0
src/main/java/io/zipcoder/casino/CardGame/Face.java View File

21
     private Face(int value) {
21
     private Face(int value) {
22
         this.value = value;
22
         this.value = value;
23
     }
23
     }
24
+
25
+    public int getValue(){
26
+        return value;
27
+    }
24
 }
28
 }

+ 52
- 29
src/main/java/io/zipcoder/casino/CardGame/Solitaire/Solitaire.java View File

2
 
2
 
3
 import io.zipcoder.casino.CardGame.Card;
3
 import io.zipcoder.casino.CardGame.Card;
4
 import io.zipcoder.casino.CardGame.CardGame;
4
 import io.zipcoder.casino.CardGame.CardGame;
5
+import io.zipcoder.casino.CardGame.Deck;
5
 import io.zipcoder.casino.Player;
6
 import io.zipcoder.casino.Player;
6
-//
7
-//public class Solitaire extends CardGame {
8
-//
9
-//    private Player player;
10
-//    private Tableau tableau;
11
-//    private Foundation foundation;
12
-//    private java.util.Stack wastepile;
13
-//
14
-//
15
-//    public Solitaire(Player player) {
16
-//        this.player = player;
17
-//    }
18
-//
19
-//    public void moveable() {
20
-//    }
21
-//
22
-//    public void receivable() {
23
-//    }
24
-//
25
-//    public Card draw() {
26
-//        return null;
27
-//    }
28
-//
29
-//    public void flip() {
30
-//        // shoulder technically be card.setCovered(false); or something like that
31
-//        boolean covered = false;
32
-//    }
33
-//
34
-//}
7
+
8
+public class Solitaire extends CardGame {
9
+
10
+    private Player player;
11
+    private Tableau tableau;
12
+    private Foundation foundation;
13
+    private java.util.Stack wastepile;
14
+
15
+    public static Deck solitaireDeck = new Deck();
16
+
17
+
18
+    public Solitaire(Player player) {
19
+        this.player = player;
20
+    }
21
+
22
+    public void start(){
23
+
24
+    }
25
+
26
+    public void end() {
27
+
28
+    }
29
+
30
+    public void takeATurn() {
31
+
32
+    }
33
+
34
+    public void addPlayer() {
35
+
36
+    }
37
+
38
+    public void removePlayer() {
39
+
40
+    }
41
+
42
+    public void moveable() {
43
+    }
44
+
45
+    public void receivable() {
46
+    }
47
+
48
+    public Card draw() {
49
+        return solitaireDeck.draw();
50
+    }
51
+
52
+    public void flip() {
53
+        // shoulder technically be card.setCovered(false); or something like that
54
+        boolean covered = false;
55
+    }
56
+
57
+}

+ 48
- 2
src/main/java/io/zipcoder/casino/CardGame/Solitaire/Tableau.java View File

6
 
6
 
7
 public class Tableau {
7
 public class Tableau {
8
 
8
 
9
-    private Stack stack;
9
+    //consider making an undo method
10
+
11
+    public Stack<Card> stack;
12
+    public static Stack<Card> tempStack;
10
 
13
 
11
     public Tableau(){
14
     public Tableau(){
12
-        this.stack = new Stack<Card>();
15
+        this.stack = new Stack<>();
16
+        this.tempStack = new Stack<>();
13
     }
17
     }
14
 
18
 
19
+    public Integer size() { return stack.size(); }
20
+
21
+    public void add(Card c){
22
+        stack.push(c);
23
+    }
24
+
25
+    //need to preface with a find stack method. maybe at higher level that feeds into this.stack
26
+    // - so that player doesn't have to select the stack, he can just type in card to place onto.
27
+
28
+    //c = the top card of the subStack you want to pull - ex. 6D, 5C, 4H, 3S, 2D, AS; if pulling 4H and down, c = 4H.
29
+    public Stack<Card> pull(Card c){
30
+        if(stack.contains(c)){
31
+            while(!stack.peek().equals(c))  tempStack.push(stack.pop());
32
+            tempStack.push(stack.pop());
33
+            }
34
+        return tempStack;
35
+        }
36
+
37
+    //does not need parameter. with a stack representing each column, will simply call 'stack'.place() to drop the tempStack on top of it.
38
+    public void place(){
39
+        if (this.canReceive(tempStack.peek())){
40
+//            for(Card c : tempStack){
41
+//                tempStack.pop();
42
+//                stack.push(c);
43
+//            }
44
+            while(tempStack.iterator().hasNext()){
45
+                stack.push(tempStack.pop());
46
+            }
47
+//           tempStack.forEach(e -> stack.push(tempStack.pop()));
48
+        }
49
+    }
50
+
51
+    //checks whether 'top' card of stack is opposite color and 1 above passed card
52
+    private boolean canReceive(Card c) {
53
+        if ((this.stack.peek().getFace().getValue() - 1) == c.getFace().getValue()
54
+                && (this.stack.peek().isBlack() != c.isBlack())) {
55
+            return true;
56
+        } else {
57
+            System.out.println("Can't match " + stack.peek().toString() + " and " + c.toString());
58
+            return false;
59
+        }
60
+    }
15
 }
61
 }

+ 1
- 0
src/main/java/io/zipcoder/casino/CardGame/Suit.java View File

8
 
8
 
9
     private Suit() {
9
     private Suit() {
10
     }
10
     }
11
+
11
 }
12
 }

+ 8
- 0
src/test/java/io/zipcoder/casino/CardGame/DeckTest.java View File

18
     }
18
     }
19
 
19
 
20
     @Test
20
     @Test
21
+    public void testDraw() {
22
+        String actual = deck.draw().toString();
23
+        String expected = "CLUBS-KING";
24
+
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+
28
+    @Test
21
     public void testShuffle() {
29
     public void testShuffle() {
22
         deck.shuffle();
30
         deck.shuffle();
23
         String actual = deck.deckOfCards.get(0).toString();
31
         String actual = deck.deckOfCards.get(0).toString();

+ 83
- 0
src/test/java/io/zipcoder/casino/CardGame/Solitaire/TableauTest.java View File

1
+package io.zipcoder.casino.CardGame.Solitaire;
2
+
3
+import io.zipcoder.casino.CardGame.Card;
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+import sun.tools.jconsole.Tab;
7
+
8
+import static io.zipcoder.casino.CardGame.Card.toCard;
9
+import static io.zipcoder.casino.CardGame.Solitaire.Solitaire.solitaireDeck;
10
+import static io.zipcoder.casino.CardGame.Solitaire.Tableau.tempStack;
11
+
12
+public class TableauTest {
13
+
14
+    Tableau stack1 = new Tableau();
15
+    Tableau stack2 = new Tableau();
16
+
17
+    @Test
18
+    public void addTest() {
19
+        stack1.add(solitaireDeck.draw());
20
+        stack1.add(solitaireDeck.draw());
21
+        stack1.add(solitaireDeck.draw());
22
+        stack1.add(solitaireDeck.draw());
23
+
24
+        Integer actual = stack1.size();
25
+        Integer expected = 4;
26
+
27
+        for(Card c : stack1.stack) System.out.println(c.toString());
28
+        Assert.assertEquals(expected,actual);
29
+    }
30
+
31
+    @Test
32
+    public void pullTest() {
33
+        stack1.add(solitaireDeck.draw());
34
+        stack1.add(solitaireDeck.draw());
35
+        stack1.add(solitaireDeck.draw());
36
+        stack1.add(solitaireDeck.draw());
37
+
38
+        stack1.pull(toCard("Queen","Clubs"));
39
+        Integer actual = tempStack.size();
40
+        Integer expected = 3;
41
+
42
+        for(Card c : stack1.stack) System.out.println(c.toString());
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+
46
+    @Test
47
+    public void placeTest() {
48
+        stack1.add(solitaireDeck.draw());
49
+        stack1.add(solitaireDeck.draw());
50
+        stack1.add(solitaireDeck.draw());
51
+        stack1.add(solitaireDeck.draw());
52
+
53
+        stack2.add(toCard("King","Hearts"));
54
+        stack1.pull(toCard("Queen","Clubs"));
55
+        stack2.place();
56
+        Integer actual = stack2.size();
57
+        Integer expected = 4;
58
+
59
+        for(Card c : stack2.stack) System.out.println("Stack 2 " + c.toString());
60
+        for(Card c : stack1.stack) System.out.println("Stack 1 " + c.toString());
61
+        for(Card c : tempStack) System.out.println("tempStack " + c.toString());
62
+        Assert.assertEquals(expected,actual);
63
+    }
64
+
65
+    @Test
66
+    public void placeTest2() {
67
+        stack1.add(solitaireDeck.draw());
68
+        stack1.add(solitaireDeck.draw());
69
+        stack1.add(solitaireDeck.draw());
70
+        stack1.add(solitaireDeck.draw());
71
+
72
+        stack2.add(toCard("King","Hearts"));
73
+        stack1.pull(toCard("JACK","Clubs"));
74
+        stack2.place();
75
+        Integer actual = stack2.size();
76
+        Integer expected = 1;
77
+
78
+        for(Card c : stack2.stack) System.out.println("Stack 2 " + c.toString());
79
+        for(Card c : stack1.stack) System.out.println("Stack 1 " + c.toString());
80
+        for(Card c : tempStack) System.out.println("tempStack " + c.toString());
81
+        Assert.assertEquals(expected,actual);
82
+    }
83
+}