Tests and better instantiation

This commit is contained in:
lieght
2025-12-12 16:47:17 +01:00
parent c30c118c04
commit a60c702306
10 changed files with 136 additions and 34 deletions

View File

@@ -141,17 +141,6 @@ public final class App extends Application {
}
stage.show();
var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
games.put("tictactoe", BitboardReversi.class);
games.put("reversi", BitboardReversi.class);
var s = new MasterServer(6666, games);
try {
s.start();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void setKeybinds(StackPane root) {

View File

@@ -10,7 +10,8 @@ import org.toop.framework.game.players.OnlinePlayer;
public class ReversiBitController extends GenericGameController {
public ReversiBitController(Player[] players) {
BitboardReversi game = new BitboardReversi(players);
BitboardReversi game = new BitboardReversi();
game.init(players);
ThreadBehaviour thread = new LocalThreadBehaviour(game);
for (Player player : players) {
if (player instanceof OnlinePlayer){

View File

@@ -10,7 +10,8 @@ import org.toop.framework.game.players.OnlinePlayer;
public class TicTacToeBitController extends GenericGameController {
public TicTacToeBitController(Player[] players) {
BitboardTicTacToe game = new BitboardTicTacToe(players);
BitboardTicTacToe game = new BitboardTicTacToe();
game.init(players);
ThreadBehaviour thread = new LocalThreadBehaviour(game);
for (Player player : players) {
if (player instanceof OnlinePlayer){

View File

@@ -13,13 +13,19 @@ public abstract class BitboardGame implements TurnBasedGame {
private Player[] players;
// long is 64 bits. Every game has a limit of 64 cells maximum.
private final long[] playerBitboard;
private long[] playerBitboard;
private int currentTurn = 0;
private int playerCount;
public BitboardGame(int columnSize, int rowSize, int playerCount, Player[] players) {
public BitboardGame(int columnSize, int rowSize, int playerCount) {
this.columnSize = columnSize;
this.rowSize = rowSize;
this.players = players;
this.playerCount = playerCount;
}
@Override
public void init(Player[] players) {
this.players = players;
this.playerBitboard = new long[playerCount];
Arrays.fill(playerBitboard, 0L);

View File

@@ -13,8 +13,8 @@ public class BitboardReversi extends BitboardGame {
private final long notAFile = 0xfefefefefefefefeL;
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
public BitboardReversi(Player[] players) {
super(8, 8, 2, players);
public BitboardReversi() {
super(8, 8, 2);
// Black (player 0)
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
@@ -23,6 +23,11 @@ public class BitboardReversi extends BitboardGame {
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8)));
}
@Override
public void init(Player[] players) {
super.init(players);
}
public BitboardReversi(BitboardReversi other) {
super(other);
}

View File

@@ -17,9 +17,15 @@ public class BitboardTicTacToe extends BitboardGame {
0b001010100L // anti-diagonal
};
public BitboardTicTacToe(Player[] players) {
super(3, 3, 2, players);
public BitboardTicTacToe() {
super(3, 3, 2);
}
@Override
public void init(Player[] players) {
super.init(players);
}
public BitboardTicTacToe(BitboardTicTacToe other) {
super(other);
}

View File

@@ -1,6 +1,9 @@
package org.toop.framework.gameFramework.model.game;
import org.toop.framework.gameFramework.model.player.Player;
public interface TurnBasedGame extends Playable, PlayerProvider, BoardProvider, DeepCopyable<TurnBasedGame> {
void init(Player[] players);
int getCurrentTurn();
int getPlayerCount();
int getWinner();

View File

@@ -1,7 +1,5 @@
package org.toop.framework.gameFramework.model.player;
import org.toop.framework.gameFramework.model.game.DeepCopyable;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
public interface Player extends NameProvider, MoveProvider, DeepCopyable<Player> {
}
public interface Player extends NameProvider, MoveProvider, DeepCopyable<Player> {}

View File

@@ -1,7 +1,9 @@
package org.toop.framework.networking.server;
import org.toop.framework.game.BitboardGame;
import org.toop.framework.game.players.LocalPlayer;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.ArrayList;
import java.util.List;
@@ -11,7 +13,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class Server implements GameServer {
final private Map<String, Class<? extends TurnBasedGame>> gameTypes;
public List<OnlineGame<TurnBasedGame>> games = new ArrayList<>();
final private List<OnlineGame<TurnBasedGame>> games = new ArrayList<>();
final private Map<Long, ServerUser> users = new ConcurrentHashMap<>();
public Server(Map<String, Class<? extends TurnBasedGame>> gameTypes) {
@@ -30,20 +32,25 @@ public class Server implements GameServer {
return gameTypes.keySet().toArray(new String[0]);
}
// public List<OnlineGame<BitboardGame<?>>> ongoingGames() {
// return List.of();
// }
public List<OnlineGame<TurnBasedGame>> ongoingGames() {
return games;
}
public void startGame(String gameType, User... users) {
if (!gameTypes.containsKey(gameType)) return;
try {
var game = new Game(gameTypes.get(gameType).getDeclaredConstructor().newInstance(), users);
games.addLast(game);
} catch (Exception e) {
throw new RuntimeException(e);
}
Player[] players = new Player[users.length];
for (int i = 0; i < users.length; i++) {
players[i] = new LocalPlayer(users[i].name());
}
var game = new Game(gameTypes.get(gameType).getDeclaredConstructor().newInstance(), users);
game.game().init(players);
games.addLast(game);
} catch (Exception ignored) {}
}
public String[] onlineUsers() {

View File

@@ -1,11 +1,97 @@
package org.toop.framework.networking.server;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ServerTest {
static class TurnBasedGameMock implements TurnBasedGame {
private Player[] players;
public TurnBasedGameMock() {}
@Override
public void init(Player[] players) {
this.players = players;
}
@Override
public int getCurrentTurn() {
return 0;
}
@Override
public int getPlayerCount() {
return 0;
}
@Override
public int getWinner() {
return 0;
}
@Override
public long[] getBoard() {
return new long[0];
}
@Override
public TurnBasedGame deepCopy() {
return null;
}
@Override
public long getLegalMoves() {
return 0;
}
@Override
public PlayResult play(long move) {
return null;
}
@Override
public Player getPlayer(int index) {
return null;
}
}
private Server server;
@BeforeEach
public void setup() {
new Server();
void setup() {
var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
games.put("tictactoe", TurnBasedGameMock.class);
games.put("reversi", TurnBasedGameMock.class);
server = new Server(games);
}
@Test
void testGameTypes() {
String[] expected = {"tictactoe", "reversi"};
String[] actual = server.gameTypes();
Arrays.sort(expected);
Arrays.sort(actual);
Assertions.assertArrayEquals(expected, actual);
}
@Test
void testStartGame() {
server.startGame("tictactoe", new User(0, "A"), new User(1, "B"));
Assertions.assertEquals(1, server.ongoingGames().size());
}
}