From 7bda64b5de2dd9d061f0959c19ae27fc0c951aa4 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 27 Mar 2022 22:45:26 -0700 Subject: [PATCH] Changed to the Dice module to allow for specifying the number of dice and sides. --- build.gradle | 2 +- .../net/thauvin/erik/mobibot/modules/War.java | 3 +- .../net/thauvin/erik/mobibot/modules/Dice.kt | 69 ++++++++----------- .../erik/mobibot/modules/RockPaperScissors.kt | 10 +-- .../thauvin/erik/mobibot/modules/DiceTest.kt | 15 ++-- version.properties | 6 +- 6 files changed, 46 insertions(+), 59 deletions(-) diff --git a/build.gradle b/build.gradle index 846da30..e0ca1c2 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,7 @@ mainClassName = packageName + '.Mobibot' ext.versions = [ log4j: '2.17.2', - pmd : '6.43.0', + pmd : '6.44.0', ] repositories { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/War.java b/src/main/java/net/thauvin/erik/mobibot/modules/War.java index 484d3e8..89c98fb 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/War.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/War.java @@ -86,6 +86,8 @@ public final class War extends AbstractModule { i = RANDOM.nextInt(DECK.length); y = RANDOM.nextInt(DECK.length); + event.respond("you drew " + bold(DECK[i]) + SUITS[RANDOM.nextInt(SUITS.length)]); + final String result; if (i < y) { result = bold("lost") + '.'; @@ -95,7 +97,6 @@ public final class War extends AbstractModule { result = bold("tied") + ". This means " + bold("WAR!"); } - event.respond("you drew " + bold(DECK[i]) + SUITS[RANDOM.nextInt(SUITS.length)]); event.getBot().sendIRC().action(channel, "drew " + bold(DECK[y]) + SUITS[RANDOM.nextInt(SUITS.length)] + " and " + result); } while (i == y); diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt index ff623d7..3619ec7 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Dice.kt @@ -32,7 +32,6 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.Utils.bold -import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.helpFormat import org.pircbotx.hooks.types.GenericMessageEvent @@ -43,61 +42,47 @@ class Dice : AbstractModule() { override val name = "Dice" override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { - val botRoll = roll() - val roll = roll() - val botTotal = botRoll.first + botRoll.second - val total = roll.first + roll.second - with(event.bot()) { - event.respond( - "you rolled 2 dice: ${roll.first.bold()} + ${roll.second.bold()} for a total of ${total.bold()}" - ) - - val result = when (winLoseOrTie(botTotal, total)) { - Result.WIN -> "wins" - Result.LOSE -> "lost" - else -> "tied" - } - - sendIRC().action( - channel, - "rolled 2 dice: ${botRoll.first.bold()} + ${botRoll.second.bold()} for a total of ${botTotal.bold()}" + - " and ${result.bold()}." - ) - + val arg = if (args.isBlank()) "2d6" else args.trim() + val match = Regex("^([1-9]|[12][0-9]|3[0-2])[dD]([1-9]|[12][0-9]|3[0-2])$").find(arg) + if (match != null) { + val (dice, sides) = match.destructured + event.respond("you rolled " + roll(dice.toInt(), sides.toInt())) + } else { + helpResponse(event) } } - enum class Result { - WIN, LOSE, TIE - } - - private fun roll(): Pair { - return (1..6).random() to (1..6).random() - } - companion object { // Dice command private const val DICE_CMD = "dice" @JvmStatic - fun winLoseOrTie(bot: Int, player: Int): Result { - return when { - bot > player -> { - Result.WIN - } - bot < player -> { - Result.LOSE - } - else -> { - Result.TIE + fun roll(dice: Int, sides: Int): String { + val result = StringBuilder() + var total = 0 + + repeat(dice) { + val roll = (1..sides).random() + total += roll + + if (result.isNotEmpty()) { + result.append(" + ") } + + result.append(roll.bold()) } + + if (dice != 1) { + result.append(" = ${total.bold()}") + } + + return result.toString() } } init { commands.add(DICE_CMD) - help.add("To roll the dice:") - help.add(helpFormat("%c $DICE_CMD")) + help.add("To roll 2 dice with 6 sides:") + help.add(helpFormat("%c $DICE_CMD [2d6]")) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt index a3e7045..1143b38 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/RockPaperScissors.kt @@ -105,16 +105,10 @@ class RockPaperScissors : AbstractModule() { sendIRC().action(channel, "tied: ${hand.name} vs. ${botHand.name}") } hand.beats(botHand) -> { - sendIRC().action( - channel, - "lost: ${hand.name.bold()} ${hand.action} ${botHand.name}" - ) + sendIRC().action(channel, "lost: ${hand.name.bold()} ${hand.action} ${botHand.name}") } else -> { - sendIRC().action( - channel, - "wins: ${botHand.name.bold()} ${botHand.action} ${hand.name}" - ) + sendIRC().action(channel, "wins: ${botHand.name.bold()} ${botHand.action} ${hand.name}") } } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/DiceTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/DiceTest.kt index a4a84c8..2befdb6 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/DiceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/DiceTest.kt @@ -35,13 +35,20 @@ package net.thauvin.erik.mobibot.modules import assertk.assertThat import assertk.assertions.isEqualTo +import assertk.assertions.matches import org.testng.annotations.Test class DiceTest { @Test - fun testWinLoseOrTie() { - assertThat(Dice.winLoseOrTie(6, 6), "6 vs. 6").isEqualTo(Dice.Result.TIE) - assertThat(Dice.winLoseOrTie(6, 5), "6 vs. 5").isEqualTo(Dice.Result.WIN) - assertThat(Dice.winLoseOrTie(5, 6), "5 vs. 6").isEqualTo(Dice.Result.LOSE) + fun testRoll() { + assertThat(Dice.roll(1, 1), "1d1").isEqualTo("\u00021\u0002") + assertThat(Dice.roll(2, 1), "2d1") + .isEqualTo("\u00021\u0002 + \u00021\u0002 = \u00022\u0002") + assertThat(Dice.roll(5, 1), "5d1") + .isEqualTo("\u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 = \u00025\u0002") + assertThat(Dice.roll(2, 6), "2d6") + .matches("\u0002[1-6]\u0002 \\+ \u0002[1-6]\u0002 = \u0002[1-9][0-2]?\u0002".toRegex()) + assertThat(Dice.roll(3, 7), "3d7") + .matches("\u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 = \u0002\\d{1,2}\u0002".toRegex()) } } diff --git a/version.properties b/version.properties index 521aa7d..ce7184b 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Sun Mar 27 13:34:01 PDT 2022 -version.buildmeta=101 +#Sun Mar 27 22:37:50 PDT 2022 +version.buildmeta=142 version.major=0 version.minor=8 version.patch=0 version.prerelease=rc version.project=mobibot -version.semver=0.8.0-rc+101 +version.semver=0.8.0-rc+142