mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
commit ofzo
This commit is contained in:
@@ -7,6 +7,7 @@ public abstract class Game {
|
||||
NORMAL,
|
||||
DRAW,
|
||||
WIN,
|
||||
MOVE_SKIPPED,
|
||||
}
|
||||
|
||||
public record Move(int position, char value) {}
|
||||
|
||||
@@ -38,7 +38,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
private void updateFilledCellsSet() {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (board[i] == 'W' || board[i] == 'B') {
|
||||
filledCells.add(new Point(i / 8, i % 8));
|
||||
filledCells.add(new Point(i % columnSize, i / rowSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,12 +49,11 @@ public final class Reversi extends TurnBasedGame {
|
||||
char[][] boardGrid = makeBoardAGrid();
|
||||
char currentPlayer = (currentTurn==0) ? 'B' : 'W';
|
||||
Set<Point> adjCell = getAdjacentCells(boardGrid);
|
||||
|
||||
for (Point point : adjCell){
|
||||
Move[] moves = getFlipsForPotentialMove(point,boardGrid,currentPlayer);
|
||||
int score = moves.length;
|
||||
if (score > 0){
|
||||
legalMoves.add(new Move(point.x + point.y * 8, currentPlayer));
|
||||
legalMoves.add(new Move(point.x + point.y * rowSize, currentPlayer));
|
||||
}
|
||||
}
|
||||
return legalMoves.toArray(new Move[0]);
|
||||
@@ -107,7 +106,7 @@ public final class Reversi extends TurnBasedGame {
|
||||
|
||||
while (isOnBoard(x, y) && boardGrid[y][x] == opponent) {
|
||||
|
||||
movesToFlip.add(new Move(x+y*8, currentPlayer));
|
||||
movesToFlip.add(new Move(x+y*rowSize, currentPlayer));
|
||||
x += dirX;
|
||||
y += dirY;
|
||||
}
|
||||
@@ -118,13 +117,13 @@ public final class Reversi extends TurnBasedGame {
|
||||
}
|
||||
|
||||
private boolean isOnBoard(int x, int y) {
|
||||
return x >= 0 && x < 8 && y >= 0 && y < 8;
|
||||
return x >= 0 && x < columnSize && y >= 0 && y < rowSize;
|
||||
}
|
||||
|
||||
public char[][] makeBoardAGrid() {
|
||||
char[][] boardGrid = new char[8][8];
|
||||
char[][] boardGrid = new char[rowSize][columnSize];
|
||||
for (int i = 0; i < 64; i++) {
|
||||
boardGrid[i / 8][i % 8] = board[i]; //boardGrid[y / row] [x / column]
|
||||
boardGrid[i / rowSize][i % columnSize] = board[i]; //boardGrid[y / row] [x / column]
|
||||
}
|
||||
return boardGrid;
|
||||
}
|
||||
@@ -139,16 +138,22 @@ public final class Reversi extends TurnBasedGame {
|
||||
}
|
||||
}
|
||||
if (moveIsLegal) {
|
||||
Move[] moves = getFlipsForPotentialMove(new Point(move.position()%8,move.position()/8), makeBoardAGrid(), move.value());
|
||||
Move[] moves = getFlipsForPotentialMove(new Point(move.position()%columnSize,move.position()/rowSize), makeBoardAGrid(), move.value());
|
||||
board[move.position()] = move.value();
|
||||
IO.println(move.position() +" "+ move.value());
|
||||
for (Move m : moves) {
|
||||
board[m.position()] = m.value();
|
||||
}
|
||||
updateFilledCellsSet();
|
||||
filledCells.add(new Point(move.position() % rowSize, move.position() / columnSize));
|
||||
//updateFilledCellsSet();
|
||||
nextTurn();
|
||||
if (getLegalMoves().length == 0) {
|
||||
skipMyTurn();
|
||||
return State.MOVE_SKIPPED;
|
||||
}
|
||||
return State.NORMAL;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -105,6 +105,34 @@ class ReversiTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPlayerMustSkipTurnIfNoValidMoves() {
|
||||
game.play(new Game.Move(19, 'B'));
|
||||
game.play(new Game.Move(34, 'W'));
|
||||
game.play(new Game.Move(45, 'B'));
|
||||
game.play(new Game.Move(11, 'W'));
|
||||
game.play(new Game.Move(42, 'B'));
|
||||
game.play(new Game.Move(54, 'W'));
|
||||
game.play(new Game.Move(37, 'B'));
|
||||
game.play(new Game.Move(46, 'W'));
|
||||
game.play(new Game.Move(63, 'B'));
|
||||
game.play(new Game.Move(62, 'W'));
|
||||
game.play(new Game.Move(29, 'B'));
|
||||
game.play(new Game.Move(50, 'W'));
|
||||
game.play(new Game.Move(55, 'B'));
|
||||
game.play(new Game.Move(30, 'W'));
|
||||
game.play(new Game.Move(53, 'B'));
|
||||
game.play(new Game.Move(38, 'W'));
|
||||
game.play(new Game.Move(61, 'B'));
|
||||
game.play(new Game.Move(52, 'W'));
|
||||
game.play(new Game.Move(51, 'B'));
|
||||
game.play(new Game.Move(60, 'W'));
|
||||
game.play(new Game.Move(59, 'B'));
|
||||
assertEquals('B', game.getCurrentPlayer());
|
||||
game.play(ai.findBestMove(game,5));
|
||||
game.play(ai.findBestMove(game,5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAISelectsLegalMove() {
|
||||
Game.Move move = ai.findBestMove(game,4);
|
||||
|
||||
Reference in New Issue
Block a user