mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Tests and better instantiation
This commit is contained in:
@@ -141,17 +141,6 @@ public final class App extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stage.show();
|
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) {
|
private void setKeybinds(StackPane root) {
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import org.toop.framework.game.players.OnlinePlayer;
|
|||||||
|
|
||||||
public class ReversiBitController extends GenericGameController {
|
public class ReversiBitController extends GenericGameController {
|
||||||
public ReversiBitController(Player[] players) {
|
public ReversiBitController(Player[] players) {
|
||||||
BitboardReversi game = new BitboardReversi(players);
|
BitboardReversi game = new BitboardReversi();
|
||||||
|
game.init(players);
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
||||||
for (Player player : players) {
|
for (Player player : players) {
|
||||||
if (player instanceof OnlinePlayer){
|
if (player instanceof OnlinePlayer){
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import org.toop.framework.game.players.OnlinePlayer;
|
|||||||
|
|
||||||
public class TicTacToeBitController extends GenericGameController {
|
public class TicTacToeBitController extends GenericGameController {
|
||||||
public TicTacToeBitController(Player[] players) {
|
public TicTacToeBitController(Player[] players) {
|
||||||
BitboardTicTacToe game = new BitboardTicTacToe(players);
|
BitboardTicTacToe game = new BitboardTicTacToe();
|
||||||
|
game.init(players);
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
||||||
for (Player player : players) {
|
for (Player player : players) {
|
||||||
if (player instanceof OnlinePlayer){
|
if (player instanceof OnlinePlayer){
|
||||||
|
|||||||
@@ -13,13 +13,19 @@ public abstract class BitboardGame implements TurnBasedGame {
|
|||||||
private Player[] players;
|
private Player[] players;
|
||||||
|
|
||||||
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
// 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 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.columnSize = columnSize;
|
||||||
this.rowSize = rowSize;
|
this.rowSize = rowSize;
|
||||||
this.players = players;
|
this.playerCount = playerCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
this.players = players;
|
||||||
this.playerBitboard = new long[playerCount];
|
this.playerBitboard = new long[playerCount];
|
||||||
|
|
||||||
Arrays.fill(playerBitboard, 0L);
|
Arrays.fill(playerBitboard, 0L);
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ public class BitboardReversi extends BitboardGame {
|
|||||||
private final long notAFile = 0xfefefefefefefefeL;
|
private final long notAFile = 0xfefefefefefefefeL;
|
||||||
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
||||||
|
|
||||||
public BitboardReversi(Player[] players) {
|
public BitboardReversi() {
|
||||||
super(8, 8, 2, players);
|
super(8, 8, 2);
|
||||||
|
|
||||||
// Black (player 0)
|
// Black (player 0)
|
||||||
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
|
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)));
|
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
super.init(players);
|
||||||
|
}
|
||||||
|
|
||||||
public BitboardReversi(BitboardReversi other) {
|
public BitboardReversi(BitboardReversi other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,15 @@ public class BitboardTicTacToe extends BitboardGame {
|
|||||||
0b001010100L // anti-diagonal
|
0b001010100L // anti-diagonal
|
||||||
};
|
};
|
||||||
|
|
||||||
public BitboardTicTacToe(Player[] players) {
|
public BitboardTicTacToe() {
|
||||||
super(3, 3, 2, players);
|
super(3, 3, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
super.init(players);
|
||||||
|
}
|
||||||
|
|
||||||
public BitboardTicTacToe(BitboardTicTacToe other) {
|
public BitboardTicTacToe(BitboardTicTacToe other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.toop.framework.gameFramework.model.game;
|
package org.toop.framework.gameFramework.model.game;
|
||||||
|
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
public interface TurnBasedGame extends Playable, PlayerProvider, BoardProvider, DeepCopyable<TurnBasedGame> {
|
public interface TurnBasedGame extends Playable, PlayerProvider, BoardProvider, DeepCopyable<TurnBasedGame> {
|
||||||
|
void init(Player[] players);
|
||||||
int getCurrentTurn();
|
int getCurrentTurn();
|
||||||
int getPlayerCount();
|
int getPlayerCount();
|
||||||
int getWinner();
|
int getWinner();
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.toop.framework.gameFramework.model.player;
|
package org.toop.framework.gameFramework.model.player;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.DeepCopyable;
|
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> {}
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package org.toop.framework.networking.server;
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
import org.toop.framework.game.BitboardGame;
|
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.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -11,7 +13,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
public class Server implements GameServer {
|
public class Server implements GameServer {
|
||||||
|
|
||||||
final private Map<String, Class<? extends TurnBasedGame>> gameTypes;
|
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<>();
|
final private Map<Long, ServerUser> users = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public Server(Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
public Server(Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
||||||
@@ -30,20 +32,25 @@ public class Server implements GameServer {
|
|||||||
return gameTypes.keySet().toArray(new String[0]);
|
return gameTypes.keySet().toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public List<OnlineGame<BitboardGame<?>>> ongoingGames() {
|
public List<OnlineGame<TurnBasedGame>> ongoingGames() {
|
||||||
// return List.of();
|
return games;
|
||||||
// }
|
}
|
||||||
|
|
||||||
public void startGame(String gameType, User... users) {
|
public void startGame(String gameType, User... users) {
|
||||||
if (!gameTypes.containsKey(gameType)) return;
|
if (!gameTypes.containsKey(gameType)) return;
|
||||||
|
|
||||||
try {
|
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() {
|
public String[] onlineUsers() {
|
||||||
|
|||||||
@@ -1,11 +1,97 @@
|
|||||||
package org.toop.framework.networking.server;
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
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 {
|
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
|
@BeforeEach
|
||||||
public void setup() {
|
void setup() {
|
||||||
new Server();
|
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user