Skip to content

Commit 5eecfe3

Browse files
feat: Pos gets to see neighbours
1 parent 4040331 commit 5eecfe3

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/main/scala/Move.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ object Move {
359359
Situation.Abalone(m.situationBefore),
360360
Board.Abalone(m.after),
361361
m.autoEndTurn,
362-
None, // capture. @TODO: Could be the pos of the piece that was on targetSquare described by the move (because line moves are basically just marbles jumping over other ones)
362+
None, // capture. @TODO: Could be the pos of the piece that was on targetSquare described by the move (because line moves (and by extension, pushes) are basically just marbles jumping over other ones)
363363
None,
364364
None,
365365
None,

src/main/scala/abalone/Pos.scala

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ case class Pos private (index: Int) extends AnyVal {
194194
@inline def file = File of this // column (as if it was an index in a 1D array)
195195
@inline def rank = Rank of this // horizontal row, makes sense in a 2D array
196196

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

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ abstract class Variant private[variant] (
9898
situation.board.piecesOf(situation.player).flatMap {
9999
case ((pos, piece)) =>
100100
Map(pos ->
101-
Stone.dirs.flatMap { _(pos) } // if the piece would remain on the board after applying a direction...
102-
.filterNot(situation.board.pieces.contains(_)) // ...keep it only if the landing square is free of marble
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
103103
.map(landingSquare =>
104104
Move(piece, pos, landingSquare, situation, boardAfter(situation, pos, landingSquare), false)
105105
)
@@ -123,10 +123,17 @@ abstract class Variant private[variant] (
123123
// case ((pos, piece)) => {
124124
// Map(pos -> List({
125125
// Stone.dirs.flatMap { // if the piece would remain on the board after applying a direction...
126-
// case dir => dir(pos)
126+
// case dir => (dir, dir(pos))
127+
// case _ => None
127128
// }.flatMap { // ...check that it would move to square occupied by one of our pieces
128-
// case (landingSquare) if (situation.board.pieces.contains(landingSquare) && situation.board.pieces(landingSquare).player == situation.player) =>
129-
// Some(Move(piece, pos, landingSquare, situation, boardAfter(situation, pos, landingSquare), false))
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+
130137
// case _ => None
131138
// }
132139
// }).flatten)

src/test/scala/abalone/AbaloneFenTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class AbaloneFenTest extends AbaloneTest with ValidatedMatchers {
105105
pieces.filter(p => p._2.player == P2).size must_== 14
106106
}
107107

108-
"has an even number of valid moves, as the position is symetrical" in {
108+
"has an even number of valid moves, as the position is symmetrical" in {
109109
board.variant.validMoves(situation).foldLeft(0)(_ + _._2.size) % 2 must_== 0
110110
}
111111
}

0 commit comments

Comments
 (0)