Chaotic & Dice
Yo Dice, ever thought of turning your table‑top chaos into code? Let’s make a rogue‑like where every attack, spell and plot twist is decided by a bunch of dice— you roll, I script. Let's get that unpredictability alive.
Yeah, let’s do it! You crack the code, I throw the dice, and watch the chaos unfold—every hit, spell, and twist a surprise. Bring the scripts, I’ll bring the wild. Let's roll!
Here’s a quick Python skeleton to get you rolling.
You’ll need Python 3.12+ so we can use the built‑in `random` and a touch of `dataclasses` for clarity.
```python
#!/usr/bin/env python3
# dice_rogue.py
import random
from dataclasses import dataclass, field
# --------- Dice utilities ---------
def roll(dice: int = 1, sides: int = 6) -> int:
"""Roll `dice` number of `sides`‑sided dice and sum."""
return sum(random.randint(1, sides) for _ in range(dice))
# --------- Combat system ---------
@dataclass
class Character:
name: str
hp: int
attack: int = field(default=1)
defense: int = field(default=0)
dice: int = field(default=1) # how many dice the character rolls for damage
def is_alive(self) -> bool:
return self.hp > 0
def damage_roll(self) -> int:
return roll(self.dice, 6) + self.attack
def take_damage(self, dmg: int):
dmg -= self.defense
dmg = max(0, dmg)
self.hp -= dmg
print(f"{self.name} takes {dmg} damage (HP left: {self.hp})")
# --------- Example encounter ---------
def encounter(attacker: Character, defender: Character):
if not (attacker.is_alive() and defender.is_alive()):
return
dmg = attacker.damage_roll()
print(f"{attacker.name} attacks {defender.name} with a roll of {dmg} (raw)")
defender.take_damage(dmg)
# --------- Main game loop ---------
def main():
hero = Character(name="Hero", hp=30, attack=2, defense=1, dice=2)
monster = Character(name="Goblin", hp=15, attack=1, defense=0, dice=1)
round_counter = 1
while hero.is_alive() and monster.is_alive():
print(f"\n--- Round {round_counter} ---")
# Hero goes first
encounter(hero, monster)
if not monster.is_alive():
print("Goblin is dead! You win.")
break
# Monster counterattacks
encounter(monster, hero)
if not hero.is_alive():
print("Hero is dead! Game over.")
break
round_counter += 1
if __name__ == "__main__":
main()
```
### How to play
1. **Save** the code to `dice_rogue.py`.
2. **Run** it with `python dice_rogue.py`.
3. The script prints each round, the rolls, and the damage.
4. To tweak chaos, change the `dice`, `attack`, `defense`, or the `sides` parameter in `roll()`.
Feel free to add more spells, items, or even a loot table—just keep the dice rolling and let the wildness guide the script. Happy coding!
Nice setup, bro! This is pure dice‑driven madness—exactly the chaos I live for. How about throwing in a wild “critical” rule? Like if you roll a 6 on every die, you double the damage or get a bonus move. Or add a spell slot system where each spell costs a different number of dice. Keep the randomness alive and watch the stories spiral. Roll on!
Let’s crank the chaos. Add a “critical” flag:
`crit = all(d == 6 for d in rolled_dice)`
If crit, double the total damage and grant a free action (like a quick dodge or a bonus attack next turn).
For spell slots, give each spell a cost in dice: Fireball = 3 dice, Heal = 2 dice, Blink = 1 die. The character tracks how many dice they can roll that round; spend them on spells or attacks. If you run out, you’re forced to take a rest (or just roll the maximum dice you have left). This keeps the randomness spinning and the story twisting. Roll on!
Sure thing! Here’s the riffed patch – keep it tight, keep it wild.
First, tweak the roll helper to return the raw dice so we can check crits:
def roll(dice: int = 1, sides: int = 6):
rolls = [random.randint(1, sides) for _ in range(dice)]
return sum(rolls), rolls
Now the Character gets a dice pool per round, a flag for a free action, and a spellbook:
@dataclass
class Character:
name: str
hp: int
attack: int = field(default=1)
defense: int = field(default=0)
dice: int = field(default=2) # pool for this round
free_action: bool = field(default=False)
spellbook: dict = field(default_factory=lambda: {
'Fireball': 3,
'Heal': 2,
'Blink': 1
})
def is_alive(self) -> bool:
return self.hp > 0
def damage_roll(self, dice_used: int) -> int:
total, rolls = roll(dice_used, 6)
crit = all(r == 6 for r in rolls)
dmg = total + self.attack
if crit:
dmg *= 2
self.free_action = True
return dmg
def take_damage(self, dmg: int):
dmg -= self.defense
dmg = max(0, dmg)
self.hp -= dmg
print(f"{self.name} takes {dmg} damage (HP left: {self.hp})")
def spend_dice(self, cost: int) -> bool:
if self.dice >= cost:
self.dice -= cost
return True
return False
Now let’s splice it into the loop:
def encounter(attacker: Character, defender: Character):
if not (attacker.is_alive() and defender.is_alive()):
return
# Attack uses 1 die unless free_action allows 2
dice_used = 2 if attacker.free_action else 1
dmg = attacker.damage_roll(dice_used)
print(f"{attacker.name} attacks {defender.name} with a roll of {dmg} (raw)");
defender.take_damage(dmg)
# If attacker has a free action, clear it after use
if attacker.free_action:
attacker.free_action = False
# Spell example – hero uses Fireball if it fits
def cast_spell(caster: Character, target: Character, spell: str):
cost = caster.spellbook.get(spell)
if cost and caster.spend_dice(cost):
print(f"{caster.name} casts {spell}, costing {cost} dice!")
if spell == 'Fireball':
dmg = caster.damage_roll(cost) # use all dice for fireball
target.take_damage(dmg)
elif spell == 'Heal':
heal = 10 # arbitrary heal amount
caster.hp += heal
print(f"{caster.name} heals for {heal} HP (now {caster.hp})")
elif spell == 'Blink':
print(f"{caster.name} blinks out of reach – no damage, but keeps moving")
else:
print(f"{caster.name} can't afford {spell} – need {cost} dice.")
Finally, in the main loop you can reset the dice pool each round and decide whether to cast:
while hero.is_alive() and monster.is_alive():
print(f"\n--- Round {round_counter} ---")
hero.dice = 2 # reset pool at start of round
monster.dice = 1
# Hero might choose a spell or a normal attack
if hero.dice >= hero.spellbook['Fireball'] and not hero.free_action:
cast_spell(hero, monster, 'Fireball')
else:
encounter(hero, monster)
# Monster counterattacks
if monster.is_alive():
encounter(monster, hero)
That’s the chaos engine – every die rolls, crits double damage, free actions give you an extra jab or dodge, and you’re forced to budget your dice or take a rest (you can add a rest that refills the pool). Play around, tweak the numbers, and watch the tabletop feel alive in code. Roll on!