bugfixes and a few tests
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -7,6 +7,14 @@
|
||||
<groupId>com.tman</groupId>
|
||||
<artifactId>MauMau</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
||||
@@ -17,4 +17,11 @@ public class Card {
|
||||
public VALUENUM getValue() {
|
||||
return CARD_VALUE;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (CARD_TYPE == TYPENUM.JOKER){
|
||||
return CARD_TYPE.name();
|
||||
}
|
||||
return CARD_TYPE.name() + " " + CARD_VALUE.name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Dealer {
|
||||
public List<Card> getHand() {
|
||||
List<Card> deck = this.deck.getDeck();
|
||||
List<Card> hand = new ArrayList<>();
|
||||
for (int i = 0; i < 7; i++) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
hand.add(deck.get(i));
|
||||
deck.remove(i);
|
||||
}
|
||||
@@ -42,6 +42,7 @@ public class Dealer {
|
||||
public Card getInitialCard() {
|
||||
List<Card> deck = this.deck.getDeck();
|
||||
Card card = deck.get(0);
|
||||
deck.remove(0);
|
||||
this.deck.addUsedCard(card);
|
||||
return card;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import mau.mau.players.Human;
|
||||
import mau.mau.players.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Game {
|
||||
@@ -13,7 +14,8 @@ public class Game {
|
||||
private Dealer dealer;
|
||||
private Human human;
|
||||
private Referee referee = new Referee();
|
||||
private List<Player> players = new ArrayList<>();
|
||||
private List<Player> turnOrder = new ArrayList<>();
|
||||
private Boolean skipNextPlayer = false;
|
||||
|
||||
public Game() {
|
||||
setupGame();
|
||||
@@ -23,28 +25,41 @@ public class Game {
|
||||
private void setupGame() {
|
||||
dealer = new Dealer(deck);
|
||||
human = new Human(dealer);
|
||||
players.add(human);
|
||||
players.add(new Bot(dealer));
|
||||
dealer.dealCards(players);
|
||||
turnOrder.add(human);
|
||||
turnOrder.add(new Bot(dealer));
|
||||
dealer.dealCards(turnOrder);
|
||||
currentCard = dealer.getInitialCard();
|
||||
}
|
||||
|
||||
private void playGame() {
|
||||
while (!human.handIsEmpty()) {
|
||||
if (players.size() == 1) {
|
||||
System.out.println("Game has ended");
|
||||
break;
|
||||
}
|
||||
for (Player player : players) {
|
||||
while (turnOrder.size() > 1) {
|
||||
for (Player player : turnOrder) {
|
||||
if (skipNextPlayer) {
|
||||
skipNextPlayer = false;
|
||||
continue;
|
||||
}
|
||||
System.out.println("Current card is " + currentCard.getType() + " " + currentCard.getValue());
|
||||
Card card = player.getPlay(currentCard);
|
||||
if (card != null) {
|
||||
playCard(card);
|
||||
}
|
||||
if (player.getHandSize() == 0) {
|
||||
if (Referee.isValidEndCard(currentCard)) {
|
||||
System.out.println(player.getClass().getSimpleName() + " won!");
|
||||
turnOrder.remove(player);
|
||||
break;
|
||||
} else {
|
||||
System.out.println("You can't end with this card!");
|
||||
player.addCardToHand(card);
|
||||
}
|
||||
turnOrder.remove(player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(deck.getDeck().size() + " cards left in deck");
|
||||
players.removeIf(Player::handIsEmpty);
|
||||
turnOrder.removeIf(Player::handIsEmpty);
|
||||
}
|
||||
System.out.println("Game has ended");
|
||||
}
|
||||
|
||||
public void playCard(Card card) {
|
||||
@@ -52,31 +67,39 @@ public class Game {
|
||||
deck.addUsedCard(card);
|
||||
}
|
||||
|
||||
public void handleSpecialCard(Card card) {
|
||||
public void handleTurn(Card card) {
|
||||
TYPENUM cardType = card.getType();
|
||||
VALUENUM cardValue = card.getValue();
|
||||
if (cardType == TYPENUM.JOKER) {
|
||||
// Draw 5 cards
|
||||
// Choose color
|
||||
return;
|
||||
}
|
||||
switch (cardValue) {
|
||||
case TWO:
|
||||
// Draw 2 cards
|
||||
break;
|
||||
handleTwo();
|
||||
return;
|
||||
case SEVEN:
|
||||
case KING:
|
||||
// Keep turn
|
||||
break;
|
||||
return;
|
||||
case EIGHT:
|
||||
// Skip next player
|
||||
break;
|
||||
return;
|
||||
case JACK:
|
||||
// Draw 5 cards
|
||||
// Change card type
|
||||
break;
|
||||
return;
|
||||
case ACE:
|
||||
// Change direction
|
||||
break;
|
||||
Collections.reverse(turnOrder);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTwo(){
|
||||
Player player = turnOrder.get(1);
|
||||
player.addCardToHand(dealer.drawCard());
|
||||
player.addCardToHand(dealer.drawCard());
|
||||
skipNextPlayer = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mau.mau;
|
||||
|
||||
public class Referee {
|
||||
|
||||
public static boolean isValidMove(Card currentCard, Card playedCard) {
|
||||
TYPENUM currentCardType = currentCard.getType();
|
||||
VALUENUM currentCardValue = currentCard.getValue();
|
||||
@@ -11,4 +12,20 @@ public class Referee {
|
||||
|| playedCardType == currentCardType
|
||||
|| playedCardValue == currentCardValue;
|
||||
}
|
||||
|
||||
public static boolean isMauCard(Card card) {
|
||||
TYPENUM cardType = card.getType();
|
||||
VALUENUM cardValue = card.getValue();
|
||||
if (cardType == TYPENUM.JOKER) {
|
||||
return true;
|
||||
}
|
||||
return switch (cardValue) {
|
||||
case TWO, SEVEN, EIGHT, JACK, KING, ACE -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
public static boolean isValidEndCard(Card card) {
|
||||
return !isMauCard(card);
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,9 @@ public abstract class Player {
|
||||
|
||||
public Player(Dealer dealer) {
|
||||
this.dealer = dealer;
|
||||
this.hand = dealer.getHand();
|
||||
}
|
||||
|
||||
protected List<Card> getHand() {
|
||||
public List<Card> getHand() {
|
||||
return hand;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,32 @@
|
||||
package mau.mau;public class CardTest {
|
||||
package mau.mau;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class CardTest {
|
||||
|
||||
@Test
|
||||
public void isAceOfSpadesPrintedCorrectlyTest() {
|
||||
//Assign
|
||||
Card card = new Card(TYPENUM.SPADES, VALUENUM.ACE);
|
||||
|
||||
//Act
|
||||
String result = card.toString();
|
||||
|
||||
//Assert
|
||||
assertEquals("SPADES ACE", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isJokerPrintedCorrectlyTest() {
|
||||
//Assign
|
||||
Card card = new Card(TYPENUM.JOKER, VALUENUM.TWO);
|
||||
|
||||
//Act
|
||||
String result = card.toString();
|
||||
|
||||
//Assert
|
||||
assertEquals("JOKER", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,21 @@
|
||||
package mau.mau;public class DealerTest {
|
||||
package mau.mau;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DealerTest {
|
||||
|
||||
@Test
|
||||
public void isDealerAGoodShufflerTest() {
|
||||
//Assign
|
||||
Deck deck = new Deck();
|
||||
Dealer sut = new Dealer(deck);
|
||||
|
||||
//Act
|
||||
// sut.shuffleDeck();
|
||||
|
||||
//Assert
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
package mau.mau;public class DeckTest {
|
||||
package mau.mau;
|
||||
|
||||
public class DeckTest {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
Bot
|
||||
|
||||
Human
|
||||
|
||||
Player
|
||||
|
||||
~~Card~~
|
||||
|
||||
Dealer
|
||||
|
||||
Deck
|
||||
|
||||
Game
|
||||
|
||||
~~Referee~~
|
||||
|
||||
Utils
|
||||
Reference in New Issue
Block a user