added json support and optimized -> broke -> fixed bot

This commit is contained in:
2022-09-20 22:29:52 +02:00
parent f05b837c67
commit 4e50e3c295
14 changed files with 116 additions and 61 deletions

View File

@@ -14,6 +14,11 @@
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>
</dependencies>
<build>

View File

@@ -38,11 +38,13 @@ public class CardHandler {
}
private void handleEight(TurnTable turnTable) {
System.out.println("Eighty waity");
turnTable.skipNextPlayer();
}
private void handleJack(TurnTable turnTable) {
Referee.setAllowedCards(turnTable.getCurrentPlayer().getTypeChoice());
System.out.printf("%s chose %s\n", turnTable.getCurrentPlayer().getName(), turnTable.getCurrentPlayer().getTypeChoice());
turnTable.nextPlayer();
}
@@ -51,6 +53,7 @@ public class CardHandler {
}
private void handleAce(TurnTable turnTable) {
System.out.printf("%s spins everybody right round baby right round\n", turnTable.getCurrentPlayer().getName());
turnTable.reverseDirection();
turnTable.nextPlayer();
}
@@ -58,7 +61,9 @@ public class CardHandler {
private void handleJoker(TurnTable turnTable) {
Player nextPlayer = turnTable.getNextPlayer();
drawCards(nextPlayer, 5);
System.out.printf("%s was forced to draw 5 cards\n", nextPlayer.getName());
Referee.setAllowedCards(nextPlayer.getTypeChoice());
System.out.printf("%s chose %s\n", nextPlayer.getName(), nextPlayer.getTypeChoice());
turnTable.skipNextPlayer();
}

View File

@@ -26,19 +26,10 @@ public class Game {
private void setupGame() {
System.out.println(ConsoleColors.RED_BOLD + Messages.WELCOME + ConsoleColors.RESET);
dealer = new Dealer(deck);
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);
}
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));
players.add(new Human(dealer));
players.add(new Bot(dealer));
players.add(new Bot(dealer));
players.add(new Bot(dealer));
turnTable = new TurnTable(players);
dealer.dealCards(players);
currentCard = dealer.getInitialCard();
@@ -59,7 +50,7 @@ public class Game {
}
System.out.println(deck.getDeck().size() + " cards left in deck");
} else {
if(Referee.isValidEndCard(playedCard)) {
if(Referee.isValidEndCard(currentCard, playedCard)) {
playCard(playedCard);
System.out.println(player.getName() + " won!");
} else {

View 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);
}
}

View File

@@ -7,7 +7,7 @@ public class Messages {
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 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_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";

View File

@@ -31,8 +31,8 @@ public class Referee {
};
}
public static boolean isValidEndCard(Card card) {
return !isMauCard(card);
public static boolean isValidEndCard(Card currentCard, Card playedCard) {
return !isMauCard(playedCard) && isValidMove(currentCard, playedCard);
}
public static void setAllowedCards(TYPENUM type) {

View File

@@ -8,43 +8,42 @@ import java.util.Scanner;
public class Bot extends Player {
public Bot(String name, Dealer dealer) {
super(name, dealer);
private static int nameIndex = 0;
private static String JSON = "src/main/resources/languages/en.json";
private static String JSON_KEY = "botnames";
public Bot(Dealer dealer) {
super(dealer);
}
@Override
public Card getPlay(Card currentCard) {
List<Card> hand = getHand();
if (hand.size() == 1 && Referee.isValidEndCard(hand.get(0)) && Referee.isValidMove(hand.get(0), currentCard)) {
Card card = hand.get(0);
System.out.println(getName() + " played " + card);
hand.remove(0);
System.out.println(getName() + " now has " + hand.size() + " cards");
return card;
} else if (hand.size() == 1) {
hand.add(drawCard());
System.out.println(getName() + " drew a card");
System.out.println(getName() + " now has " + hand.size() + " cards");
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;
if (getHandSize() > 1){
for (Card card : hand) {
if (!Referee.allowedTypes.isEmpty()) {
if (Referee.allowedTypes.contains(card.getType())) {
Referee.allowedTypes.clear();
} else {
continue;
}
} else if (!Referee.isValidMove(card, currentCard)) {
continue;
}
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());
@@ -75,7 +74,16 @@ public class Bot extends Player {
if (typeChoice == null || typeChoice == TYPENUM.JOKER) {
typeChoice = TYPENUM.values()[(int) (Math.random() * 4)];
}
System.out.println(getName() + " chose " + 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++;
}
}
}

View File

@@ -7,12 +7,13 @@ import java.util.Scanner;
public class Human extends Player {
public Human(String name, Dealer dealer) {
super(name, dealer);
public Human(Dealer dealer) {
super(dealer);
}
@Override
public Card getPlay(Card currentCard) {
System.out.println();
printHand();
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() {
List<Card> hand = getHand();
for (int i = 0; i < hand.size(); i++) {

View File

@@ -12,9 +12,9 @@ public abstract class Player {
private List<Card> hand;
private Dealer dealer;
public Player(String name, Dealer dealer) {
this.name = name;
public Player(Dealer dealer) {
this.dealer = dealer;
name = askForName();
}
public List<Card> getHand() {
@@ -54,4 +54,6 @@ public abstract class Player {
public String getName() {
return name;
}
public abstract String askForName();
}

View File

@@ -0,0 +1,7 @@
{
"botnames": [
"Jordan",
"MaarKoet",
"El Tigre"
]
}

View File

@@ -25,9 +25,9 @@ public class CardHandlerTest {
sut = new CardHandler();
players = new ArrayList<>();
turnTable = new TurnTable(players);
bot1 = new Bot("Bot1", dealer);
bot2 = new Bot("Bot2", dealer);
bot3 = new Bot("Bot3", dealer);
bot1 = new Bot(dealer);
bot2 = new Bot(dealer);
bot3 = new Bot(dealer);
}
@Test

View File

@@ -100,9 +100,10 @@ public class RefereeTest {
public void isAceOfSpadesValidEndcardTest(){
//Arrange
Card testCardOne = new Card(TYPENUM.SPADES, VALUENUM.ACE);
Card testCurrentCard = new Card(TYPENUM.HEARTS, VALUENUM.ACE);
//Act
boolean results = Referee.isValidEndCard(testCardOne);
boolean results = Referee.isValidEndCard(testCurrentCard, testCardOne);
//Assert
assertFalse(results);
@@ -112,9 +113,10 @@ public class RefereeTest {
public void isThreeOfHeartsValidEndcardTest(){
//Arrange
Card testCardOne = new Card(TYPENUM.HEARTS, VALUENUM.THREE);
Card testCurrentCard = new Card(TYPENUM.HEARTS, VALUENUM.ACE);
//Act
boolean results = Referee.isValidEndCard(testCardOne);
boolean results = Referee.isValidEndCard(testCurrentCard, testCardOne);
//Assert
assertTrue(results);

View File

@@ -18,9 +18,9 @@ public class TurnTableTest {
public void setUp() {
Dealer dealer = new Dealer(new Deck());
players = new ArrayList<>();
bot1 = new Bot("Bot1", dealer);
bot2 = new Bot("Bot2", dealer);
bot3 = new Bot("Bot3", dealer);
bot1 = new Bot(dealer);
bot2 = new Bot(dealer);
bot3 = new Bot(dealer);
}
@Test

View File

@@ -20,7 +20,7 @@ public class BotTest {
public void setUp() {
deck = new Deck();
dealer = new Dealer(deck);
sut = new Bot("Koet", dealer);
sut = new Bot(dealer);
hand = new ArrayList<>();
}