Skip to content

Commit

Permalink
count disputed territory as 0
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Jul 20, 2024
1 parent 3452090 commit 255cb6f
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/main/client/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
export const StompContext = createContext()

export const base = "/app"
export const BLACK = 2
export const WHITE = 4
export const BLACK = 32
export const WHITE = 64

export async function tfetch(url, options) {
let response
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/bernd/game/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

public class Board {

public static final int BLACK = 2;
public static final int WHITE = 4;
public static final int B = 32;
public static final int W = 64;
public static final int FORBIDDEN = 1;
public static final int TERRITORY = 2;
public static final int REMOVED = 4;

private static StoneGroup getStoneGroup(
int[][] board,
int xx,
int yy) {
int color = board[yy][xx];
int dim = board.length;
BoardUpdate update = BoardUpdate.builder(dim, 16);
BoardUpdate update = BoardUpdate.builder(dim);
int liberties = 0;
PointSet pointsChecked = PointSet.create(dim);
PointQueue pointsToCheck = PointQueue.create(dim);
Expand Down Expand Up @@ -70,7 +73,7 @@ public static int[][] removeDeadStonesAround(
if (color == 0) {
return board;
}
int oppositeColor = color == WHITE ? BLACK : WHITE;
int oppositeColor = color == W ? B : W;
int size = board.length;
int[][] result = board;
// Above
Expand Down
45 changes: 31 additions & 14 deletions src/main/java/com/bernd/game/Count.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.bernd.game;

import com.bernd.util.BoardUpdate;
import java.util.Arrays;

import static com.bernd.game.Board.B;
import static com.bernd.game.Board.TERRITORY;
import static com.bernd.game.Board.W;

public class Count {

private static int getImpliedColor(
Expand All @@ -19,7 +26,7 @@ private static int getImpliedColor(
int x = ptId % dim;
pointsChecked.add(x, y);
if (board[y][x] != 0) {
return board[y][x] + 1; // add territory flag
return board[y][x] + TERRITORY;
}
if (y > 0 && !pointsChecked.has(x, y - 1)) {
pointsToCheck.offer(x, y - 1);
Expand All @@ -37,62 +44,71 @@ private static int getImpliedColor(
throw new RuntimeException("empty board");
}

private static void getStoneGroup(
private static void markStonesAround(
int[][] acc,
int[][] board,
int xx,
int yy) {
int color = getImpliedColor(board, xx, yy);
if (color % 2 == 0) {
if ((color & TERRITORY) == 0) {
acc[yy][xx] = color;
return;
}
int baseColor = color - 1; // remove territory flag
boolean oppositeStonesFound = false;
int baseColor = color - TERRITORY;
int oppositeColor = baseColor == W ? B : W;
int dim = board.length;
BoardUpdate updater = BoardUpdate.builder(dim, 64);
PointQueue pointsToCheck = PointQueue.create(dim);
pointsToCheck.offer(xx, yy);
while (!pointsToCheck.isEmpty()) {
int ptId = pointsToCheck.poll();
int y = ptId / dim;
int x = ptId % dim;
acc[y][x] = color;
updater.add(x, y);
if (y > 0) {
int c = board[y - 1][x];
if (c != 0 && c != baseColor) {
throw new RuntimeException("remove dead stones");
if (c == oppositeColor) {
oppositeStonesFound = true;
}
if (c == 0 && acc[y - 1][x] != color) {
pointsToCheck.offer(x, y - 1);
}
}
if (y < dim - 1) {
int c = board[y + 1][x];
if (c != 0 && c != baseColor) {
throw new RuntimeException("remove dead stones");
if (c == oppositeColor) {
oppositeStonesFound = true;
}
if (c == 0 && acc[y + 1][x] != color) {
pointsToCheck.offer(x, y + 1);
}
}
if (x > 0) {
int c = board[y][x - 1];
if (c != 0 && c != baseColor) {
throw new RuntimeException("remove dead stones");
if (c == oppositeColor) {
oppositeStonesFound = true;
}
if (c == 0 && acc[y][x - 1] != color) {
pointsToCheck.offer(x - 1, y);
}
}
if (x < dim - 1) {
int c = board[y][x + 1];
if (c != 0 && c != baseColor) {
throw new RuntimeException("remove dead stones");
if (c == oppositeColor) {
oppositeStonesFound = true;
}
if (c == 0 && acc[y][x + 1] != color) {
pointsToCheck.offer(x + 1, y);
}
}
}
if (oppositeStonesFound) {
for (int i = 0; i < updater.size(); i++) {
acc[updater.y(i)][updater.x(i)] = 0;
}
}
}

public static int[][] count(
Expand All @@ -101,8 +117,8 @@ public static int[][] count(
for (int y = 0; y < board.length; y++) {
int[] row = board[y];
for (int x = 0; x < row.length; x++) {
if (acc[y][x] == 0) {
getStoneGroup(acc, board, x, y);
if (acc[y][x] == -1) {
markStonesAround(acc, board, x, y);
}
}
}
Expand All @@ -113,6 +129,7 @@ private static int[][] createAcc(int[][] board) {
int[][] result = new int[board.length][];
for (int i = 0; i < board.length; i++) {
result[i] = new int[result.length];
Arrays.fill(result[i], -1);
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record Game(
public Game update(Move move) {
int x = move.x();
int y = move.y();
int color = currentUser.equals(black().name()) ? Board.BLACK : Board.WHITE;
int color = currentUser.equals(black().name()) ? Board.B : Board.W;
Function<int[][], int[][]> update = BoardUpdate.create(board.length, x, y, color);
int[][] rows = update.apply(board);
int[][] newRows = Board.removeDeadStonesAround(rows, x, y);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/bernd/util/BoardUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class BoardUpdate implements Function<int[][], int[][]> {

private static final int SHIFT = 256;
private static final int SHIFT = 256; // > max(B, W)
private int pos;
private final int dim;
private int[] updates;
Expand All @@ -22,6 +22,10 @@ public static BoardUpdate builder(int dim, int size) {
return new BoardUpdate(dim, new int[size]);
}

public static BoardUpdate builder(int dim) {
return new BoardUpdate(dim, new int[16]);
}

public static Function<int[][], int[][]> create(
int dim, int x, int y, int value) {
BoardUpdate result = builder(dim, 1);
Expand All @@ -38,6 +42,10 @@ public void add(int x, int y, int value) {
pos++;
}

public void add(int x, int y) {
add(x, y, 0);
}

public void add(Point point, int value) {
add(point.x(), point.y(), value);
}
Expand Down
52 changes: 30 additions & 22 deletions src/test/java/com/bernd/game/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,60 @@

import org.junit.jupiter.api.Test;

import static com.bernd.game.Board.B;
import static com.bernd.game.Board.W;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

class BoardTest {

@Test
void testRemoveOneStone() {
int[][] position = new int[][]{
new int[]{2, 4},
new int[]{4, 0},
new int[]{B, W},
new int[]{W, 0},
};
int[][] result = Board.removeDeadStonesAround(position, 0, 1);
assertArrayEquals(new int[]{0, 4}, result[0]);
assertArrayEquals(new int[]{4, 0}, result[1]);
assertArrayEquals(new int[][]{
new int[]{0, W},
new int[]{W, 0},
}, result);
}

@Test
void testRemoveFourStones() {
int[][] position = new int[][]{
new int[]{0, 0, 0, 0, 0},
new int[]{0, 0, 2, 2, 0},
new int[]{0, 2, 4, 4, 2},
new int[]{2, 4, 4, 2, 0},
new int[]{0, 2, 2, 0, 0},
new int[]{0, 0, B, B, 0},
new int[]{0, B, W, W, B},
new int[]{B, W, W, B, 0},
new int[]{0, B, B, 0, 0},
};
int[][] result = Board.removeDeadStonesAround(position, 4, 2);
assertArrayEquals(new int[]{0, 0, 0, 0, 0}, result[0]);
assertArrayEquals(new int[]{0, 0, 2, 2, 0}, result[1]);
assertArrayEquals(new int[]{0, 2, 0, 0, 2}, result[2]);
assertArrayEquals(new int[]{2, 0, 0, 2, 0}, result[3]);
assertArrayEquals(new int[]{0, 2, 2, 0, 0}, result[4]);
assertArrayEquals(new int[][]{
new int[]{0, 0, 0, 0, 0},
new int[]{0, 0, B, B, 0},
new int[]{0, B, 0, 0, B},
new int[]{B, 0, 0, B, 0},
new int[]{0, B, B, 0, 0},
}, result);
}

@Test
void testRemoveStones() {
int[][] position = new int[][]{
new int[]{4, 2, 0, 0, 0},
new int[]{2, 4, 0, 0, 0},
new int[]{2, 4, 0, 0, 0},
new int[]{4, 2, 0, 0, 0},
new int[]{W, B, 0, 0, 0},
new int[]{B, W, 0, 0, 0},
new int[]{B, W, 0, 0, 0},
new int[]{W, B, 0, 0, 0},
new int[]{0, 0, 0, 0, 0},
};
int[][] result = Board.removeDeadStonesAround(position, 0, 0);
assertArrayEquals(new int[]{4, 2, 0, 0, 0}, result[0]);
assertArrayEquals(new int[]{0, 4, 0, 0, 0}, result[1]);
assertArrayEquals(new int[]{0, 4, 0, 0, 0}, result[2]);
assertArrayEquals(new int[]{4, 2, 0, 0, 0}, result[3]);
assertArrayEquals(new int[]{0, 0, 0, 0, 0}, result[4]);
assertArrayEquals(new int[][]{
new int[]{W, B, 0, 0, 0},
new int[]{0, W, 0, 0, 0},
new int[]{0, W, 0, 0, 0},
new int[]{W, B, 0, 0, 0},
new int[]{0, 0, 0, 0, 0},
}, result);
}
}
Loading

0 comments on commit 255cb6f

Please sign in to comment.