added json support and optimized -> broke -> fixed bot
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -14,6 +14,11 @@
|
|||||||
<version>RELEASE</version>
|
<version>RELEASE</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20220320</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -38,11 +38,13 @@ public class CardHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleEight(TurnTable turnTable) {
|
private void handleEight(TurnTable turnTable) {
|
||||||
|
System.out.println("Eighty waity");
|
||||||
turnTable.skipNextPlayer();
|
turnTable.skipNextPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleJack(TurnTable turnTable) {
|
private void handleJack(TurnTable turnTable) {
|
||||||
Referee.setAllowedCards(turnTable.getCurrentPlayer().getTypeChoice());
|
Referee.setAllowedCards(turnTable.getCurrentPlayer().getTypeChoice());
|
||||||
|
System.out.printf("%s chose %s\n", turnTable.getCurrentPlayer().getName(), turnTable.getCurrentPlayer().getTypeChoice());
|
||||||
turnTable.nextPlayer();
|
turnTable.nextPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +53,7 @@ public class CardHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleAce(TurnTable turnTable) {
|
private void handleAce(TurnTable turnTable) {
|
||||||
|
System.out.printf("%s spins everybody right round baby right round\n", turnTable.getCurrentPlayer().getName());
|
||||||
turnTable.reverseDirection();
|
turnTable.reverseDirection();
|
||||||
turnTable.nextPlayer();
|
turnTable.nextPlayer();
|
||||||
}
|
}
|
||||||
@@ -58,7 +61,9 @@ public class CardHandler {
|
|||||||
private void handleJoker(TurnTable turnTable) {
|
private void handleJoker(TurnTable turnTable) {
|
||||||
Player nextPlayer = turnTable.getNextPlayer();
|
Player nextPlayer = turnTable.getNextPlayer();
|
||||||
drawCards(nextPlayer, 5);
|
drawCards(nextPlayer, 5);
|
||||||
|
System.out.printf("%s was forced to draw 5 cards\n", nextPlayer.getName());
|
||||||
Referee.setAllowedCards(nextPlayer.getTypeChoice());
|
Referee.setAllowedCards(nextPlayer.getTypeChoice());
|
||||||
|
System.out.printf("%s chose %s\n", nextPlayer.getName(), nextPlayer.getTypeChoice());
|
||||||
turnTable.skipNextPlayer();
|
turnTable.skipNextPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,19 +26,10 @@ public class Game {
|
|||||||
private void setupGame() {
|
private void setupGame() {
|
||||||
System.out.println(ConsoleColors.RED_BOLD + Messages.WELCOME + ConsoleColors.RESET);
|
System.out.println(ConsoleColors.RED_BOLD + Messages.WELCOME + ConsoleColors.RESET);
|
||||||
dealer = new Dealer(deck);
|
dealer = new Dealer(deck);
|
||||||
Scanner scanner = new Scanner(System.in);
|
players.add(new Human(dealer));
|
||||||
System.out.println(Messages.ASK_FOR_PLAYER_NAME);
|
players.add(new Bot(dealer));
|
||||||
String input = scanner.nextLine();
|
players.add(new Bot(dealer));
|
||||||
if (input.equals("")) {
|
players.add(new Bot(dealer));
|
||||||
input = "Koet";
|
|
||||||
System.out.println(Messages.BAD_PLAYER_NAME);
|
|
||||||
}
|
|
||||||
new Scanner(System.in).nextLine();
|
|
||||||
Human human = new Human(input, dealer);
|
|
||||||
players.add(human);
|
|
||||||
players.add(new Bot("Jordan", dealer));
|
|
||||||
players.add(new Bot("MaarKoet", dealer));
|
|
||||||
players.add(new Bot("El Tigre", dealer));
|
|
||||||
turnTable = new TurnTable(players);
|
turnTable = new TurnTable(players);
|
||||||
dealer.dealCards(players);
|
dealer.dealCards(players);
|
||||||
currentCard = dealer.getInitialCard();
|
currentCard = dealer.getInitialCard();
|
||||||
@@ -59,7 +50,7 @@ public class Game {
|
|||||||
}
|
}
|
||||||
System.out.println(deck.getDeck().size() + " cards left in deck");
|
System.out.println(deck.getDeck().size() + " cards left in deck");
|
||||||
} else {
|
} else {
|
||||||
if(Referee.isValidEndCard(playedCard)) {
|
if(Referee.isValidEndCard(currentCard, playedCard)) {
|
||||||
playCard(playedCard);
|
playCard(playedCard);
|
||||||
System.out.println(player.getName() + " won!");
|
System.out.println(player.getName() + " won!");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
21
src/main/java/mau/mau/JSONderulo.java
Normal file
21
src/main/java/mau/mau/JSONderulo.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package mau.mau;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
public class JSONderulo {
|
||||||
|
|
||||||
|
public static JSONObject parseJSONFile(String filename) throws JSONException, IOException {
|
||||||
|
String content = new String(Files.readAllBytes(Paths.get(filename)));
|
||||||
|
return new JSONObject(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JSONArray getJSONArrayFromJSONFile(String filename, String key) throws JSONException, IOException {
|
||||||
|
return parseJSONFile(filename).getJSONArray(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ public class Messages {
|
|||||||
|
|
||||||
public static final String WELCOME = "Welcome to Dutch Mau Mau!";
|
public static final String WELCOME = "Welcome to Dutch Mau Mau!";
|
||||||
public static final String ASK_FOR_PLAYER_NAME = "What shall be thy name?";
|
public static final String ASK_FOR_PLAYER_NAME = "What shall be thy name?";
|
||||||
public static final String BAD_PLAYER_NAME = "What a pathetic response, thy shall be known as Koet";
|
public static final String BAD_PLAYER_NAME = "What a pathetic response, thy shall be knoweth as Koet";
|
||||||
public static final String PLAYER_PLAYED_CARD = KEY_PLAYER_NAME + " played " + KEY_CARD;
|
public static final String PLAYER_PLAYED_CARD = KEY_PLAYER_NAME + " played " + KEY_CARD;
|
||||||
public static final String PLAYER_DRAW_CARDS = KEY_PLAYER_NAME + " drew " + KEY_NUMBER_OF_CARDS + " cards";
|
public static final String PLAYER_DRAW_CARDS = KEY_PLAYER_NAME + " drew " + KEY_NUMBER_OF_CARDS + " cards";
|
||||||
public static final String PLAYER_HAS_X_CARDS = KEY_PLAYER_NAME + " now has " + KEY_NUMBER_OF_CARDS + " cards";
|
public static final String PLAYER_HAS_X_CARDS = KEY_PLAYER_NAME + " now has " + KEY_NUMBER_OF_CARDS + " cards";
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ public class Referee {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isValidEndCard(Card card) {
|
public static boolean isValidEndCard(Card currentCard, Card playedCard) {
|
||||||
return !isMauCard(card);
|
return !isMauCard(playedCard) && isValidMove(currentCard, playedCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setAllowedCards(TYPENUM type) {
|
public static void setAllowedCards(TYPENUM type) {
|
||||||
|
|||||||
@@ -8,43 +8,42 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Bot extends Player {
|
public class Bot extends Player {
|
||||||
|
|
||||||
public Bot(String name, Dealer dealer) {
|
private static int nameIndex = 0;
|
||||||
super(name, dealer);
|
private static String JSON = "src/main/resources/languages/en.json";
|
||||||
|
private static String JSON_KEY = "botnames";
|
||||||
|
|
||||||
|
public Bot(Dealer dealer) {
|
||||||
|
super(dealer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Card getPlay(Card currentCard) {
|
public Card getPlay(Card currentCard) {
|
||||||
List<Card> hand = getHand();
|
List<Card> hand = getHand();
|
||||||
|
|
||||||
if (hand.size() == 1 && Referee.isValidEndCard(hand.get(0)) && Referee.isValidMove(hand.get(0), currentCard)) {
|
if (getHandSize() > 1){
|
||||||
Card card = hand.get(0);
|
for (Card card : hand) {
|
||||||
System.out.println(getName() + " played " + card);
|
if (!Referee.allowedTypes.isEmpty()) {
|
||||||
hand.remove(0);
|
if (Referee.allowedTypes.contains(card.getType())) {
|
||||||
System.out.println(getName() + " now has " + hand.size() + " cards");
|
Referee.allowedTypes.clear();
|
||||||
return card;
|
} else {
|
||||||
} else if (hand.size() == 1) {
|
continue;
|
||||||
hand.add(drawCard());
|
}
|
||||||
System.out.println(getName() + " drew a card");
|
} else if (!Referee.isValidMove(card, currentCard)) {
|
||||||
System.out.println(getName() + " now has " + hand.size() + " cards");
|
continue;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Card card : hand) {
|
|
||||||
if (Referee.allowedTypes.isEmpty()) {
|
|
||||||
if (Referee.isValidMove(card, currentCard)) {
|
|
||||||
System.out.println(getName() + " played " + card);
|
|
||||||
hand.remove(card);
|
|
||||||
System.out.println(getName() + " now has " + hand.size() + " cards");
|
|
||||||
return card;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Referee.allowedTypes.contains(card.getType())) {
|
|
||||||
Referee.allowedTypes.clear();
|
|
||||||
System.out.println(getName() + " played " + card);
|
|
||||||
hand.remove(card);
|
|
||||||
System.out.println(getName() + " now has " + hand.size() + " cards");
|
|
||||||
return card;
|
|
||||||
}
|
}
|
||||||
|
System.out.println(getName() + " played " + card);
|
||||||
|
hand.remove(card);
|
||||||
|
System.out.println(getName() + " now has " + getHandSize() + " cards");
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Card lastCard = hand.get(0);
|
||||||
|
if(Referee.isValidEndCard(currentCard, lastCard)){
|
||||||
|
Card card = hand.get(0);
|
||||||
|
System.out.println(getName() + " played " + card);
|
||||||
|
hand.remove(0);
|
||||||
|
System.out.println(getName() + " now has " + getHandSize() + " cards");
|
||||||
|
return card;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hand.add(drawCard());
|
hand.add(drawCard());
|
||||||
@@ -75,7 +74,16 @@ public class Bot extends Player {
|
|||||||
if (typeChoice == null || typeChoice == TYPENUM.JOKER) {
|
if (typeChoice == null || typeChoice == TYPENUM.JOKER) {
|
||||||
typeChoice = TYPENUM.values()[(int) (Math.random() * 4)];
|
typeChoice = TYPENUM.values()[(int) (Math.random() * 4)];
|
||||||
}
|
}
|
||||||
System.out.println(getName() + " chose " + typeChoice);
|
|
||||||
return typeChoice;
|
return typeChoice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String askForName() {
|
||||||
|
try {
|
||||||
|
return JSONderulo.getJSONArrayFromJSONFile(JSON, JSON_KEY).getString(nameIndex++);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error reading botnames from JSON file");
|
||||||
|
return "Koet " + nameIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,12 +7,13 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Human extends Player {
|
public class Human extends Player {
|
||||||
|
|
||||||
public Human(String name, Dealer dealer) {
|
public Human(Dealer dealer) {
|
||||||
super(name, dealer);
|
super(dealer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Card getPlay(Card currentCard) {
|
public Card getPlay(Card currentCard) {
|
||||||
|
System.out.println();
|
||||||
printHand();
|
printHand();
|
||||||
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
@@ -85,6 +86,19 @@ public class Human extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String askForName() {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println(Messages.ASK_FOR_PLAYER_NAME);
|
||||||
|
String input = scanner.nextLine();
|
||||||
|
if (input.equals("")) {
|
||||||
|
input = "Koet";
|
||||||
|
System.out.println(Messages.BAD_PLAYER_NAME);
|
||||||
|
}
|
||||||
|
scanner.nextLine();
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
private void printHand() {
|
private void printHand() {
|
||||||
List<Card> hand = getHand();
|
List<Card> hand = getHand();
|
||||||
for (int i = 0; i < hand.size(); i++) {
|
for (int i = 0; i < hand.size(); i++) {
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ public abstract class Player {
|
|||||||
private List<Card> hand;
|
private List<Card> hand;
|
||||||
private Dealer dealer;
|
private Dealer dealer;
|
||||||
|
|
||||||
public Player(String name, Dealer dealer) {
|
public Player(Dealer dealer) {
|
||||||
this.name = name;
|
|
||||||
this.dealer = dealer;
|
this.dealer = dealer;
|
||||||
|
name = askForName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Card> getHand() {
|
public List<Card> getHand() {
|
||||||
@@ -54,4 +54,6 @@ public abstract class Player {
|
|||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract String askForName();
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/main/resources/languages/en.json
Normal file
7
src/main/resources/languages/en.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"botnames": [
|
||||||
|
"Jordan",
|
||||||
|
"MaarKoet",
|
||||||
|
"El Tigre"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -25,9 +25,9 @@ public class CardHandlerTest {
|
|||||||
sut = new CardHandler();
|
sut = new CardHandler();
|
||||||
players = new ArrayList<>();
|
players = new ArrayList<>();
|
||||||
turnTable = new TurnTable(players);
|
turnTable = new TurnTable(players);
|
||||||
bot1 = new Bot("Bot1", dealer);
|
bot1 = new Bot(dealer);
|
||||||
bot2 = new Bot("Bot2", dealer);
|
bot2 = new Bot(dealer);
|
||||||
bot3 = new Bot("Bot3", dealer);
|
bot3 = new Bot(dealer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -100,9 +100,10 @@ public class RefereeTest {
|
|||||||
public void isAceOfSpadesValidEndcardTest(){
|
public void isAceOfSpadesValidEndcardTest(){
|
||||||
//Arrange
|
//Arrange
|
||||||
Card testCardOne = new Card(TYPENUM.SPADES, VALUENUM.ACE);
|
Card testCardOne = new Card(TYPENUM.SPADES, VALUENUM.ACE);
|
||||||
|
Card testCurrentCard = new Card(TYPENUM.HEARTS, VALUENUM.ACE);
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
boolean results = Referee.isValidEndCard(testCardOne);
|
boolean results = Referee.isValidEndCard(testCurrentCard, testCardOne);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
assertFalse(results);
|
assertFalse(results);
|
||||||
@@ -112,9 +113,10 @@ public class RefereeTest {
|
|||||||
public void isThreeOfHeartsValidEndcardTest(){
|
public void isThreeOfHeartsValidEndcardTest(){
|
||||||
//Arrange
|
//Arrange
|
||||||
Card testCardOne = new Card(TYPENUM.HEARTS, VALUENUM.THREE);
|
Card testCardOne = new Card(TYPENUM.HEARTS, VALUENUM.THREE);
|
||||||
|
Card testCurrentCard = new Card(TYPENUM.HEARTS, VALUENUM.ACE);
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
boolean results = Referee.isValidEndCard(testCardOne);
|
boolean results = Referee.isValidEndCard(testCurrentCard, testCardOne);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
assertTrue(results);
|
assertTrue(results);
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ public class TurnTableTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
Dealer dealer = new Dealer(new Deck());
|
Dealer dealer = new Dealer(new Deck());
|
||||||
players = new ArrayList<>();
|
players = new ArrayList<>();
|
||||||
bot1 = new Bot("Bot1", dealer);
|
bot1 = new Bot(dealer);
|
||||||
bot2 = new Bot("Bot2", dealer);
|
bot2 = new Bot(dealer);
|
||||||
bot3 = new Bot("Bot3", dealer);
|
bot3 = new Bot(dealer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class BotTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
deck = new Deck();
|
deck = new Deck();
|
||||||
dealer = new Dealer(deck);
|
dealer = new Dealer(deck);
|
||||||
sut = new Bot("Koet", dealer);
|
sut = new Bot(dealer);
|
||||||
hand = new ArrayList<>();
|
hand = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user