-
Notifications
You must be signed in to change notification settings - Fork 0
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
mavericks065
wants to merge
9
commits into
main
Choose a base branch
from
working_to_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d4b671
empty game
damiengouyette 4e6aea2
cassie
cassiebrooks efe5c85
WIP
motrieux-thomas 545093f
szeswee
szeswee 450df09
Change score implementation
gregoirechauvet 5e225cb
Finish Spare
mavericks065 97f90d1
wip
antoineneff 2d6c20b
strike logic
pierrekarpov bb58a59
Refactor strike
gregoirechauvet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
if (isStrike(i)) { | ||
res += rolls(i) | ||
} | ||
if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) { | ||
res += rolls(i) | ||
} | ||
} | ||
res | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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