mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Drawing of board on canvas is now done from bitboards rather than translating.
This commit is contained in:
@@ -101,29 +101,13 @@ public abstract class BitGameCanvas<T extends BitboardGame<T>> implements GameCa
|
|||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int[] translateLegalMoves(long legalMoves){
|
public void loopOverBoard(long bb, Consumer<Integer> onCell){
|
||||||
int[] output = new int[Long.bitCount(legalMoves)];
|
while (bb != 0) {
|
||||||
int j = 0;
|
int idx = Long.numberOfTrailingZeros(bb); // index of least-significant 1-bit
|
||||||
for(int i = 0; i < 64; i++){
|
onCell.accept(idx);
|
||||||
if ((legalMoves & (1L << i)) != 0){
|
|
||||||
output[j] = i;
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static int[] translateBoard(long[] playerBitboard){
|
bb &= bb - 1; // clear LSB 1-bit
|
||||||
int[] output = new int[64];
|
|
||||||
Arrays.fill(output, -1);
|
|
||||||
for(int i = 0; i < playerBitboard.length; i++){
|
|
||||||
for (int j = 0; j < 64; j++){
|
|
||||||
if ((playerBitboard[i] & (1L << j)) != 0){
|
|
||||||
output[j] = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render() {
|
private void render() {
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import org.toop.game.games.reversi.BitboardReversi;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi>{
|
public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi> {
|
||||||
public ReversiBitCanvas() {
|
public ReversiBitCanvas() {
|
||||||
super(Color.GRAY, new Color(0f,0.4f,0.2f,1f), (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, 8, 8, 5, true);
|
super(Color.GRAY, new Color(0f, 0.4f, 0.2f, 1f), (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, 8, 8, 5, true);
|
||||||
canvas.setOnMouseMoved(event -> {
|
canvas.setOnMouseMoved(event -> {
|
||||||
double mouseX = event.getX();
|
double mouseX = event.getX();
|
||||||
double mouseY = event.getY();
|
double mouseY = event.getY();
|
||||||
@@ -27,21 +27,16 @@ public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int turnCoordsIntoCellId(double x, double y) {
|
private int turnCoordsIntoCellId(double x, double y) {
|
||||||
final int column = (int) ((x / this.width) * rowSize);
|
final int column = (int) ((x / this.width) * rowSize);
|
||||||
final int row = (int) ((y / this.height) * columnSize);
|
final int row = (int) ((y / this.height) * columnSize);
|
||||||
return column + row * rowSize;
|
return column + row * rowSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redraw(BitboardReversi gameCopy) {
|
public void redraw(BitboardReversi gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
int[] board = translateBoard(gameCopy.getBoard());
|
long[] board = gameCopy.getBoard();
|
||||||
for (int i = 0; i < board.length; i++) {
|
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
||||||
if (board[i] == 0) {
|
loopOverBoard(board[1], (i) -> drawDot(Color.BLACK, i));
|
||||||
drawDot(Color.WHITE, i);
|
|
||||||
} else if (board[i] == 1) {
|
|
||||||
drawDot(Color.BLACK, i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.toop.app.App;
|
|||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
||||||
public TicTacToeBitCanvas() {
|
public TicTacToeBitCanvas() {
|
||||||
@@ -23,26 +24,15 @@ public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
|||||||
@Override
|
@Override
|
||||||
public void redraw(BitboardTicTacToe gameCopy) {
|
public void redraw(BitboardTicTacToe gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
drawMoves(translateBoard(gameCopy.getBoard()));
|
drawMoves(gameCopy.getBoard());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawMoves(int[] gameBoard){
|
private void drawMoves(long[] gameBoard){
|
||||||
// Draw each square
|
loopOverBoard(gameBoard[0], (i) -> drawX(Color.RED, i));
|
||||||
for (int i = 0; i < 9; i++){
|
loopOverBoard(gameBoard[1], (i) -> drawO(Color.BLUE, i));
|
||||||
// If square isn't empty, draw player move
|
|
||||||
if (gameBoard[i] != -1){
|
|
||||||
drawPlayerMove(gameBoard[i], i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawPlayerMove(int player, int move) {
|
|
||||||
switch (player) {
|
|
||||||
case 0 -> drawX(Color.RED, move);
|
|
||||||
case 1 -> drawO(Color.BLUE, move);
|
|
||||||
default -> super.drawPlayerMove(player, move);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawX(Color color, int cell) {
|
public void drawX(Color color, int cell) {
|
||||||
graphics.setStroke(color);
|
graphics.setStroke(color);
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
|
|||||||
return players[getCurrentPlayerIndex()];
|
return players[getCurrentPlayerIndex()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long[] getBoard() {return this.playerBitboard;}
|
public long[] getBoard() {return this.playerBitboard;}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user