This commit is contained in:
ramollia
2025-09-24 15:52:58 +02:00
parent 9fdd74326a
commit da777f5300
38 changed files with 35 additions and 37 deletions

View File

@@ -0,0 +1,57 @@
package org.toop.game;
// Todo: refactor
public abstract class GameBase {
public enum State {
INVALID,
NORMAL,
DRAW,
WIN,
}
public static char EMPTY = '-';
protected int size;
public char[] grid;
protected Player[] players;
public int currentPlayer;
public GameBase(int size, Player player1, Player player2) {
this.size = size;
grid = new char[size * size];
for (int i = 0; i < grid.length; i++) {
grid[i] = EMPTY;
}
players = new Player[2];
players[0] = player1;
players[1] = player2;
currentPlayer = 0;
}
public boolean isInside(int index) {
return index >= 0 && index < size * size;
}
public int getSize() {
return size;
}
public char[] getGrid() {
return grid;
}
public Player[] getPlayers() {
return players;
}
public Player getCurrentPlayer() {
return players[currentPlayer];
}
public abstract State play(int index);
}

View File

@@ -0,0 +1,20 @@
package org.toop.game;
// Todo: refactor
public class Player {
String name;
char symbol;
Player(String name, char symbol) {
this.name = name;
this.symbol = symbol;
}
public String getName() {
return this.name;
}
public char getSymbol() {
return this.symbol;
}
}

View File

@@ -0,0 +1,225 @@
package org.toop.tictactoe;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.game.GameBase;
import org.toop.game.Player;
import org.toop.backend.tictactoe.ParsedCommand;
import org.toop.backend.tictactoe.TicTacToeServerCommand;
// Todo: refactor
public class TicTacToe extends GameBase implements Runnable {
protected static final Logger logger = LogManager.getLogger(TicTacToe.class);
public Thread gameThread;
public String gameId;
public BlockingQueue<ParsedCommand> commandQueue = new LinkedBlockingQueue<>();
public BlockingQueue<String> sendQueue = new LinkedBlockingQueue<>();
public int movesLeft;
public TicTacToe(String player1, String player2) {
super(3, new Player(player1, 'X'), new Player(player2, 'O'));
movesLeft = size * size;
}
/**
* Used for the server.
*
* @param player1
* @param player2
* @param gameId
*/
public TicTacToe(String player1, String player2, String gameId) {
super(3, new Player(player1, 'X'), new Player(player2, 'O'));
this.gameId = gameId;
movesLeft = size * size;
}
public void addCommandToQueue(ParsedCommand command) {
commandQueue.add(command);
}
private ParsedCommand takeFromCommandQueue() {
try {
return this.commandQueue.take();
} catch (InterruptedException e) {
logger.error("Taking from queue interrupted, in game with id: {}", this.gameId);
return null;
}
}
private void addSendToQueue(String send) {
try {
sendQueue.put(send);
} catch (InterruptedException e) {
logger.error("Sending to queue interrupted, in game with id: {}", this.gameId);
}
}
@Override
public void run() {
this.gameThread = new Thread(this::gameThread);
this.gameThread.start();
}
private void gameThread() {
boolean running = true;
while (running) {
ParsedCommand cmd = takeFromCommandQueue();
// Get next command if there was no command
if (cmd == null) {
continue;
}
// Do something based which command was given
switch (cmd.command) {
case TicTacToeServerCommand.MOVE:
{
// TODO: Check if it is this player's turn, not required for local play (I
// think?).
// Convert given argument to integer
Object arg = cmd.arguments.getFirst();
int index;
try {
index = Integer.parseInt((String) arg);
} catch (Exception e) {
logger.error("Error parsing argument to String or Integer");
continue;
}
// Attempt to play the move
State state = play(index);
if (state != State.INVALID) {
// Tell all players who made a move and what move was made
// TODO: What is the reaction of the game? WIN, DRAW etc?
String player = getCurrentPlayer().getName();
addSendToQueue(
"SVR GAME MOVE {PLAYER: \""
+ player
+ "\", DETAILS: \"<reactie spel op zet>\",MOVE: \""
+ index
+ "\"}\n");
}
// Check move result
switch (state) {
case State.WIN:
{
// Win
running = false;
addSendToQueue(
"SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\","
+ " PLAYERTWOSCORE: \"<score speler2>\", COMMENT:"
+ " \"<commentaar op resultaat>\"}\n");
break;
}
case State.DRAW:
{
// Draw
running = false;
addSendToQueue(
"SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\","
+ " PLAYERTWOSCORE: \"<score speler2>\", COMMENT:"
+ " \"<commentaar op resultaat>\"}\n");
break;
}
case State.NORMAL:
{
// Valid move but not end of game
addSendToQueue("SVR GAME YOURTURN");
break;
}
case State.INVALID:
{
// Invalid move
break;
}
}
}
}
}
}
@Override
public State play(int index) {
if (!validateMove(index)) {
return State.INVALID;
}
grid[index] = getCurrentPlayer().getSymbol();
movesLeft--;
if (checkWin()) {
return State.WIN;
}
if (movesLeft <= 0) {
return State.DRAW;
}
currentPlayer = (currentPlayer + 1) % players.length;
return State.NORMAL;
}
public boolean validateMove(int index) {
return movesLeft > 0 && isInside(index) && grid[index] == EMPTY;
}
public boolean checkWin() {
// Horizontal
for (int i = 0; i < 3; i++) {
final int index = i * 3;
if (grid[index] != EMPTY
&& grid[index] == grid[index + 1]
&& grid[index] == grid[index + 2]) {
return true;
}
}
// Vertical
for (int i = 0; i < 3; i++) {
int index = i;
if (grid[index] != EMPTY
&& grid[index] == grid[index + 3]
&& grid[index] == grid[index + 6]) {
return true;
}
}
// B-Slash
if (grid[0] != EMPTY && grid[0] == grid[4] && grid[0] == grid[8]) {
return true;
}
// F-Slash
if (grid[2] != EMPTY && grid[2] == grid[4] && grid[2] == grid[6]) {
return true;
}
return false;
}
/** For AI use only. */
public void decrementMovesLeft() {
movesLeft--;
}
/** This method copies the board, mainly for AI use. */
public TicTacToe copyBoard() {
TicTacToe clone = new TicTacToe(players[0].getName(), players[1].getName());
System.arraycopy(this.grid, 0, clone.grid, 0, this.grid.length);
clone.movesLeft = this.movesLeft;
clone.currentPlayer = this.currentPlayer;
return clone;
}
}

View File

@@ -0,0 +1,139 @@
package org.toop.tictactoe;
import org.toop.game.GameBase;
// Todo: refactor
public class TicTacToeAI {
/**
* This method tries to find the best move by seeing if it can set a winning move, if not, it
* will do a minimax.
*/
public int findBestMove(TicTacToe game) {
int bestVal = -100; // set bestVal to something impossible
int bestMove = 10; // set bestMove to something impossible
int winningMove = -5;
boolean empty = true;
for (char cell : game.grid) {
if (!(cell == GameBase.EMPTY)) {
empty = false;
break;
}
}
if (empty) { // start in a random corner
return switch ((int) (Math.random() * 4)) {
case 1 -> 2;
case 2 -> 6;
case 3 -> 8;
default -> 0;
};
}
// simulate all possible moves on the field
for (int i = 0; i < game.grid.length; i++) {
if (game.validateMove(i)) { // check if the move is legal here
TicTacToe copyGame = game.copyBoard(); // make a copy of the game
GameBase.State result = copyGame.play(i); // play a move on the copy board
int thisMoveValue;
if (result == GameBase.State.WIN) {
return i; // just return right away if you can win on the next move
}
for (int index = 0; index < game.grid.length; index++) {
if (game.validateMove(index)) {
TicTacToe opponentCopy = copyGame.copyBoard();
GameBase.State opponentResult = opponentCopy.play(index);
if (opponentResult == GameBase.State.WIN) {
winningMove = index;
}
}
}
thisMoveValue =
doMinimax(copyGame, game.movesLeft, false); // else look at other moves
if (thisMoveValue
> bestVal) { // if better move than the current best, change the move
bestVal = thisMoveValue;
bestMove = i;
}
}
}
if (winningMove > -5) {
return winningMove;
}
return bestMove; // return the best move when we've done everything
}
/**
* This method simulates all the possible future moves in the game through a copy in search of
* the best move.
*/
public int doMinimax(TicTacToe game, int depth, boolean maximizing) {
boolean state = game.checkWin(); // check for a win (base case stuff)
if (state) {
if (maximizing) {
// it's the maximizing players turn and someone has won. this is not good, so return
// a negative value
return -10 + depth;
} else {
// it is the turn of the AI and it has won! this is good for us, so return a
// positive value above 0
return 10 - depth;
}
} else {
boolean empty = false;
for (char cell :
game.grid) { // else, look at draw conditions. we check per cell if it's empty
// or not
if (cell == GameBase.EMPTY) {
empty = true; // if a thing is empty, set to true
break; // break the loop
}
}
if (!empty
|| depth == 0) { // if the grid is full or the depth is 0 (both meaning game is
// over) return 0 for draw
return 0;
}
}
int bestVal; // set the value to the highest possible
if (maximizing) { // it's the maximizing players turn, the AI
bestVal = -100;
for (int i = 0; i < game.grid.length; i++) { // loop through the grid
if (game.validateMove(i)) {
TicTacToe copyGame = game.copyBoard();
copyGame.play(i); // play the move on a copy board
int value =
doMinimax(copyGame, depth - 1, false); // keep going with the minimax
bestVal =
Math.max(
bestVal,
value); // select the best value for the maximizing player (the
// AI)
}
}
} else { // it's the minimizing players turn, the player
bestVal = 100;
for (int i = 0; i < game.grid.length; i++) { // loop through the grid
if (game.validateMove(i)) {
TicTacToe copyGame = game.copyBoard();
copyGame.play(i); // play the move on a copy board
int value = doMinimax(copyGame, depth - 1, true); // keep miniMaxing
bestVal =
Math.min(
bestVal,
value); // select the lowest score for the minimizing player,
// they want to make it hard for us
}
}
}
return bestVal;
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="AppConfig">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%17.17t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>