Reworked using enum class.

This commit is contained in:
Erik C. Thauvin 2020-03-24 14:55:32 -07:00
parent a381b06a0a
commit 5bf6901ce2
2 changed files with 68 additions and 86 deletions

View file

@ -43,82 +43,74 @@ import kotlin.random.Random
class RockPaperScissors : AbstractModule() { class RockPaperScissors : AbstractModule() {
init { init {
with(commands) { with(commands) {
add(Shapes.ROCK.value) add(Hands.ROCK.name.toLowerCase())
add(Shapes.SCISSORS.value) add(Hands.PAPER.name.toLowerCase())
add(Shapes.PAPER.value) add(Hands.SCISSORS.name.toLowerCase())
} }
} }
enum class Shapes(val value: String) { enum class Hands(val action: String) {
ROCK("rock"), PAPER("paper"), SCISSORS("scissors") ROCK("crushes") {
override fun beats(hand: Hands): Boolean {
return hand == SCISSORS
} }
},
enum class Results { PAPER("covers") {
WIN, LOSE, DRAW override fun beats(hand: Hands): Boolean {
return hand == ROCK
} }
},
SCISSORS("cuts") {
override fun beats(hand: Hands): Boolean {
return hand == PAPER
}
};
abstract fun beats(hand: Hands): Boolean
}
companion object { companion object {
/** // For testing.
* Returns the the randomly picked shape, result and action (cuts, crushes, covers, vs.) fun winLoseOrDraw(player: String, bot: String ): String {
*/ val hand = Hands.valueOf(player.toUpperCase())
fun winLoseOrDraw(hand: Shapes): Triple<Shapes, Results, String> { val botHand = Hands.valueOf(bot.toUpperCase())
val botHand = Shapes.values()[Random.nextInt(0, Shapes.values().size)]
val result: Results return when {
val action: String hand == botHand -> "draw"
if (botHand == hand) { hand.beats(botHand) -> "win"
result = Results.DRAW else -> "lose"
action = "vs."
} else {
val shapes = arrayOf(hand, botHand)
if (shapes.contains(Shapes.ROCK) && shapes.contains(Shapes.SCISSORS)) {
action = "crushes"
result = if (hand == Shapes.ROCK) {
Results.WIN
} else {
Results.LOSE
} }
} else if (shapes.contains(Shapes.PAPER) && shapes.contains(Shapes.ROCK)) {
action = "covers"
result = if (hand == Shapes.PAPER) {
Results.WIN
} else {
Results.LOSE
}
} else { // SCISSORS vs. PAPER
action = "cuts"
result = if (hand == Shapes.SCISSORS) {
Results.WIN
} else {
Results.LOSE
}
}
}
return Triple(botHand, result, action)
} }
} }
override fun commandResponse(bot: Mobibot?, sender: String?, cmd: String?, args: String?, isPrivate: Boolean) { override fun commandResponse(bot: Mobibot, sender: String, cmd: String, args: String?, isPrivate: Boolean) {
val result = winLoseOrDraw(Shapes.valueOf(cmd!!.toUpperCase())) val hand = Hands.valueOf(cmd.toUpperCase())
when (result.second) { val botHand = Hands.values()[Random.nextInt(0, Hands.values().size)]
Results.WIN -> bot!!.action( when {
"${Utils.green(cmd)} ${Utils.bold(result.third)} ${Utils.red(result.first.value)} ~ You win ~" hand == botHand -> {
bot.action("${Utils.green(hand.name)} vs. ${Utils.green(botHand.name)} ~ The game is tied ~")
}
hand.beats(botHand) -> {
bot.action(
"${Utils.green(hand.name)} ${Utils.bold(hand.action)} ${Utils.red(botHand.name)} ~ You win ~"
) )
Results.LOSE -> bot!!.action( }
"${Utils.green(result.first.value)} ${Utils.bold(result.third)} ${Utils.red(cmd)} ~ You lose ~" else -> {
) bot.action(
else -> bot!!.action( "${Utils.green(botHand.name)} ${Utils.bold(botHand.action)} ${Utils.red(botHand.name)} ~ You lose ~"
"${Utils.green(cmd)} ${Utils.bold(result.third)} ${Utils.green(result.first.value)}"
+ " ~ The game is tied ~"
) )
} }
} }
}
override fun helpResponse(bot: Mobibot?, sender: String?, args: String?, isPrivate: Boolean) { override fun helpResponse(bot: Mobibot, sender: String, args: String?, isPrivate: Boolean) {
bot!!.send(sender, "To play Rock Paper Scissors:") bot.send(sender, "To play Rock Paper Scissors:")
bot.send( bot.send(
sender, sender,
bot.helpIndent("${bot.nick}: ${Shapes.ROCK.value} or ${Shapes.PAPER.value} or ${Shapes.SCISSORS.value}") bot.helpIndent(
"${bot.nick}: ${Hands.ROCK.name.toLowerCase()} or ${Hands.PAPER.name.toLowerCase()}"
+ " or ${Hands.SCISSORS.name.toLowerCase()}"
)
) )
} }
} }

View file

@ -32,36 +32,26 @@
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.modules.RockPaperScissors.Results
import net.thauvin.erik.mobibot.modules.RockPaperScissors.Shapes
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test import org.testng.annotations.Test
class RockPaperScissorsTest { class RockPaperScissorsTest {
@Test(invocationCount = 5) @Test
fun testWinLoseOrDraw() { fun testWinLoseOrDraw() {
var play = RockPaperScissors.winLoseOrDraw(Shapes.SCISSORS) assertThat(
// println("SCISSORS vs ${play.first}: ${playsecond}") RockPaperScissors.winLoseOrDraw("scissors", "paper")).`as`("scissors vs. paper").isEqualTo("win")
when (play.first) { assertThat(
Shapes.SCISSORS -> assertThat(play.second).`as`("SCISSORS vs ${play.first}").isEqualTo(Results.DRAW) RockPaperScissors.winLoseOrDraw("paper", "rock")).`as`("paper vs. rock").isEqualTo("win")
Shapes.ROCK -> assertThat(play.second).`as`("SCISSORS vs ${play.first}").isEqualTo(Results.LOSE) assertThat(
else -> assertThat(play.second).`as`("SCISSORS vs ${play.first}").isEqualTo(Results.WIN) RockPaperScissors.winLoseOrDraw("rock", "scissors")).`as`("rock vs. scissors").isEqualTo("win")
} assertThat(
RockPaperScissors.winLoseOrDraw("paper", "scissors")).`as`("paper vs. scissors").isEqualTo("lose")
play = RockPaperScissors.winLoseOrDraw(Shapes.ROCK) assertThat(
// println("ROCK vs ${play.first}: ${playsecond}") RockPaperScissors.winLoseOrDraw("rock", "paper")).`as`("rock vs. paper").isEqualTo("lose")
when (play.first) { assertThat(
Shapes.SCISSORS -> assertThat(play.second).`as`("ROCK vs ${play.first}").isEqualTo(Results.WIN) RockPaperScissors.winLoseOrDraw("scissors", "rock")).`as`("scissors vs. rock").isEqualTo("lose")
Shapes.ROCK -> assertThat(play.second).`as`("ROCK vs ${play.first}").isEqualTo(Results.DRAW) assertThat(
else -> assertThat(play.second).`as`("ROCK vs ${play.first}").isEqualTo(Results.LOSE) RockPaperScissors.winLoseOrDraw("scissors", "scissors")).`as`("scissors vs. scissors").isEqualTo("draw")
}
play = RockPaperScissors.winLoseOrDraw(Shapes.PAPER)
// println("PAPER vs ${play.first}: ${playsecond}")
when (play.first) {
Shapes.SCISSORS -> assertThat(play.second).`as`("PAPER vs ${play.first}").isEqualTo(Results.LOSE)
Shapes.ROCK -> assertThat(play.second).`as`("PAPER vs ${play.first}").isEqualTo(Results.WIN)
else -> assertThat(play.second).`as`("PAPER vs ${play.first}").isEqualTo(Results.DRAW)
}
} }
} }