Initial commit

This commit is contained in:
2022-09-15 15:15:11 +02:00
commit e4231e903d
18 changed files with 500 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -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

13
.idea/compiler.xml generated Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="MauMau" />
</profile>
</annotationProcessing>
</component>
</project>

7
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml generated Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

14
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

17
pom.xml Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tman</groupId>
<artifactId>MauMau</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -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;
}
}

View File

@@ -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<Card> shuffleDeck(List<Card> unshuffledDeck) {
List<Card> 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<Card> getHand() {
List<Card> deck = this.deck.getDeck();
List<Card> hand = new ArrayList<>();
for (int i = 0; i < 7; i++) {
hand.add(deck.get(i));
deck.remove(i);
}
return hand;
}
public void dealCards(List<Player> players) {
for (Player player : players) {
player.setHand(getHand());
}
}
public Card getInitialCard() {
List<Card> deck = this.deck.getDeck();
Card card = deck.get(0);
this.deck.addUsedCard(card);
return card;
}
public List<Card> drawCards(int cardsToDraw) {
if (deck.deckIsEmpty(cardsToDraw)) {
deck.setDeck(shuffleDeck(deck.getUsedCards()));
deck.setUsedCards(new ArrayList<>());
}
List<Card> cards = new ArrayList<>();
for (int i = 0; i < cardsToDraw; i++) {
cards.add(deck.drawCard());
}
return cards;
}
public Card drawCard() {
return drawCards(1).get(0);
}
}

View File

@@ -0,0 +1,54 @@
package mau.mau;
import java.util.ArrayList;
import java.util.List;
public class Deck {
private List<Card> deck = new ArrayList<>();
private List<Card> 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<Card> getDeck() {
return deck;
}
public void setDeck(List<Card> deck){
this.deck = deck;
}
public List<Card> getUsedCards(){
return usedCards;
}
public void setUsedCards(List<Card> 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);
}
}

View File

@@ -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<Player> 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;
}
}
}

View File

@@ -0,0 +1,7 @@
package mau.mau;
public class MauMau {
public static void main(String[] args) {
Game game = new Game();
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,5 @@
package mau.mau;
public enum TYPENUM {
SPADES, HEARTS, DIAMOND, CLUBS, JOKER;
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,34 @@
package mau.mau.players;
import java.util.List;
import java.util.Scanner;
public class Bot extends Player {
private List<Card> 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;
}
}

View File

@@ -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<Card> 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());
}
}
}

View File

@@ -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<Card> hand;
private Dealer dealer;
public Player(Dealer dealer) {
this.dealer = dealer;
this.hand = dealer.getHand();
}
protected List<Card> getHand() {
return hand;
}
public List<Card> setHand(List<Card> 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);
}