|
| 1 | +package net.zomis.games.impl |
| 2 | + |
| 3 | +import net.zomis.games.WinResult |
| 4 | +import net.zomis.games.common.PlayerIndex |
| 5 | +import net.zomis.games.common.next |
| 6 | +import net.zomis.games.dsl.ActionRuleScope |
| 7 | +import net.zomis.games.dsl.GameCreator |
| 8 | +import net.zomis.games.dsl.ReplayableScope |
| 9 | +import net.zomis.games.dsl.Viewable |
| 10 | +import kotlin.random.Random |
| 11 | + |
| 12 | +data class LiarsDiceBet(val amount: Int, val value: Int): Comparable<LiarsDiceBet> { |
| 13 | + override fun compareTo(other: LiarsDiceBet): Int { |
| 14 | + if (amount != other.amount) return amount - other.amount |
| 15 | + return value - other.value |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class LiarsDicePlayer(val index: Int, var dice: MutableList<Int>) { |
| 20 | + fun removeDie() { |
| 21 | + if (dice.isEmpty()) return |
| 22 | + dice.removeAt(dice.lastIndex) |
| 23 | + } |
| 24 | + |
| 25 | + val eliminated: Boolean get() = dice.isEmpty() |
| 26 | +} |
| 27 | +class LiarsDice(val config: LiarsDiceConfig, val playerCount: Int): Viewable { |
| 28 | + val players = (0 until playerCount).map { LiarsDicePlayer(it, (1..5).toMutableList()) } |
| 29 | + var currentPlayerIndex: Int = 0 |
| 30 | + val currentPlayer get() = players[currentPlayerIndex] |
| 31 | + |
| 32 | + val diceCount get() = players.sumBy { it.dice.size } |
| 33 | + |
| 34 | + var bet: Pair<LiarsDicePlayer, LiarsDiceBet>? = null |
| 35 | + fun currentBet(): LiarsDiceBet? = bet?.second |
| 36 | + |
| 37 | + fun isSpotOn() = currentBet() == correctBet(currentBet()!!.value) |
| 38 | + fun isLie() = correctBet(currentBet()!!.value) < currentBet()!! |
| 39 | + |
| 40 | + fun correctBet(value: Int) = LiarsDiceBet(players.sumBy { player -> player.dice.count { it == value } }, value) |
| 41 | + fun nextPlayer() { |
| 42 | + do { |
| 43 | + this.currentPlayerIndex = this.currentPlayerIndex.next(players.size) |
| 44 | + } while (this.currentPlayer.eliminated) |
| 45 | + } |
| 46 | + |
| 47 | + override fun toView(viewer: PlayerIndex): Any? { |
| 48 | + return mapOf( |
| 49 | + "currentPlayer" to currentPlayerIndex, |
| 50 | + "config" to config, |
| 51 | + "better" to bet?.first?.index, |
| 52 | + "bet" to bet?.second, |
| 53 | + "players" to players.map { |
| 54 | + mapOf( |
| 55 | + "playerIndex" to it.index, |
| 56 | + "dice" to if (it.index == viewer) it.dice else it.dice.map { 0 } |
| 57 | + ) |
| 58 | + } |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +data class LiarsDiceConfig( |
| 65 | + val allowHigherQuantityAnyFace: Boolean = true, |
| 66 | + val allowHigherFaceAnyQuantity: Boolean = false, |
| 67 | + val onesAreWild: Boolean = false, |
| 68 | + val allowSpotOn: Boolean = true, |
| 69 | + val spotOnEveryoneLoses: Boolean = false, |
| 70 | + val callingWildsFirstResetsThem: Boolean = false, // point 5 at https://www.wikihow.com/Play-Liar%27s-Dice |
| 71 | + val twoDiceLeftBetOnSum: Boolean = false // If 2 players left with only one die each, bet on sum of the dice. (point 9 in wikihow) |
| 72 | +) |
| 73 | + |
| 74 | +object LiarsDiceGame { |
| 75 | + val random = Random.Default |
| 76 | + fun newRound(game: LiarsDice, replayable: ReplayableScope) { |
| 77 | + game.bet = null |
| 78 | + game.players.forEach { |
| 79 | + it.dice = replayable.ints("player-" + it.index) { it.dice.map { random.nextInt(1, 6) } }.toMutableList() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + val factory = GameCreator(LiarsDice::class) |
| 84 | + val liar = factory.singleAction("liar") |
| 85 | + val spotOn = factory.singleAction("spotOn") |
| 86 | + val bet = factory.action("bet", LiarsDiceBet::class) |
| 87 | + |
| 88 | + val game = factory.game("LiarsDice") { |
| 89 | + setup(LiarsDiceConfig::class) { |
| 90 | + players(2..16) |
| 91 | + defaultConfig { LiarsDiceConfig() } |
| 92 | + init { LiarsDice(config, playerCount) } |
| 93 | + } |
| 94 | + rules { |
| 95 | + gameStart { |
| 96 | + newRound(game, replayable) |
| 97 | + } |
| 98 | + |
| 99 | + allActions.precondition { game.currentPlayerIndex == playerIndex } |
| 100 | + |
| 101 | + action(liar) { |
| 102 | + precondition { game.bet != null } |
| 103 | + effect { |
| 104 | + logRevealAllDice("liar", this) |
| 105 | + if (game.isLie()) { |
| 106 | + val losingPlayer = game.bet!!.first |
| 107 | + log { "$player called liar and is correct! ${player(losingPlayer.index)} loses one die." } |
| 108 | + losingPlayer.removeDie() |
| 109 | + game.currentPlayerIndex = losingPlayer.index |
| 110 | + } else { |
| 111 | + log { "$player called liar incorrectly and loses one die." } |
| 112 | + game.currentPlayer.removeDie() |
| 113 | + } |
| 114 | + newRound(game, replayable) |
| 115 | + } |
| 116 | + } |
| 117 | + action(spotOn) { |
| 118 | + precondition { game.bet != null } |
| 119 | + precondition { game.config.allowSpotOn } |
| 120 | + effect { |
| 121 | + logRevealAllDice("spot-on", this) |
| 122 | + if (game.isSpotOn()) { |
| 123 | + val losingPlayer = game.bet!!.first |
| 124 | + if (game.config.spotOnEveryoneLoses) { |
| 125 | + log { "$player called spot-on and was correct! Everyone loses one die." } |
| 126 | + game.players.filter { it != game.currentPlayer }.forEach { it.removeDie() } |
| 127 | + } else { |
| 128 | + log { "$player called spot-on and was correct! ${player(losingPlayer.index)} loses one die." } |
| 129 | + losingPlayer.removeDie() |
| 130 | + } |
| 131 | + game.currentPlayerIndex = losingPlayer.index |
| 132 | + } else { |
| 133 | + log { "$player called spot-on incorrectly and loses one die." } |
| 134 | + game.currentPlayer.removeDie() |
| 135 | + } |
| 136 | + newRound(game, replayable) |
| 137 | + } |
| 138 | + } |
| 139 | + action(bet) { |
| 140 | + choose { |
| 141 | + val bet = context.game.bet?.second ?: LiarsDiceBet(1, 0) |
| 142 | + options({ bet.amount..context.game.diceCount }) {amount -> |
| 143 | + val min = if (amount == bet.amount) bet.value + 1 else 1 |
| 144 | + options({ min..6 }) {value -> |
| 145 | + parameter(LiarsDiceBet(amount, value)) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + effect { |
| 150 | + game.bet = game.currentPlayer to action.parameter |
| 151 | + game.nextPlayer() |
| 152 | + log { "$player bets ${action.amount} ${action.value}'s" } |
| 153 | + } |
| 154 | + } |
| 155 | + allActions.after { |
| 156 | + val remaining = eliminations.remainingPlayers() |
| 157 | + game.players.filter { it.eliminated && remaining.contains(it.index) }.forEach { |
| 158 | + eliminations.result(it.index, WinResult.LOSS) |
| 159 | + } |
| 160 | + if (eliminations.remainingPlayers().size == 1) { |
| 161 | + eliminations.eliminateRemaining(WinResult.WIN) |
| 162 | + } |
| 163 | + while (game.currentPlayer.eliminated) { |
| 164 | + game.currentPlayerIndex = game.currentPlayerIndex.next(game.playerCount) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private fun logRevealAllDice(call: String, scope: ActionRuleScope<LiarsDice, *>) { |
| 171 | + scope.log { "$player calls $call and everyone reveals their dice!" } |
| 172 | + scope.game.players.filter { !it.eliminated }.forEach { |
| 173 | + scope.log { "${player(it.index)} had ${it.dice.sorted().joinToString(", ")}" } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments