Skip to content

Commit 2277cf1

Browse files
chore: clean up a bit more Pos and Variant
1 parent 24067bc commit 2277cf1

File tree

2 files changed

+6
-35
lines changed

2 files changed

+6
-35
lines changed

src/main/scala/abalone/Pos.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,11 @@ case class Pos private (index: Int) extends AnyVal {
190190
def right: Option[Pos] = Pos.at(file.index + 1, rank.index)
191191
def downRight: Option[Pos] = Pos.at(file.index, rank.index - 1)
192192
def upRight: Option[Pos] = Pos.at(file.index + 1, rank.index + 1)
193+
def neighbours: List[Option[Pos]] = List(left, downLeft, upLeft, right, downRight, upRight)
193194

194195
@inline def file = File of this // column (as if it was an index in a 1D array)
195196
@inline def rank = Rank of this // horizontal row, makes sense in a 2D array
196197

197-
def neighbours: List[Option[Pos]] = List(left, downLeft, upLeft, right, downRight, upRight)
198-
199198
// these 3 below might be handy
200199
// def touches(other: Pos): Boolean = xDist(other) <= 1 && yDist(other) <= 1
201200
// def xDist(other: Pos) = abs(file - other.file)

src/main/scala/abalone/variant/Variant.scala

+5-33
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ abstract class Variant private[variant] (
7575
'a' to '3' or 'c' to '4' are line moves of a single marble
7676
7777
we want to have :
78-
1. moves of 1 marble (to an empty square) + side moves
79-
2. side moves (as they are based on moves of 1 marble)
80-
3. line moves of 2+ marbles
81-
4. pushes
78+
1. moves of 1 marble + side moves (as we are reusing moves of 1 marble)
79+
2. line moves of 2+ marbles + pushes ((re-do a pass on line moves that were stuck ?))
8280
then merge these as valid moves.
8381
*/
8482
def validMoves(situation: Situation): Map[Pos, List[Move]] = {
@@ -98,18 +96,11 @@ abstract class Variant private[variant] (
9896
situation.board.piecesOf(situation.player).flatMap {
9997
case ((pos, piece)) =>
10098
Map(pos ->
101-
pos.neighbours.flatten // for each direction that would remain on the board...
102-
.filterNot(situation.board.pieces.contains(_)) // ...keep the piece only if the landing square is empty
99+
pos.neighbours.flatten
100+
.filterNot(situation.board.pieces.contains(_))
103101
.map(landingSquare =>
104102
Move(piece, pos, landingSquare, situation, boardAfter(situation, pos, landingSquare), false)
105103
)
106-
// instead of .filterNot etc, if we wanted to take care of several cases directly from here, we could have written :
107-
// .flatMap {
108-
// case (landingSquare) if (!situation.board.pieces.contains(landingSquare)) =>
109-
// Some(Move(piece, pos, landingSquare, situation, boardAfter(situation, pos, landingSquare), false))
110-
// case (landingSquare) if (another condition)
111-
// case _ => None
112-
// }
113104
)
114105
}.toMap
115106

@@ -119,27 +110,8 @@ abstract class Variant private[variant] (
119110

120111
def validLineMoves(@nowarn situation: Situation): Map[Pos, List[Move]] = {
121112
Map()
122-
// situation.board.piecesOf(situation.player).flatMap {
123-
// case ((pos, piece)) => {
124-
// Map(pos -> List({
125-
// Stone.dirs.flatMap { // if the piece would remain on the board after applying a direction...
126-
// case dir => (dir, dir(pos))
127-
// case _ => None
128-
// }.flatMap { // ...check that it would move to square occupied by one of our pieces
129-
// case ((dir: Direction, landingSquare: Option[Pos]))
130-
// if (situation.board.pieces.contains(landingSquare) && situation.board.pieces(landingSquare).player == situation.player) =>
131-
// // let's check if the next square is occupied by us too, or empty :
132-
// if(dir(landingSquare))
133-
// Some(Move(piece, pos, landingSquare, situation, boardAfter(situation, pos, landingSquare), false))
134-
// else
135-
// None
136-
137-
// case _ => None
138-
// }
139-
// }).flatten)
140-
// }
141-
// }.toMap
142113
}
114+
143115

144116
def validPushes(@nowarn situation: Situation): Map[Pos, List[Move]] = {
145117
Map()

0 commit comments

Comments
 (0)