mirror of
https://github.com/2OOP/pism.git
synced 2026-02-05 03:14:50 +00:00
Tests and better instantiation
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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> {}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user