Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validMoves and boardAfter #188

Draft
wants to merge 26 commits into
base: 503-abalone
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5d63998
feat: moves of 2 marbles
vincentfrochot Oct 21, 2024
033fd13
chore: codestyle
vincentfrochot Oct 21, 2024
471f1bc
feat: generate movesOf2 based on pairs of marbles
vincentfrochot Oct 21, 2024
2fc34c9
feat: generate validMoves
vincentfrochot Oct 23, 2024
b8a3c35
chore: move sideMoves methods under parent one
vincentfrochot Oct 23, 2024
8d08872
chore: use dropRight on List
vincentfrochot Oct 23, 2024
17357a6
chore: map(func) equals map(func(_))
vincentfrochot Oct 23, 2024
70b3abd
chore: situation.board.piecesOf(x) as val
vincentfrochot Oct 23, 2024
832dc59
chore: codestyle & reword some tests
vincentfrochot Oct 23, 2024
14b016e
chore: codestyle - direction can be computed from 2 Pos
vincentfrochot Oct 23, 2024
ece31e5
chore: refactor side moves
vincentfrochot Oct 24, 2024
d8f559b
chore: improve codestyle
vincentfrochot Oct 24, 2024
763e0db
feat: boardAfter
vincentfrochot Oct 25, 2024
ef24f2e
chore: use Directions instead of Option[String]
vincentfrochot Oct 25, 2024
6b42fcb
chore: remove done TODO
vincentfrochot Oct 25, 2024
e5f54a4
feat: 3fold, improve codestyle
vincentfrochot Oct 29, 2024
c75eea8
chore: codestyle Variant.piecesAfterAction
vincentfrochot Oct 29, 2024
93be03b
fix: Game make use of metrics
vincentfrochot Oct 29, 2024
8e3b9dc
chore: remove copy pasted false comment
vincentfrochot Oct 29, 2024
3e2f3be
fix: get rid of autoEndTurn in Move
vincentfrochot Oct 29, 2024
cfc8552
fix: revert autoEndTurn to get it back
vincentfrochot Oct 29, 2024
182321a
fix: side moves pieceMap update
vincentfrochot Oct 29, 2024
92d5961
feat: remove move cat in validMovesOf2And3
vincentfrochot Oct 29, 2024
69066c6
chore: remove unused dir as String methods
vincentfrochot Oct 29, 2024
4d85c75
feat: update PieceMap based on 2 Pos only
vincentfrochot Oct 30, 2024
98de331
feat: replayMove invoke boardAfter
vincentfrochot Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/scala/abalone/Board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ case class Board(

def piecesOf(player: Player): PieceMap = pieces.filter(_._2.is(player))

def isEmptySquare(pos: Option[Pos]): Boolean = pos.fold(false)(!this.pieces.contains(_))

def withHistory(h: History): Board = copy(history = h)
def updateHistory(f: History => History) = copy(history = f(history))

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/abalone/Move.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ case class Move(
) extends Action(situationBefore, after, metrics) {

def situationAfter =
Situation(finalizeAfter, if (autoEndTurn) !piece.player else piece.player)
Situation(finalizeAfter, !piece.player)

def applyVariantEffect: Move = before.variant addVariantEffect this

Expand Down
50 changes: 48 additions & 2 deletions src/main/scala/abalone/Pos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,43 @@ case class Pos private (index: Int) extends AnyVal {
def right: Option[Pos] = Pos.at(file.index + 1, rank.index)
def downRight: Option[Pos] = Pos.at(file.index, rank.index - 1)
def upRight: Option[Pos] = Pos.at(file.index + 1, rank.index + 1)
def neighbours: List[Option[Pos]] = List(left, downLeft, upLeft, right, downRight, upRight)

def neighbours: List[Option[Pos]] = List(left, upLeft, upRight, right, downRight, downLeft)

// NOTE - *neighbourhood
// these below only work for neighbour pos but that's probably fine as in Abalone we only move to (potentially extended) neighbourhood
def dir(dir: Option[String]): Option[Pos] = dir match {
case Some("left") => this.left
case Some("upLeft") => this.upLeft
case Some("upRight") => this.upRight
case Some("right") => this.right
case Some("downRight") => this.downRight
case Some("downLeft") => this.downLeft
case _ => None
}
def dir(dir: String): Option[Pos] = dir match {
case "left" => this.left
case "upLeft" => this.upLeft
case "upRight" => this.upRight
case "right" => this.right
case "downRight" => this.downRight
case "downLeft" => this.downLeft
case _ => None
}

def dir(pos: Pos): Option[String] =
(pos.file.index - this.file.index, pos.rank.index - this.rank.index) match {
case (0, 1) => Some("upLeft")
case (0, -1) => Some("downRight")
case (1, 1) => Some("upRight")
case (1, 0) => Some("right")
case (-1, 0) => Some("left")
case (-1, -1) => Some("downLeft")
case _ => None
}

def isInLine(pos1: Pos, pos2: Pos): Boolean = this.dir(pos1) == pos1.dir(pos2)
// *end of note about neighbourhood

@inline def file = File of this // column (as if it was an index in a 1D array)
@inline def rank = Rank of this // horizontal row, makes sense in a 2D array
Expand Down Expand Up @@ -238,7 +274,7 @@ case class Pos private (index: Int) extends AnyVal {

def key = file.toString + rank.toString
def officialNotationKey = s"${File(rank.index).getOrElse("")}${Rank(file.index).getOrElse("")}"
override def toString = officialNotationKey
override def toString = key
}

object Pos {
Expand Down Expand Up @@ -282,6 +318,16 @@ object Pos {
if (isInHexagon(x + File.all.size * y)) Some(new Pos(x + File.all.size * y))
else None

def sideMovesDirsFromDir(dir: Option[String]): List[String] = dir match {
case Some("left") => List("downLeft", "upLeft")
case Some("upLeft") => List("left", "upRight")
case Some("upRight") => List("upLeft", "right")
case Some("right") => List("upRight", "downRight")
case Some("downRight") => List("right", "downLeft")
case Some("downLeft") => List("downRight", "left")
case _ => List()
}

def fromKey(key: String): Option[Pos] = allKeys get key

def piotr(c: Char): Option[Pos] = allPiotrs get c
Expand Down
Loading