added method for sorting the flipped pieces by distance to Move.position

This commit is contained in:
Ticho Hidding
2025-10-19 02:18:11 +02:00
parent 3a120803e3
commit 0447f7b0fe
2 changed files with 29 additions and 3 deletions

View File

@@ -83,6 +83,11 @@ public abstract class GameCanvas {
graphics.clearRect(0, 0, width, height); graphics.clearRect(0, 0, width, height);
} }
public void clearCell(int cellIndex) {
Cell cell = cells[cellIndex];
graphics.clearRect(cell.x, cell.y, cell.width, cell.height);
}
public void render() { public void render() {
graphics.setFill(color); graphics.setFill(color);

View File

@@ -15,6 +15,7 @@ public final class Reversi extends TurnBasedGame {
private int movesTaken; private int movesTaken;
public static final char FIRST_MOVE = 'B'; public static final char FIRST_MOVE = 'B';
private Set<Point> filledCells = new HashSet<>(); private Set<Point> filledCells = new HashSet<>();
private Move[] mostRecentlyFlippedPieces;
public Reversi() { public Reversi() {
super(8, 8, 2); super(8, 8, 2);
@@ -138,14 +139,14 @@ public final class Reversi extends TurnBasedGame {
} }
} }
if (moveIsLegal) { if (moveIsLegal) {
Move[] moves = getFlipsForPotentialMove(new Point(move.position()%columnSize,move.position()/rowSize), makeBoardAGrid(), move.value()); Move[] moves = sortMovesFromCenter(getFlipsForPotentialMove(new Point(move.position()%columnSize,move.position()/rowSize), makeBoardAGrid(), move.value()),move);
mostRecentlyFlippedPieces = moves;
board[move.position()] = move.value(); board[move.position()] = move.value();
IO.println(move.position() +" "+ move.value()); IO.println(move.position() +" "+ move.value());
for (Move m : moves) { for (Move m : moves) {
board[m.position()] = m.value(); board[m.position()] = m.value();
} }
filledCells.add(new Point(move.position() % rowSize, move.position() / columnSize)); filledCells.add(new Point(move.position() % rowSize, move.position() / columnSize));
//updateFilledCellsSet();
nextTurn(); nextTurn();
if (getLegalMoves().length == 0) { if (getLegalMoves().length == 0) {
skipMyTurn(); skipMyTurn();
@@ -153,12 +154,12 @@ public final class Reversi extends TurnBasedGame {
} }
return State.NORMAL; return State.NORMAL;
} }
return null; return null;
} }
public void skipMyTurn(){ public void skipMyTurn(){
IO.println("TURN " + getCurrentPlayer() + " SKIPPED"); IO.println("TURN " + getCurrentPlayer() + " SKIPPED");
//todo notify user that a turn has been skipped
nextTurn(); nextTurn();
} }
@@ -192,5 +193,25 @@ public final class Reversi extends TurnBasedGame {
} }
return new Game.Score(player1Score, player2Score); return new Game.Score(player1Score, player2Score);
} }
public Move[] sortMovesFromCenter(Move[] moves, Move center) {
int centerX = center.position()%columnSize;
int centerY = center.position()/rowSize;
IO.println("pre "+Arrays.toString(moves));
Arrays.sort(moves, (a, b) -> {
int dxA = a.position()%columnSize - centerX;
int dyA = a.position()/rowSize - centerY;
int dxB = b.position()%columnSize - centerX;
int dyB = b.position()/rowSize - centerY;
int distA = dxA * dxA + dyA * dyA;
int distB = dxB * dxB + dyB * dyB;
return Integer.compare(distA, distB);
});
IO.println("post "+Arrays.toString(moves));
return moves;
}
public Move[] getMostRecentlyFlippedPieces() {
return mostRecentlyFlippedPieces;
}
} }