From 2b60b80364946c3b4da27ec5429fa45e64e9ee38 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 2 Nov 2018 15:12:18 +0100 Subject: [PATCH] random: extract to a separate module --- pqcli/mechanic.py | 32 ++++++++++---------------------- pqcli/random.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 pqcli/random.py diff --git a/pqcli/mechanic.py b/pqcli/mechanic.py index b69429d..9b5bfc6 100644 --- a/pqcli/mechanic.py +++ b/pqcli/mechanic.py @@ -1,9 +1,9 @@ import datetime import enum -import random import typing as T from dataclasses import dataclass, field +from pqcli import random from pqcli.config import * @@ -12,10 +12,6 @@ def level_up_time(level: int) -> int: return 20 * level * 60 -def random_low(below): - return min(random.randint(0, below), random.randint(0, below)) - - def generate_name() -> str: parts = [ "br|cr|dr|fr|gr|j|kr|l|m|n|pr||||r|sh|tr|v|wh|x|y|z".split("|"), @@ -160,10 +156,10 @@ class Player: def level_up(self) -> None: self.level += 1 self.stats[Stat.hp_max] += ( - self.stats[Stat.condition] // 3 + 1 + random.randint(0, 3) + self.stats[Stat.condition] // 3 + 1 + random.below(4) ) self.stats[Stat.mp_max] += ( - self.stats[Stat.intelligence] // 3 + 1 + random.randint(0, 3) + self.stats[Stat.intelligence] // 3 + 1 + random.below(4) ) self.win_stat() self.win_stat() @@ -171,12 +167,12 @@ class Player: self.exp_bar.reset(level_up_time(self.level)) def win_stat(self) -> bool: - if random.choice([False, True]): + if random.odds(1, 2): chosen_stat = random.choice(list(Stat)) else: # favor the beststat so it will tend to clump t = sum(value ** 2 for _stat, value in self.stats) - t = random.randint(0, t - 1) + t = random.below(t) chosen_stat: Stat = None for stat, value in self.stats: chosen_stat = stat @@ -194,11 +190,8 @@ class Player: def win_spell(self) -> None: self.spell_book.add( SPELLS[ - random_low( - min( - self.stats[Stat.wisdom] + self.level - 1, - len(SPELLS) - 1, - ) + random.below_low( + min(self.stats[Stat.wisdom] + self.level, len(SPELLS)) ) ], 1, @@ -236,15 +229,10 @@ class StatsBuilder: stats = Stats() for stat in PRIME_STATS: stats[stat] = ( - 3 - + random.randint(0, 5) - + random.randint(0, 5) - + random.randint(0, 5) + 3 + random.below(6) + random.below(6) + random.below(6) ) - stats[Stat.hp_max] = random.randint(0, 7) + stats[Stat.condition] // 6 - stats[Stat.mp_max] = ( - random.randint(0, 7) + stats[Stat.intelligence] // 6 - ) + stats[Stat.hp_max] = random.below(8) + stats[Stat.condition] // 6 + stats[Stat.mp_max] = random.below(8) + stats[Stat.intelligence] // 6 self.history.append(stats) return stats diff --git a/pqcli/random.py b/pqcli/random.py new file mode 100644 index 0000000..61e543c --- /dev/null +++ b/pqcli/random.py @@ -0,0 +1,22 @@ +import random +import typing as T + + +def seed(seed: T.Any) -> None: + random.seed(seed) + + +def choice(source: T.Iterable[T.Any]) -> T.Any: + return random.choice(source) + + +def below(num: int) -> int: + return random.randint(0, num - 1) + + +def below_low(num: int) -> int: + return min(below(num), below(num)) + + +def odds(chance: int, out_of: int) -> bool: + return below(out_of) < chance