Skip to content

Commit b6e0268

Browse files
author
Daniel Barclay
committed
ColoredLines: Cleaned two level placement/move results a lot.
1 parent 49bcd3b commit b6e0268

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

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

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

3+
import cats.syntax.option._
34
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BallKind, BoardOrder, BoardPlus, BoardState, CellAddress, columnIndices, rowIndices}
45
import 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

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.us.dsb.explore.algs.coloredlines.manual.game
22

3-
import cats.syntax.option._
43
import cats.syntax.either._
54
import com.us.dsb.explore.algs.coloredlines.manual.game.board.CellAddress
6-
import com.us.dsb.explore.algs.coloredlines.manual.game.GameLogicSupport.MoveResult
7-
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BallKind, BoardPlus}
8-
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{ColumnIndex, RowIndex}
5+
import com.us.dsb.explore.algs.coloredlines.manual.game.board.BoardPlus
96

107
import scala.util.Random
118

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import BoardState._
2121
private[game] class BoardState(private[this] val cellStates: Vector[CellBallState],
2222
private[this] val onDeck: Iterable[BallKind]
2323
) {
24-
println("??? BoardState: " + this)
24+
//println("??? BoardState: " + this)
2525
//print("")
2626

2727
// internal/support methods:

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

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

33
import com.us.dsb.explore.algs.coloredlines.manual.game.board.CellAddress
4-
import com.us.dsb.explore.algs.coloredlines.manual.game.GameLogicSupport.MoveResult
5-
import com.us.dsb.explore.algs.coloredlines.manual.game._
4+
import com.us.dsb.explore.algs.coloredlines.manual.game.GameLogicSupport.BallArrivalResult
65
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BallKind, BoardPlus, BoardOrder, LineOrder}
76

87
//???? TODO: reduce repeated passing of board, etc.; maybe make LineDetector a
@@ -113,12 +112,14 @@ object LineDetector { //????? adjust most from using BoardPlus to using just Bo
113112
* @return
114113
* None if no line(s) completed; score increment otherwise
115114
*/
116-
//???? rename?: harvestAnyLines?
115+
//????? rename: doesn't handle everything: handles harvesting/reaping and
116+
// scoring, but not no-lines placement of three more balls
117117
private[game] def handleBallArrival(boardPlus: BoardPlus,
118118
ballTo: CellAddress
119-
): MoveResult = {
119+
): BallArrivalResult = {
120120
//println(s"+handleBallArrival(... ballTo = $ballTo...).1")
121121
val moveBallColor = boardPlus.getBallStateAt(ballTo).get //????
122+
println(s"??? * placed at $ballTo: $moveBallColor")
122123

123124
val allAxesResults: List[AxisResult] =
124125
lineAxes.map { lineAxis =>
@@ -133,7 +134,7 @@ object LineDetector { //????? adjust most from using BoardPlus to using just Bo
133134
(boardPlus, None) // return None for score (signal to place 3 more IF ball moved by user)
134135
case linesAxes =>
135136
val totalBallsBeingRemoved = 1 + linesAxes.map(_.axisLineAddedLength).sum
136-
println(s" scoreMove(... ballTo = $ballTo...).x totalBallsBeingRemoved = $totalBallsBeingRemoved")
137+
println(s"??? * reaped at $ballTo: $totalBallsBeingRemoved $moveBallColor balls")
137138
//???? move?
138139
// note original game scoring: score = totalBallsBeingRemoved * 4 - 10,
139140
// which seems to be from 2 pts per ball in 5-ball line, but 4 for any extra balls in line
@@ -144,7 +145,7 @@ object LineDetector { //????? adjust most from using BoardPlus to using just Bo
144145
(postLinesRemovalBoard.withAddedScore(ballPlacementScore), Some(ballPlacementScore))
145146
}
146147
//println(s"-handleBallArrival(... ballTo = $ballTo...).9 = score result = $scoreResult")
147-
MoveResult(boardResult, scoreResult.isDefined, false)
148+
BallArrivalResult(boardResult, anyRemovals = scoreResult.isDefined)
148149
}
149150

150151
}

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

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

33
import com.us.dsb.explore.algs.coloredlines.manual.game.board.CellAddress
4-
import com.us.dsb.explore.algs.coloredlines.manual.game.GameLogicSupport.MoveResult
54
import com.us.dsb.explore.algs.coloredlines.manual.game.board.{BoardPlus, ColumnIndex, RowIndex, columnIndices, rowIndices}
65
import org.scalatest.funspec.AnyFunSpec
76
import org.scalatest.matchers.should.Matchers._

0 commit comments

Comments
 (0)