Skip to content

Commit b3fbdfe

Browse files
committed
migrate to Scala 3 new control syntax
1 parent 1b5d6ca commit b3fbdfe

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

src/main/scala/introprog/BlockGame.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ abstract class BlockGame(
8585
* It draws only updated blocks aiming at the desired frame rate.
8686
* It calls each `onXXX` method if a corresponding event is detected.
8787
*/
88-
protected def gameLoop(stopWhen: => Boolean): Unit = while (!stopWhen) {
88+
protected def gameLoop(stopWhen: => Boolean): Unit = while !stopWhen do {
8989
import PixelWindow.Event
9090
val t0 = System.currentTimeMillis
9191
pixelWindow.awaitEvent(MaxWaitForEventMillis.toLong)
92-
while (pixelWindow.lastEventType != PixelWindow.Event.Undefined) {
92+
while pixelWindow.lastEventType != PixelWindow.Event.Undefined do {
9393
pixelWindow.lastEventType match {
9494
case Event.KeyPressed => onKeyDown(pixelWindow.lastKey)
9595
case Event.KeyReleased => onKeyUp(pixelWindow.lastKey)
@@ -103,17 +103,17 @@ abstract class BlockGame(
103103
gameLoopAction()
104104
drawUpdatedBlocks()
105105
val elapsed = System.currentTimeMillis - t0
106-
if ((gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis) {
106+
if (gameLoopDelayMillis - elapsed) < MaxWaitForEventMillis then {
107107
onFrameTimeOverrun(elapsed)
108108
}
109109
Thread.sleep((gameLoopDelayMillis - elapsed) max 0)
110110
}
111111

112112
/** Draw updated blocks and carry out post-update actions if any. */
113113
private def drawUpdatedBlocks(): Unit = {
114-
for (x <- blockBuffer.indices) {
115-
for (y <- blockBuffer(x).indices) {
116-
if (isBufferUpdated(x)(y)) {
114+
for x <- blockBuffer.indices do {
115+
for y <- blockBuffer(x).indices do {
116+
if isBufferUpdated(x)(y) then {
117117
val pwx = x * blockSize
118118
val pwy = y * blockSize
119119
pixelWindow.fill(pwx, pwy, blockSize, blockSize, blockBuffer(x)(y))
@@ -129,24 +129,24 @@ abstract class BlockGame(
129129
def clearWindow(): Unit = {
130130
pixelWindow.clear()
131131
clearMessageArea()
132-
for (x <- blockBuffer.indices) {
133-
for (y <- blockBuffer(x).indices) {
132+
for x <- blockBuffer.indices do {
133+
for y <- blockBuffer(x).indices do {
134134
blockBuffer(x)(y) = background
135135
}
136136
}
137137
}
138138

139139
/** Paint a block in color `c` at (`x`,`y`) in block coordinates. */
140140
def drawBlock(x: Int, y: Int, c: Color): Unit = {
141-
if (blockBuffer(x)(y) != c) {
141+
if blockBuffer(x)(y) != c then {
142142
blockBuffer(x)(y) = c
143143
isBufferUpdated(x)(y) = true
144144
}
145145
}
146146

147147
/** Erase the block at (`x`,`y`) to `background` color. */
148148
def eraseBlock(x: Int, y: Int): Unit = {
149-
if (blockBuffer(x)(y) != background) {
149+
if blockBuffer(x)(y) != background then {
150150
blockBuffer(x)(y) = background
151151
isBufferUpdated(x)(y) = true
152152
}

src/main/scala/introprog/PixelWindow.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class PixelWindow(
166166
}
167167

168168
case ke: java.awt.event.KeyEvent =>
169-
if (ke.getKeyChar == java.awt.event.KeyEvent.CHAR_UNDEFINED || ke.getKeyChar < ' ')
169+
if ke.getKeyChar == java.awt.event.KeyEvent.CHAR_UNDEFINED || ke.getKeyChar < ' ' then
170170
_lastKeyText = PixelWindow.keyTextLookup.getOrElse(ke.getKeyCode, java.awt.event.KeyEvent.getKeyText(ke.getKeyCode))
171171
else _lastKeyText = ke.getKeyChar.toString
172172

@@ -203,7 +203,7 @@ class PixelWindow(
203203
*/
204204
def awaitEvent(timeoutInMillis: Long = 1): Unit = {
205205
val e = eventQueue.poll(timeoutInMillis, java.util.concurrent.TimeUnit.MILLISECONDS)
206-
if (e != null) handleEvent(e) else _lastEventType = Event.Undefined
206+
if e != null then handleEvent(e) else _lastEventType = Event.Undefined
207207
}
208208

209209
/** Draw a line from (`x1`, `y1`) to (`x2`, `y2`) using `color` and `lineWidth`. */

src/main/scala/introprog/Swing.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object Swing {
3131

3232
/** Test if the current operating system name includes `partOfName`. */
3333
def isOS(partOfName: String): Boolean =
34-
if (partOfName.toLowerCase.startsWith("win") && isInProc("windows", "wsl", "microsoft")) true //WSL
34+
if partOfName.toLowerCase.startsWith("win") && isInProc("windows", "wsl", "microsoft") then true //WSL
3535
else scala.sys.props("os.name").toLowerCase.contains(partOfName.toLowerCase)
3636

3737
/** Check whether `/proc/version` on this filesystem contains any of the strings in `parts`.
@@ -45,16 +45,16 @@ object Swing {
4545
private var isInit = false
4646

4747
/** Init the Swing GUI toolkit and set platform-specific look and feel.*/
48-
def init(): Unit = if (!isInit) {
48+
def init(): Unit = if !isInit then {
4949
setPlatformSpecificLookAndFeel()
5050
isInit = true
5151
}
5252

5353
private def setPlatformSpecificLookAndFeel(): Unit = {
5454
import javax.swing.UIManager.setLookAndFeel
55-
if (isOS("win")) findLookAndFeel("win").foreach(setLookAndFeel)
56-
else if (isOS("linux")) findLookAndFeel("gtk").foreach(setLookAndFeel)
57-
else if (isOS("mac")) findLookAndFeel("apple").foreach(setLookAndFeel)
55+
if isOS("win") then findLookAndFeel("win").foreach(setLookAndFeel)
56+
else if isOS("linux") then findLookAndFeel("gtk").foreach(setLookAndFeel)
57+
else if isOS("mac") then findLookAndFeel("apple").foreach(setLookAndFeel)
5858
else javax.swing.UIManager.setLookAndFeel(
5959
javax.swing.UIManager.getSystemLookAndFeelClassName()
6060
)

src/main/scala/introprog/examples/TestBlockGame.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ object TestBlockGame {
5555
def rndPos: (Int, Int) = (nextInt(dim._1), nextInt(dim._2))
5656
def rndColor = new java.awt.Color(nextInt(256), nextInt(256), nextInt(256))
5757
print(".")
58-
if (isDrawingRandomBlocks) {
58+
if isDrawingRandomBlocks then {
5959
drawBlock(rndPos._1, rndPos._2, rndColor)
6060
}
6161
}

src/main/scala/introprog/examples/TestIO.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object TestIO {
1717
val highscores2 = IO.loadObject[Map[Person, Int]]("highscores.ser")
1818

1919
val isSameContents = highscores2 == highscores
20-
val testResult = if (isSameContents) "SUCCESS :)" else "FAILURE :("
20+
val testResult = if isSameContents then "SUCCESS :)" else "FAILURE :("
2121
assert(isSameContents, s"$highscores != $highscores2")
2222
println(s"$highscores == $highscores2\n$testResult")
2323

src/main/scala/introprog/examples/TestPixelWindow.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ object TestPixelWindow {
3434
square(150,200, 50)
3535
w.line(0,0,w.width,w.height)
3636

37-
while (w.lastEventType != Event.WindowClosed) {
37+
while w.lastEventType != Event.WindowClosed do {
3838
w.awaitEvent(10) // wait for next event for max 10 milliseconds
3939

40-
if (w.lastEventType != Event.Undefined) {
40+
if w.lastEventType != Event.Undefined then {
4141
println(s"lastEventType: ${w.lastEventType} => ${Event.show(w.lastEventType)}")
4242
}
4343

0 commit comments

Comments
 (0)