Skip to content

Commit

Permalink
Merge branch 'master' into ai_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jberclaz authored Jul 14, 2024
2 parents 9692450 + 06396c1 commit 4c06034
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/leflat/jass/client/ArtificialClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/leflat/jass/server/ArtificialPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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);
Expand All @@ -30,6 +31,11 @@ public ArtificialPlayer(int id, String name) {
public ArtificialPlayer() {
super(0);
}

public ArtificialPlayer(int id, String name, int strength) {
this(id, name);
this.strength = strength;
}

@Override
public void setPlayerInfo(BasePlayer player) {
Expand Down Expand Up @@ -160,10 +166,10 @@ public List<Announcement> 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;
}
Expand Down Expand Up @@ -250,13 +256,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;
}

Expand Down Expand Up @@ -354,7 +360,7 @@ int chooseBestAtout(boolean canPass) {
}
float maxScore = -10000;
for (var card : hand) {
float score = evaluateMoveReward(hand, card, 5000);
float score = evaluateMoveReward(hand, card, 5 * strength);
if (score > maxScore) {
maxScore = score;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/leflat/jass/server/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Float[]> unknownCardsInGame = new HashMap<>();
private final List<Card>[] knownCardsInHands = new List[3];
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/leflat/jass/test/MockNetworkFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
33 changes: 31 additions & 2 deletions src/test/java/FunctionalTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();});
}
}
6 changes: 4 additions & 2 deletions src/test/java/com/leflat/jass/server/GameViewTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c06034

Please sign in to comment.