From 06396c1a95caafc9368f4317ce0095b51ce4b202 Mon Sep 17 00:00:00 2001 From: Jerome Berclaz Date: Sun, 14 Jul 2024 09:07:01 -0700 Subject: [PATCH] Fixed gameview issue (#31) * cleanup * fixed unit test * removed extra logs * cleanup * Fixed test bug, setting ai strength --- .../leflat/jass/client/ArtificialClient.java | 5 +-- .../leflat/jass/server/ArtificialPlayer.java | 16 ++++++--- .../java/com/leflat/jass/server/GameView.java | 4 ++- .../leflat/jass/test/MockNetworkFactory.java | 2 +- src/test/java/FunctionalTest.java | 33 +++++++++++++++++-- .../com/leflat/jass/server/GameViewTests.java | 6 ++-- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/leflat/jass/client/ArtificialClient.java b/src/main/java/com/leflat/jass/client/ArtificialClient.java index 681f407..be0f312 100644 --- a/src/main/java/com/leflat/jass/client/ArtificialClient.java +++ b/src/main/java/com/leflat/jass/client/ArtificialClient.java @@ -7,6 +7,7 @@ import java.util.logging.*; public class ArtificialClient extends ArtificialPlayer implements IRemotePlayer { + protected final static Logger LOGGER = Logger.getLogger(ArtificialClient.class.getName()); private final IClientNetworkFactory networkFactory; private IClientNetwork network = null; private IController controller = null; @@ -17,10 +18,10 @@ public ArtificialClient(String name, String host, int gameId, IClientNetworkFac this.networkFactory = networkFactory; int result = connect(name, host, gameId); if (result < 0) { - System.err.println("Unable to connect to game " + gameId + " : " + result); + LOGGER.warning("Unable to connect to game " + gameId + " : " + result); } else { - System.out.println("Connected to game " + gameId); + LOGGER.info("Connected to game " + gameId); try { controllerThread.join(); } catch (InterruptedException e) { diff --git a/src/main/java/com/leflat/jass/server/ArtificialPlayer.java b/src/main/java/com/leflat/jass/server/ArtificialPlayer.java index b41293a..6955f63 100644 --- a/src/main/java/com/leflat/jass/server/ArtificialPlayer.java +++ b/src/main/java/com/leflat/jass/server/ArtificialPlayer.java @@ -21,12 +21,18 @@ public class ArtificialPlayer extends AbstractRemotePlayer { private int ourScore = 0; private int theirScore = 0; private int numberOfPliesWonByOwnTeam; + private int strength = 1000; public ArtificialPlayer(int id, String name) { super(id); setName(name); } + public ArtificialPlayer(int id, String name, int strength) { + this(id, name); + this.strength = strength; + } + @Override public void setPlayerInfo(BasePlayer player) { var relativePosition = getInitialRelativePosition(player); @@ -156,10 +162,10 @@ public List getAnnouncements() { if (hand.size() == 8) { // can announce only on first plie if (!announcements.isEmpty()) { - System.out.println(name + " has " + announcements.size() + " announcements"); + LOGGER.info(name + " has " + announcements.size() + " announcements"); } for (var a : announcements) { - System.out.println(name + " announces " + a); + LOGGER.info(name + " announces " + a); } return announcements; } @@ -246,13 +252,13 @@ private Card chooseBestCard() { Card bestCard = null; float bestScore = -1000; for (Card validCard : validCards) { - var score = evaluateMoveReward(hand, validCard, 10000); + var score = evaluateMoveReward(hand, validCard, strength * 10); if (score > bestScore) { bestScore = score; bestCard = validCard; } } - System.out.println(name + " : best move is " + bestCard); + LOGGER.info(name + " : best move is " + bestCard); return bestCard; } @@ -350,7 +356,7 @@ int chooseBestAtout(boolean canPass) { } float maxScore = -10000; for (var card : hand) { - float score = evaluateMoveReward(hand, card, 1000); + float score = evaluateMoveReward(hand, card, strength); if (score > maxScore) { maxScore = score; } diff --git a/src/main/java/com/leflat/jass/server/GameView.java b/src/main/java/com/leflat/jass/server/GameView.java index ac5689e..0696e2c 100644 --- a/src/main/java/com/leflat/jass/server/GameView.java +++ b/src/main/java/com/leflat/jass/server/GameView.java @@ -3,8 +3,10 @@ import com.leflat.jass.common.Card; import java.util.*; +import java.util.logging.Logger; public class GameView { + protected final static Logger LOGGER = Logger.getLogger(GameView.class.getName()); private final Random rand = new Random(); private final Map unknownCardsInGame = new HashMap<>(); private final List[] knownCardsInHands = new List[3]; @@ -50,7 +52,7 @@ public void playerHasCard(int player, int cardNumber) { public void playerHasCard(int player, Card card) { int previousNumberCardsInGame = getNumberCardsInGame(); if (!unknownCardsInGame.containsKey(card.getNumber())) { - throw new RuntimeException("Card " + card + " was not in game"); + return; } if (knownCardsInHands[player].contains(card)) { throw new RuntimeException("We already know player " + player + " has card " + card); diff --git a/src/main/java/com/leflat/jass/test/MockNetworkFactory.java b/src/main/java/com/leflat/jass/test/MockNetworkFactory.java index 4837000..f1e7c82 100644 --- a/src/main/java/com/leflat/jass/test/MockNetworkFactory.java +++ b/src/main/java/com/leflat/jass/test/MockNetworkFactory.java @@ -4,7 +4,7 @@ import com.leflat.jass.common.IClientNetworkFactory; public class MockNetworkFactory implements IClientNetworkFactory { - private static int nbrPlayers = 0; + private int nbrPlayers = 0; public MockNetworkFactory() { diff --git a/src/test/java/FunctionalTest.java b/src/test/java/FunctionalTest.java index 12ab2be..08dea8b 100644 --- a/src/test/java/FunctionalTest.java +++ b/src/test/java/FunctionalTest.java @@ -1,11 +1,14 @@ import com.leflat.jass.client.JassPlayer; +import com.leflat.jass.server.ArtificialPlayer; import com.leflat.jass.server.GameController; import com.leflat.jass.server.PlayerLeftExpection; import com.leflat.jass.test.MockNetworkFactory; import com.leflat.jass.test.MockUiFactory; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class FunctionalTest { @Test @@ -28,10 +31,36 @@ void functional_test() throws PlayerLeftExpection { game.addPlayer(player3); game.addPlayer(player4); - assertTrue(game.isGameFull()); + Assertions.assertTrue(game.isGameFull()); game.setNoWait(true); - game.run(); + assertDoesNotThrow(() -> {game.run();}); + } + + @Test + void functional_test_with_artificial_player() throws PlayerLeftExpection { + var game = new GameController(0); + game.setNoWait(true); + + var clientNetworkFactory = new MockNetworkFactory(); + var player1 = new JassPlayer(clientNetworkFactory, new MockUiFactory()); + player1.setName("Berte"); + var player2 = new JassPlayer(clientNetworkFactory, new MockUiFactory()); + player2.setName("GC"); + var player3 = new JassPlayer(clientNetworkFactory, new MockUiFactory()); + player3.setName("Pischus"); + var player4 = new ArtificialPlayer(3, "Wein", 20); + + game.addPlayer(player1); + game.addPlayer(player2); + game.addPlayer(player3); + game.addPlayer(player4); + + Assertions.assertTrue(game.isGameFull()); + + game.setNoWait(true); + + assertDoesNotThrow(() -> {game.run();}); } } diff --git a/src/test/java/com/leflat/jass/server/GameViewTests.java b/src/test/java/com/leflat/jass/server/GameViewTests.java index a823fad..ba8238f 100644 --- a/src/test/java/com/leflat/jass/server/GameViewTests.java +++ b/src/test/java/com/leflat/jass/server/GameViewTests.java @@ -73,8 +73,10 @@ public void test_player_has_card() { assertEquals(unknownCardsInGame.size(), 25); assertEquals(knownCardsInHands[2].size(), 2); - assertThrows(RuntimeException.class, () -> gameView.playerHasCard(2, new Card(12))); - assertThrows(RuntimeException.class, () -> gameView.playerHasCard(1, new Card(12))); + assertDoesNotThrow(() -> { + gameView.playerHasCard(2, new Card(12)); + }); + assertDoesNotThrow(() -> gameView.playerHasCard(1, new Card(12))); } @Test