Changed to the Dice module to allow for specifying the number of dice and sides.

This commit is contained in:
Erik C. Thauvin 2022-03-27 22:45:26 -07:00
parent 61248387d8
commit 7bda64b5de
6 changed files with 46 additions and 59 deletions

View file

@ -28,7 +28,7 @@ mainClassName = packageName + '.Mobibot'
ext.versions = [ ext.versions = [
log4j: '2.17.2', log4j: '2.17.2',
pmd : '6.43.0', pmd : '6.44.0',
] ]
repositories { repositories {

View file

@ -86,6 +86,8 @@ public final class War extends AbstractModule {
i = RANDOM.nextInt(DECK.length); i = RANDOM.nextInt(DECK.length);
y = RANDOM.nextInt(DECK.length); y = RANDOM.nextInt(DECK.length);
event.respond("you drew " + bold(DECK[i]) + SUITS[RANDOM.nextInt(SUITS.length)]);
final String result; final String result;
if (i < y) { if (i < y) {
result = bold("lost") + '.'; result = bold("lost") + '.';
@ -95,7 +97,6 @@ public final class War extends AbstractModule {
result = bold("tied") + ". This means " + bold("WAR!"); 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)] + event.getBot().sendIRC().action(channel, "drew " + bold(DECK[y]) + SUITS[RANDOM.nextInt(SUITS.length)] +
" and " + result); " and " + result);
} while (i == y); } while (i == y);

View file

@ -32,7 +32,6 @@
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.bold
import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
@ -43,61 +42,47 @@ class Dice : AbstractModule() {
override val name = "Dice" override val name = "Dice"
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
val botRoll = roll() val arg = if (args.isBlank()) "2d6" else args.trim()
val roll = roll() val match = Regex("^([1-9]|[12][0-9]|3[0-2])[dD]([1-9]|[12][0-9]|3[0-2])$").find(arg)
val botTotal = botRoll.first + botRoll.second if (match != null) {
val total = roll.first + roll.second val (dice, sides) = match.destructured
with(event.bot()) { event.respond("you rolled " + roll(dice.toInt(), sides.toInt()))
event.respond( } else {
"you rolled 2 dice: ${roll.first.bold()} + ${roll.second.bold()} for a total of ${total.bold()}" helpResponse(event)
)
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()}."
)
} }
} }
enum class Result {
WIN, LOSE, TIE
}
private fun roll(): Pair<Int, Int> {
return (1..6).random() to (1..6).random()
}
companion object { companion object {
// Dice command // Dice command
private const val DICE_CMD = "dice" private const val DICE_CMD = "dice"
@JvmStatic @JvmStatic
fun winLoseOrTie(bot: Int, player: Int): Result { fun roll(dice: Int, sides: Int): String {
return when { val result = StringBuilder()
bot > player -> { var total = 0
Result.WIN
} repeat(dice) {
bot < player -> { val roll = (1..sides).random()
Result.LOSE total += roll
}
else -> { if (result.isNotEmpty()) {
Result.TIE result.append(" + ")
} }
result.append(roll.bold())
} }
if (dice != 1) {
result.append(" = ${total.bold()}")
}
return result.toString()
} }
} }
init { init {
commands.add(DICE_CMD) commands.add(DICE_CMD)
help.add("To roll the dice:") help.add("To roll 2 dice with 6 sides:")
help.add(helpFormat("%c $DICE_CMD")) help.add(helpFormat("%c $DICE_CMD [2d6]"))
} }
} }

View file

@ -105,16 +105,10 @@ class RockPaperScissors : AbstractModule() {
sendIRC().action(channel, "tied: ${hand.name} vs. ${botHand.name}") sendIRC().action(channel, "tied: ${hand.name} vs. ${botHand.name}")
} }
hand.beats(botHand) -> { hand.beats(botHand) -> {
sendIRC().action( sendIRC().action(channel, "lost: ${hand.name.bold()} ${hand.action} ${botHand.name}")
channel,
"lost: ${hand.name.bold()} ${hand.action} ${botHand.name}"
)
} }
else -> { else -> {
sendIRC().action( sendIRC().action(channel, "wins: ${botHand.name.bold()} ${botHand.action} ${hand.name}")
channel,
"wins: ${botHand.name.bold()} ${botHand.action} ${hand.name}"
)
} }
} }
} }

View file

@ -35,13 +35,20 @@ package net.thauvin.erik.mobibot.modules
import assertk.assertThat import assertk.assertThat
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.matches
import org.testng.annotations.Test import org.testng.annotations.Test
class DiceTest { class DiceTest {
@Test @Test
fun testWinLoseOrTie() { fun testRoll() {
assertThat(Dice.winLoseOrTie(6, 6), "6 vs. 6").isEqualTo(Dice.Result.TIE) assertThat(Dice.roll(1, 1), "1d1").isEqualTo("\u00021\u0002")
assertThat(Dice.winLoseOrTie(6, 5), "6 vs. 5").isEqualTo(Dice.Result.WIN) assertThat(Dice.roll(2, 1), "2d1")
assertThat(Dice.winLoseOrTie(5, 6), "5 vs. 6").isEqualTo(Dice.Result.LOSE) .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())
} }
} }

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Sun Mar 27 13:34:01 PDT 2022 #Sun Mar 27 22:37:50 PDT 2022
version.buildmeta=101 version.buildmeta=142
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=rc version.prerelease=rc
version.project=mobibot version.project=mobibot
version.semver=0.8.0-rc+101 version.semver=0.8.0-rc+142