commit e4231e903d5aeb791ad2460fc5d6e52c93888162 Author: DTieman Date: Thu Sep 15 15:15:11 2022 +0200 Initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..0ffbf21 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4160c25 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.tman + MauMau + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/mau/mau/Card.java b/src/main/java/mau/mau/Card.java new file mode 100644 index 0000000..da21066 --- /dev/null +++ b/src/main/java/mau/mau/Card.java @@ -0,0 +1,20 @@ +package mau.mau; + +public class Card { + + private final TYPENUM CARD_TYPE; + private final VALUENUM CARD_VALUE; + + public Card(TYPENUM type, VALUENUM value) { + this.CARD_TYPE = type; + this.CARD_VALUE = value; + } + + public TYPENUM getType() { + return CARD_TYPE; + } + + public VALUENUM getValue() { + return CARD_VALUE; + } +} diff --git a/src/main/java/mau/mau/Dealer.java b/src/main/java/mau/mau/Dealer.java new file mode 100644 index 0000000..5504550 --- /dev/null +++ b/src/main/java/mau/mau/Dealer.java @@ -0,0 +1,64 @@ +package mau.mau; + +import mau.mau.players.Player; + +import java.util.ArrayList; +import java.util.List; + +public class Dealer { + private Deck deck; + + public Dealer(Deck deck) { + this.deck = deck; + deck.setDeck(shuffleDeck(this.deck.getDeck())); + } + + public List shuffleDeck(List unshuffledDeck) { + List shuffledDeck = new ArrayList<>(); + while (unshuffledDeck.size() > 0) { + int randomIndex = (int) (Math.random() * unshuffledDeck.size()); + shuffledDeck.add(unshuffledDeck.get(randomIndex)); + unshuffledDeck.remove(randomIndex); + } + return shuffledDeck; + } + + public List getHand() { + List deck = this.deck.getDeck(); + List hand = new ArrayList<>(); + for (int i = 0; i < 7; i++) { + hand.add(deck.get(i)); + deck.remove(i); + } + return hand; + } + + public void dealCards(List players) { + for (Player player : players) { + player.setHand(getHand()); + } + } + + public Card getInitialCard() { + List deck = this.deck.getDeck(); + Card card = deck.get(0); + this.deck.addUsedCard(card); + return card; + } + + public List drawCards(int cardsToDraw) { + if (deck.deckIsEmpty(cardsToDraw)) { + deck.setDeck(shuffleDeck(deck.getUsedCards())); + deck.setUsedCards(new ArrayList<>()); + } + List cards = new ArrayList<>(); + for (int i = 0; i < cardsToDraw; i++) { + cards.add(deck.drawCard()); + } + return cards; + } + + public Card drawCard() { + return drawCards(1).get(0); + } +} diff --git a/src/main/java/mau/mau/Deck.java b/src/main/java/mau/mau/Deck.java new file mode 100644 index 0000000..20e9979 --- /dev/null +++ b/src/main/java/mau/mau/Deck.java @@ -0,0 +1,54 @@ +package mau.mau; + +import java.util.ArrayList; +import java.util.List; + +public class Deck { + + private List deck = new ArrayList<>(); + private List usedCards = new ArrayList<>(); + + public Deck() { + for (TYPENUM type : TYPENUM.values()) { + if(type == TYPENUM.JOKER) { + deck.add(new Card(type, VALUENUM.TWO)); + deck.add(new Card(type, VALUENUM.THREE)); + } else { + for (VALUENUM value : VALUENUM.values()) { + deck.add(new Card(type, value)); + } + } + } + } + + public List getDeck() { + return deck; + } + + public void setDeck(List deck){ + this.deck = deck; + } + + public List getUsedCards(){ + return usedCards; + } + + public void setUsedCards(List usedCards){ + this.usedCards = usedCards; + } + + public Card drawCard(){ + Card card = deck.get(0); + deck.remove(0); + usedCards.add(card); + return card; + } + + public boolean deckIsEmpty(int cardsToDraw){ + return deck.size() < cardsToDraw; + } + + public void addUsedCard(Card card){ + usedCards.add(card); + } +} diff --git a/src/main/java/mau/mau/Game.java b/src/main/java/mau/mau/Game.java new file mode 100644 index 0000000..50d59d7 --- /dev/null +++ b/src/main/java/mau/mau/Game.java @@ -0,0 +1,82 @@ +package mau.mau; + +import mau.mau.players.Bot; +import mau.mau.players.Human; +import mau.mau.players.Player; + +import java.util.ArrayList; +import java.util.List; + +public class Game { + private Card currentCard; + private Deck deck = new Deck(); + private Dealer dealer; + private Human human; + private Referee referee = new Referee(); + private List players = new ArrayList<>(); + + public Game() { + setupGame(); + playGame(); + } + + private void setupGame() { + dealer = new Dealer(deck); + human = new Human(dealer); + players.add(human); + players.add(new Bot(dealer)); + dealer.dealCards(players); + currentCard = dealer.getInitialCard(); + } + + private void playGame() { + while (!human.handIsEmpty()) { + if (players.size() == 1) { + System.out.println("Game has ended"); + break; + } + for (Player player : players) { + System.out.println("Current card is " + currentCard.getType() + " " + currentCard.getValue()); + Card card = player.getPlay(currentCard); + if (card != null) { + playCard(card); + } + } + System.out.println(deck.getDeck().size() + " cards left in deck"); + players.removeIf(Player::handIsEmpty); + } + } + + public void playCard(Card card) { + currentCard = card; + deck.addUsedCard(card); + } + + public void handleSpecialCard(Card card) { + TYPENUM cardType = card.getType(); + VALUENUM cardValue = card.getValue(); + if (cardType == TYPENUM.JOKER) { + // Draw 5 cards + return; + } + switch (cardValue) { + case TWO: + // Draw 2 cards + break; + case SEVEN: + case KING: + // Keep turn + break; + case EIGHT: + // Skip next player + break; + case JACK: + // Draw 5 cards + // Change card type + break; + case ACE: + // Change direction + break; + } + } +} diff --git a/src/main/java/mau/mau/MauMau.java b/src/main/java/mau/mau/MauMau.java new file mode 100644 index 0000000..df5a0eb --- /dev/null +++ b/src/main/java/mau/mau/MauMau.java @@ -0,0 +1,7 @@ +package mau.mau; + +public class MauMau { + public static void main(String[] args) { + Game game = new Game(); + } +} \ No newline at end of file diff --git a/src/main/java/mau/mau/Referee.java b/src/main/java/mau/mau/Referee.java new file mode 100644 index 0000000..c143916 --- /dev/null +++ b/src/main/java/mau/mau/Referee.java @@ -0,0 +1,14 @@ +package mau.mau; + +public class Referee { + public static boolean isValidMove(Card currentCard, Card playedCard) { + TYPENUM currentCardType = currentCard.getType(); + VALUENUM currentCardValue = currentCard.getValue(); + TYPENUM playedCardType = playedCard.getType(); + VALUENUM playedCardValue = playedCard.getValue(); + + return playedCardType == TYPENUM.JOKER + || playedCardType == currentCardType + || playedCardValue == currentCardValue; + } +} diff --git a/src/main/java/mau/mau/TYPENUM.java b/src/main/java/mau/mau/TYPENUM.java new file mode 100644 index 0000000..67a8e72 --- /dev/null +++ b/src/main/java/mau/mau/TYPENUM.java @@ -0,0 +1,5 @@ +package mau.mau; + +public enum TYPENUM { + SPADES, HEARTS, DIAMOND, CLUBS, JOKER; +} diff --git a/src/main/java/mau/mau/Utils.java b/src/main/java/mau/mau/Utils.java new file mode 100644 index 0000000..66a06a3 --- /dev/null +++ b/src/main/java/mau/mau/Utils.java @@ -0,0 +1,12 @@ +package mau.mau; + +public class Utils { + public static boolean isNumeric(String str) { + try { + int i = Integer.parseInt(str); + } catch(NumberFormatException nfe) { + return false; + } + return true; + } +} diff --git a/src/main/java/mau/mau/VALUENUM.java b/src/main/java/mau/mau/VALUENUM.java new file mode 100644 index 0000000..2356b33 --- /dev/null +++ b/src/main/java/mau/mau/VALUENUM.java @@ -0,0 +1,21 @@ +package mau.mau; + +public enum VALUENUM { + TWO (2), THREE (3), + FOUR (4), FIVE (5), + SIX (6), SEVEN (7), + EIGHT (8), NINE (9), + TEN (10), JACK (11), + QUEEN (12), KING (13), + ACE (13); + + private final int VALUE; + + VALUENUM(int value) { + VALUE = value; + } + + public int getValue() { + return VALUE; + } +} diff --git a/src/main/java/mau/mau/players/Bot.java b/src/main/java/mau/mau/players/Bot.java new file mode 100644 index 0000000..f28c3c0 --- /dev/null +++ b/src/main/java/mau/mau/players/Bot.java @@ -0,0 +1,34 @@ +package mau.mau.players; + +import java.util.List; +import java.util.Scanner; + +public class Bot extends Player { + private List hand; + private Dealer dealer; + + public Bot(Dealer dealer) { + super(dealer); + this.hand = super.getHand(); + } + + @Override + public Card getPlay(Card currentCard) { + new Scanner(System.in).nextLine(); + + for(Card card : hand) { + TYPENUM cardType = card.getType(); + VALUENUM cardValue = card.getValue(); + if(Referee.isValidMove(card, currentCard)) { + System.out.println("Bot played " + cardType + " " + cardValue); + hand.remove(card); + System.out.println("Bot now has " + hand.size() + " cards"); + return card; + } + } + hand.add(drawCard()); + System.out.println("Bot drew a card"); + System.out.println("Bot now has " + hand.size() + " cards"); + return null; + } +} diff --git a/src/main/java/mau/mau/players/Human.java b/src/main/java/mau/mau/players/Human.java new file mode 100644 index 0000000..7b52838 --- /dev/null +++ b/src/main/java/mau/mau/players/Human.java @@ -0,0 +1,60 @@ +package mau.mau.players; + +import mau.mau.Card; +import mau.mau.Dealer; +import mau.mau.Referee; +import mau.mau.Utils; + +import java.util.List; +import java.util.Scanner; + +public class Human extends Player { + private List hand; + + public Human(Dealer dealer) { + super(dealer); + this.hand = super.getHand(); + } + + @Override + public Card getPlay(Card currentCard) { + printHand(); + + Scanner scanner = new Scanner(System.in); + + while (true){ + String input = scanner.nextLine(); + if(input.equals("quit")){ + System.out.println("You quit the game!"); + System.exit(69); + } + if (input.equals("draw")) { + Card card = drawCard(); + System.out.println("You drew a " + card.getType() + " " + card.getValue()); + hand.add(card); + return null; + } + if (Utils.isNumeric(input)) { + int cardIndex = Integer.parseInt(input); + if (cardIndex < 0 || cardIndex > hand.size()){ + System.out.println("Invalid card"); + continue; + } + Card card = hand.get(cardIndex); + if (!Referee.isValidMove(card, currentCard)) { + System.out.println("Invalid move"); + continue; + } + hand.remove(cardIndex); + return card; + } + System.out.println("Invalid input"); + } + } + + private void printHand(){ + for (int i = 0; i < hand.size(); i++) { + System.out.println("(" + i + ") " + hand.get(i).getType() + " " + hand.get(i).getValue()); + } + } +} diff --git a/src/main/java/mau/mau/players/Player.java b/src/main/java/mau/mau/players/Player.java new file mode 100644 index 0000000..08ffb12 --- /dev/null +++ b/src/main/java/mau/mau/players/Player.java @@ -0,0 +1,48 @@ +package mau.mau.players; + +import mau.mau.Card; +import mau.mau.Dealer; + +import java.util.List; + +public abstract class Player { + private List hand; + private Dealer dealer; + + public Player(Dealer dealer) { + this.dealer = dealer; + this.hand = dealer.getHand(); + } + + protected List getHand() { + return hand; + } + + public List setHand(List hand) { + return this.hand = hand; + } + + public Boolean handIsEmpty(){ + return hand.isEmpty(); + } + + public void addCardToHand(Card card){ + hand.add(card); + } + + public Card drawCard() { + return dealer.drawCard(); + } + + public Card playCard(int cardIndex){ + Card playCard = hand.get(cardIndex); + hand.remove(cardIndex); + return playCard; + } + + public int getHandSize(){ + return hand.size(); + } + + public abstract Card getPlay(Card currentCard); +}