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

@@ -95,7 +95,7 @@ public abstract class GameCanvas {
}
}
public void draw(Color color, int cell) {
public void drawDot(Color color, int cell) {
final float x = cells[cell].x() + gapSize;
final float y = cells[cell].y() + gapSize;
@@ -103,7 +103,7 @@ public abstract class GameCanvas {
final float height = cells[cell].height() - gapSize * 2;
graphics.setFill(color);
graphics.fillRect(x, y, width, height);
graphics.fillOval(x, y, width, height);
}
public void resize(int width, int height) {

View File

@@ -0,0 +1,36 @@
package org.toop.app.canvas;
import javafx.scene.layout.Background;
import javafx.scene.paint.Color;
import org.toop.game.Game;
import java.util.function.Consumer;
public class ReversiCanvas extends GameCanvas{
private Game.Move[] mostRecentLegalMoves;
public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, width, height, 8, 8, 10, true, onCellClicked);
drawStartingDots();
}
public void drawStartingDots(){
drawDot(Color.BLACK,28);
drawDot(Color.WHITE,36);
drawDot(Color.BLACK,35);
drawDot(Color.WHITE,27);
}
public void drawLegalMoves(Game.Move[] moves){
mostRecentLegalMoves = moves;
for(Game.Move move : moves){
drawDot(Color.RED,move.position());
}
}
public void removeLegalMoves(){
if (mostRecentLegalMoves != null){
for(Game.Move move : mostRecentLegalMoves){
drawDot(Color.GRAY,move.position()); //todo get current background color
}
}
mostRecentLegalMoves = null;
}
}

View File

@@ -5,6 +5,7 @@ import org.toop.app.layer.Container;
import org.toop.app.layer.Layer;
import org.toop.app.layer.NodeBuilder;
import org.toop.app.layer.containers.VerticalContainer;
import org.toop.app.layer.layers.game.ReversiLayer;
import org.toop.local.AppContext;
import javafx.geometry.Pos;
@@ -24,7 +25,7 @@ public final class MainLayer extends Layer {
});
final var othelloButton = NodeBuilder.button(AppContext.getString("othello"), () -> {
App.activate(new MultiplayerLayer());
App.activate(new ReversiLayer());
});
final var creditsButton = NodeBuilder.button(AppContext.getString("credits"), () -> {

View File

@@ -0,0 +1,59 @@
package org.toop.app.layer.layers.game;
import javafx.geometry.Pos;
import javafx.scene.paint.Color;
import org.toop.app.App;
import org.toop.app.canvas.ReversiCanvas;
import org.toop.app.layer.*;
import org.toop.app.layer.containers.HorizontalContainer;
import org.toop.app.layer.containers.VerticalContainer;
import org.toop.app.layer.layers.MainLayer;
import org.toop.game.reversi.Reversi;
import org.toop.game.reversi.ReversiAI;
import org.toop.local.AppContext;
public class ReversiLayer extends Layer{
private ReversiCanvas canvas;
private Reversi reversi;
private ReversiAI reversiAI;
public ReversiLayer(){
super("bg-secondary"); //make reversiboard background dark green
canvas = new ReversiCanvas(Color.GREEN,(App.getHeight() / 100) * 75, (App.getHeight() / 100) * 75, (cell) -> {
IO.println("clicked reversi cell: "+cell);
});
reversi = new Reversi() ;
reversiAI = new ReversiAI();
reload();
}
@Override
public void reload() {
popAll();
canvas.resize((App.getHeight() / 100) * 75, (App.getHeight() / 100) * 75);
for (int i = 0; i < reversi.board.length; i++) {
final char value = reversi.board[i];
if (value == 'B') {
canvas.drawDot(Color.BLACK, i);
} else if (value == 'W') {
canvas.drawDot(Color.WHITE, i);
}
}
final var backButton = NodeBuilder.button(AppContext.getString("back"), () -> {
App.activate(new MainLayer());
});
final Container controlContainer = new VerticalContainer(5);
controlContainer.addNodes(backButton);
final Container informationContainer = new HorizontalContainer(15);
addContainer(controlContainer, Pos.BOTTOM_LEFT, 2, -2, 0, 0);
addContainer(informationContainer, Pos.TOP_LEFT, 2, 2, 0, 0);
addGameCanvas(canvas, Pos.CENTER, 0, 0);
}
}