mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Refactor board_randomizer for clarity and pylint recs
This commit is contained in:
+13
-24
@@ -1,41 +1,30 @@
|
|||||||
#!/bin/python
|
'''Module to generate random Boggle boards for testing'''
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from random import randint, shuffle
|
from random import randint, shuffle
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
def read_dice_file(path):
|
def read_dice_file(path):
|
||||||
|
'''Return list of die strings from file ignoring all comments (#)'''
|
||||||
with open(path, 'r+', encoding="utf-8") as file:
|
with open(path, 'r+', encoding="utf-8") as file:
|
||||||
# Ignore first two lines
|
return [''.join(line.rstrip().split(',')) for line in file.readlines() if line[0] != "#"]
|
||||||
file.readline()
|
|
||||||
file.readline()
|
|
||||||
dice = []
|
|
||||||
line = ''.join(file.readline().rstrip().split(','))
|
|
||||||
while(line):
|
|
||||||
dice.append(line)
|
|
||||||
line = ''.join(file.readline().rstrip().split(','))
|
|
||||||
|
|
||||||
return dice
|
def roll_die(die: str):
|
||||||
|
'''Return a face of the given die string to simulate rolling a die'''
|
||||||
def roll_die(die):
|
|
||||||
return str(die[randint(0, len(die) - 1)])
|
return str(die[randint(0, len(die) - 1)])
|
||||||
|
|
||||||
def roll_dice(dice):
|
def roll_dice(dice: list[str]):
|
||||||
rolls = []
|
'''Return a random roll for each die'''
|
||||||
for die in dice:
|
return [ roll_die(die) for die in dice ]
|
||||||
roll = roll_die(die)
|
|
||||||
rolls.append(roll)
|
|
||||||
|
|
||||||
return rolls
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(argv) != 2:
|
if len(argv) != 2:
|
||||||
print('Usage: python3 <board_randomizer.py> <dice_file>')
|
print('Usage: python3 board_randomizer.py <dice_file>')
|
||||||
else:
|
else:
|
||||||
dice = read_dice_file(argv[1])
|
dice_strings = read_dice_file(argv[1])
|
||||||
shuffle(dice)
|
shuffle(dice_strings)
|
||||||
rolls = roll_dice(dice)
|
rolls = roll_dice(dice_strings)
|
||||||
|
|
||||||
# Format dice rolls for boards file
|
# Format dice rolls for boards file
|
||||||
board_size = int(sqrt(len(rolls)))
|
board_size = int(sqrt(len(rolls)))
|
||||||
for i in range(0, len(dice), board_size):
|
for i in range(0, len(dice_strings), board_size):
|
||||||
print("".join(rolls[i:i+board_size]))
|
print("".join(rolls[i:i+board_size]))
|
||||||
|
|||||||
Reference in New Issue
Block a user