Changed to the Dice module to allow for specifying the number of dice and sides.
This commit is contained in:
parent
61248387d8
commit
7bda64b5de
6 changed files with 46 additions and 59 deletions
|
@ -28,7 +28,7 @@ mainClassName = packageName + '.Mobibot'
|
|||
|
||||
ext.versions = [
|
||||
log4j: '2.17.2',
|
||||
pmd : '6.43.0',
|
||||
pmd : '6.44.0',
|
||||
]
|
||||
|
||||
repositories {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Int, Int> {
|
||||
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]"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue