Skip to content

Commit 2bf99b8

Browse files
authored
Integrate scalafmt CI step (#6)
1 parent 836212a commit 2bf99b8

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

.github/workflows/pull_request.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ jobs:
2121
java-version: 'adopt:1.11.0-11'
2222
node-version: '16.7.0'
2323

24-
# - name: Check code format
25-
# run: sbt scalafmtCheckAll
24+
- name: Check code format
25+
run: sbt scalafmtCheckAll
2626

2727
- name: Compile
2828
run: CI=true sbt compile

project/plugins.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1")
2+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
23

src/main/scala/scalajsGames/games/AstroLander.scala

+24-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package games
44
import org.scalajs.dom
55
import scala.util.Random
66

7-
case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
7+
case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game {
88
val points = {
99
var current = 450
1010
var pts = List.empty[Point]
@@ -28,10 +28,10 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
2828
x
2929
}
3030

31-
(0 to 21).foreach{n =>
32-
if (n == flat+1) current = current
33-
else if (n == cliff1+1) current = current - Random.nextInt(25) - 150
34-
else if (n == cliff2+2) current = current - Random.nextInt(25) + 150
31+
(0 to 21).foreach { n =>
32+
if (n == flat + 1) current = current
33+
else if (n == cliff1 + 1) current = current - Random.nextInt(25) - 150
34+
else if (n == cliff2 + 2) current = current - Random.nextInt(25) + 150
3535
else current = current - Random.nextInt(50) + 25
3636

3737
if (current > bounds.y) current = (2 * bounds.y - current).toInt
@@ -49,15 +49,14 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
4949

5050
def shipPoints = Seq(
5151
craftPos + Point(15, 0).rotate(theta),
52-
craftPos + Point(7, 0).rotate(theta + 127.5/180 * Math.PI),
53-
craftPos + Point(7, 0).rotate(theta - 127.5/180 * Math.PI)
52+
craftPos + Point(7, 0).rotate(theta + 127.5 / 180 * Math.PI),
53+
craftPos + Point(7, 0).rotate(theta - 127.5 / 180 * Math.PI)
5454
)
5555
def draw(ctx: dom.CanvasRenderingContext2D) = {
5656
ctx.textAlign = "left"
5757
ctx.fillStyle = Color.Black
5858
ctx.fillRect(0, 0, bounds.x, bounds.y)
5959

60-
6160
ctx.fillStyle = if (craftVel.length < 3) Color.Green else Color.White
6261
ctx.fillText("Speed: " + (craftVel.length * 10).toInt.toDouble / 10, 20, 50)
6362

@@ -69,7 +68,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
6968

7069
ctx.beginPath()
7170
ctx.moveTo(0, bounds.y)
72-
for(p <- points) ctx.lineTo(p.x, p.y)
71+
for (p <- points) ctx.lineTo(p.x, p.y)
7372
ctx.lineTo(bounds.x, bounds.y)
7473
ctx.fill()
7574

@@ -87,7 +86,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
8786
p + Point(a, a).rotate(angle - offset),
8887
p + Point(b, b).rotate(angle - offset + width),
8988
p + Point(c, c).rotate(angle - offset),
90-
p + Point(b, b).rotate(angle - offset - width),
89+
p + Point(b, b).rotate(angle - offset - width),
9190
p + Point(a, a).rotate(angle - offset)
9291
)
9392
}
@@ -96,7 +95,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
9695
}
9796

9897
ctx.strokeStyle = Color.Red
99-
if (fuel > 0){
98+
if (fuel > 0) {
10099
if (lastKeys(37)) drawFlame(craftPos, theta + math.Pi / 4)
101100
if (lastKeys(39)) drawFlame(craftPos, theta - math.Pi / 4)
102101
if (lastKeys(40)) drawFlame(craftPos, theta)
@@ -106,7 +105,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
106105
var lastKeys: Set[Int] = Set()
107106
def update(keys: Set[Int]) = {
108107
lastKeys = keys
109-
if (fuel > 0){
108+
if (fuel > 0) {
110109
if (keys(37)) craftVel += Point(0.5, 0).rotate(theta + math.Pi / 4)
111110
if (keys(39)) craftVel += Point(0.5, 0).rotate(theta - math.Pi / 4)
112111
if (keys(40)) craftVel += Point(0.5, 0).rotate(theta)
@@ -116,29 +115,28 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
116115
craftVel += Point(0, 0.2)
117116
craftPos += craftVel
118117

119-
120-
121-
val hit = points.flatMap{ p =>
118+
val hit = points.flatMap { p =>
122119
val prevIndex = points.lastIndexWhere(_.x < craftPos.x)
123120
if (prevIndex == -1 || prevIndex == 21) None
124-
else{
121+
else {
125122
val prev = points(prevIndex)
126123
val next = points(prevIndex + 1)
127124
val height = (craftPos.x - prev.x) / (next.x - prev.x) * (next.y - prev.y) + prev.y
128125
if (height > craftPos.y) None
129-
else Some{
130-
val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
131-
val landingSkew = math.abs(craftVel.x / craftVel.y)
132-
133-
if (groundGradient > 0.1) Failure("landing area too steep")
134-
else if (landingSkew > 1) Failure("too much horiontal velocity")
135-
else if(craftVel.length > 3) Failure("coming in too fast")
136-
else Success
137-
}
126+
else
127+
Some {
128+
val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
129+
val landingSkew = math.abs(craftVel.x / craftVel.y)
130+
131+
if (groundGradient > 0.1) Failure("landing area too steep")
132+
else if (landingSkew > 1) Failure("too much horiontal velocity")
133+
else if (craftVel.length > 3) Failure("coming in too fast")
134+
else Success
135+
}
138136
}
139137
}
140138

141-
hit.headOption.map{
139+
hit.headOption.map {
142140
case Success =>
143141
result = Some("You have landed successfully.")
144142
resetGame()

src/main/scala/scalajsGames/games/Snake.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ case class Snake(bounds: Point, resetGame: () => Unit) extends Game {
8989
x match {
9090
case Apple(_, s) =>
9191
/*
92-
* Instead of using red apple and bonus apple default score (2pts and 5pts):
93-
* - Red apple: +1 score (from 2 / 2 = 1)
94-
* - Bonus apple: +2 score (from 5 / 2 = 2)
95-
*
96-
* This is for a better understanding for the user about the values of the apples
97-
*/
92+
* Instead of using red apple and bonus apple default score (2pts and 5pts):
93+
* - Red apple: +1 score (from 2 / 2 = 1)
94+
* - Bonus apple: +2 score (from 5 / 2 = 2)
95+
*
96+
* This is for a better understanding for the user about the values of the apples
97+
*/
9898
score += s / 2
9999
length += s
100100
case _ =>

0 commit comments

Comments
 (0)