11package 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 }
54import com .us .dsb .explore .algs .coloredlines .manual .game .lines .LineDetector
65
76import 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 */
0 commit comments