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

Working to refactor #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions BowlingGame/src/main/scala/Game.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Game {
var rolls: List[Int] = List()

def roll(pins: Int): Unit = {
if (rolls.length >= 20) {
throw new IllegalArgumentException()
}
rolls = rolls :+ pins
if (pins == 10 && rolls.length % 2 == 1) {
rolls = rolls :+ 0
}
}

private def isStrike(index: Int): Boolean = {
if (index <= 2) {
return false
}

val even = index % 2 == 0
val shouldFirstRollBeDoubled = even && rolls(index-2) == 10
val shouldSecondRollBeDoubled = rolls(index-3) == 10
shouldFirstRollBeDoubled || shouldSecondRollBeDoubled
}

def score(): Int = {
var res = 0
for (i <- rolls.indices) {
res += rolls(i)
val even = i % 2 == 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly disagree with this without any details 😄 @mavericks065
With love ❤️,
Flo

if (isStrike(i)) {
res += rolls(i)
}
if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) {
res += rolls(i)
}
}
res
}
}
11 changes: 0 additions & 11 deletions BowlingGame/src/test/scala/Example.scala

This file was deleted.

80 changes: 80 additions & 0 deletions BowlingGame/src/test/scala/GameSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.scalatest._
import flatspec._
import matchers._

class GameSpec extends AnyFlatSpec with should.Matchers {
"beginning of game" should "return score of 0 " in {
val game = new Game()
val result = game.score()
result should be(0)
}

"game after one roll of 1" should "return a score of 1" in {
val game = new Game()
game.roll(1)

val result = game.score()
result should be(1)
}

"full game" should "have 10 frames" in {
val game = new Game()
for (i <- 0 until 20) {
game.roll(1);
}
val result = game.score()
result should be(20)
}

"full game" should "not have more than 10 frames" in {
val game = new Game()
for (i <- 0 until 20) {
game.roll(1);
}
assertThrows[IllegalArgumentException] {
game.roll(1)
}
}

"a spare" should "compute the score properly" in {
val game = new Game()
game.roll(7)
game.roll(3)
game.roll(4)

game.score() should be (18)
}

"a game" should "compute the double the next roll after a spare" in {
val game = new Game()
game.roll(7)
game.roll(3)
game.roll(7)
game.roll(1)

game.score() should be (25)
}

"a game" should "compute a strike" in {
val game = new Game()
game.roll(10)
game.roll(3)
game.roll(4)

game.score() should be (24)
}

"a game" should "compute a 0-10 spare" in {
val game = new Game()
game.roll(0)
game.roll(10)
game.roll(3)
game.roll(4)

game.score() should be (20)
}




}