Merge remote-tracking branch 'origin/Development' into Reversi

# Conflicts:
#	app/src/main/java/org/toop/app/canvas/GameCanvas.java
#	app/src/main/java/org/toop/app/layer/layers/MainLayer.java
#	game/src/main/java/org/toop/game/Game.java
#	game/src/main/java/org/toop/game/othello/Othello.java
#	game/src/main/java/org/toop/game/othello/OthelloAI.java
This commit is contained in:
Ticho Hidding
2025-10-14 11:25:46 +02:00
107 changed files with 4507 additions and 3457 deletions

View File

@@ -3,36 +3,39 @@ package org.toop.game;
import java.util.Arrays;
public abstract class Game {
public enum State {
NORMAL, DRAW, WIN,
}
public enum State {
NORMAL,
DRAW,
WIN,
}
public record Move(int position, char value) {}
public record Move(int position, char value) {}
public record Score(int player1Score, int player2Score) {}
public static final char EMPTY = (char)0;
public final int rowSize;
public final int columnSize;
public final char[] board;
public final int rowSize;
public final int columnSize;
public final char[] board;
protected Game(int rowSize, int columnSize) {
assert rowSize > 0 && columnSize > 0;
protected Game(int rowSize, int columnSize) {
assert rowSize > 0 && columnSize > 0;
this.rowSize = rowSize;
this.columnSize = columnSize;
this.rowSize = rowSize;
this.columnSize = columnSize;
board = new char[rowSize * columnSize];
Arrays.fill(board, EMPTY);
}
board = new char[rowSize * columnSize];
Arrays.fill(board, EMPTY);
}
protected Game(Game other) {
rowSize = other.rowSize;
columnSize = other.columnSize;
board = Arrays.copyOf(other.board, other.board.length);
}
protected Game(Game other) {
rowSize = other.rowSize;
columnSize = other.columnSize;
board = Arrays.copyOf(other.board, other.board.length);
}
public abstract Move[] getLegalMoves();
public abstract State play(Move move);
}
public abstract Move[] getLegalMoves();
public abstract State play(Move move);
}

View File

@@ -1,25 +1,27 @@
package org.toop.game;
public abstract class TurnBasedGame extends Game {
public final int turns;
public final int turns;
protected int currentTurn;
protected int currentTurn;
protected TurnBasedGame(int rowSize, int columnSize, int turns) {
super(rowSize, columnSize);
protected TurnBasedGame(int rowSize, int columnSize, int turns) {
super(rowSize, columnSize);
assert turns >= 2;
this.turns = turns;
}
}
protected TurnBasedGame(TurnBasedGame other) {
super(other);
turns = other.turns;
currentTurn = other.currentTurn;
}
protected TurnBasedGame(TurnBasedGame other) {
super(other);
turns = other.turns;
currentTurn = other.currentTurn;
}
protected void nextTurn() {
currentTurn = (currentTurn + 1) % turns;
}
protected void nextTurn() {
currentTurn = (currentTurn + 1) % turns;
}
public int getCurrentTurn() { return currentTurn; }
}
public int getCurrentTurn() {
return currentTurn;
}
}

View File

@@ -1,8 +1,7 @@
package org.toop.game.tictactoe;
import org.toop.game.TurnBasedGame;
import java.util.ArrayList;
import org.toop.game.TurnBasedGame;
public final class TicTacToe extends TurnBasedGame {
private int movesLeft;
@@ -19,8 +18,8 @@ public final class TicTacToe extends TurnBasedGame {
@Override
public Move[] getLegalMoves() {
final ArrayList<Move> legalMoves = new ArrayList<>();
final char currentValue = getCurrentValue();
final ArrayList<Move> legalMoves = new ArrayList<>();
final char currentValue = getCurrentValue();
for (int i = 0; i < board.length; i++) {
if (board[i] == EMPTY) {
@@ -44,12 +43,12 @@ public final class TicTacToe extends TurnBasedGame {
return State.WIN;
}
nextTurn();
nextTurn();
if (movesLeft <= 2) {
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
return State.DRAW;
}
if (movesLeft <= 0 || checkForEarlyDraw(this)) {
return State.DRAW;
}
}
return State.NORMAL;
@@ -60,7 +59,9 @@ public final class TicTacToe extends TurnBasedGame {
for (int i = 0; i < 3; i++) {
final int index = i * 3;
if (board[index] != EMPTY && board[index] == board[index + 1] && board[index] == board[index + 2]) {
if (board[index] != EMPTY
&& board[index] == board[index + 1]
&& board[index] == board[index + 2]) {
return true;
}
}
@@ -83,7 +84,7 @@ public final class TicTacToe extends TurnBasedGame {
private boolean checkForEarlyDraw(TicTacToe game) {
for (final Move move : game.getLegalMoves()) {
final TicTacToe copy = new TicTacToe(game);
final TicTacToe copy = new TicTacToe(game);
if (copy.play(move) == State.WIN || !checkForEarlyDraw(copy)) {
return false;
@@ -93,7 +94,7 @@ public final class TicTacToe extends TurnBasedGame {
return true;
}
private char getCurrentValue() {
return currentTurn == 0? 'X' : 'O';
}
}
private char getCurrentValue() {
return currentTurn == 0 ? 'X' : 'O';
}
}