# ==============================================================================
"""DICE : display a graphical representation for a list of random dice rolls"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
User input : <number of dice> (must be an integer between 1 and 8)
App output : graphical representation for the provided number of dice rolls"""
# ==============================================================================
from ezCLI import *
from random import randrange
# ------------------------------------------------------------------------------
def roll(n:int) -> int:
  """return a list of 'n' random dice rolls"""
  return [1 + randrange(6) for loop in range(n)]
# ------------------------------------------------------------------------------
def dice(rolls:list) -> str:
  """return a graphical representation for a list of random dice rolls"""
  # first create the dice font as a single string with 5 lines of 60 characters
  font = """
┌───────┐:┌───────┐:┌───────┐:┌───────┐:┌───────┐:┌───────┐
│       │:│     ■ │:│     ■ │:│ ■   ■ │:│ ■   ■ │:│ ■   ■ │
│   ■   │:│       │:│   ■   │:│       │:│   ■   │:│ ■   ■ │
│       │:│ ■     │:│ ■     │:│ ■   ■ │:│ ■   ■ │:│ ■   ■ │
└───────┘:└───────┘:└───────┘:└───────┘:└───────┘:└───────┘
"""
  # then convert font into a 5x6 matrix by splitting string at '\n' and at ':'
  font = [line.split(':') for line in font.strip().split('\n')]
  # for each line, extract the chars of the current digit from the font matrix
  lines = [[line[roll-1] for roll in rolls] for line in font]
  # and finally, join everything as a multi-line string
  return '\n'.join(' '.join(line) for line in lines)
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' and return the graphical representation for dice rolls"""
  n = convert(command)
  assert type(n) is int and 0<n<9, "number must be an integer between 1 and 8"
  return dice(roll(n)) # roll 'n' dice and return its graphical representation
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter number of dice")
# ==============================================================================
