mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
legal moves now get highlighted in red
This commit is contained in:
@@ -104,6 +104,7 @@ public abstract class GameCanvas {
|
|||||||
|
|
||||||
graphics.setFill(color);
|
graphics.setFill(color);
|
||||||
graphics.fillOval(x, y, width, height);
|
graphics.fillOval(x, y, width, height);
|
||||||
|
IO.println("gothere");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resize(int width, int height) {
|
public void resize(int width, int height) {
|
||||||
|
|||||||
@@ -22,13 +22,14 @@ public class ReversiCanvas extends GameCanvas{
|
|||||||
public void drawLegalMoves(Game.Move[] moves){
|
public void drawLegalMoves(Game.Move[] moves){
|
||||||
mostRecentLegalMoves = moves;
|
mostRecentLegalMoves = moves;
|
||||||
for(Game.Move move : moves){
|
for(Game.Move move : moves){
|
||||||
drawDot(Color.RED,move.position());
|
IO.println("Legal Moves:" + move.position());
|
||||||
|
drawDot(new Color(1f,0,0,0.25f),move.position());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void removeLegalMoves(){
|
public void removeLegalMoves(){
|
||||||
if (mostRecentLegalMoves != null){
|
if (mostRecentLegalMoves != null){
|
||||||
for(Game.Move move : mostRecentLegalMoves){
|
for(Game.Move move : mostRecentLegalMoves){
|
||||||
drawDot(Color.GRAY,move.position()); //todo get current background color
|
drawDot(Color.GRAY,move.position()); //todo get current background color or make this redraw the entire board
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mostRecentLegalMoves = null;
|
mostRecentLegalMoves = null;
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ public class ReversiLayer extends Layer{
|
|||||||
|
|
||||||
|
|
||||||
reload();
|
reload();
|
||||||
|
canvas.drawLegalMoves(reversi.getLegalMoves());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,10 +4,16 @@ import org.toop.game.Game;
|
|||||||
import org.toop.game.TurnBasedGame;
|
import org.toop.game.TurnBasedGame;
|
||||||
import org.toop.game.tictactoe.TicTacToe;
|
import org.toop.game.tictactoe.TicTacToe;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
public final class Reversi extends TurnBasedGame {
|
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<Point>();
|
||||||
|
|
||||||
public Reversi() {
|
public Reversi() {
|
||||||
super(8, 8, 2);
|
super(8, 8, 2);
|
||||||
@@ -17,6 +23,7 @@ public final class Reversi extends TurnBasedGame {
|
|||||||
public Reversi(Reversi other) {
|
public Reversi(Reversi other) {
|
||||||
super(other);
|
super(other);
|
||||||
this.movesTaken = other.movesTaken;
|
this.movesTaken = other.movesTaken;
|
||||||
|
this.filledCells = other.filledCells;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -25,16 +32,97 @@ public final class Reversi extends TurnBasedGame {
|
|||||||
board[28] = 'B';
|
board[28] = 'B';
|
||||||
board[35] = 'B';
|
board[35] = 'B';
|
||||||
board[36] = 'W';
|
board[36] = 'W';
|
||||||
|
updateFilledCellsSet();
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Move[] getLegalMoves() {
|
public Move[] getLegalMoves() {
|
||||||
|
final ArrayList<Move> legalMoves = new ArrayList<>();
|
||||||
char[][] boardGrid = makeBoardAGrid();
|
char[][] boardGrid = makeBoardAGrid();
|
||||||
if(currentTurn == 1){
|
char currentPlayer = (currentTurn==0) ? 'B' : 'W';
|
||||||
|
Set<Point> adjCell = getAdjacentCells(boardGrid);
|
||||||
|
for (Point point : adjCell){
|
||||||
|
int score = getScoreForPotentialMove(point,boardGrid,currentPlayer);
|
||||||
|
if (score > 0){
|
||||||
|
legalMoves.add(new Move(point.x + point.y * 8, currentPlayer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return legalMoves.toArray(new Move[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Point> getAdjacentCells(char[][] boardGrid) {
|
||||||
|
Set<Point> possibleCells = new HashSet<>();
|
||||||
|
for (Point point : filledCells) { //for every filled cell
|
||||||
|
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++){ //check adjacent cells
|
||||||
|
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ //orthogonally and diagonally
|
||||||
|
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
|
||||||
|
if (deltaColumn == 0 || deltaRow == 0 //continue if out of bounds
|
||||||
|
|| !isOnBoard(newX, newY)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (boardGrid[newX][newY] == Game.EMPTY) { //check if the cell is empty
|
||||||
|
possibleCells.add(new Point(newX, newY)); //and then add it to the set of possible moves
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return possibleCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScoreForPotentialMove(Point point, char[][] boardGrid, char currentPlayer) {
|
||||||
|
int score = 0;
|
||||||
|
for (int deltaColumn = -1; deltaColumn <= 1; deltaColumn++) {
|
||||||
|
for (int deltaRow = -1; deltaRow <= 1; deltaRow++) {
|
||||||
|
if (deltaColumn == 0 && deltaRow == 0){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
score += getScoreInDirection(point,boardGrid,currentPlayer,deltaColumn,deltaRow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getScoreInDirection(Point point, char[][] boardGrid, char currentPlayer, int dirX, int dirY) {
|
||||||
|
char opponent = getOpponent(currentPlayer);
|
||||||
|
int x = point.x + dirX;
|
||||||
|
int y = point.y + dirY;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
if (!isOnBoard(x, y) || boardGrid[y][x] != opponent) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (isOnBoard(x, y) && boardGrid[y][x] == opponent) {
|
||||||
|
count++;
|
||||||
|
x += dirX;
|
||||||
|
y += dirY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isOnBoard(x, y) && boardGrid[y][x] == currentPlayer) {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isOnBoard(int x, int y) {
|
||||||
|
return x >= 0 && x < 8 && y >= 0 && y < 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
private char getOpponent(char currentPlayer){
|
||||||
|
if (currentPlayer == 'B') {
|
||||||
|
return 'W';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 'B';
|
||||||
}
|
}
|
||||||
return new Move[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public char[][] makeBoardAGrid() {
|
public char[][] makeBoardAGrid() {
|
||||||
@@ -46,9 +134,15 @@ public final class Reversi extends TurnBasedGame {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public State play(Move move) {
|
public State play(Move move) {
|
||||||
|
filledCells.add(new Point(move.position() / 8, move.position() % 8));
|
||||||
|
nextTurn();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void skipMyTurn(){
|
||||||
|
nextTurn();
|
||||||
|
}
|
||||||
|
|
||||||
public char getCurrentPlayer() {
|
public char getCurrentPlayer() {
|
||||||
if (currentTurn == 0){
|
if (currentTurn == 0){
|
||||||
return 'B';
|
return 'B';
|
||||||
|
|||||||
Reference in New Issue
Block a user