From ebb5907279f61139a1d835c4f48bbf41940db012 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 13:52:06 +0000 Subject: [PATCH 01/19] Refactor uncommunicative names --- .../games/trivia/runner/GameRunner.kt | 8 +++---- .../com/adaptionsoft/games/uglytrivia/Game.kt | 22 +++++++++---------- .../com/adaptionsoft/games/trivia/SomeTest.kt | 13 ----------- 3 files changed, 15 insertions(+), 28 deletions(-) delete mode 100644 exercises/kotlin/src/test/kotlin/com/adaptionsoft/games/trivia/SomeTest.kt diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/trivia/runner/GameRunner.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/trivia/runner/GameRunner.kt index 08c522e4..13a8eaf1 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/trivia/runner/GameRunner.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/trivia/runner/GameRunner.kt @@ -7,9 +7,9 @@ object GameRunner { fun main(args: Array) { val aGame = Game() - aGame.add("Chet") - aGame.add("Pat") - aGame.add("Sue") + aGame.addPlayer("Chet") + aGame.addPlayer("Pat") + aGame.addPlayer("Sue") val rand = Random(args[0].toLong()) @@ -18,7 +18,7 @@ object GameRunner { aGame.roll(rand.nextInt(5) + 1) if (rand.nextInt(9) == 7) { - GameRunner.notAWinner = aGame.wrongAnswer() + GameRunner.notAWinner = aGame.wasIncorrectlyAnswered() } else { GameRunner.notAWinner = aGame.wasCorrectlyAnswered() } diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index ce8e46cc..f524f8b1 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -6,7 +6,7 @@ class Game { internal var players = ArrayList() internal var places = IntArray(6) internal var purses = IntArray(6) - internal var inPenaltyBox = BooleanArray(6) + internal var isInPenaltyBox = BooleanArray(6) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -30,23 +30,23 @@ class Game { } fun isPlayable(): Boolean { - return howManyPlayers() >= 2 + return getNumberOfPlayers() >= 2 } - fun add(playerName: String): Boolean { + fun addPlayer(playerName: String): Boolean { players.add(playerName) - places[howManyPlayers()] = 0 - purses[howManyPlayers()] = 0 - inPenaltyBox[howManyPlayers()] = false + places[getNumberOfPlayers()] = 0 + purses[getNumberOfPlayers()] = 0 + isInPenaltyBox[getNumberOfPlayers()] = false println(playerName + " was added") println("They are player number " + players.size) return true } - fun howManyPlayers(): Int { + fun getNumberOfPlayers(): Int { return players.size } @@ -54,7 +54,7 @@ class Game { println(players[currentPlayer].toString() + " is the current player") println("They have rolled a " + roll) - if (inPenaltyBox[currentPlayer]) { + if (isInPenaltyBox[currentPlayer]) { if (roll % 2 != 0) { isGettingOutOfPenaltyBox = true @@ -111,7 +111,7 @@ class Game { } fun wasCorrectlyAnswered(): Boolean { - if (inPenaltyBox[currentPlayer]) { + if (isInPenaltyBox[currentPlayer]) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") purses[currentPlayer]++ @@ -149,10 +149,10 @@ class Game { } } - fun wrongAnswer(): Boolean { + fun wasIncorrectlyAnswered(): Boolean { println("Question was incorrectly answered") println(players[currentPlayer].toString() + " was sent to the penalty box") - inPenaltyBox[currentPlayer] = true + isInPenaltyBox[currentPlayer] = true currentPlayer++ if (currentPlayer == players.size) currentPlayer = 0 diff --git a/exercises/kotlin/src/test/kotlin/com/adaptionsoft/games/trivia/SomeTest.kt b/exercises/kotlin/src/test/kotlin/com/adaptionsoft/games/trivia/SomeTest.kt deleted file mode 100644 index 33e7b557..00000000 --- a/exercises/kotlin/src/test/kotlin/com/adaptionsoft/games/trivia/SomeTest.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.adaptionsoft.games.trivia - -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test - - -class SomeTest { - - @Test - fun true_is_true() { - assertTrue(false) - } -} From d7ab82e25b0c97840077ca66a925a1f4d211aeb6 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 14:04:20 +0000 Subject: [PATCH 02/19] Refactor duplicate code --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index f524f8b1..5e3e3399 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -59,13 +59,7 @@ class Game { isGettingOutOfPenaltyBox = true println(players[currentPlayer].toString() + " is getting out of the penalty box") - places[currentPlayer] = places[currentPlayer] + roll - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12 - - println(players[currentPlayer].toString() - + "'s new location is " - + places[currentPlayer]) - println("The category is " + currentCategory()) + movePlayer(roll) askQuestion() } else { println(players[currentPlayer].toString() + " is not getting out of the penalty box") @@ -73,19 +67,24 @@ class Game { } } else { - - places[currentPlayer] = places[currentPlayer] + roll - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12 - - println(players[currentPlayer].toString() - + "'s new location is " - + places[currentPlayer]) - println("The category is " + currentCategory()) + movePlayer(roll) askQuestion() } } + private fun movePlayer(roll: Int) { + places[currentPlayer] = places[currentPlayer] + roll + if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12 + + println( + players[currentPlayer].toString() + + "'s new location is " + + places[currentPlayer] + ) + println("The category is " + currentCategory()) + } + private fun askQuestion() { if (currentCategory() === "Pop") println(popQuestions.removeFirst()) @@ -114,20 +113,14 @@ class Game { if (isInPenaltyBox[currentPlayer]) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") - purses[currentPlayer]++ - println(players[currentPlayer].toString() - + " now has " - + purses[currentPlayer] - + " Gold Coins.") + addCoin() val winner = didPlayerWin() - currentPlayer++ - if (currentPlayer == players.size) currentPlayer = 0 + updateCurrentPlayer() return winner } else { - currentPlayer++ - if (currentPlayer == players.size) currentPlayer = 0 + updateCurrentPlayer() return true } @@ -135,28 +128,37 @@ class Game { } else { println("Answer was corrent!!!!") - purses[currentPlayer]++ - println(players[currentPlayer].toString() - + " now has " - + purses[currentPlayer] - + " Gold Coins.") + addCoin() val winner = didPlayerWin() - currentPlayer++ - if (currentPlayer == players.size) currentPlayer = 0 + updateCurrentPlayer() return winner } } + private fun addCoin() { + purses[currentPlayer]++ + println( + players[currentPlayer].toString() + + " now has " + + purses[currentPlayer] + + " Gold Coins." + ) + } + fun wasIncorrectlyAnswered(): Boolean { println("Question was incorrectly answered") println(players[currentPlayer].toString() + " was sent to the penalty box") isInPenaltyBox[currentPlayer] = true + updateCurrentPlayer() + return true + } + + private fun updateCurrentPlayer() { currentPlayer++ if (currentPlayer == players.size) currentPlayer = 0 - return true } private fun didPlayerWin(): Boolean { From b569ce6f06b28a5e32a8639d81cb5a759254356c Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 14:08:32 +0000 Subject: [PATCH 03/19] refactor: extract variable --- .../src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 5e3e3399..88c08aa2 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -55,7 +55,8 @@ class Game { println("They have rolled a " + roll) if (isInPenaltyBox[currentPlayer]) { - if (roll % 2 != 0) { + val isRollOdd = roll % 2 != 0 + if (isRollOdd) { isGettingOutOfPenaltyBox = true println(players[currentPlayer].toString() + " is getting out of the penalty box") From 2a27577206786da1fb0a70ce448b930da62f9f3a Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 14:11:36 +0000 Subject: [PATCH 04/19] refactor duplicate code --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 88c08aa2..23bee4e1 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -17,16 +17,16 @@ class Game { internal var isGettingOutOfPenaltyBox: Boolean = false init { - for (i in 0..49) { - popQuestions.addLast("Pop Question " + i) - scienceQuestions.addLast("Science Question " + i) - sportsQuestions.addLast("Sports Question " + i) - rockQuestions.addLast(createRockQuestion(i)) + for (questionIndex in 0..49) { + popQuestions.addLast(createQuestion("Pop", questionIndex)) + scienceQuestions.addLast(createQuestion("Science", questionIndex)) + sportsQuestions.addLast(createQuestion("Sports", questionIndex)) + rockQuestions.addLast(createQuestion("Rock", questionIndex)) } } - fun createRockQuestion(index: Int): String { - return "Rock Question " + index + fun createQuestion(category: String, index: Int): String { + return "$category Question " + index } fun isPlayable(): Boolean { From 7ae182e56059fbf883db75e8c1e98808aa16276a Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 14:55:38 +0000 Subject: [PATCH 05/19] refactor: extract magic number --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 23bee4e1..5422be8c 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -3,10 +3,15 @@ package com.adaptionsoft.games.uglytrivia import java.util.* class Game { + private val NUMBER_OF_PLAYERS = 6 + private val NUMBER_OF_QUESTIONS = 50 + private val NUMBER_OF_PLACES = 12 + private val COINS_TO_WIN = 6 + internal var players = ArrayList() - internal var places = IntArray(6) - internal var purses = IntArray(6) - internal var isInPenaltyBox = BooleanArray(6) + internal var places = IntArray(NUMBER_OF_PLAYERS) + internal var purses = IntArray(NUMBER_OF_PLAYERS) + internal var isInPenaltyBox = BooleanArray(NUMBER_OF_PLAYERS) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -17,7 +22,7 @@ class Game { internal var isGettingOutOfPenaltyBox: Boolean = false init { - for (questionIndex in 0..49) { + for (questionIndex in 0..= 2 - } - fun addPlayer(playerName: String): Boolean { @@ -76,7 +77,7 @@ class Game { private fun movePlayer(roll: Int) { places[currentPlayer] = places[currentPlayer] + roll - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12 + if (places[currentPlayer] >= NUMBER_OF_PLACES) places[currentPlayer] = places[currentPlayer] - NUMBER_OF_PLACES println( players[currentPlayer].toString() @@ -98,15 +99,9 @@ class Game { } private fun currentCategory(): String { - if (places[currentPlayer] == 0) return "Pop" - if (places[currentPlayer] == 4) return "Pop" - if (places[currentPlayer] == 8) return "Pop" - if (places[currentPlayer] == 1) return "Science" - if (places[currentPlayer] == 5) return "Science" - if (places[currentPlayer] == 9) return "Science" - if (places[currentPlayer] == 2) return "Sports" - if (places[currentPlayer] == 6) return "Sports" - if (places[currentPlayer] == 10) return "Sports" + if (places[currentPlayer] % 4 == 0) return "Pop" + if (places[currentPlayer] % 4 == 1) return "Science" + if (places[currentPlayer] % 4 == 2) return "Sports" return "Rock" } @@ -163,6 +158,6 @@ class Game { } private fun didPlayerWin(): Boolean { - return purses[currentPlayer] != 6 + return purses[currentPlayer] != COINS_TO_WIN } } \ No newline at end of file From 26a68c3210e8c0681aac21fafc45ea3a1d673a50 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Mon, 8 Jan 2024 15:20:39 +0000 Subject: [PATCH 06/19] rename constants --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 5422be8c..62875eb8 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -3,15 +3,15 @@ package com.adaptionsoft.games.uglytrivia import java.util.* class Game { - private val NUMBER_OF_PLAYERS = 6 - private val NUMBER_OF_QUESTIONS = 50 + private val MAX_NUMBER_OF_PLAYERS = 6 + private val MAX_NUMBER_OF_QUESTIONS = 50 private val NUMBER_OF_PLACES = 12 private val COINS_TO_WIN = 6 internal var players = ArrayList() - internal var places = IntArray(NUMBER_OF_PLAYERS) - internal var purses = IntArray(NUMBER_OF_PLAYERS) - internal var isInPenaltyBox = BooleanArray(NUMBER_OF_PLAYERS) + internal var places = IntArray(MAX_NUMBER_OF_PLAYERS) + internal var purses = IntArray(MAX_NUMBER_OF_PLAYERS) + internal var isInPenaltyBox = BooleanArray(MAX_NUMBER_OF_PLAYERS) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -22,7 +22,7 @@ class Game { internal var isGettingOutOfPenaltyBox: Boolean = false init { - for (questionIndex in 0.. Date: Tue, 9 Jan 2024 10:27:45 +0000 Subject: [PATCH 07/19] refactor: extract addCoin(), updateCurrentPlayer() and shouldGameContinue() into one method --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 62875eb8..28d2f693 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -109,30 +109,25 @@ class Game { if (isInPenaltyBox[currentPlayer]) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") - addCoin() - - val shouldContinueGame = shouldContinueGame() - updateCurrentPlayer() - - return shouldContinueGame + return updateRoundAndCheckIfShouldContinue() } else { updateCurrentPlayer() return true } - } else { - println("Answer was corrent!!!!") - addCoin() - - val shouldContinueGame = shouldContinueGame() - updateCurrentPlayer() - - return shouldContinueGame + return updateRoundAndCheckIfShouldContinue() } } + private fun updateRoundAndCheckIfShouldContinue(): Boolean { + addCoin() + val shouldContinueGame = shouldContinueGame() + updateCurrentPlayer() + return shouldContinueGame + } + private fun addCoin() { purses[currentPlayer]++ println( From e537779c8da83fbb73e08fd2fc4939cdc59bf6d2 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 10:32:18 +0000 Subject: [PATCH 08/19] refactor: decompose conditional --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 28d2f693..9bd5811c 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -35,8 +35,6 @@ class Game { } fun addPlayer(playerName: String): Boolean { - - players.add(playerName) places[getNumberOfPlayers()] = 0 purses[getNumberOfPlayers()] = 0 @@ -59,7 +57,6 @@ class Game { val isRollOdd = roll % 2 != 0 if (isRollOdd) { isGettingOutOfPenaltyBox = true - println(players[currentPlayer].toString() + " is getting out of the penalty box") movePlayer(roll) askQuestion() @@ -99,12 +96,18 @@ class Game { } private fun currentCategory(): String { - if (places[currentPlayer] % 4 == 0) return "Pop" - if (places[currentPlayer] % 4 == 1) return "Science" - if (places[currentPlayer] % 4 == 2) return "Sports" + if (isPopRound()) return "Pop" + if (isScienceRound()) return "Science" + if (isSportsRound()) return "Sports" return "Rock" } + private fun isSportsRound() = places[currentPlayer] % 4 == 2 + + private fun isScienceRound() = places[currentPlayer] % 4 == 1 + + private fun isPopRound() = places[currentPlayer] % 4 == 0 + fun wasCorrectlyAnswered(): Boolean { if (isInPenaltyBox[currentPlayer]) { if (isGettingOutOfPenaltyBox) { @@ -114,7 +117,6 @@ class Game { updateCurrentPlayer() return true } - } else { println("Answer was corrent!!!!") return updateRoundAndCheckIfShouldContinue() From 6a22b6e134ed51d503b321246f7e4464aeb74860 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 11:20:31 +0000 Subject: [PATCH 09/19] refactor: extract isRollOdd as method instead of variable --- .../kotlin/com/adaptionsoft/games/uglytrivia/Game.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 9bd5811c..9f2443ec 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -54,8 +54,7 @@ class Game { println("They have rolled a " + roll) if (isInPenaltyBox[currentPlayer]) { - val isRollOdd = roll % 2 != 0 - if (isRollOdd) { + if (isRollOdd(roll)) { isGettingOutOfPenaltyBox = true println(players[currentPlayer].toString() + " is getting out of the penalty box") movePlayer(roll) @@ -72,6 +71,8 @@ class Game { } + private fun isRollOdd(roll: Int) = roll % 2 != 0 + private fun movePlayer(roll: Int) { places[currentPlayer] = places[currentPlayer] + roll if (places[currentPlayer] >= NUMBER_OF_PLACES) places[currentPlayer] = places[currentPlayer] - NUMBER_OF_PLACES @@ -97,9 +98,9 @@ class Game { private fun currentCategory(): String { if (isPopRound()) return "Pop" - if (isScienceRound()) return "Science" - if (isSportsRound()) return "Sports" - return "Rock" + else if (isScienceRound()) return "Science" + else if (isSportsRound()) return "Sports" + else return "Rock" } private fun isSportsRound() = places[currentPlayer] % 4 == 2 From eaa754686d82e4134532415201096cc18b109f00 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 12:05:15 +0000 Subject: [PATCH 10/19] refactor: extract Board, Player and Place classes --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 85 ++++++++++++++----- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 9f2443ec..2075f1fd 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -2,16 +2,64 @@ package com.adaptionsoft.games.uglytrivia import java.util.* +class Board { + private val places = ArrayList() + private val boardSize = 12 + + init { + createBoard(boardSize) + } + + private fun createBoard(boardSize: Int) { + for (i in 0..boardSize) { + places.add(Place(i)) + } + } +} + +class Player (val name: String){ + var purse: Int = 0 + var isInPenaltyBox: Boolean = false + + fun addCoin() { + purse++ + println( + name + + " now has " + + purse + + " Gold Coins." + ) + } +} + +class Place (position: Int) { + val questionCategory: Category + + init { + questionCategory = when (position % 4) { + 0 -> Category.Pop + 1 -> Category.Science + 2 -> Category.Sports + else -> Category.Rock + } + } +} + +enum class Category { + Pop, Science, Sports, Rock +} + class Game { + private val board = Board() + private val MAX_NUMBER_OF_PLAYERS = 6 private val MAX_NUMBER_OF_QUESTIONS = 50 private val NUMBER_OF_PLACES = 12 private val COINS_TO_WIN = 6 - internal var players = ArrayList() + internal var players = ArrayList() internal var places = IntArray(MAX_NUMBER_OF_PLAYERS) internal var purses = IntArray(MAX_NUMBER_OF_PLAYERS) - internal var isInPenaltyBox = BooleanArray(MAX_NUMBER_OF_PLAYERS) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -35,10 +83,9 @@ class Game { } fun addPlayer(playerName: String): Boolean { - players.add(playerName) + players.add(Player(playerName)) places[getNumberOfPlayers()] = 0 purses[getNumberOfPlayers()] = 0 - isInPenaltyBox[getNumberOfPlayers()] = false println(playerName + " was added") println("They are player number " + players.size) @@ -50,17 +97,17 @@ class Game { } fun roll(roll: Int) { - println(players[currentPlayer].toString() + " is the current player") + println(players[currentPlayer].name + " is the current player") println("They have rolled a " + roll) - if (isInPenaltyBox[currentPlayer]) { + if (players[currentPlayer].isInPenaltyBox) { if (isRollOdd(roll)) { isGettingOutOfPenaltyBox = true - println(players[currentPlayer].toString() + " is getting out of the penalty box") + println(players[currentPlayer].name + " is getting out of the penalty box") movePlayer(roll) askQuestion() } else { - println(players[currentPlayer].toString() + " is not getting out of the penalty box") + println(players[currentPlayer].name + " is not getting out of the penalty box") isGettingOutOfPenaltyBox = false } @@ -78,7 +125,7 @@ class Game { if (places[currentPlayer] >= NUMBER_OF_PLACES) places[currentPlayer] = places[currentPlayer] - NUMBER_OF_PLACES println( - players[currentPlayer].toString() + players[currentPlayer].name + "'s new location is " + places[currentPlayer] ) @@ -110,7 +157,7 @@ class Game { private fun isPopRound() = places[currentPlayer] % 4 == 0 fun wasCorrectlyAnswered(): Boolean { - if (isInPenaltyBox[currentPlayer]) { + if (players[currentPlayer].isInPenaltyBox) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") return updateRoundAndCheckIfShouldContinue() @@ -125,26 +172,18 @@ class Game { } private fun updateRoundAndCheckIfShouldContinue(): Boolean { - addCoin() + players[currentPlayer].addCoin() val shouldContinueGame = shouldContinueGame() updateCurrentPlayer() return shouldContinueGame } - private fun addCoin() { - purses[currentPlayer]++ - println( - players[currentPlayer].toString() - + " now has " - + purses[currentPlayer] - + " Gold Coins." - ) - } + fun wasIncorrectlyAnswered(): Boolean { println("Question was incorrectly answered") - println(players[currentPlayer].toString() + " was sent to the penalty box") - isInPenaltyBox[currentPlayer] = true + println(players[currentPlayer].name + " was sent to the penalty box") + players[currentPlayer].isInPenaltyBox = true updateCurrentPlayer() return true @@ -156,6 +195,6 @@ class Game { } private fun shouldContinueGame(): Boolean { - return purses[currentPlayer] != COINS_TO_WIN + return players[currentPlayer].purse != COINS_TO_WIN } } \ No newline at end of file From bcc7d75c069604225bd6908a68af5f842e5f4447 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 13:46:52 +0000 Subject: [PATCH 11/19] refactor: take places out of Game class --- .../src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 2075f1fd..5989a726 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -33,7 +33,7 @@ class Player (val name: String){ } class Place (position: Int) { - val questionCategory: Category + private val questionCategory: Category init { questionCategory = when (position % 4) { @@ -59,7 +59,6 @@ class Game { internal var players = ArrayList() internal var places = IntArray(MAX_NUMBER_OF_PLAYERS) - internal var purses = IntArray(MAX_NUMBER_OF_PLAYERS) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -85,7 +84,6 @@ class Game { fun addPlayer(playerName: String): Boolean { players.add(Player(playerName)) places[getNumberOfPlayers()] = 0 - purses[getNumberOfPlayers()] = 0 println(playerName + " was added") println("They are player number " + players.size) From cf774635da68e8c97e95062239afc1f2e3af0d26 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 13:56:00 +0000 Subject: [PATCH 12/19] refactor: extract places property to Player class as position --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 5989a726..d542bc57 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -20,6 +20,7 @@ class Board { class Player (val name: String){ var purse: Int = 0 var isInPenaltyBox: Boolean = false + var position: Int = 0 fun addCoin() { purse++ @@ -52,13 +53,11 @@ enum class Category { class Game { private val board = Board() - private val MAX_NUMBER_OF_PLAYERS = 6 private val MAX_NUMBER_OF_QUESTIONS = 50 private val NUMBER_OF_PLACES = 12 private val COINS_TO_WIN = 6 internal var players = ArrayList() - internal var places = IntArray(MAX_NUMBER_OF_PLAYERS) internal var popQuestions = LinkedList() internal var scienceQuestions = LinkedList() @@ -83,7 +82,6 @@ class Game { fun addPlayer(playerName: String): Boolean { players.add(Player(playerName)) - places[getNumberOfPlayers()] = 0 println(playerName + " was added") println("They are player number " + players.size) @@ -119,13 +117,13 @@ class Game { private fun isRollOdd(roll: Int) = roll % 2 != 0 private fun movePlayer(roll: Int) { - places[currentPlayer] = places[currentPlayer] + roll - if (places[currentPlayer] >= NUMBER_OF_PLACES) places[currentPlayer] = places[currentPlayer] - NUMBER_OF_PLACES + players[currentPlayer].position += roll + if (players[currentPlayer].position >= NUMBER_OF_PLACES) players[currentPlayer].position -= NUMBER_OF_PLACES println( players[currentPlayer].name + "'s new location is " - + places[currentPlayer] + + players[currentPlayer].position ) println("The category is " + currentCategory()) } @@ -148,11 +146,11 @@ class Game { else return "Rock" } - private fun isSportsRound() = places[currentPlayer] % 4 == 2 + private fun isSportsRound() = players[currentPlayer].position % 4 == 2 - private fun isScienceRound() = places[currentPlayer] % 4 == 1 + private fun isScienceRound() = players[currentPlayer].position % 4 == 1 - private fun isPopRound() = places[currentPlayer] % 4 == 0 + private fun isPopRound() = players[currentPlayer].position % 4 == 0 fun wasCorrectlyAnswered(): Boolean { if (players[currentPlayer].isInPenaltyBox) { From 670336bb49a01189671f71f109db419bd05a4250 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 13:57:44 +0000 Subject: [PATCH 13/19] remove now unused getNumberOfPlayers() method --- .../src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index d542bc57..b2be983b 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -88,10 +88,6 @@ class Game { return true } - fun getNumberOfPlayers(): Int { - return players.size - } - fun roll(roll: Int) { println(players[currentPlayer].name + " is the current player") println("They have rolled a " + roll) From 35dc3e2b31ac96881d0abd54134c3fb871921bc4 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 14:08:56 +0000 Subject: [PATCH 14/19] refactor: changed currentCategory() to be less coupled to Game class --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index b2be983b..74488769 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -121,32 +121,32 @@ class Game { + "'s new location is " + players[currentPlayer].position ) - println("The category is " + currentCategory()) + println("The category is " + currentCategory(players[currentPlayer].position)) } private fun askQuestion() { - if (currentCategory() === "Pop") + if (currentCategory(players[currentPlayer].position) === "Pop") println(popQuestions.removeFirst()) - if (currentCategory() === "Science") + if (currentCategory(players[currentPlayer].position) === "Science") println(scienceQuestions.removeFirst()) - if (currentCategory() === "Sports") + if (currentCategory(players[currentPlayer].position) === "Sports") println(sportsQuestions.removeFirst()) - if (currentCategory() === "Rock") + if (currentCategory(players[currentPlayer].position) === "Rock") println(rockQuestions.removeFirst()) } - private fun currentCategory(): String { - if (isPopRound()) return "Pop" - else if (isScienceRound()) return "Science" - else if (isSportsRound()) return "Sports" + private fun currentCategory(playerPosition: Int): String { + if (isPopRound(playerPosition)) return "Pop" + else if (isScienceRound(playerPosition)) return "Science" + else if (isSportsRound(playerPosition)) return "Sports" else return "Rock" } - private fun isSportsRound() = players[currentPlayer].position % 4 == 2 + private fun isSportsRound(playerPosition: Int) = playerPosition % 4 == 2 - private fun isScienceRound() = players[currentPlayer].position % 4 == 1 + private fun isScienceRound(playerPosition: Int) = playerPosition % 4 == 1 - private fun isPopRound() = players[currentPlayer].position % 4 == 0 + private fun isPopRound(playerPosition: Int) = playerPosition % 4 == 0 fun wasCorrectlyAnswered(): Boolean { if (players[currentPlayer].isInPenaltyBox) { From 99037f798ab060e4a61e368a3c6ef87687facc49 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 14:53:26 +0000 Subject: [PATCH 15/19] refactor: move question category logic to Place class --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 74488769..50c429e2 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -15,6 +15,10 @@ class Board { places.add(Place(i)) } } + + fun getPlace(position: Int): Place { + return places[position] + } } class Player (val name: String){ @@ -44,6 +48,10 @@ class Place (position: Int) { else -> Category.Rock } } + fun category(): String { + return questionCategory.toString() + } + } enum class Category { @@ -121,32 +129,22 @@ class Game { + "'s new location is " + players[currentPlayer].position ) - println("The category is " + currentCategory(players[currentPlayer].position)) + println("The category is " + getCurrentPlace().category()) } private fun askQuestion() { - if (currentCategory(players[currentPlayer].position) === "Pop") + if (getCurrentPlace().category() === "Pop") println(popQuestions.removeFirst()) - if (currentCategory(players[currentPlayer].position) === "Science") + if (getCurrentPlace().category() === "Science") println(scienceQuestions.removeFirst()) - if (currentCategory(players[currentPlayer].position) === "Sports") + if (getCurrentPlace().category() === "Sports") println(sportsQuestions.removeFirst()) - if (currentCategory(players[currentPlayer].position) === "Rock") + if (getCurrentPlace().category() === "Rock") println(rockQuestions.removeFirst()) } - private fun currentCategory(playerPosition: Int): String { - if (isPopRound(playerPosition)) return "Pop" - else if (isScienceRound(playerPosition)) return "Science" - else if (isSportsRound(playerPosition)) return "Sports" - else return "Rock" - } - - private fun isSportsRound(playerPosition: Int) = playerPosition % 4 == 2 - - private fun isScienceRound(playerPosition: Int) = playerPosition % 4 == 1 + private fun getCurrentPlace() = board.getPlace(players.get(currentPlayer).position) - private fun isPopRound(playerPosition: Int) = playerPosition % 4 == 0 fun wasCorrectlyAnswered(): Boolean { if (players[currentPlayer].isInPenaltyBox) { From 99f5aa50d6bdbacc126de381450d426b5de4ceb4 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 15:08:09 +0000 Subject: [PATCH 16/19] refactor: extract questions to QuestionBank class --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 50c429e2..51352564 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -51,7 +51,28 @@ class Place (position: Int) { fun category(): String { return questionCategory.toString() } +} + +class QuestionBank { + private val MAX_NUMBER_OF_QUESTIONS = 50 + + internal var pop = LinkedList() + internal var science = LinkedList() + internal var sports = LinkedList() + internal var rock = LinkedList() + + init { + for (questionIndex in 0..() - - internal var popQuestions = LinkedList() - internal var scienceQuestions = LinkedList() - internal var sportsQuestions = LinkedList() - internal var rockQuestions = LinkedList() + private val board = Board() + private val questionBank = QuestionBank() + private var players = ArrayList() internal var currentPlayer = 0 internal var isGettingOutOfPenaltyBox: Boolean = false - init { - for (questionIndex in 0.. Date: Tue, 9 Jan 2024 15:19:04 +0000 Subject: [PATCH 17/19] refactor: convert string concat to template, split updateRoundAndCheckIfShouldContinue() into separate methods --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index 51352564..f5d5e81c 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -29,10 +29,7 @@ class Player (val name: String){ fun addCoin() { purse++ println( - name - + " now has " - + purse - + " Gold Coins." + "$name now has $purse Gold Coins." ) } } @@ -87,19 +84,19 @@ class Game { private val questionBank = QuestionBank() private var players = ArrayList() - internal var currentPlayer = 0 + private var currentPlayer = 0 internal var isGettingOutOfPenaltyBox: Boolean = false fun addPlayer(playerName: String): Boolean { players.add(Player(playerName)) - println(playerName + " was added") + println("$playerName was added") println("They are player number " + players.size) return true } fun roll(roll: Int) { println(players[currentPlayer].name + " is the current player") - println("They have rolled a " + roll) + println("They have rolled a $roll") if (players[currentPlayer].isInPenaltyBox) { if (isRollOdd(roll)) { @@ -148,24 +145,23 @@ class Game { if (players[currentPlayer].isInPenaltyBox) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") - return updateRoundAndCheckIfShouldContinue() + players[currentPlayer].addCoin() + val shouldContinueGame = shouldContinueGame() + updateCurrentPlayer() + return shouldContinueGame } else { updateCurrentPlayer() return true } } else { println("Answer was corrent!!!!") - return updateRoundAndCheckIfShouldContinue() + players[currentPlayer].addCoin() + val shouldContinueGame = shouldContinueGame() + updateCurrentPlayer() + return shouldContinueGame } } - private fun updateRoundAndCheckIfShouldContinue(): Boolean { - players[currentPlayer].addCoin() - val shouldContinueGame = shouldContinueGame() - updateCurrentPlayer() - return shouldContinueGame - } - fun wasIncorrectlyAnswered(): Boolean { println("Question was incorrectly answered") println(players[currentPlayer].name + " was sent to the penalty box") From 6c669909b25c2ccf83d9a483e553538efea42755 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 15:27:20 +0000 Subject: [PATCH 18/19] refactor: extract currentPlayer into field on Game class --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index f5d5e81c..b5b48150 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -84,28 +84,30 @@ class Game { private val questionBank = QuestionBank() private var players = ArrayList() - private var currentPlayer = 0 - internal var isGettingOutOfPenaltyBox: Boolean = false + private var currentPlayerIndex = 0 + private lateinit var currentPlayer: Player + private var isGettingOutOfPenaltyBox: Boolean = false fun addPlayer(playerName: String): Boolean { players.add(Player(playerName)) println("$playerName was added") println("They are player number " + players.size) + currentPlayer = players[currentPlayerIndex] return true } fun roll(roll: Int) { - println(players[currentPlayer].name + " is the current player") + println(currentPlayer.name + " is the current player") println("They have rolled a $roll") - if (players[currentPlayer].isInPenaltyBox) { + if (currentPlayer.isInPenaltyBox) { if (isRollOdd(roll)) { isGettingOutOfPenaltyBox = true - println(players[currentPlayer].name + " is getting out of the penalty box") + println(currentPlayer.name + " is getting out of the penalty box") movePlayer(roll) askQuestion() } else { - println(players[currentPlayer].name + " is not getting out of the penalty box") + println(currentPlayer.name + " is not getting out of the penalty box") isGettingOutOfPenaltyBox = false } } else { @@ -117,13 +119,13 @@ class Game { private fun isRollOdd(roll: Int) = roll % 2 != 0 private fun movePlayer(roll: Int) { - players[currentPlayer].position += roll - if (players[currentPlayer].position >= NUMBER_OF_PLACES) players[currentPlayer].position -= NUMBER_OF_PLACES + currentPlayer.position += roll + if (currentPlayer.position >= NUMBER_OF_PLACES) currentPlayer.position -= NUMBER_OF_PLACES println( - players[currentPlayer].name + currentPlayer.name + "'s new location is " - + players[currentPlayer].position + + currentPlayer.position ) println("The category is " + getCurrentPlace().category()) } @@ -139,13 +141,13 @@ class Game { println(questionBank.rock.removeFirst()) } - private fun getCurrentPlace() = board.getPlace(players[currentPlayer].position) + private fun getCurrentPlace() = board.getPlace(currentPlayer.position) fun wasCorrectlyAnswered(): Boolean { - if (players[currentPlayer].isInPenaltyBox) { + if (currentPlayer.isInPenaltyBox) { if (isGettingOutOfPenaltyBox) { println("Answer was correct!!!!") - players[currentPlayer].addCoin() + currentPlayer.addCoin() val shouldContinueGame = shouldContinueGame() updateCurrentPlayer() return shouldContinueGame @@ -155,7 +157,7 @@ class Game { } } else { println("Answer was corrent!!!!") - players[currentPlayer].addCoin() + currentPlayer.addCoin() val shouldContinueGame = shouldContinueGame() updateCurrentPlayer() return shouldContinueGame @@ -164,19 +166,20 @@ class Game { fun wasIncorrectlyAnswered(): Boolean { println("Question was incorrectly answered") - println(players[currentPlayer].name + " was sent to the penalty box") - players[currentPlayer].isInPenaltyBox = true + println(currentPlayer.name + " was sent to the penalty box") + currentPlayer.isInPenaltyBox = true updateCurrentPlayer() return true } private fun updateCurrentPlayer() { - currentPlayer++ - if (currentPlayer == players.size) currentPlayer = 0 + currentPlayerIndex++ + if (currentPlayerIndex == players.size) currentPlayerIndex = 0 + currentPlayer = players[currentPlayerIndex] } private fun shouldContinueGame(): Boolean { - return players[currentPlayer].purse != COINS_TO_WIN + return currentPlayer.purse != COINS_TO_WIN } } \ No newline at end of file From 5aa2121d638f32c5109ab05d72f8ebca1aecfef8 Mon Sep 17 00:00:00 2001 From: Shu Wang Date: Tue, 9 Jan 2024 15:30:02 +0000 Subject: [PATCH 19/19] refactor: move askQuestion() into QuestionBank class --- .../com/adaptionsoft/games/uglytrivia/Game.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt index b5b48150..5faa4d28 100644 --- a/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt +++ b/exercises/kotlin/src/main/kotlin/com/adaptionsoft/games/uglytrivia/Game.kt @@ -70,6 +70,17 @@ class QuestionBank { private fun createQuestion(category: String, index: Int): String { return "$category Question $index" } + + fun askQuestion(place: Place) { + if (place.category() === "Pop") + println(pop.removeFirst()) + if (place.category() === "Science") + println(science.removeFirst()) + if (place.category() === "Sports") + println(sports.removeFirst()) + if (place.category() === "Rock") + println(rock.removeFirst()) + } } enum class Category { @@ -105,14 +116,14 @@ class Game { isGettingOutOfPenaltyBox = true println(currentPlayer.name + " is getting out of the penalty box") movePlayer(roll) - askQuestion() + questionBank.askQuestion(getCurrentPlace()) } else { println(currentPlayer.name + " is not getting out of the penalty box") isGettingOutOfPenaltyBox = false } } else { movePlayer(roll) - askQuestion() + questionBank.askQuestion(getCurrentPlace()) } } @@ -130,17 +141,6 @@ class Game { println("The category is " + getCurrentPlace().category()) } - private fun askQuestion() { - if (getCurrentPlace().category() === "Pop") - println(questionBank.pop.removeFirst()) - if (getCurrentPlace().category() === "Science") - println(questionBank.science.removeFirst()) - if (getCurrentPlace().category() === "Sports") - println(questionBank.sports.removeFirst()) - if (getCurrentPlace().category() === "Rock") - println(questionBank.rock.removeFirst()) - } - private fun getCurrentPlace() = board.getPlace(currentPlayer.position) fun wasCorrectlyAnswered(): Boolean { @@ -168,7 +168,6 @@ class Game { println("Question was incorrectly answered") println(currentPlayer.name + " was sent to the penalty box") currentPlayer.isInPenaltyBox = true - updateCurrentPlayer() return true }