Made the GameState enum it's own file and fixed imports

This commit is contained in:
2025-10-29 14:26:41 +01:00
parent ff611724e7
commit 44d8ffbef2
10 changed files with 63 additions and 52 deletions

View File

@@ -1,6 +1,7 @@
package org.toop.game.Connect4;
import org.toop.game.TurnBasedGame;
import org.toop.game.enumerators.GameState;
import java.util.ArrayList;
@@ -31,7 +32,7 @@ public class Connect4 extends TurnBasedGame {
}
@Override
public State play(Move move) {
public GameState play(Move move) {
assert move != null;
assert move.position() >= 0 && move.position() < board.length;
assert move.value() == getCurrentValue();
@@ -49,13 +50,13 @@ public class Connect4 extends TurnBasedGame {
movesLeft--;
if (checkForWin()) {
return State.WIN;
return GameState.WIN;
}
nextTurn();
return State.NORMAL;
return GameState.NORMAL;
}
private boolean checkForWin() {

View File

@@ -2,6 +2,7 @@ package org.toop.game.Connect4;
import org.toop.game.AI;
import org.toop.game.Game;
import org.toop.game.enumerators.GameState;
import org.toop.game.tictactoe.TicTacToe;
public class Connect4AI extends AI<Connect4> {
@@ -34,11 +35,11 @@ public class Connect4AI extends AI<Connect4> {
private int getMoveScore(Connect4 game, int depth, Game.Move move, boolean maximizing) {
final Connect4 copy = new Connect4(game);
final Game.State state = copy.play(move);
final GameState state = copy.play(move);
switch (state) {
case Game.State.DRAW: return 0;
case Game.State.WIN: return maximizing? depth + 1 : -depth - 1;
case GameState.DRAW: return 0;
case GameState.WIN: return maximizing? depth + 1 : -depth - 1;
}
if (depth <= 0) {

View File

@@ -1,16 +1,10 @@
package org.toop.game;
import org.toop.game.enumerators.GameState;
import java.util.Arrays;
public abstract class Game {
public enum State {
NORMAL,
DRAW,
WIN,
TURN_SKIPPED,
}
public record Move(int position, char value) {}
public static final char EMPTY = (char)0;
@@ -37,5 +31,5 @@ public abstract class Game {
public abstract Move[] getLegalMoves();
public abstract State play(Move move);
public abstract GameState play(Move move);
}

View File

@@ -0,0 +1,9 @@
package org.toop.game.enumerators;
public enum GameState {
NORMAL,
DRAW,
WIN,
TURN_SKIPPED,
}

View File

@@ -2,6 +2,7 @@ package org.toop.game.reversi;
import org.toop.game.Game;
import org.toop.game.TurnBasedGame;
import org.toop.game.enumerators.GameState;
import java.awt.*;
import java.util.ArrayList;
@@ -130,7 +131,7 @@ public final class Reversi extends TurnBasedGame {
return boardGrid;
}
@Override
public State play(Move move) {
public GameState play(Move move) {
Move[] legalMoves = getLegalMoves();
boolean moveIsLegal = false;
for (Move legalMove : legalMoves) {
@@ -151,19 +152,19 @@ public final class Reversi extends TurnBasedGame {
if (getLegalMoves().length == 0) {
skipMyTurn();
if (getLegalMoves().length > 0) {
return State.TURN_SKIPPED;
return GameState.TURN_SKIPPED;
}
else {
Score score = getScore();
if (score.player1Score() == score.player2Score()) {
return State.DRAW;
return GameState.DRAW;
}
else {
return State.WIN;
return GameState.WIN;
}
}
}
return State.NORMAL;
return GameState.NORMAL;
}
return null;
}

View File

@@ -2,6 +2,7 @@ package org.toop.game.tictactoe;
import java.util.ArrayList;
import org.toop.game.TurnBasedGame;
import org.toop.game.enumerators.GameState;
public final class TicTacToe extends TurnBasedGame {
private int movesLeft;
@@ -31,7 +32,7 @@ public final class TicTacToe extends TurnBasedGame {
}
@Override
public State play(Move move) {
public GameState play(Move move) {
assert move != null;
assert move.position() >= 0 && move.position() < board.length;
assert move.value() == getCurrentValue();
@@ -40,18 +41,18 @@ public final class TicTacToe extends TurnBasedGame {
movesLeft--;
if (checkForWin()) {
return State.WIN;
return GameState.WIN;
}
nextTurn();
if (movesLeft <= 2) {
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
return State.DRAW;
return GameState.DRAW;
}
}
return State.NORMAL;
return GameState.NORMAL;
}
private boolean checkForWin() {
@@ -86,7 +87,7 @@ public final class TicTacToe extends TurnBasedGame {
for (final Move move : game.getLegalMoves()) {
final TicTacToe copy = new TicTacToe(game);
if (copy.play(move) == State.WIN || !checkForEarlyDraw(copy)) {
if (copy.play(move) == GameState.WIN || !checkForEarlyDraw(copy)) {
return false;
}
}

View File

@@ -2,6 +2,7 @@ package org.toop.game.tictactoe;
import org.toop.game.AI;
import org.toop.game.Game;
import org.toop.game.enumerators.GameState;
public final class TicTacToeAI extends AI<TicTacToe> {
@Override
@@ -59,11 +60,11 @@ public final class TicTacToeAI extends AI<TicTacToe> {
private int getMoveScore(TicTacToe game, int depth, Game.Move move, boolean maximizing) {
final TicTacToe copy = new TicTacToe(game);
final Game.State state = copy.play(move);
final GameState state = copy.play(move);
switch (state) {
case Game.State.DRAW: return 0;
case Game.State.WIN: return maximizing? depth + 1 : -depth - 1;
case GameState.DRAW: return 0;
case GameState.WIN: return maximizing? depth + 1 : -depth - 1;
}
if (depth <= 0) {