added some comments and made some methods a bit more readable

This commit is contained in:
Ticho Hidding
2025-11-01 15:12:09 +01:00
parent 6dc05e7123
commit 295c61c7eb

View File

@@ -72,7 +72,7 @@ public final class Reversi extends TurnBasedGame {
|| !isOnBoard(newX, newY)) { || !isOnBoard(newX, newY)) {
continue; continue;
} }
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty
possibleCells.add(new Point(newX, newY)); //and then add it to the set of possible moves possibleCells.add(new Point(newX, newY)); //and then add it to the set of possible moves
} }
} }
@@ -83,13 +83,13 @@ public final class Reversi extends TurnBasedGame {
public Move[] getFlipsForPotentialMove(Point point, char currentPlayer) { public Move[] getFlipsForPotentialMove(Point point, char currentPlayer) {
final ArrayList<Move> movesToFlip = new ArrayList<>(); final ArrayList<Move> movesToFlip = new ArrayList<>();
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++) { for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++) { //for all directions
for (int deltaRow = -1; deltaRow <= 1; deltaRow++) { for (int deltaRow = -1; deltaRow <= 1; deltaRow++) {
if (deltaColumn == 0 && deltaRow == 0){ if (deltaColumn == 0 && deltaRow == 0){
continue; continue;
} }
Move[] moves = getFlipsInDirection(point,makeBoardAGrid(),currentPlayer,deltaColumn,deltaRow); Move[] moves = getFlipsInDirection(point,makeBoardAGrid(),currentPlayer,deltaColumn,deltaRow);
if (moves != null) { if (moves != null) { //getFlipsInDirection
movesToFlip.addAll(Arrays.asList(moves)); movesToFlip.addAll(Arrays.asList(moves));
} }
} }
@@ -103,18 +103,18 @@ public final class Reversi extends TurnBasedGame {
int x = point.x + dirX; int x = point.x + dirX;
int y = point.y + dirY; int y = point.y + dirY;
if (!isOnBoard(x, y) || boardGrid[y][x] != opponent) { if (!isOnBoard(x, y) || boardGrid[y][x] != opponent) { //there must first be an opponents tile
return null; return null;
} }
while (isOnBoard(x, y) && boardGrid[y][x] == opponent) { while (isOnBoard(x, y) && boardGrid[y][x] == opponent) { //count the opponents tiles in this direction
movesToFlip.add(new Move(x+y*this.getRowSize(), currentPlayer)); movesToFlip.add(new Move(x+y*this.getRowSize(), currentPlayer));
x += dirX; x += dirX;
y += dirY; y += dirY;
} }
if (isOnBoard(x, y) && boardGrid[y][x] == currentPlayer) { if (isOnBoard(x, y) && boardGrid[y][x] == currentPlayer) {
return movesToFlip.toArray(new Move[0]); return movesToFlip.toArray(new Move[0]); //only return the count if last tile is ours
} }
return null; return null;
} }
@@ -126,7 +126,7 @@ public final class Reversi extends TurnBasedGame {
private char[][] makeBoardAGrid() { private char[][] makeBoardAGrid() {
char[][] boardGrid = new char[this.getRowSize()][this.getColumnSize()]; char[][] boardGrid = new char[this.getRowSize()][this.getColumnSize()];
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
boardGrid[i / this.getRowSize()][i % this.getColumnSize()] = this.getBoard()[i]; //boardGrid[y / row] [x / column] boardGrid[i / this.getRowSize()][i % this.getColumnSize()] = this.getBoard()[i]; //boardGrid[y -> row] [x -> column]
} }
return boardGrid; return boardGrid;
} }
@@ -135,39 +135,40 @@ public final class Reversi extends TurnBasedGame {
public GameState play(Move move) { public GameState play(Move move) {
Move[] legalMoves = getLegalMoves(); Move[] legalMoves = getLegalMoves();
boolean moveIsLegal = false; boolean moveIsLegal = false;
for (Move legalMove : legalMoves) { for (Move legalMove : legalMoves) { //check if the move is legal
if (move.equals(legalMove)) { if (move.equals(legalMove)) {
moveIsLegal = true; moveIsLegal = true;
break; break;
} }
} }
if (moveIsLegal) { if (!moveIsLegal) {
Move[] moves = sortMovesFromCenter(getFlipsForPotentialMove(new Point(move.position()%this.getColumnSize(),move.position()/this.getRowSize()), move.value()),move); return null;
mostRecentlyFlippedPieces = moves; }
this.setBoard(move);
for (Move m : moves) { Move[] moves = sortMovesFromCenter(getFlipsForPotentialMove(new Point(move.position()%this.getColumnSize(),move.position()/this.getRowSize()), move.value()),move);
this.setBoard(m); mostRecentlyFlippedPieces = moves;
this.setBoard(move); //place the move on the board
for (Move m : moves) {
this.setBoard(m); //flip the correct pieces on the board
}
filledCells.add(new Point(move.position() % this.getRowSize(), move.position() / this.getColumnSize()));
nextTurn();
if (getLegalMoves().length == 0) { //skip the players turn when there are no legal moves
skipMyTurn();
if (getLegalMoves().length > 0) {
return GameState.TURN_SKIPPED;
} }
filledCells.add(new Point(move.position() % this.getRowSize(), move.position() / this.getColumnSize())); else { //end the game when neither player has a legal move
nextTurn(); Score score = getScore();
if (getLegalMoves().length == 0) { if (score.player1Score() == score.player2Score()) {
skipMyTurn(); return GameState.DRAW;
if (getLegalMoves().length > 0) {
return GameState.TURN_SKIPPED;
} }
else { else {
Score score = getScore(); return GameState.WIN;
if (score.player1Score() == score.player2Score()) {
return GameState.DRAW;
}
else {
return GameState.WIN;
}
} }
} }
return GameState.NORMAL;
} }
return null; return GameState.NORMAL;
} }
private void skipMyTurn(){ private void skipMyTurn(){
@@ -206,7 +207,7 @@ public final class Reversi extends TurnBasedGame {
} }
return new Score(player1Score, player2Score); return new Score(player1Score, player2Score);
} }
private Move[] sortMovesFromCenter(Move[] moves, Move center) { private Move[] sortMovesFromCenter(Move[] moves, Move center) { //sorts the pieces to be flipped for animation purposes
int centerX = center.position()%this.getColumnSize(); int centerX = center.position()%this.getColumnSize();
int centerY = center.position()/this.getRowSize(); int centerY = center.position()/this.getRowSize();
Arrays.sort(moves, (a, b) -> { Arrays.sort(moves, (a, b) -> {