Advertisement
angeldp

deepSpaceD-6

Dec 30th, 2023
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.26 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. function module for Deep Space D-6
  4. Created on Wed Dec 27 20:25:12 2023
  5.  
  6. @author:
  7. """
  8. # IMPORTS
  9.  
  10. # The random module is used to generate random values
  11. # Used in generate_crew() and commander() to use randint()
  12. from random import(randint)
  13. # The os module is used to interact with the system
  14. # Used in clear_screen() to use os.name() and os.system()
  15. from os import(name, system)
  16.  
  17. # Functions to be developed, necessary for the operation
  18.  
  19. def new_threat(deck):
  20.     """
  21.    To Be Developed
  22.  
  23.    Parameters
  24.    ----------
  25.    deck : array
  26.        Deck of threat cards in play.
  27.    
  28.    Returns
  29.    -------
  30.    deck: array
  31.        Updated deck
  32.        
  33.    """
  34.     deck.remove(deck[0])
  35.     print('We get a threat card from the deck')
  36.     return(deck)
  37.  
  38. def activate_threat():
  39.     """
  40.    To Be Developed
  41.  
  42.    Returns
  43.    -------
  44.    None.
  45.  
  46.    """
  47.     threat_dice = randint(1,6)
  48.     print(f'trigger threats corresponding to the number {threat_dice}')
  49.    
  50. def tactic(tacticn):
  51.     """
  52.    To Be Developed
  53.  
  54.    Parameters
  55.    ----------
  56.    tacticn : int
  57.        Number of crew members (tactics) in play.
  58.    
  59.    Returns
  60.    -------
  61.    None.
  62.  
  63.    """
  64.     print(f"The tactic's actions are carried out x {tacticn}")
  65.    
  66. def doctor(doctorn):
  67.     """
  68.    To Be Developed
  69.  
  70.    Parameters
  71.    ----------
  72.    tacticn : int
  73.        Number of crew members (doctors) in play.
  74.    
  75.    Returns
  76.    -------
  77.    None.
  78.  
  79.    """
  80.     print(f"The doctor's actions are carried out x {doctorn}")
  81.    
  82. def scientist(scientistn):
  83.     """
  84.    To Be Developed
  85.  
  86.    Parameters
  87.    ----------
  88.    tacticn : int
  89.        Number of crew members (scientists) in play.
  90.    
  91.    Returns
  92.    -------
  93.    None.
  94.  
  95.    """
  96.     print(f"The scientist's actions are carried out x {scientistn}")
  97.    
  98. def engineer(engineern):
  99.     """
  100.    To Be Developed
  101.  
  102.    Parameters
  103.    ----------
  104.    tacticn : int
  105.        Number of crew members (engineers) in play.
  106.    
  107.    Returns
  108.    -------
  109.    None.
  110.  
  111.    """
  112.     print(f"The engineer's actions are carried out x {engineern}")
  113.  
  114. # Completed functions in alpha version
  115.  
  116. def clear_screen():
  117.     """
  118.    Clear the user screen based on the operating system used
  119.  
  120.    Returns
  121.    -------
  122.    None.
  123.  
  124.    """
  125.    
  126.     if name == "posix":
  127.         system("clear")
  128.     elif name == "ce" or name == "nt" or name == "dos":
  129.         system ("cls")
  130.  
  131. def generate_crew(crewn):
  132.     """
  133.    Receives the number of active crew members (crewn)
  134.    to complete with random values ​​between 1 and 6
  135.    a crew sized array.
  136.  
  137.    Parameters
  138.    ----------
  139.    crewn : int
  140.        Number of crew members (dice) in play.
  141.  
  142.    Returns
  143.    -------
  144.    crew : array
  145.        Result of the dice roll.
  146.  
  147.    """
  148.    
  149.     # Loop through the array storing random values between 1 and 6
  150.     crew = []
  151.     for i in range(crewn):
  152.         crew.append(randint(1, 6))
  153.     return crew
  154.  
  155. def pretty_shown_crew(crew):
  156.     """
  157.    Shows crew as a ASCII dice roll
  158.  
  159.    Parameters
  160.    ----------
  161.    crew : array
  162.        Result of the dice roll.
  163.  
  164.    Returns
  165.    -------
  166.    None.
  167.  
  168.    """
  169.     # dice sides in ascii
  170.     PRETTY_DICE = {
  171.         1: (
  172.             "┌─────────┐",
  173.             "│         │",
  174.             "│    ●    │",
  175.             "│         │",
  176.             "└─────────┘",
  177.             ),
  178.         2: (
  179.             "┌─────────┐",
  180.             "│  ●      │",
  181.             "│         │",
  182.             "│      ●  │",
  183.             "└─────────┘",
  184.             ),
  185.         3: (
  186.             "┌─────────┐",
  187.             "│  ●      │",
  188.             "│    ●    │",
  189.             "│      ●  │",
  190.             "└─────────┘",
  191.             ),
  192.         4: (
  193.             "┌─────────┐",
  194.             "│  ●   ●  │",
  195.             "│         │",
  196.             "│  ●   ●  │",
  197.             "└─────────┘",
  198.             ),
  199.         5: (
  200.             "┌─────────┐",
  201.             "│  ●   ●  │",
  202.             "│    ●    │",
  203.             "│  ●   ●  │",
  204.             "└─────────┘",
  205.             ),
  206.         6: (
  207.             "┌─────────┐",
  208.             "│  ●   ●  │",
  209.             "│  ●   ●  │",
  210.             "│  ●   ●  │",
  211.             "└─────────┘",
  212.             ),
  213.         }
  214.     #  dice height (rows) and separator (sep)
  215.     DICE_ROWS = len(PRETTY_DICE[1])
  216.     DICE_SEP = ' '
  217.     pretty_faces = []
  218.     for n in crew:
  219.         pretty_faces.append(PRETTY_DICE[n])
  220.         dice_faces_rows = []
  221.         for row_id in range(DICE_ROWS):
  222.             row_components = []
  223.             for die in pretty_faces:
  224.                 row_components.append(die[row_id])
  225.             row_string = DICE_SEP.join(row_components)
  226.             dice_faces_rows.append(row_string)
  227.     width = len(dice_faces_rows[0])
  228.     diagram_header = " CREW ".center(width, "~")
  229.     dice_faces_diagram = "\n".join([diagram_header] + dice_faces_rows)
  230.     # the result is shown
  231.     print(f"{dice_faces_diagram}\n")
  232.  
  233. def scan_threats(crew, scant, deck):
  234.     """
  235.    loops through the 'crew' array to get the number of sixes.
  236.    If 'scant' plus that number is less than 3 the variable 'scant'
  237.    is updated; If it is equal to or greater than 3, it calculates
  238.    the module of that value between 3 and saves it in 'scant'
  239.    and draws a new threat card [new_threat()].
  240.    It will have to do this as many times as the integer part
  241.    indicates by dividing the number of scanners by 3
  242.  
  243.    Parameters
  244.    ----------
  245.    crew : array
  246.        Result of the dice roll.
  247.    scant : int
  248.        Number of busy scanners. Value between 0 and 2.
  249.    Returns
  250.    -------
  251.    scant : int
  252.        Updated number of busy scanners. Value between 0 and 2.
  253.  
  254.    """
  255.     # 'n6' will store the number of sixes in 'crew'
  256.     n6 = 0
  257.     for i in crew:
  258.         if i == 6:
  259.             n6 += 1
  260.     # The number of scanners is updated
  261.     scant = scant + n6
  262.  
  263.     # If 'scant' is equal to or greater than 3,
  264.     # I draw as many threat cards as groups of 3
  265.     # have been obtained and I update scant
  266.     # with the rest of the division
  267.    
  268.     if scant >= 3:
  269.         rango = scant // 3
  270.         for n in range(rango):
  271.             new_threat(deck)
  272.         scant = scant % 3
  273.        
  274.     return(scant)  
  275.  
  276. def commander(crew):
  277.     """
  278.    Actions to perform with each '1' obtained in the roll.
  279.    It must ask if you want to change any crew member or re-roll
  280.    available crew.  
  281.    It will need to validate all user input.
  282.    It needs the crew array and returns it modified
  283.    
  284.    Parameters
  285.    ----------
  286.    crew : array
  287.        Result of the dice roll.
  288.  
  289.    Returns
  290.    -------
  291.    crew : array
  292.        Dice roll result modified by commander.
  293.  
  294.    """
  295.  
  296.     print('Commander available:')
  297.     # The key pressed by the user is stored in 'c'.
  298.     c = 'x'
  299.     oldcrew = ''
  300.     # Prompt until a valid value is obtained
  301.     while c.lower() != 'c' and c.lower() != 'r':
  302.         c = input("Do you want to change any members of your crew (c) \n \
  303.        or re-roll de dice (r)? c/r\n")
  304.     if c.lower() == 'c':
  305.         # At this point 'c' is used to validate that the number is on the
  306.         # 'crew' (an unavailable crew member cannot be changed)
  307.         c = 'n'
  308.         while c == 'n':
  309.             # 'cont' stores the position in the array in which
  310.             # the indicated value is located
  311.             cont = 0
  312.             oldcrew = int(input("Number of the crew member to be replaced:"))
  313.             # If a 6 (scanner) has been indicated, it moves on to the next
  314.             #iteration without doing anything else (continues requesting
  315.             # a crew member until a valid value is received)
  316.             if oldcrew == 6:
  317.                 continue
  318.             for i in crew:
  319.                 if i == oldcrew:
  320.                 # If the entered value is available the new value is requested,
  321.                 # making sure that a value between 2 and 5 is indicated.
  322.                     newcrew = 1
  323.                     while newcrew < 2 or newcrew > 5:
  324.                         newcrew = int(input("Value of the new crew member (2-5)"))
  325.                     crew[cont] = newcrew
  326.                     # 'c' is updated to stop prompting.
  327.                     c = ''
  328.                     break
  329.                 cont += 1
  330.     elif c.lower() == 'r':
  331.         ncrew = []
  332.         cont = 0
  333.         while cont < len(crew):
  334.             if crew[cont] == 1 or crew[cont] == 6:
  335.                 ncrew.append(crew[cont])
  336.             else:
  337.                 ncrew.append(randint(1,6))
  338.             cont += 1
  339.         # Crew update
  340.         crew = ncrew
  341.     if c == 'c': print(f'Successfully changed {oldcrew} to {newcrew}')
  342.     print(f"Nueva tripulación: {crew}")
  343.     pretty_shown_crew(crew)
  344.     return(crew)
  345.  
  346. def assign_crew(crew):
  347.     """
  348.    If there are commanders in the roll, it is responsible for updating the
  349.    crew and, in any case, calls the functions of each crew member with
  350.    the number of them available.
  351.  
  352.    Parameters
  353.    ----------
  354.    crew : array
  355.        Result of the dice roll.
  356.  
  357.    Returns
  358.    -------
  359.    None.
  360.  
  361.    """
  362.     # The new crew
  363.     ncrew = []
  364.     # Number of crew members of each type
  365.     tacticn = 0
  366.     doctorn = 0
  367.     scientistn = 0
  368.     engineern = 0
  369.     # in search of the commander
  370.     for i in range(len(crew)):
  371.         if crew[i] == 1: ncrew = commander(crew)
  372.  
  373.     # Crew update if modified
  374.     if len(ncrew) != 0 : crew = ncrew    
  375.     # For the rest of the tasks, the appropriate functions are called
  376.     # with the number of crew members of each type.
  377.     for i in range(len(crew)):
  378.         if crew[i] == 2: tacticn += 1
  379.         elif crew[i] == 3: doctorn += 1
  380.         elif crew[i] == 4: scientistn += 1
  381.         elif crew[i] == 5: engineern += 1
  382.     if tacticn > 0: tactic(tacticn)
  383.     if doctorn > 0: doctor(doctorn)
  384.     if scientistn > 0: scientist(scientistn)
  385.     if engineern > 0: engineer(engineern)
  386.  
  387. # MAIN
  388.  
  389. def main_program():
  390.     deck = list(range(0,9))
  391.     print(deck)
  392.     i = 0
  393.     scant = 0
  394.     crewn = 6
  395.     while i < len(deck):
  396.         clear_screen()
  397.         print(f'\n\nRound #{i + 1}')
  398.         print("1:\nHere is the dice roll")
  399.         crew = generate_crew(crewn)
  400.         print(crew)
  401.         pretty_shown_crew(crew)
  402.         scant = scan_threats(crew, scant, deck)
  403.         print(f"2:\nYou have {scant} scanners in use and {crewn} crew\n \
  404.        members available for the next round")
  405.         print("3:")
  406.         assign_crew(crew)
  407.         print(crew)
  408.         print("4:")
  409.         deck = new_threat(deck)
  410.         print(f"{len(deck)} card(s) remaining")
  411.         print("5:")
  412.         activate_threat()
  413.         crewn = 6 - scant
  414.         if crewn < 1:
  415.             print('You lost!')
  416.             return(0)
  417.         print(f"6:\n{crewn} crew members available")
  418.         i += 1
  419.         c = input("Press enter to start the next round")
  420.     print('Won!')
  421.     c = input('Press enter to exit')
  422.     return(0)
  423.  
  424. main_program()
  425.  
  426.  
  427.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement