Skip to content

Commit 1c660a3

Browse files
author
Matt Tucker
committed
Store MultiPointState in History to prevent doubling when there is no need
1 parent f8e36ad commit 1c660a3

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/main/scala/History.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ case class Score(p1: Int = 0, p2: Int = 0) {
2020

2121
}
2222

23+
case class MultiPointState(target: Int, p1Points: Int = 0, p2Points: Int = 0)
24+
2325
sealed abstract class History(
2426
val lastTurn: List[Uci] = List.empty,
2527
val currentTurn: List[Uci] = List.empty,
@@ -32,6 +34,7 @@ sealed abstract class History(
3234
val kingMoves: draughts.KingMoves = draughts.KingMoves(),
3335
val score: Score = Score(0, 0),
3436
val captures: Score = Score(0, 0),
37+
val multiPointState: Option[MultiPointState] = None,
3538
val halfMoveClock: Int = 0
3639
) {
3740

@@ -119,7 +122,8 @@ object History {
119122
forcedTurn = h.forcedTurn,
120123
positionHashes = h.positionHashes,
121124
halfMoveClock = h.halfMoveClock,
122-
score = h.score
125+
score = h.score,
126+
multiPointState = h.multiPointState
123127
)
124128

125129
final case class Abalone(h: abalone.History)
@@ -154,6 +158,7 @@ object History {
154158
kingMoves: draughts.KingMoves = draughts.KingMoves(),
155159
score: Score = Score(0, 0),
156160
captures: Score = Score(0, 0),
161+
multiPointState: Option[MultiPointState] = None,
157162
halfMoveClock: Int = 0
158163
): History = lib match {
159164
case GameLogic.Draughts() =>
@@ -228,7 +233,8 @@ object History {
228233
forcedTurn = forcedTurn,
229234
positionHashes = positionHashes,
230235
halfMoveClock = halfMoveClock,
231-
score = score
236+
score = score,
237+
multiPointState = multiPointState
232238
)
233239
)
234240
case GameLogic.Abalone() =>

src/main/scala/backgammon/CubeData.scala

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package strategygames.backgammon
22

3-
import strategygames.Player
3+
import strategygames.{ MultiPointState, Player }
44

55
case class CubeData(
66
value: Int,
@@ -9,11 +9,16 @@ case class CubeData(
99
rejected: Boolean
1010
) {
1111

12-
def canOffer(player: Player): Boolean =
13-
!underOffer && value < 64 && owner != Some(!player)
12+
def canOffer(player: Player, multiPointState: Option[MultiPointState]): Boolean =
13+
!underOffer &&
14+
owner != Some(!player) &&
15+
value < CubeData.MAX_VALUE &&
16+
value < multiPointState.fold(CubeData.MAX_VALUE)(mps =>
17+
mps.target - player.fold(mps.p1Points, mps.p2Points)
18+
)
1419

1520
def offer(player: Player): CubeData =
16-
if (value >= 64) sys.error("Cannot offer cube beyond 64")
21+
if (value >= CubeData.MAX_VALUE) sys.error(s"Cannot offer cube beyond {CubeData.MAX_VALUE}")
1722
else if (value > 1 && Some(player) != owner) sys.error(s"Cube offer from invalid player: ${player}")
1823
else if (value == 1) copy(owner = Some(player), underOffer = true)
1924
else copy(underOffer = true)
@@ -31,5 +36,9 @@ case class CubeData(
3136
}
3237

3338
object CubeData {
39+
3440
val init = CubeData(1, None, false, false)
41+
42+
val MAX_VALUE = 64
43+
3544
}

src/main/scala/backgammon/History.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package strategygames.backgammon
22

3-
import strategygames.Score
3+
import strategygames.{ MultiPointState, Score }
44

55
import format.Uci
66

@@ -11,6 +11,7 @@ case class History(
1111
justUsedUndo: Boolean = false,
1212
positionHashes: PositionHash = Array.empty,
1313
score: Score = Score(0, 0),
14+
multiPointState: Option[MultiPointState] = None,
1415
// this is tracking fullMove for Backgammon
1516
halfMoveClock: Int = 0
1617
) {

src/main/scala/backgammon/variant/Variant.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ abstract class Variant private[variant] (
446446
else
447447
situation.board.cubeData match {
448448
case Some(cubeData) =>
449-
if (cubeData.canOffer(situation.player))
449+
if (cubeData.canOffer(situation.player, situation.board.history.multiPointState))
450450
List(
451451
CubeAction(
452452
interaction = OfferDouble,

0 commit comments

Comments
 (0)