Skip to content

Commit

Permalink
Upgrade scala (#102)
Browse files Browse the repository at this point in the history
- Update Scala from 2.11.6 to 3.3.1
- Update ScalaTest from 2.11_2.2.4 to 3_3.2.17
- Fix sbt version
- Introduce a GitHub Action to ensure project can be compiled without any error
  • Loading branch information
seblm authored Aug 23, 2024
1 parent ae03d1b commit b03e10f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Scala

on:
push:
branches: [ master ]
paths:
- 'scala/**'
pull_request:
branches: [ master ]
paths:
- 'scala/**'

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: 'sbt'
- run: sbt compile
working-directory: scala
1 change: 0 additions & 1 deletion scala/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
target
.idea
10 changes: 6 additions & 4 deletions scala/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
scalaVersion := "2.11.6"
lazy val scala = project
.in(file("."))
.settings(
scalaVersion := "3.3.1",

scalacOptions ++= Seq("-feature", "-deprecation")

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test
)
1 change: 1 addition & 0 deletions scala/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.7
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import com.adaptionsoft.games.uglytrivia.Game
import java.util.Random


object GameRunner {
var notAWinner = false
object GameRunner:
var notAWinner = true

def main(args: Array[String]) {
@main def main(): Unit =
var aGame = new Game();
aGame.add("Chet")
aGame.add("Pat")
aGame.add("Sue")

var rand: Random = new Random

do {
while notAWinner do
aGame.roll(rand.nextInt(5) + 1)
if (rand.nextInt(9) == 7) {
notAWinner = aGame.wrongAnswer
}
else {
notAWinner = aGame.wasCorrectlyAnswered
}
} while (notAWinner)
}
}
27 changes: 9 additions & 18 deletions scala/src/main/scala/com/adaptionsoft/games/uglytrivia/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.adaptionsoft.games.uglytrivia
import java.util.{LinkedList, ArrayList}


class Game {
class Game:
var players: ArrayList[String] = new ArrayList[String]
var places: Array[Int] = new Array[Int](6)
var purses: Array[Int] = new Array[Int](6)
Expand All @@ -15,36 +15,33 @@ class Game {
var currentPlayer: Int = 0
var isGettingOutOfPenaltyBox: Boolean = false

def initialize() {
def initialize() =
var i: Int = 0
while (i < 50) {
while (i < 50) do
popQuestions.addLast("Pop Question " + i)
scienceQuestions.addLast(("Science Question " + i))
sportsQuestions.addLast(("Sports Question " + i))
rockQuestions.addLast(createRockQuestion(i))
i += 1
}
}

initialize()

def createRockQuestion(index: Int): String = "Rock Question " + index

def isPlayable: Boolean = (howManyPlayers >= 2)

def add(playerName: String): Boolean = {
def add(playerName: String): Boolean =
players.add(playerName)
places(howManyPlayers) = 0
purses(howManyPlayers) = 0
inPenaltyBox(howManyPlayers) = false
println(playerName + " was added")
println("They are player number " + players.size)
true
}

def howManyPlayers: Int = players.size

def roll(roll: Int): Unit = {
def roll(roll: Int): Unit =
println(players.get(currentPlayer) + " is the current player")
println("They have rolled a " + roll)
if (inPenaltyBox(currentPlayer)) {
Expand All @@ -69,16 +66,14 @@ class Game {
println("The category is " + currentCategory)
askQuestion
}
}

private def askQuestion: Unit = {
private def askQuestion: Unit =
if (currentCategory == "Pop") println(popQuestions.removeFirst)
if (currentCategory == "Science") println(scienceQuestions.removeFirst)
if (currentCategory == "Sports") println(sportsQuestions.removeFirst)
if (currentCategory == "Rock") println(rockQuestions.removeFirst)
}

private def currentCategory: String = {
private def currentCategory: String =
if (places(currentPlayer) == 0) return "Pop"
if (places(currentPlayer) == 4) return "Pop"
if (places(currentPlayer) == 8) return "Pop"
Expand All @@ -89,9 +84,8 @@ class Game {
if (places(currentPlayer) == 6) return "Sports"
if (places(currentPlayer) == 10) return "Sports"
"Rock"
}

def wasCorrectlyAnswered: Boolean = {
def wasCorrectlyAnswered: Boolean =
if (inPenaltyBox(currentPlayer)) {
if (isGettingOutOfPenaltyBox) {
println("Answer was correct!!!!")
Expand All @@ -117,16 +111,13 @@ class Game {
if (currentPlayer == players.size) currentPlayer = 0
winner
}
}

def wrongAnswer: Boolean = {
def wrongAnswer: Boolean =
println("Question was incorrectly answered")
println(players.get(currentPlayer) + " was sent to the penalty box")
inPenaltyBox(currentPlayer) = true
currentPlayer += 1
if (currentPlayer == players.size) currentPlayer = 0
true
}

private def didPlayerWin: Boolean = !(purses(currentPlayer) == 6)
}

0 comments on commit b03e10f

Please sign in to comment.