Skip to content

Commit bba333f

Browse files
authored
Format (#66)
add scalafmt conf and check in CI
1 parent a7a27e4 commit bba333f

File tree

94 files changed

+4038
-1033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+4038
-1033
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ jobs:
88
- uses: actions/checkout@v1
99
- uses: olafurpg/setup-scala@v5
1010
- name: Test
11-
run: sbt test
11+
run: sbt scalafmtCheckAll test

.scalafmt.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = 2.5.2

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ resolvers += Resolver.mavenLocal
1313
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.2" % "test"
1414
libraryDependencies += "com.github.writethemfirst" % "approvals-java" % "0.4" % "test"
1515
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.13.1" % "test"
16+
libraryDependencies += "org.scalameta" %% "scalafmt-core" % "2.5.2"
1617

1718
enablePlugins(JmhPlugin)
1819
enablePlugins(JavaAppPackaging)

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
logLevel := Level.Warn
22
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3")
33
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3")
4+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.0")
45

src/main/scala/com/truelaurel/algorithm/alphabeta/AlphaBetaAi.scala

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import scala.annotation.tailrec
77
/**
88
* @param heuristic must represent higher chance of success for state.nextPlayer
99
*/
10-
case class AlphaBetaAi[S <: GameState[Boolean], M](rules: RulesFor2p[S, M],
11-
heuristic: S => Double) {
10+
case class AlphaBetaAi[S <: GameState[Boolean], M](
11+
rules: RulesFor2p[S, M],
12+
heuristic: S => Double
13+
) {
1214

1315
val MIN = Double.MinValue
1416
val MAX = Double.MaxValue
@@ -18,20 +20,16 @@ case class AlphaBetaAi[S <: GameState[Boolean], M](rules: RulesFor2p[S, M],
1820
best(sorted, MIN, MAX, state, depth).move.getOrElse(sorted.head)
1921
}
2022

21-
2223
def sortedMoves(state: S): Seq[M] = {
2324
rules.validMoves(state).sortBy(m => heuristic(rules.applyMove(state, m)))
2425
}
2526

26-
def negamax(state: S,
27-
depth: Int,
28-
alpha: Double,
29-
beta: Double): Double = {
27+
def negamax(state: S, depth: Int, alpha: Double, beta: Double): Double = {
3028
val player = state.nextPlayer
3129
rules.outcome(state) match {
3230
case Wins(`player`) => MAX
33-
case Wins(_) => MIN
34-
case Draw => 0
31+
case Wins(_) => MIN
32+
case Draw => 0
3533
case Undecided =>
3634
val moves = sortedMoves(state)
3735
if (depth == 0 || moves.isEmpty) heuristic(state)
@@ -40,22 +38,30 @@ case class AlphaBetaAi[S <: GameState[Boolean], M](rules: RulesFor2p[S, M],
4038
}
4139

4240
@tailrec
43-
final def best(moves: Seq[M],
44-
alpha: Double,
45-
beta: Double,
46-
state: S,
47-
depth: Int,
48-
currentBest: Option[M] = None): ScoredMove = {
41+
final def best(
42+
moves: Seq[M],
43+
alpha: Double,
44+
beta: Double,
45+
state: S,
46+
depth: Int,
47+
currentBest: Option[M] = None
48+
): ScoredMove = {
4949
if (beta > alpha && moves.nonEmpty) {
5050
val move = moves.head
5151
val nextState = rules.applyMove(state, move)
5252
val evaluation = -negamax(nextState, depth - 1, -beta, -alpha)
5353
val newBest = if (evaluation > alpha) Some(move) else currentBest
54-
best(moves.tail, alpha max evaluation, beta max evaluation, state, depth, newBest)
54+
best(
55+
moves.tail,
56+
alpha max evaluation,
57+
beta max evaluation,
58+
state,
59+
depth,
60+
newBest
61+
)
5562
} else ScoredMove(alpha, currentBest)
5663
}
5764

58-
5965
case class ScoredMove(score: Double, move: Option[M])
6066

6167
}

src/main/scala/com/truelaurel/algorithm/alphabeta/AlphaBetaAi2.scala

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package com.truelaurel.algorithm.alphabeta
22

33
import com.truelaurel.algorithm.game._
4-
import com.truelaurel.samplegames.wondev.domain.{MutableWondevState, WondevAction}
4+
import com.truelaurel.samplegames.wondev.domain.{
5+
MutableWondevState,
6+
WondevAction
7+
}
58

69
/**
710
* @param heuristic must represent higher chance of success for state.nextPlayer
811
*/
9-
case class AlphaBetaAi2(rules: RulesFor2p[MutableWondevState, WondevAction],
10-
heuristic: MutableWondevState => Double,
11-
moveHeuristc: (WondevAction, MutableWondevState) => Double
12-
) {
12+
case class AlphaBetaAi2(
13+
rules: RulesFor2p[MutableWondevState, WondevAction],
14+
heuristic: MutableWondevState => Double,
15+
moveHeuristc: (WondevAction, MutableWondevState) => Double
16+
) {
1317

1418
type S = MutableWondevState
1519
type M = WondevAction
1620

1721
val MIN = Double.MinValue
1822
val MAX = Double.MaxValue
1923

20-
2124
def bestMove(state: S, depth: Int): M = {
2225
val moves = rules.validMoves(state).sortBy(moveHeuristc(_, state))
2326

@@ -28,7 +31,13 @@ case class AlphaBetaAi2(rules: RulesFor2p[MutableWondevState, WondevAction],
2831
while (i < moves.size) {
2932
val move = moves(i)
3033

31-
val valueForMove = alphabeta(rules.applyMove(state, move), depth - 1, currentAlpha, MAX, maximizingPlayer = false)
34+
val valueForMove = alphabeta(
35+
rules.applyMove(state, move),
36+
depth - 1,
37+
currentAlpha,
38+
MAX,
39+
maximizingPlayer = false
40+
)
3241
state.writable.undo()
3342
if (valueForMove > value) {
3443
bestMove = move
@@ -40,7 +49,13 @@ case class AlphaBetaAi2(rules: RulesFor2p[MutableWondevState, WondevAction],
4049
bestMove
4150
}
4251

43-
def alphabeta(state: S, depth: Int, alpha: Double, beta: Double, maximizingPlayer: Boolean): Double = {
52+
def alphabeta(
53+
state: S,
54+
depth: Int,
55+
alpha: Double,
56+
beta: Double,
57+
maximizingPlayer: Boolean
58+
): Double = {
4459
if (depth == 0) {
4560
heuristic(state)
4661
} else {
@@ -54,7 +69,16 @@ case class AlphaBetaAi2(rules: RulesFor2p[MutableWondevState, WondevAction],
5469
var currentAlpha = alpha
5570
while (i < moves.size) {
5671
val move = moves(i)
57-
value = Math.max(value, alphabeta(rules.applyMove(state, move), depth - 1, currentAlpha, beta, maximizingPlayer = false))
72+
value = Math.max(
73+
value,
74+
alphabeta(
75+
rules.applyMove(state, move),
76+
depth - 1,
77+
currentAlpha,
78+
beta,
79+
maximizingPlayer = false
80+
)
81+
)
5882
state.writable.undo()
5983
currentAlpha = Math.max(currentAlpha, value)
6084
if (beta <= currentAlpha)
@@ -68,7 +92,16 @@ case class AlphaBetaAi2(rules: RulesFor2p[MutableWondevState, WondevAction],
6892
var currentBeta = beta
6993
while (i < moves.size) {
7094
val move = moves(i)
71-
value = Math.min(value, alphabeta(rules.applyMove(state, move), depth - 1, alpha, currentBeta, maximizingPlayer = true))
95+
value = Math.min(
96+
value,
97+
alphabeta(
98+
rules.applyMove(state, move),
99+
depth - 1,
100+
alpha,
101+
currentBeta,
102+
maximizingPlayer = true
103+
)
104+
)
72105
state.writable.undo()
73106
currentBeta = Math.min(currentBeta, value)
74107
if (currentBeta <= alpha)

src/main/scala/com/truelaurel/algorithm/dichotomy/Dichotomy.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ object Dichotomy {
1111
* @return the target value where we can't go lower anymore
1212
*/
1313
def search(low: Int, high: Int, lower: Int => Boolean): Int = {
14-
if (low == high) low else {
14+
if (low == high) low
15+
else {
1516
val mid = (low + high) / 2
1617
if (lower(mid)) {
1718
search(low, mid, lower)

src/main/scala/com/truelaurel/algorithm/game/GameRules.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ trait GameRules[P, S <: GameState[P], M] {
1313
def outcome(state: S): Outcome[P]
1414

1515
@tailrec
16-
final def judge(players: Map[P, S => M],
17-
debug: S => Unit,
18-
state: S = initial): Outcome[P] = {
16+
final def judge(
17+
players: Map[P, S => M],
18+
debug: S => Unit,
19+
state: S = initial
20+
): Outcome[P] = {
1921
debug(state)
2022
outcome(state) match {
2123
case Undecided =>
@@ -40,10 +42,10 @@ trait GameRules[P, S <: GameState[P], M] {
4042
def playRec(s: S): Outcome[P] = {
4143
outcome(s) match {
4244
case Undecided => playRec(applyMove(s, selectMove(s)))
43-
case decided => decided
45+
case decided => decided
4446
}
4547
}
4648

4749
playRec(state)
4850
}
49-
}
51+
}

src/main/scala/com/truelaurel/algorithm/game/Outcome.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ case class Wins[P](p: P) extends Outcome[P]
66

77
case object Undecided extends Outcome[Nothing]
88

9-
case object Draw extends Outcome[Nothing]
9+
case object Draw extends Outcome[Nothing]
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.truelaurel.algorithm.game
22

3-
trait RulesFor2p[S <: GameState[Boolean], M]
4-
extends GameRules[Boolean, S, M] {
3+
trait RulesFor2p[S <: GameState[Boolean], M] extends GameRules[Boolean, S, M] {
54

6-
7-
def judge(truePl: S => M,
8-
falsePl: S => M,
9-
debug: S => Unit): Outcome[Boolean] =
5+
def judge(
6+
truePl: S => M,
7+
falsePl: S => M,
8+
debug: S => Unit
9+
): Outcome[Boolean] =
1010
judge(Map(true -> truePl, false -> falsePl), debug, initial)
1111

1212
}

0 commit comments

Comments
 (0)