Skip to content

Commit

Permalink
added test and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Dun committed Oct 20, 2015
1 parent a2a1244 commit ca6a4e7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
58 changes: 57 additions & 1 deletion app/src/main/java/nl/roelvandun/setgame/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ enum Filling {
}

enum Form {
DIAMONDS, WAVE, OVAL;
DIAMONDS("<>"), WAVE("~"), OVAL("O");

public final String character;

Form(String character) {
this.character = character;
}
}

@Override
Expand All @@ -52,4 +58,54 @@ public int hashCode() {
result = 31 * result + form.hashCode();
return result;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();

switch (this.color) {
case GREEN:
sb.append("green");
break;
case PURPLE:
sb.append("purple");
break;
case RED:
sb.append("red");
break;
}

sb.append(" \t ");

switch (this.filling) {
case NONE:
sb.append("[ ]");
break;
case HALF:
sb.append("[\\]");
break;
case FULL:
sb.append("[*]");
break;
}

sb.append(" \t ");

switch (this.amount) {
case ONE:
sb.append(this.form.character);
break;
case TWO:
sb.append(this.form.character);
sb.append(this.form.character);
break;
case THREE:
sb.append(this.form.character);
sb.append(this.form.character);
sb.append(this.form.character);
break;
}
return sb.toString();
}

}
4 changes: 2 additions & 2 deletions app/src/main/java/nl/roelvandun/setgame/Deck.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.roelvandun.setgame;

import java.util.Collections;
import java.util.EmptyStackException;
import java.util.Stack;

public class Deck {
Expand All @@ -12,7 +13,6 @@ public Deck() {
for (int j = 0; j < Card.Amount.values().length; j++) {
for (int k = 0; k < Card.Filling.values().length; k++) {
for (int l = 0; l < Card.Form.values().length; l++) {

Card.Color color = Card.Color.values()[i];
Card.Amount amount = Card.Amount.values()[j];
Card.Filling filling = Card.Filling.values()[k];
Expand All @@ -34,7 +34,7 @@ public void shuffle() {
Collections.shuffle(cards);
}

public Card draw() {
public Card draw() throws EmptyStackException {
return cards.pop();
}
}
13 changes: 11 additions & 2 deletions app/src/main/java/nl/roelvandun/setgame/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.roelvandun.setgame;

import java.util.EmptyStackException;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -22,7 +23,10 @@ public Game() {
public void start() {
deck.shuffle();


draw3Cards();
draw3Cards();
draw3Cards();
draw3Cards();
}

public Deck getDeck() {
Expand All @@ -33,5 +37,10 @@ public Set<Card> getTableCards() {
return tableCards;
}

public
public void draw3Cards() throws EmptyStackException {
getTableCards().add(getDeck().draw());
getTableCards().add(getDeck().draw());
getTableCards().add(getDeck().draw());
}

}
20 changes: 18 additions & 2 deletions app/src/test/java/nl/roelvandun/setgame/CardTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package nl.roelvandun.setgame;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class CardTest {

private Card card;

@Test
public void equalsWorks() throws Exception {
Card card = new Card(Card.Color.GREEN, Card.Amount.THREE, Card.Filling.FULL, Card.Form.DIAMONDS);
Card identicalCard = new Card(Card.Color.GREEN, Card.Amount.THREE, Card.Filling.FULL, Card.Form.DIAMONDS);

assertThat(card, equalTo(identicalCard));
}

@Before
public void setUp() throws Exception {
card = new Card(Card.Color.GREEN, Card.Amount.THREE, Card.Filling.FULL, Card.Form.DIAMONDS);
}

@Test
public void hashCodeWorks() throws Exception {
Card card = new Card(Card.Color.GREEN, Card.Amount.THREE, Card.Filling.FULL, Card.Form.DIAMONDS);
Card identicalCard = new Card(Card.Color.GREEN, Card.Amount.THREE, Card.Filling.FULL, Card.Form.DIAMONDS);

assertThat(card.hashCode(), equalTo(identicalCard.hashCode()));
}

@Test
public void toStringWorks() throws Exception {
Card redNoneOneOvalCard = new Card(Card.Color.RED, Card.Amount.ONE, Card.Filling.NONE, Card.Form.OVAL);
Card purpleHalfTwoWaveCard = new Card(Card.Color.PURPLE, Card.Amount.TWO, Card.Filling.HALF, Card.Form.WAVE);

assertThat(redNoneOneOvalCard.toString(), equalTo("red \t [ ] \t O"));
assertThat(purpleHalfTwoWaveCard.toString(), equalTo("purple \t [\\] \t ~~"));
assertThat(card.toString(), equalTo("green \t [*] \t <><><>"));
}
}
5 changes: 0 additions & 5 deletions app/src/test/java/nl/roelvandun/setgame/DeckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,4 @@ public void draw_reducesDeckSizeBy1() throws Exception {
assertThat(drawnCard, equalTo(topCard));
}

@Test
public void draw3_reducesDeckSizeBy3() throws Exception {


}
}
23 changes: 17 additions & 6 deletions app/src/test/java/nl/roelvandun/setgame/GameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ public void setUp() throws Exception {
}

@Test
public void gameHasADeck() throws Exception {
public void game_constructed_hasADeck() throws Exception {
assertThat(game.getDeck(), notNullValue());
}

@Test
public void drawCards_atLeast3InStack_adds3cardsToTable() throws Exception {
int initialTableCardsCount = game.getTableCards().size();

game.draw3Cards();

int afterDrawingTableCardsCount = game.getTableCards().size();

assertThat(afterDrawingTableCardsCount, equalTo(initialTableCardsCount + 3));
}

@Test(expected = Exception.class)
public void drawCards_emptyStack_throwsException() throws Exception {

}

@Test
public void start_tableHas12Cards() throws Exception {
game.start();

assertThat(game.getTableCards().size(), equalTo(12));
}

// start may only be called once

// deal 3 new cards
// - 3 new cards are added to the table && deck.length -= 3
// - when stack is empty
}

0 comments on commit ca6a4e7

Please sign in to comment.