Skip to content

Commit 88f10fe

Browse files
committed
fix self play
1 parent 862c6de commit 88f10fe

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

src/main/java/com/bernd/GameController.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import com.bernd.model.AcceptRequest;
66
import com.bernd.model.ActiveGame;
77
import com.bernd.model.Game;
8-
import com.bernd.model.GameMove;
98
import com.bernd.model.Move;
109
import com.bernd.model.OpenGame;
1110
import com.bernd.model.ViewGame;
1211
import com.bernd.util.Auth;
1312
import com.bernd.util.RandomString;
14-
import java.security.Principal;
15-
import java.util.Objects;
13+
import org.springframework.http.HttpStatus;
1614
import org.springframework.http.ResponseEntity;
1715
import org.springframework.messaging.core.MessageSendingOperations;
1816
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -22,6 +20,12 @@
2220
import org.springframework.web.bind.annotation.PostMapping;
2321
import org.springframework.web.bind.annotation.RequestBody;
2422
import org.springframework.web.bind.annotation.ResponseBody;
23+
import org.springframework.web.server.ResponseStatusException;
24+
25+
import java.security.Principal;
26+
import java.util.Objects;
27+
28+
import static com.bernd.util.Util.COLORS;
2529

2630
@Controller
2731
public class GameController {
@@ -47,7 +51,7 @@ public class GameController {
4751
public ViewGame getGame(@PathVariable String id) {
4852
Game game = games.get(id);
4953
if (game == null) {
50-
return null;
54+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such game");
5155
}
5256
return game.toView();
5357
}
@@ -58,10 +62,8 @@ public void action(Move move, Principal p) {
5862
if (p == null || game == null) {
5963
return;
6064
}
61-
String principal = Objects.toString(p.getName(), "");
62-
int color = principal.equals(game.black().name()) ? Board.B :
63-
principal.equals(game.white().name()) ? Board.W : 0;
64-
if (!canMove(game, color)) {
65+
int color = getCurrentColor(game, Objects.toString(p.getName(), ""));
66+
if (color == 0) {
6567
return;
6668
}
6769
Move updatedMove = move.withColor(color).withMoveNumber(game.moves().size());
@@ -74,25 +76,33 @@ public void action(Move move, Principal p) {
7476
}
7577
}
7678

77-
private boolean canMove(Game game, int color) {
79+
private int getCurrentColor(Game game, String principal) {
7880
if (game.gameHasEnded()) {
79-
return false;
81+
return 0;
8082
}
83+
if (game.handicap() != 0) {
84+
return Board.B;
85+
}
86+
int color = getColor(game, principal);
8187
if (color == 0) {
82-
return false;
88+
return 0;
8389
}
8490
MoveList moves = game.moves();
85-
if (game.counting()) {
86-
return true;
91+
if (moves.isEmpty()) {
92+
return Board.B;
8793
}
88-
if (game.handicap() != 0) {
89-
return color == Board.B;
94+
return moves.get(moves.size() - 1).color() ^ COLORS;
95+
}
96+
97+
private static int getColor(Game game, String principal) {
98+
if (!(game.isBlack(principal) || game.isWhite(principal))) {
99+
return 0;
90100
}
91-
if (moves.isEmpty()) {
92-
return color == Board.B;
101+
if (game.isSelfPlay()) {
102+
return game.moves().size() + game.handicap() % 2 == 0 ?
103+
Board.B : Board.W;
93104
}
94-
GameMove lastMove = moves.get(moves.size() - 1);
95-
return color != lastMove.color();
105+
return game.isBlack(principal) ? Board.B : Board.W;
96106
}
97107

98108
@ResponseBody

src/main/java/com/bernd/game/MoveList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.bernd.model.GameMove;
44
import com.bernd.model.Move;
5+
56
import java.util.ArrayList;
67
import java.util.List;
78

@@ -18,7 +19,7 @@ public static MoveList create(int size) {
1819
}
1920

2021
public void addGameEndMarker() {
21-
moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, true));
22+
moves.add(new GameMove(moves.size(), 0, true, -1, -1, true, false, false, true));
2223
}
2324

2425
public void add(Move move, boolean counting) {

src/main/java/com/bernd/model/Game.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Game update(Move move) {
3939
}
4040

4141
private Game updateInternal(Move move) {
42+
moves.add(move, counting);
4243
if (move.agreeCounting()) {
4344
if ((countingAgreed | move.color()) == COLORS) {
4445
moves.addGameEndMarker();
@@ -47,7 +48,6 @@ private Game updateInternal(Move move) {
4748
.withCountingAgreed(countingAgreed | move.color())
4849
.build();
4950
}
50-
moves.add(move, counting);
5151
if (counting) {
5252
int[][] updated = move.resetCounting() ?
5353
Toggle.resetCounting(board) :
@@ -153,4 +153,16 @@ public boolean gameHasEnded() {
153153
public GameBuilder toBuilder() {
154154
return GameBuilder.builder(this);
155155
}
156+
157+
public boolean isSelfPlay() {
158+
return black.name().equals(white.name());
159+
}
160+
161+
public boolean isWhite(String name) {
162+
return white.name().equals(name);
163+
}
164+
165+
public boolean isBlack(String name) {
166+
return black.name().equals(name);
167+
}
156168
}

src/main/java/com/bernd/model/GameMove.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public record GameMove(
88
int y,
99
boolean counting,
1010
boolean resetCounting,
11+
boolean agreeCounting,
1112
boolean end) {
1213
}

src/main/java/com/bernd/model/Move.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public record Move(
1111
int y) {
1212

1313
public GameMove toGameMove(boolean counting) {
14-
return new GameMove(n, color, pass, x, y, counting, resetCounting, false);
14+
return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, false);
1515
}
1616

1717
public GameMove gameEnd(boolean counting) {
18-
return new GameMove(n, color, pass, x, y, counting, resetCounting, true);
18+
return new GameMove(n, color, pass, x, y, counting, resetCounting, agreeCounting, true);
1919
}
2020

2121
public Move withColor(int color) {

0 commit comments

Comments
 (0)