Add oving 4

This commit is contained in:
Andreas Omholt Olsen
2026-02-02 10:57:55 +01:00
parent 37b0f931ce
commit 7dd68c1ed8
41 changed files with 1702 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
package oving4.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardDeckTest {
private CardDeck cardDeck;
private static void checkDeck(CardDeck deck, String deckAsString) {
String[] toStrings = deckAsString.split(",");
assertEquals(toStrings.length, deck.getCardCount(),
"CardDeck does not have the correct size");
int i = 0;
for (String toString : toStrings) {
Card card = deck.getCard(i);
String cardString = String.valueOf(card.getSuit()) + card.getFace();
assertEquals(toString, cardString,
String.format("Card at position %d was incorrect. CardDeck should have been %s",
i + 1, toStrings));
i++;
}
}
@BeforeEach
public void setUp() {
cardDeck = new CardDeck(2);
}
@Test
@DisplayName("Check that CardDeck is initialized to S1,S2,H1,H2,D1,D2,C1,C2")
public void testConstructor() {
CardDeckTest.checkDeck(cardDeck, "S1,S2,H1,H2,D1,D2,C1,C2");
}
@Test
@DisplayName("Check that CardDeck is shuffled to S1,D1,S2,D2,H1,C1,H2,C2")
public void testShufflePerfectly() {
cardDeck.shufflePerfectly();
CardDeckTest.checkDeck(cardDeck, "S1,D1,S2,D2,H1,C1,H2,C2");
}
@Test
@DisplayName("Check that deal gives out the last three cards")
public void testDeal() {
CardHand hand = new CardHand();
cardDeck.deal(hand, 3);
CardDeckTest.checkDeck(cardDeck, "S1,S2,H1,H2,D1");
assertThrows(IllegalArgumentException.class, () -> {
cardDeck.deal(null, 1);
}, "Cannot deal to null");
}
}

View File

@@ -0,0 +1,75 @@
package oving4.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardHandTest {
private CardHand cardHand;
private static void checkHand(CardHand hand, String deckAsString) {
String[] toStrings = deckAsString.split(",");
assertEquals(toStrings.length, hand.getCardCount(),
"The number of cards in the hand was incorrect");
int i = 0;
for (String toString : toStrings) {
Card card = hand.getCard(i);
String cardString = String.valueOf(card.getSuit()) + card.getFace();
assertEquals(toString, cardString, String.format(
"The card at position %d was incorrect. The hand should have contained %s",
i + 1, toStrings));
i++;
}
}
@BeforeEach
public void setUp() {
cardHand = new CardHand();
}
@Test
@DisplayName("Check that CardHand is initialized to empty")
public void testConstructor() {
CardDeck deck = new CardDeck(2);
deck.deal(cardHand, 3);
CardHandTest.checkHand(cardHand, "C2,C1,D2");
}
@Test
@DisplayName("Test the addCard method")
public void testAddCard() {
CardDeck deck = new CardDeck(2);
deck.deal(cardHand, 3);
CardHandTest.checkHand(cardHand, "C2,C1,D2");
cardHand.addCard(new Card('H', 1));
CardHandTest.checkHand(cardHand, "C2,C1,D2,H1");
assertThrows(IllegalArgumentException.class, () -> {
cardHand.addCard(null);
}, "Cannot add null card");
}
@Test
@DisplayName("Test the deal and play methods")
public void testDealPlay() {
CardDeck deck = new CardDeck(2);
deck.deal(cardHand, 3);
CardHandTest.checkHand(cardHand, "C2,C1,D2");
cardHand.play(1);
CardHandTest.checkHand(cardHand, "C2,D2");
cardHand.play(0);
CardHandTest.checkHand(cardHand, "D2");
cardHand.play(0);
assertEquals(cardHand.getCardCount(), 0);
}
}

View File

@@ -0,0 +1,46 @@
package oving4.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardTest {
private static void checkCard(Card card, char suit, int face) {
assertEquals(card.getSuit(), suit);
assertEquals(card.getFace(), face);
}
@Test
@DisplayName("Check that the constructor creates Card objects with correct values")
public void testConstructor() {
CardTest.checkCard(new Card('S', 1), 'S', 1);
CardTest.checkCard(new Card('S', 13), 'S', 13);
CardTest.checkCard(new Card('H', 1), 'H', 1);
CardTest.checkCard(new Card('H', 13), 'H', 13);
CardTest.checkCard(new Card('D', 1), 'D', 1);
CardTest.checkCard(new Card('D', 13), 'D', 13);
CardTest.checkCard(new Card('C', 1), 'C', 1);
CardTest.checkCard(new Card('C', 13), 'C', 13);
assertThrows(IllegalArgumentException.class, () -> {
new Card('X', 1);
}, "Should not be able to create a card of type X");
assertThrows(IllegalArgumentException.class, () -> {
new Card('S', 0);
}, "Should not be able to create a card with value 0");
assertThrows(IllegalArgumentException.class, () -> {
new Card('C', 14);
}, "Should not be able to create a card with value 14");
}
@Test
@DisplayName("Check that toString works as expected")
public void testToString() {
assertEquals("S1", new Card('S', 1).toString());
assertEquals("H13", new Card('H', 13).toString());
}
}