Initial commit
This commit is contained in:
20
src/main/java/mau/mau/Card.java
Normal file
20
src/main/java/mau/mau/Card.java
Normal 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;
|
||||
}
|
||||
}
|
||||
64
src/main/java/mau/mau/Dealer.java
Normal file
64
src/main/java/mau/mau/Dealer.java
Normal 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);
|
||||
}
|
||||
}
|
||||
54
src/main/java/mau/mau/Deck.java
Normal file
54
src/main/java/mau/mau/Deck.java
Normal 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);
|
||||
}
|
||||
}
|
||||
82
src/main/java/mau/mau/Game.java
Normal file
82
src/main/java/mau/mau/Game.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
src/main/java/mau/mau/MauMau.java
Normal file
7
src/main/java/mau/mau/MauMau.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package mau.mau;
|
||||
|
||||
public class MauMau {
|
||||
public static void main(String[] args) {
|
||||
Game game = new Game();
|
||||
}
|
||||
}
|
||||
14
src/main/java/mau/mau/Referee.java
Normal file
14
src/main/java/mau/mau/Referee.java
Normal 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;
|
||||
}
|
||||
}
|
||||
5
src/main/java/mau/mau/TYPENUM.java
Normal file
5
src/main/java/mau/mau/TYPENUM.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package mau.mau;
|
||||
|
||||
public enum TYPENUM {
|
||||
SPADES, HEARTS, DIAMOND, CLUBS, JOKER;
|
||||
}
|
||||
12
src/main/java/mau/mau/Utils.java
Normal file
12
src/main/java/mau/mau/Utils.java
Normal 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;
|
||||
}
|
||||
}
|
||||
21
src/main/java/mau/mau/VALUENUM.java
Normal file
21
src/main/java/mau/mau/VALUENUM.java
Normal 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;
|
||||
}
|
||||
}
|
||||
34
src/main/java/mau/mau/players/Bot.java
Normal file
34
src/main/java/mau/mau/players/Bot.java
Normal 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;
|
||||
}
|
||||
}
|
||||
60
src/main/java/mau/mau/players/Human.java
Normal file
60
src/main/java/mau/mau/players/Human.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
48
src/main/java/mau/mau/players/Player.java
Normal file
48
src/main/java/mau/mau/players/Player.java
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user