mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Moved files to better named folders
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package org.toop.eventbus;
|
package org.toop.eventbus;
|
||||||
|
|
||||||
|
import org.toop.server.backend.tictactoe.ServerCommand;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -123,7 +125,7 @@ public class Events implements IEvents {
|
|||||||
/**
|
/**
|
||||||
* Triggers when a command is sent to a server.
|
* Triggers when a command is sent to a server.
|
||||||
*/
|
*/
|
||||||
public record OnCommand(org.toop.server.ServerCommand command, String[] args, String result) {}
|
public record OnCommand(ServerCommand command, String[] args, String result) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggers when the server client receives a message.
|
* Triggers when the server client receives a message.
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
package org.toop.server;
|
|
||||||
|
|
||||||
import java.util.EnumSet;
|
|
||||||
|
|
||||||
public enum ServerCommand {
|
|
||||||
/**
|
|
||||||
* Login, "username"
|
|
||||||
*/
|
|
||||||
LOGIN,
|
|
||||||
/**
|
|
||||||
* Logout, "username"
|
|
||||||
*/
|
|
||||||
LOGOUT,
|
|
||||||
EXIT,
|
|
||||||
QUIT,
|
|
||||||
DISCONNECT,
|
|
||||||
BYE,
|
|
||||||
GET,
|
|
||||||
SUBSCRIBE,
|
|
||||||
MOVE,
|
|
||||||
CHALLENGE,
|
|
||||||
FORFEIT,
|
|
||||||
MESSAGE,
|
|
||||||
HELP;
|
|
||||||
|
|
||||||
private static final EnumSet<ServerCommand> VALID_COMMANDS = EnumSet.of(
|
|
||||||
ServerCommand.LOGIN, ServerCommand.LOGOUT, ServerCommand.EXIT,
|
|
||||||
ServerCommand.QUIT, ServerCommand.DISCONNECT, ServerCommand.BYE,
|
|
||||||
ServerCommand.GET, ServerCommand.SUBSCRIBE, ServerCommand.MOVE,
|
|
||||||
ServerCommand.CHALLENGE, ServerCommand.FORFEIT,
|
|
||||||
ServerCommand.MESSAGE, ServerCommand.HELP
|
|
||||||
);
|
|
||||||
|
|
||||||
public static EnumSet<ServerCommand> getValidCommands() {
|
|
||||||
return VALID_COMMANDS;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Garbage code.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param command Checks if string is a valid command.
|
|
||||||
* @return returns a boolean if string is a valid command.
|
|
||||||
*/
|
|
||||||
public static boolean isValid(String command) {
|
|
||||||
try {
|
|
||||||
ServerCommand.valueOf(command.toUpperCase());
|
|
||||||
return true;
|
|
||||||
} catch (IllegalArgumentException err) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Return something better
|
|
||||||
/**
|
|
||||||
* @param command Converts a string into a ServerCommand.
|
|
||||||
* @return returns a ServerCommand enum.
|
|
||||||
*/
|
|
||||||
public static ServerCommand getCommand(String command) {
|
|
||||||
if (isValid(command)) {
|
|
||||||
ServerCommand.valueOf(command.toUpperCase());
|
|
||||||
return ServerCommand.valueOf(command.toUpperCase());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,39 @@
|
|||||||
package org.toop.server.backend.tictactoe;
|
package org.toop.server.backend.tictactoe;
|
||||||
|
|
||||||
|
import org.toop.server.backend.tictactoe.game.TicTacToe;
|
||||||
import org.toop.server.backend.TcpServer;
|
import org.toop.server.backend.TcpServer;
|
||||||
import org.toop.game.TTT;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class TicTacToeServer extends TcpServer {
|
public class TicTacToeServer extends TcpServer {
|
||||||
public TicTacToeServer(int port) throws IOException {
|
public TicTacToeServer(int port) throws IOException {
|
||||||
super(port);
|
super(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
logger.info("Server listening on port {}", port);
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
Socket clientSocket = this.serverSocket.accept();
|
||||||
|
logger.info("Connected to client: {}", clientSocket.getInetAddress());
|
||||||
|
|
||||||
|
new Thread(() -> this.startWorkers(clientSocket)).start();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void gameThread(String main_character, String opponent_character) {
|
private void gameThread(String main_character, String opponent_character) {
|
||||||
TTT ticTacToe = new TTT(main_character, opponent_character);
|
TicTacToe ticTacToe = new TicTacToe(main_character, opponent_character);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// TODO: Game
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package org.toop.server.backend.tictactoe;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
public enum TicTacToeServerCommand {
|
||||||
|
/**
|
||||||
|
* Login, "username"
|
||||||
|
*/
|
||||||
|
LOGIN,
|
||||||
|
/**
|
||||||
|
* Logout, "username"
|
||||||
|
*/
|
||||||
|
LOGOUT,
|
||||||
|
EXIT,
|
||||||
|
QUIT,
|
||||||
|
DISCONNECT,
|
||||||
|
BYE,
|
||||||
|
GET,
|
||||||
|
SUBSCRIBE,
|
||||||
|
MOVE,
|
||||||
|
CHALLENGE,
|
||||||
|
FORFEIT,
|
||||||
|
MESSAGE,
|
||||||
|
HELP;
|
||||||
|
|
||||||
|
private static final EnumSet<TicTacToeServerCommand> VALID_COMMANDS = EnumSet.of(
|
||||||
|
TicTacToeServerCommand.LOGIN, TicTacToeServerCommand.LOGOUT, TicTacToeServerCommand.EXIT,
|
||||||
|
TicTacToeServerCommand.QUIT, TicTacToeServerCommand.DISCONNECT, TicTacToeServerCommand.BYE,
|
||||||
|
TicTacToeServerCommand.GET, TicTacToeServerCommand.SUBSCRIBE, TicTacToeServerCommand.MOVE,
|
||||||
|
TicTacToeServerCommand.CHALLENGE, TicTacToeServerCommand.FORFEIT,
|
||||||
|
TicTacToeServerCommand.MESSAGE, TicTacToeServerCommand.HELP
|
||||||
|
);
|
||||||
|
|
||||||
|
public static EnumSet<TicTacToeServerCommand> getValidCommands() {
|
||||||
|
return VALID_COMMANDS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Garbage code.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param command Checks if string is a valid command.
|
||||||
|
* @return returns a boolean if string is a valid command.
|
||||||
|
*/
|
||||||
|
public static boolean isValid(String command) {
|
||||||
|
try {
|
||||||
|
TicTacToeServerCommand.valueOf(command.toUpperCase());
|
||||||
|
return true;
|
||||||
|
} catch (IllegalArgumentException err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Return something better
|
||||||
|
/**
|
||||||
|
* @param command Converts a string into a TicTacToeServerCommand.
|
||||||
|
* @return returns a TicTacToeServerCommand enum.
|
||||||
|
*/
|
||||||
|
public static TicTacToeServerCommand getCommand(String command) {
|
||||||
|
if (isValid(command)) {
|
||||||
|
TicTacToeServerCommand.valueOf(command.toUpperCase());
|
||||||
|
return TicTacToeServerCommand.valueOf(command.toUpperCase());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
package org.toop.server;
|
package org.toop.server.backend.tictactoe;
|
||||||
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
public enum ServerMessage {
|
public enum TicTacToeServerMessage {
|
||||||
OK,
|
OK,
|
||||||
ERR,
|
ERR,
|
||||||
SVR;
|
SVR;
|
||||||
|
|
||||||
private static final EnumSet<ServerMessage> VALID_COMMANDS = EnumSet.of(
|
private static final EnumSet<TicTacToeServerMessage> VALID_COMMANDS = EnumSet.of(
|
||||||
ServerMessage.OK, ServerMessage.ERR, ServerMessage.SVR
|
TicTacToeServerMessage.OK, TicTacToeServerMessage.ERR, TicTacToeServerMessage.SVR
|
||||||
);
|
);
|
||||||
|
|
||||||
public static EnumSet<ServerMessage> getValidCommands() {
|
public static EnumSet<TicTacToeServerMessage> getValidCommands() {
|
||||||
return VALID_COMMANDS;
|
return VALID_COMMANDS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ public enum ServerMessage {
|
|||||||
*/
|
*/
|
||||||
public static boolean isValid(String command) {
|
public static boolean isValid(String command) {
|
||||||
try {
|
try {
|
||||||
ServerMessage.valueOf(command.toUpperCase());
|
TicTacToeServerMessage.valueOf(command.toUpperCase());
|
||||||
return true;
|
return true;
|
||||||
} catch (IllegalArgumentException err) {
|
} catch (IllegalArgumentException err) {
|
||||||
return false;
|
return false;
|
||||||
@@ -34,10 +34,10 @@ public enum ServerMessage {
|
|||||||
* @param command Converts a string into a ServerCommand.
|
* @param command Converts a string into a ServerCommand.
|
||||||
* @return returns a ServerCommand enum.
|
* @return returns a ServerCommand enum.
|
||||||
*/
|
*/
|
||||||
public static ServerMessage getCommand(String command) {
|
public static TicTacToeServerMessage getCommand(String command) {
|
||||||
if (isValid(command)) {
|
if (isValid(command)) {
|
||||||
ServerMessage.valueOf(command.toUpperCase());
|
TicTacToeServerMessage.valueOf(command.toUpperCase());
|
||||||
return ServerMessage.valueOf(command.toUpperCase());
|
return TicTacToeServerMessage.valueOf(command.toUpperCase());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game;
|
package org.toop.server.backend.tictactoe.game;
|
||||||
|
|
||||||
public abstract class GameBase {
|
public abstract class GameBase {
|
||||||
protected Player[] players;
|
protected Player[] players;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game;
|
package org.toop.server.backend.tictactoe.game;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game;
|
package org.toop.server.backend.tictactoe.game;
|
||||||
|
|
||||||
public class Player {
|
public class Player {
|
||||||
private String name;
|
private String name;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game;
|
package org.toop.server.backend.tictactoe.game;
|
||||||
|
|
||||||
public enum State {
|
public enum State {
|
||||||
INVALID,
|
INVALID,
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game;
|
package org.toop.server.backend.tictactoe.game;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -3,7 +3,7 @@ package org.toop.server.frontend;
|
|||||||
import org.toop.Main;
|
import org.toop.Main;
|
||||||
import org.toop.eventbus.Events;
|
import org.toop.eventbus.Events;
|
||||||
import org.toop.eventbus.GlobalEventBus;
|
import org.toop.eventbus.GlobalEventBus;
|
||||||
import org.toop.server.ServerCommand;
|
import org.toop.server.backend.tictactoe.ServerCommand;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.toop.game.MinMaxTTT;
|
import org.toop.server.backend.tictactoe.game.MinMaxTicTacToe;
|
||||||
import org.toop.game.TTT;
|
import org.toop.server.backend.tictactoe.game.TicTacToe;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class MinMaxTTTTest {
|
class MinMaxTicTacToeTest {
|
||||||
|
|
||||||
// makegame makes a board situation so we can test the ai. thats it really, the rest is easy to follow id say
|
// makegame makes a board situation so we can test the AI. that's it really, the rest is easy to follow id say
|
||||||
private TTT makeGame(String board, int currentPlayer) {
|
private TicTacToe makeGame(String board, int currentPlayer) {
|
||||||
TTT game = new TTT("AI", "Human");
|
TicTacToe game = new TicTacToe("AI", "Human");
|
||||||
// Fill the board
|
// Fill the board
|
||||||
for (int i = 0; i < board.length(); i++) {
|
for (int i = 0; i < board.length(); i++) {
|
||||||
char c = board.charAt(i);
|
char c = board.charAt(i);
|
||||||
@@ -22,56 +22,56 @@ class MinMaxTTTTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindBestMove_AIImmediateWin() {
|
void testFindBestMove_AIImmediateWin() {
|
||||||
TTT game = makeGame("XX OO ", 0);
|
TicTacToe game = makeGame("XX OO ", 0);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int bestMove = ai.findBestMove(game);
|
int bestMove = ai.findBestMove(game);
|
||||||
assertEquals(2, bestMove, "AI has to take winning move at 2");
|
assertEquals(2, bestMove, "AI has to take winning move at 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindBestMove_BlockOpponentWin() {
|
void testFindBestMove_BlockOpponentWin() {
|
||||||
TTT game = makeGame("OO X ", 0); // 0 = AI's turn
|
TicTacToe game = makeGame("OO X ", 0); // 0 = AI's turn
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int bestMove = ai.findBestMove(game);
|
int bestMove = ai.findBestMove(game);
|
||||||
assertEquals(2, bestMove, "AI should block opponent win at 2");
|
assertEquals(2, bestMove, "AI should block opponent win at 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFindBestMove_ChooseDrawIfNoWin() {
|
void testFindBestMove_ChooseDrawIfNoWin() {
|
||||||
TTT game = makeGame("XOXOX O ", 0);
|
TicTacToe game = makeGame("XOXOX O ", 0);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int bestMove = ai.findBestMove(game);
|
int bestMove = ai.findBestMove(game);
|
||||||
assertTrue(bestMove == 6 || bestMove == 8, "AI should draw");
|
assertTrue(bestMove == 6 || bestMove == 8, "AI should draw");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMinimax_ScoreWin() {
|
void testMinimax_ScoreWin() {
|
||||||
TTT game = makeGame("XXX ", 0);
|
TicTacToe game = makeGame("XXX ", 0);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int score = ai.doMinimax(game, 5, false);
|
int score = ai.doMinimax(game, 5, false);
|
||||||
assertTrue(score > 0, "AI win scored positively");
|
assertTrue(score > 0, "AI win scored positively");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMinimax_ScoreLoss() {
|
void testMinimax_ScoreLoss() {
|
||||||
TTT game = makeGame("OOO ", 1);
|
TicTacToe game = makeGame("OOO ", 1);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int score = ai.doMinimax(game, 5, true);
|
int score = ai.doMinimax(game, 5, true);
|
||||||
assertTrue(score < 0, "AI loss is negative");
|
assertTrue(score < 0, "AI loss is negative");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMinimax_ScoreDraw() {
|
void testMinimax_ScoreDraw() {
|
||||||
TTT game = makeGame("XOXOXOOXO", 0);
|
TicTacToe game = makeGame("XOXOXOOXO", 0);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int score = ai.doMinimax(game, 5, true);
|
int score = ai.doMinimax(game, 5, true);
|
||||||
assertEquals(0, score, "Draw should be zero!");
|
assertEquals(0, score, "Draw should be zero!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMiniMax_MultipleMoves() {
|
void testMiniMax_MultipleMoves() {
|
||||||
TTT game = makeGame(" X OX O ", 0);
|
TicTacToe game = makeGame(" X OX O ", 0);
|
||||||
MinMaxTTT ai = new MinMaxTTT();
|
MinMaxTicTacToe ai = new MinMaxTicTacToe();
|
||||||
int bestMove = ai.findBestMove(game);
|
int bestMove = ai.findBestMove(game);
|
||||||
assertTrue(bestMove == 0 || bestMove == 2, "Can look at multiple moves!");
|
assertTrue(bestMove == 0 || bestMove == 2, "Can look at multiple moves!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user