Skip to content

Commit 00d46ab

Browse files
author
Daniel Barclay
committed
ColoredLines: Refining Board: Removed on-deck delegation from BoardPlus.
1 parent 4360659 commit 00d46ab

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

src/main/scala/com/us/dsb/explore/algs/coloredlines/manual/game/GameLogicSupport.scala

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.us.dsb.explore.algs.coloredlines.manual.game
22

3-
import com.us.dsb.explore.algs.coloredlines.manual.game.board.CellAddress
4-
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BallKind, BoardPlus, BoardOrder, columnIndices, rowIndices}
3+
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BallKind, BoardOrder, BoardPlus, BoardState, CellAddress, columnIndices, rowIndices}
54
import com.us.dsb.explore.algs.coloredlines.manual.game.lines.LineDetector
65

76
import java.util
@@ -35,12 +34,16 @@ object GameLogicSupport {
3534
//??? clarify re placing next three balls (re interpreting differently in different contexts
3635
anyRemovals: Boolean)
3736
{
38-
println(s"??? ${this}")
37+
println(s"??? $this")
3938
//??? print("")
4039
}
4140

41+
//???? parameterize
42+
private[this] def replenishOnDeckBalls(board: BoardState)(implicit rng: Random): BoardState =
43+
board.withOnDeckBalls(List.fill(3)(pickRandomBallKind()))
44+
4245
/**
43-
* @param board
46+
* @param boardPlus
4447
* expected to be empty //???? maybe refactor something?
4548
*/
4649
private[game] def placeInitialBalls(boardPlus: BoardPlus)(implicit rng: Random): MoveResult = {
@@ -50,12 +53,13 @@ object GameLogicSupport {
5053
case (resultSoFar, _) =>
5154
val address =
5255
pickRandomEmptyCell(resultSoFar.boardPlus).getOrElse(scala.sys.error("Unexpectedly full board"))
53-
val postPlacementBoard = resultSoFar.boardPlus.withBallAt(address, pickRandomBallKind())
54-
val placementHandlingResult = LineDetector.handleBallArrival(postPlacementBoard, address)
56+
val postPlacementBoardPlus = resultSoFar.boardPlus.withBallAt(address, pickRandomBallKind())
57+
val placementHandlingResult = LineDetector.handleBallArrival(postPlacementBoardPlus, address)
5558
MoveResult(placementHandlingResult.boardPlus, placementHandlingResult.anyRemovals)
5659
}
57-
//???? parameterize
58-
postPlacementsResult.copy(boardPlus = postPlacementsResult.boardPlus.withOnDeckBalls(List.fill(3)(pickRandomBallKind())))
60+
61+
val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementsResult.boardPlus.boardState)
62+
postPlacementsResult.copy(boardPlus = postPlacementsResult.boardPlus.withBoardState(replenishedOnDeckBoard))
5963
}
6064

6165
private[game] sealed trait Action
@@ -123,28 +127,32 @@ object GameLogicSupport {
123127
val postPlacementResult =
124128
//???? for 1 to 3, consume on-deck ball from list, and then place (better for internal state view);;
125129
// can replenish incrementally or later; later might show up better in internal state view
126-
boardPlus.getOnDeckBalls
130+
boardPlus.boardState.getOnDeckBalls
127131
.foldLeft(MoveResult(boardPlus, false)) {
128132
case (curMoveResult, onDeckBall) =>
129133
pickRandomEmptyCell(curMoveResult.boardPlus) match {
130134
case None => // board full; break out early (game will become over)
131135
curMoveResult
132136
case Some(address) =>
133-
val postDeueueBoardPlus =
134-
curMoveResult.boardPlus.withOnDeckBalls(curMoveResult.boardPlus.getOnDeckBalls.tail)
135-
val postPlacementBoard = postDeueueBoardPlus.withBallAt(address, onDeckBall)
136-
LineDetector.handleBallArrival(postPlacementBoard, address)
137+
val postPlacementBoardPlus = {
138+
val curBoardState = curMoveResult.boardPlus.boardState
139+
val postDeueueBoard =
140+
curBoardState
141+
.withOnDeckBalls(curBoardState.getOnDeckBalls.tail)
142+
.withBallAt(address, onDeckBall)
143+
val postDeueueBoardPlus = curMoveResult.boardPlus.withBoardState(postDeueueBoard)
144+
postDeueueBoardPlus
145+
}
146+
LineDetector.handleBallArrival(postPlacementBoardPlus, address)
137147
}
138148
}
139-
//???? parameterize?
140-
//????? check re duplicate on-deck code (look for other "fill(3)"
141-
postPlacementResult.copy(boardPlus = postPlacementResult.boardPlus.withOnDeckBalls(List.fill(3)(pickRandomBallKind())))
142-
}
149+
150+
val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementResult.boardPlus.boardState)
151+
postPlacementResult.copy(boardPlus = postPlacementResult.boardPlus.withBoardState(replenishedOnDeckBoard))}
143152

144153
private[game] def doPass(boardPlus: BoardPlus)(implicit rng: Random): MoveResult =
145154
placeNextBalls(boardPlus)
146155

147-
148156
//???: likely move core algorithm out; possibly move outer code into BoardPlus/BoardState:
149157
/**
150158
* @param toTapCell - must be empty */

src/main/scala/com/us/dsb/explore/algs/coloredlines/manual/game/board/BoardPlus.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private[game] object BoardPlus {
99
/**
1010
* CURRENTLY: Core board state (now wrapped), not score yet, tap-UI selection state
1111
*/
12-
private[game] class BoardPlus(private[this] val boardState: BoardState,
12+
private[game] class BoardPlus(private[game] val boardState: BoardState,
1313
private[this] val score: Int,
1414
//???? move to (low-level) tap-UI state:
1515
private[this] val selectionAddress: Option[CellAddress]
@@ -24,12 +24,6 @@ private[game] class BoardPlus(private[this] val boardState: BoardState,
2424
selectionAddress: Option[CellAddress] = selectionAddress) =
2525
new BoardPlus(boardState, score, selectionAddress)
2626

27-
// on-deck balls:
28-
private[game] def getOnDeckBalls: Iterable[BallKind] = boardState.getOnDeckBalls
29-
30-
private[game] def withOnDeckBalls(newBalls: Iterable[BallKind]): BoardPlus =
31-
copy(boardState.withOnDeckBalls(newBalls))
32-
3327
// grid balls:
3428

3529
private[game] def isFull: Boolean = boardState.isFull
@@ -61,6 +55,11 @@ private[game] class BoardPlus(private[this] val boardState: BoardState,
6155
}
6256
}
6357

58+
// (lower-level) board state
59+
60+
private[game] def withBoardState(boardState: BoardState): BoardPlus =
61+
copy(boardState = boardState)
62+
6463
// (running) score:
6564

6665
private[game] def withAddedScore(increment: Int): BoardPlus =

src/test/scala/com/us/dsb/explore/algs/coloredlines/manual/game/board/BoardPlusTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BoardPlusTest extends AnyFunSpec {
2222
}
2323
}
2424
it("- with empty board--empty on-deck list") {
25-
assert(boardPlus.getOnDeckBalls.isEmpty)
25+
assert(boardPlus.boardState.getOnDeckBalls.isEmpty)
2626
}
2727
it("- with no selection") {
2828
assert(boardPlus.hasAnyCellSelected == false)

0 commit comments

Comments
 (0)