1
1
package com .us .dsb .explore .algs .coloredlines .manual .game
2
2
3
+ import cats .syntax .option ._
3
4
import com .us .dsb .explore .algs .coloredlines .manual .game .board .{BallKind , BoardOrder , BoardPlus , BoardState , CellAddress , columnIndices , rowIndices }
4
5
import com .us .dsb .explore .algs .coloredlines .manual .game .lines .LineDetector
5
6
@@ -12,7 +13,7 @@ object GameLogicSupport {
12
13
13
14
// (was "private[this]" before test calls:)
14
15
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*/ ))
16
17
17
18
// (was "private[this]" before test calls:)
18
19
@ tailrec
@@ -29,16 +30,12 @@ object GameLogicSupport {
29
30
}
30
31
}
31
32
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
+ )
39
37
{
40
38
println(s " ??? $this" )
41
- // ??? print("")
42
39
}
43
40
44
41
// ???? parameterize
@@ -49,16 +46,15 @@ object GameLogicSupport {
49
46
* @param boardPlus
50
47
* expected to be empty //???? maybe refactor something?
51
48
*/
52
- private [game] def placeInitialBalls (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult = {
49
+ private [game] def placeInitialBalls (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult = {
53
50
val postPlacementsResult =
54
51
// ???? parameterize:
55
- (1 to 5 ).foldLeft(MoveResult (boardPlus, false , false )) {
52
+ (1 to 5 ).foldLeft(BallArrivalResult (boardPlus, false )) {
56
53
case (resultSoFar, _) =>
57
54
val address =
58
55
pickRandomEmptyCell(resultSoFar.boardPlus).getOrElse(scala.sys.error(" Unexpectedly full board" ))
59
56
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)
62
58
}
63
59
64
60
val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementsResult.boardPlus.boardState)
@@ -126,12 +122,12 @@ object GameLogicSupport {
126
122
action
127
123
}
128
124
129
- private [this ] def placeNextBalls (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult = {
125
+ private [this ] def placeNextBalls (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult = {
130
126
val postPlacementResult =
131
127
// ???? for 1 to 3, consume on-deck ball from list, and then place (better for internal state view);;
132
128
// can replenish incrementally or later; later might show up better in internal state view
133
129
boardPlus.boardState.getOnDeckBalls
134
- .foldLeft(MoveResult (boardPlus, false , false )) {
130
+ .foldLeft(BallArrivalResult (boardPlus, false )) {
135
131
case (curMoveResult, onDeckBall) =>
136
132
pickRandomEmptyCell(curMoveResult.boardPlus) match {
137
133
case None => // board full; break out early (game will become over)
@@ -153,7 +149,7 @@ object GameLogicSupport {
153
149
val replenishedOnDeckBoard = replenishOnDeckBalls(postPlacementResult.boardPlus.boardState)
154
150
postPlacementResult.copy(boardPlus = postPlacementResult.boardPlus.withBoardState(replenishedOnDeckBoard))}
155
151
156
- private [game] def doPass (boardPlus : BoardPlus )(implicit rng : Random ): MoveResult =
152
+ private [game] def doPass (boardPlus : BoardPlus )(implicit rng : Random ): BallArrivalResult =
157
153
placeNextBalls(boardPlus)
158
154
159
155
// ???: likely move core algorithm out; possibly move outer code into BoardPlus/BoardState:
@@ -212,34 +208,35 @@ object GameLogicSupport {
212
208
loop
213
209
}
214
210
211
+ case class MoveBallResult (boardPlus : BoardPlus ,
212
+ clearSelection : Boolean )
213
+ {
214
+ println(s " ??? $this" )
215
+ }
215
216
216
217
private [game] def doTryMoveBall (boardPlus : BoardPlus ,
217
218
from : CellAddress ,
218
219
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)
226
223
// - see note near some Option/etc. re encoding only valid moves at
227
224
// that point in move-execution path
228
225
val canMoveBall = pathExists(boardPlus, from, to)
229
226
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 )
232
229
case true =>
233
230
val moveBallColor = boardPlus.getBallStateAt(from).get // ????
234
231
val postMoveBoard = boardPlus.withNoBallAt(from).withBallAt(to, moveBallColor)
235
232
236
233
val postReapingResult = LineDetector .handleBallArrival(postMoveBoard, to)
237
234
val postPostReadingResult =
238
- if (! postReapingResult.anyRemovals )
235
+ if (! postReapingResult.anyRemovals)
239
236
placeNextBalls(postReapingResult.boardPlus)
240
237
else
241
238
postReapingResult
242
- postPostReadingResult.copy( clearSelection = true )
239
+ MoveBallResult ( postPostReadingResult.boardPlus, clearSelection = true )
243
240
}
244
241
}
245
242
0 commit comments