start to reversi logic

This commit is contained in:
Ticho Hidding
2025-10-08 17:27:50 +02:00
parent a302f0d24d
commit 5a3490af2e
10 changed files with 265 additions and 33 deletions

View File

@@ -9,6 +9,8 @@ public abstract class Game {
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;

View File

@@ -1,19 +0,0 @@
package org.toop.game.othello;
import org.toop.game.TurnBasedGame;
public final class Othello extends TurnBasedGame {
Othello() {
super(8, 8, 2);
}
@Override
public Move[] getLegalMoves() {
return new Move[0];
}
@Override
public State play(Move move) {
return null;
}
}

View File

@@ -1,11 +0,0 @@
package org.toop.game.othello;
import org.toop.game.AI;
import org.toop.game.Game;
public final class OthelloAI extends AI<Othello> {
@Override
public Game.Move findBestMove(Othello game, int depth) {
return null;
}
}

View File

@@ -0,0 +1,74 @@
package org.toop.game.reversi;
import org.toop.game.Game;
import org.toop.game.TurnBasedGame;
import org.toop.game.tictactoe.TicTacToe;
public final class Reversi extends TurnBasedGame {
private int movesTaken;
public static final char FIRST_MOVE = 'B';
public Reversi() {
super(8, 8, 2);
addStartPieces();
}
public Reversi(Reversi other) {
super(other);
this.movesTaken = other.movesTaken;
}
private void addStartPieces() {
board[27] = 'W';
board[28] = 'B';
board[35] = 'B';
board[36] = 'W';
}
@Override
public Move[] getLegalMoves() {
char[][] boardGrid = makeBoardAGrid();
if(currentTurn == 1){
}
return new Move[0];
}
public char[][] makeBoardAGrid() {
char[][] boardGrid = new char[8][8];
for (int i = 0; i < 64; i++) {
boardGrid[i / 8][i % 8] = board[i];
}
return boardGrid;
}
@Override
public State play(Move move) {
return null;
}
public char getCurrentPlayer() {
if (currentTurn == 0){
return 'B';
}
else {
return 'W';
}
}
public Game.Score getScore(){
int player1Score = 0, player2Score = 0;
for (int count = 0; count < 63; count++) {
if (board[count] == 'W') {
player1Score += 1;
}
if (board[count] == 'B') {
player2Score += 1;
}
}
return new Game.Score(player1Score, player2Score);
}
}

View File

@@ -0,0 +1,11 @@
package org.toop.game.reversi;
import org.toop.game.AI;
import org.toop.game.Game;
public final class ReversiAI extends AI<Reversi> {
@Override
public Game.Move findBestMove(Reversi game, int depth) {
return null;
}
}

View File

@@ -0,0 +1,79 @@
package org.toop.game.tictactoe;
import org.toop.game.Game;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.toop.game.reversi.Reversi;
import org.toop.game.reversi.ReversiAI;
import static org.junit.jupiter.api.Assertions.*;
class ReversiTest {
private Reversi game;
private ReversiAI ai;
@BeforeEach
void setup() {
game = new Reversi();
ai = new ReversiAI();
}
@Test
void testCorrectStartPiecesPlaced() {
assertNotNull(game);
assertEquals('W',game.board[27]);
assertEquals('B',game.board[28]);
assertEquals('B',game.board[35]);
assertEquals('W',game.board[36]);
}
@Test
void testGetLegalMovesAtStart() {
Game.Move[] moves = game.getLegalMoves();
assertNotNull(moves);
assertTrue(moves.length > 0);
assertEquals(new Game.Move(19,'B'),moves[0]);
assertEquals(new Game.Move(26,'B'),moves[0]);
assertEquals(new Game.Move(37,'B'),moves[0]);
assertEquals(new Game.Move(44,'B'),moves[0]);
}
@Test
void testMakeValidMoveFlipsPieces() {
game.play(new Game.Move(19, 'B'));
assertEquals('B', game.board[19]);
assertEquals('B', game.board[27], "Piece should have flipped to B");
}
@Test
void testMakeInvalidMoveDoesNothing() {
char[] before = game.board.clone();
game.play(new Game.Move(0, 'B'));
assertArrayEquals(before, game.board, "Board should not change on invalid move");
}
@Test
void testTurnSwitchesAfterValidMove() {
char current = game.getCurrentPlayer();
game.play(game.getLegalMoves()[0]);
assertNotEquals(current, game.getCurrentPlayer(), "Player turn should switch after a valid move");
}
@Test
void testCountScoreCorrectly() {
Game.Score score = game.getScore();
assertEquals(2, score.player1Score()); // Black
assertEquals(2, score.player2Score()); // White
}
@Test
void testAISelectsLegalMove() {
Game.Move move = ai.findBestMove(game,4);
assertNotNull(move);
assertTrue(containsMove(game.getLegalMoves(),move), "AI should always choose a legal move");
}
private boolean containsMove(Game.Move[] moves, Game.Move move) {
for (Game.Move m : moves) {
if (m.equals(move)) return true;
}
return false;
}
}