From 6d4b671488e797c569b0a4e36487bb17913ef4ce Mon Sep 17 00:00:00 2001 From: Damien GOUYETTE Date: Wed, 27 Jul 2022 11:28:13 +0200 Subject: [PATCH 1/9] empty game --- BowlingGame/src/test/scala/Example.scala | 11 -------- BowlingGame/src/test/scala/GameSpec.scala | 32 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) delete mode 100644 BowlingGame/src/test/scala/Example.scala create mode 100644 BowlingGame/src/test/scala/GameSpec.scala diff --git a/BowlingGame/src/test/scala/Example.scala b/BowlingGame/src/test/scala/Example.scala deleted file mode 100644 index e289f80..0000000 --- a/BowlingGame/src/test/scala/Example.scala +++ /dev/null @@ -1,11 +0,0 @@ -import org.scalatest._ -import flatspec._ -import matchers._ - -class Example extends AnyFlatSpec with should.Matchers { - - "A List" should "drop the first values" in { - val stack = List(3, 2) - stack.drop(1) should be (List(2)) - } -} diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala new file mode 100644 index 0000000..df6940f --- /dev/null +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -0,0 +1,32 @@ +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 tested = game.score() + tested should be(0) + } + + "a game with a pin 1 " should "return 10" in { + val game = new Game() + game.roll(1) + + val tested = game.score() + + } + + +} + +class Game { + def roll(pin : Int): Unit = { + ??? + } + + def score() : Int = { + 0 + } +} \ No newline at end of file From 4e6aea2c02ffb7c7754e1b5aae6c109d32fed5ac Mon Sep 17 00:00:00 2001 From: cassiebrooks Date: Wed, 27 Jul 2022 09:37:07 +0000 Subject: [PATCH 2/9] cassie --- BowlingGame/src/test/scala/GameSpec.scala | 26 ++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index df6940f..50cd7fb 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -3,30 +3,36 @@ 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 tested = game.score() - tested should be(0) + val result = game.score() + result should be(0) } - "a game with a pin 1 " should "return 10" in { + "game after one roll of 1" should "return a score of 1" in { val game = new Game() game.roll(1) - val tested = game.score() - + val result = game.score() + result should be(1) } + "full game" should "have 10 turns?" in {} + // TODO: manage spares, manage strikes, then manage checks below: + // "one roll" should "should not be greater than 10" in {} + // "sum of rolls in one turn" should "should not be greater than 10" in {} } + class Game { - def roll(pin : Int): Unit = { - ??? + var currentScore: Int = 0 + + def roll(pins: Int): Unit = { + currentScore += pins } - def score() : Int = { - 0 + def score(): Int = { + currentScore } } \ No newline at end of file From efe5c850fa04acb1b282b57a63dc8534cee4aee2 Mon Sep 17 00:00:00 2001 From: MOTRIEUX Thomas <36075240+motrieux-thomas@users.noreply.github.com> Date: Wed, 27 Jul 2022 09:45:46 +0000 Subject: [PATCH 3/9] WIP --- BowlingGame/src/test/scala/GameSpec.scala | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index 50cd7fb..b5adbaa 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -17,7 +17,23 @@ class GameSpec extends AnyFlatSpec with should.Matchers { result should be(1) } - "full game" should "have 10 turns?" in {} + "full game" should "have 10 turns?" 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 turns?" in { + val game = new Game(); + for (i <- 0 until 22) { + game.roll(1); + } + val result = game.score() + result should be(20) + } // TODO: manage spares, manage strikes, then manage checks below: // "one roll" should "should not be greater than 10" in {} From 545093f94a5e02026786bcc2e9b5512960f4f9d7 Mon Sep 17 00:00:00 2001 From: Sze Swee Date: Wed, 27 Jul 2022 09:57:23 +0000 Subject: [PATCH 4/9] szeswee --- BowlingGame/src/main/scala/Game.scala | 16 ++++++++++++++ BowlingGame/src/test/scala/GameSpec.scala | 26 +++++++---------------- 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 BowlingGame/src/main/scala/Game.scala diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala new file mode 100644 index 0000000..e93a991 --- /dev/null +++ b/BowlingGame/src/main/scala/Game.scala @@ -0,0 +1,16 @@ +class Game { + var currentScore: Int = 0 + var pinsHit: List[Int] = List() + + def roll(pins: Int): Unit = { + if (pinsHit.length >= 20) { + throw new IllegalArgumentException() + } + currentScore += pins + pinsHit = pinsHit :+ pins + } + + def score(): Int = { + currentScore + } +} diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index b5adbaa..b3325e8 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -2,6 +2,8 @@ import org.scalatest._ import flatspec._ import matchers._ +import org.company.module + class GameSpec extends AnyFlatSpec with should.Matchers { "beginning of game" should "return score of 0 " in { val game = new Game() @@ -17,7 +19,7 @@ class GameSpec extends AnyFlatSpec with should.Matchers { result should be(1) } - "full game" should "have 10 turns?" in { + "full game" should "have 10 frames" in { val game = new Game(); for (i <- 0 until 20) { game.roll(1); @@ -26,29 +28,17 @@ class GameSpec extends AnyFlatSpec with should.Matchers { result should be(20) } - "full game" should "not have more than 10 turns?" in { + "full game" should "not have more than 10 frames" in { val game = new Game(); - for (i <- 0 until 22) { + for (i <- 0 until 20) { game.roll(1); } - val result = game.score() - result should be(20) + assertThrows[IllegalArgumentException] { + game.roll(1) + } } // TODO: manage spares, manage strikes, then manage checks below: // "one roll" should "should not be greater than 10" in {} // "sum of rolls in one turn" should "should not be greater than 10" in {} } - - -class Game { - var currentScore: Int = 0 - - def roll(pins: Int): Unit = { - currentScore += pins - } - - def score(): Int = { - currentScore - } -} \ No newline at end of file From 450df094e6cf1532886a786abff94b00cdc77d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Chauvet?= Date: Wed, 3 Aug 2022 09:27:06 +0000 Subject: [PATCH 5/9] Change score implementation --- BowlingGame/src/main/scala/Game.scala | 10 ++++++++-- BowlingGame/src/test/scala/GameSpec.scala | 15 +++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala index e93a991..8af93b4 100644 --- a/BowlingGame/src/main/scala/Game.scala +++ b/BowlingGame/src/main/scala/Game.scala @@ -6,11 +6,17 @@ class Game { if (pinsHit.length >= 20) { throw new IllegalArgumentException() } - currentScore += pins pinsHit = pinsHit :+ pins } def score(): Int = { - currentScore + var res = 0 + + for (i <- 0 until pinsHit.length) { + val even = i % 2 == 0 + res += pinsHit(i) + } + + res } } diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index b3325e8..e8b9ff2 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -2,8 +2,6 @@ import org.scalatest._ import flatspec._ import matchers._ -import org.company.module - class GameSpec extends AnyFlatSpec with should.Matchers { "beginning of game" should "return score of 0 " in { val game = new Game() @@ -20,7 +18,7 @@ class GameSpec extends AnyFlatSpec with should.Matchers { } "full game" should "have 10 frames" in { - val game = new Game(); + val game = new Game() for (i <- 0 until 20) { game.roll(1); } @@ -29,7 +27,7 @@ class GameSpec extends AnyFlatSpec with should.Matchers { } "full game" should "not have more than 10 frames" in { - val game = new Game(); + val game = new Game() for (i <- 0 until 20) { game.roll(1); } @@ -38,6 +36,15 @@ class GameSpec extends AnyFlatSpec with should.Matchers { } } + "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) + } + // TODO: manage spares, manage strikes, then manage checks below: // "one roll" should "should not be greater than 10" in {} // "sum of rolls in one turn" should "should not be greater than 10" in {} From 5e225cb61ce07b8d18f2a081988bf37f8d59a1d6 Mon Sep 17 00:00:00 2001 From: Nicolas Guignard Date: Wed, 3 Aug 2022 11:35:48 +0200 Subject: [PATCH 6/9] Finish Spare --- BowlingGame/src/main/scala/Game.scala | 16 +++++++-------- BowlingGame/src/test/scala/GameSpec.scala | 24 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala index 8af93b4..b96ec4f 100644 --- a/BowlingGame/src/main/scala/Game.scala +++ b/BowlingGame/src/main/scala/Game.scala @@ -1,22 +1,22 @@ class Game { - var currentScore: Int = 0 - var pinsHit: List[Int] = List() + var rolls: List[Int] = List() def roll(pins: Int): Unit = { - if (pinsHit.length >= 20) { + if (rolls.length >= 20) { throw new IllegalArgumentException() } - pinsHit = pinsHit :+ pins + rolls = rolls :+ pins } def score(): Int = { var res = 0 - - for (i <- 0 until pinsHit.length) { + for (i <- rolls.indices) { + res += rolls(i) val even = i % 2 == 0 - res += pinsHit(i) + if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) { + res += rolls(i) + } } - res } } diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index e8b9ff2..0e32229 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -45,7 +45,25 @@ class GameSpec extends AnyFlatSpec with should.Matchers { game.score() should be (18) } - // TODO: manage spares, manage strikes, then manage checks below: - // "one roll" should "should not be greater than 10" in {} - // "sum of rolls in one turn" should "should not be greater than 10" in {} + "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(7) + game.roll(3) + + game.score() should be (25) + } + + + + } From 97f90d10a758ad979023f34ce6c96f89baaca620 Mon Sep 17 00:00:00 2001 From: Antoine Neff <9216777+antoineneff@users.noreply.github.com> Date: Wed, 3 Aug 2022 09:46:18 +0000 Subject: [PATCH 7/9] wip --- BowlingGame/src/main/scala/Game.scala | 6 ++++++ BowlingGame/src/test/scala/GameSpec.scala | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala index b96ec4f..e102db7 100644 --- a/BowlingGame/src/main/scala/Game.scala +++ b/BowlingGame/src/main/scala/Game.scala @@ -6,6 +6,9 @@ class Game { throw new IllegalArgumentException() } rolls = rolls :+ pins + if (pins == 10 && rolls.length % 2 == 1) { + rolls = rolls :+ 0 + } } def score(): Int = { @@ -13,6 +16,9 @@ class Game { for (i <- rolls.indices) { res += rolls(i) val even = i % 2 == 0 + if (rolls(i-3) == 10) { + res += rol + } if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) { res += rolls(i) } diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index 0e32229..9847a36 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -57,10 +57,11 @@ class GameSpec extends AnyFlatSpec with should.Matchers { "a game" should "compute a strike" in { val game = new Game() - game.roll(7) + game.roll(10) game.roll(3) + game.roll(4) - game.score() should be (25) + game.score() should be (24) } From 2d6c20b62a29f299d54b466c346a6fba18af46e1 Mon Sep 17 00:00:00 2001 From: Pierre Karpov Date: Wed, 3 Aug 2022 17:55:48 +0800 Subject: [PATCH 8/9] strike logic --- BowlingGame/src/main/scala/Game.scala | 4 ++-- BowlingGame/src/test/scala/GameSpec.scala | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala index e102db7..7d67d04 100644 --- a/BowlingGame/src/main/scala/Game.scala +++ b/BowlingGame/src/main/scala/Game.scala @@ -16,8 +16,8 @@ class Game { for (i <- rolls.indices) { res += rolls(i) val even = i % 2 == 0 - if (rolls(i-3) == 10) { - res += rol + if (i > 2 && !even && (rolls(i-3) == 10 || rolls(i-2) == 10)) { // strike + res += rolls(i) } if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) { res += rolls(i) diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala index 9847a36..8fc1aae 100644 --- a/BowlingGame/src/test/scala/GameSpec.scala +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -64,6 +64,16 @@ class GameSpec extends AnyFlatSpec with should.Matchers { 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) + } + From bb58a5906ebe701b560613011b9377c3d90731b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Chauvet?= Date: Wed, 3 Aug 2022 10:12:37 +0000 Subject: [PATCH 9/9] Refactor strike --- BowlingGame/src/main/scala/Game.scala | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala index 7d67d04..14fbbf3 100644 --- a/BowlingGame/src/main/scala/Game.scala +++ b/BowlingGame/src/main/scala/Game.scala @@ -11,12 +11,23 @@ class Game { } } + 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 (i > 2 && !even && (rolls(i-3) == 10 || rolls(i-2) == 10)) { // strike + if (isStrike(i)) { res += rolls(i) } if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) {