11package com .us .dsb .explore .algs .coloredlines .manual .game
22
3+ import cats .syntax .option ._
34import com .us .dsb .explore .algs .coloredlines .manual .game .board .{BallKind , BoardOrder , BoardPlus , BoardState , CellAddress , columnIndices , rowIndices }
45import com .us .dsb .explore .algs .coloredlines .manual .game .lines .LineDetector
56
@@ -12,7 +13,7 @@ object GameLogicSupport {
1213
1314 // (was "private[this]" before test calls:)
1415 private [game] def pickRandomBallKind ()(implicit rng : Random ): BallKind =
15- BallKind .values(rng.nextInt(3 /* ???BallKind.values.size*/ ))
16+ BallKind .values(rng.nextInt(2 /* ???BallKind.values.size*/ ))
1617
1718 // (was "private[this]" before test calls:)
1819 @ tailrec
@@ -29,16 +30,12 @@ object GameLogicSupport {
2930 }
3031 }
3132
32- // ???? probably split into BoardState-level vs. level of BoardState + score
33- case class MoveResult (boardPlus : BoardPlus ,
34- // ??? clarify re placing next three balls (re interpreting differently in different contexts
35- anyRemovals : Boolean ,
36- // ?????? clean this hack (used in only one case; re-plumb that case without this):
37- clearSelection : Boolean
38- )
33+ case class BallArrivalResult (boardPlus : BoardPlus ,
34+ anyRemovals : Boolean
35+ // ??? maybe score increment (for better notification)
36+ )
3937 {
4038 println(s " ??? $this" )
41- // ??? print("")
4239 }
4340
4441 // ???? parameterize
@@ -49,16 +46,15 @@ object GameLogicSupport {
4946 * @param boardPlus
5047 * expected to be empty //???? maybe refactor something?
5148 */
52- private [game] def placeInitialBalls (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult = {
49+ private [game] def placeInitialBalls (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult = {
5350 val postPlacementsResult =
5451 // ???? parameterize:
55- (1 to 5 ).foldLeft(MoveResult (boardPlus, false , false )) {
52+ (1 to 5 ).foldLeft(BallArrivalResult (boardPlus, false )) {
5653 case (resultSoFar, _) =>
5754 val address =
5855 pickRandomEmptyCell(resultSoFar.boardPlus).getOrElse(scala.sys.error(" Unexpectedly full board" ))
5956 val postPlacementBoardPlus = resultSoFar.boardPlus.withBallAt(address, pickRandomBallKind())
60- val placementHandlingResult = LineDetector .handleBallArrival(postPlacementBoardPlus, address)
61- MoveResult (placementHandlingResult.boardPlus, placementHandlingResult.anyRemovals, false )
57+ LineDetector .handleBallArrival(postPlacementBoardPlus, address)
6258 }
6359
6460 val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementsResult.boardPlus.boardState)
@@ -126,12 +122,12 @@ object GameLogicSupport {
126122 action
127123 }
128124
129- private [this ] def placeNextBalls (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult = {
125+ private [this ] def placeNextBalls (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult = {
130126 val postPlacementResult =
131127 // ???? for 1 to 3, consume on-deck ball from list, and then place (better for internal state view);;
132128 // can replenish incrementally or later; later might show up better in internal state view
133129 boardPlus.boardState.getOnDeckBalls
134- .foldLeft(MoveResult (boardPlus, false , false )) {
130+ .foldLeft(BallArrivalResult (boardPlus, false )) {
135131 case (curMoveResult, onDeckBall) =>
136132 pickRandomEmptyCell(curMoveResult.boardPlus) match {
137133 case None => // board full; break out early (game will become over)
@@ -153,7 +149,7 @@ object GameLogicSupport {
153149 val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementResult.boardPlus.boardState)
154150 postPlacementResult.copy(boardPlus = postPlacementResult.boardPlus.withBoardState(replenishedOnDeckBoard))}
155151
156- private [game] def doPass (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult =
152+ private [game] def doPass (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult =
157153 placeNextBalls(boardPlus)
158154
159155 // ???: likely move core algorithm out; possibly move outer code into BoardPlus/BoardState:
@@ -212,34 +208,35 @@ object GameLogicSupport {
212208 loop
213209 }
214210
211+ case class MoveBallResult (boardPlus : BoardPlus ,
212+ clearSelection : Boolean )
213+ {
214+ println(s " ??? $this" )
215+ }
215216
216217 private [game] def doTryMoveBall (boardPlus : BoardPlus ,
217218 from : CellAddress ,
218219 to : CellAddress
219- )(implicit rng : Random ): MoveResult = {
220- // ?????? re-plumb returning indication of whether to clear selection
221- // - first, pull clear-selection flag out of (current) MoveResult; return
222- // tuple or wrapper move-result adding flag
223- // - eventually, separate move-ball move validation from actually moving
224- // (selection clearing depends on just validity of move, not on deleting
225- // any lines)
220+ )(implicit rng : Random ): MoveBallResult = {
221+ // ???? separate move-ball move validation from actually moving (selection
222+ // clearing depends on just validity of move, not on deleting any lines)
226223 // - see note near some Option/etc. re encoding only valid moves at
227224 // that point in move-execution path
228225 val canMoveBall = pathExists(boardPlus, from, to)
229226 canMoveBall match {
230- case false => // can't move--ignore (keep selection state)
231- MoveResult (boardPlus, anyRemovals = false , clearSelection = false )
227+ case false => // can't move--ignore (keep tap-UI selection state)
228+ MoveBallResult (boardPlus, clearSelection = false )
232229 case true =>
233230 val moveBallColor = boardPlus.getBallStateAt(from).get // ????
234231 val postMoveBoard = boardPlus.withNoBallAt(from).withBallAt(to, moveBallColor)
235232
236233 val postReapingResult = LineDetector .handleBallArrival(postMoveBoard, to)
237234 val postPostReadingResult =
238- if (! postReapingResult.anyRemovals )
235+ if (! postReapingResult.anyRemovals)
239236 placeNextBalls(postReapingResult.boardPlus)
240237 else
241238 postReapingResult
242- postPostReadingResult.copy( clearSelection = true )
239+ MoveBallResult ( postPostReadingResult.boardPlus, clearSelection = true )
243240 }
244241 }
245242
0 commit comments