Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- function module for Deep Space D-6
- Created on Wed Dec 27 20:25:12 2023
- @author:
- """
- # IMPORTS
- # The random module is used to generate random values
- # Used in generate_crew() and commander() to use randint()
- from random import(randint)
- # The os module is used to interact with the system
- # Used in clear_screen() to use os.name() and os.system()
- from os import(name, system)
- # Functions to be developed, necessary for the operation
- def new_threat(deck):
- """
- To Be Developed
- Parameters
- ----------
- deck : array
- Deck of threat cards in play.
- Returns
- -------
- deck: array
- Updated deck
- """
- deck.remove(deck[0])
- print('We get a threat card from the deck')
- return(deck)
- def activate_threat():
- """
- To Be Developed
- Returns
- -------
- None.
- """
- threat_dice = randint(1,6)
- print(f'trigger threats corresponding to the number {threat_dice}')
- def tactic(tacticn):
- """
- To Be Developed
- Parameters
- ----------
- tacticn : int
- Number of crew members (tactics) in play.
- Returns
- -------
- None.
- """
- print(f"The tactic's actions are carried out x {tacticn}")
- def doctor(doctorn):
- """
- To Be Developed
- Parameters
- ----------
- tacticn : int
- Number of crew members (doctors) in play.
- Returns
- -------
- None.
- """
- print(f"The doctor's actions are carried out x {doctorn}")
- def scientist(scientistn):
- """
- To Be Developed
- Parameters
- ----------
- tacticn : int
- Number of crew members (scientists) in play.
- Returns
- -------
- None.
- """
- print(f"The scientist's actions are carried out x {scientistn}")
- def engineer(engineern):
- """
- To Be Developed
- Parameters
- ----------
- tacticn : int
- Number of crew members (engineers) in play.
- Returns
- -------
- None.
- """
- print(f"The engineer's actions are carried out x {engineern}")
- # Completed functions in alpha version
- def clear_screen():
- """
- Clear the user screen based on the operating system used
- Returns
- -------
- None.
- """
- if name == "posix":
- system("clear")
- elif name == "ce" or name == "nt" or name == "dos":
- system ("cls")
- def generate_crew(crewn):
- """
- Receives the number of active crew members (crewn)
- to complete with random values between 1 and 6
- a crew sized array.
- Parameters
- ----------
- crewn : int
- Number of crew members (dice) in play.
- Returns
- -------
- crew : array
- Result of the dice roll.
- """
- # Loop through the array storing random values between 1 and 6
- crew = []
- for i in range(crewn):
- crew.append(randint(1, 6))
- return crew
- def pretty_shown_crew(crew):
- """
- Shows crew as a ASCII dice roll
- Parameters
- ----------
- crew : array
- Result of the dice roll.
- Returns
- -------
- None.
- """
- # dice sides in ascii
- PRETTY_DICE = {
- 1: (
- "┌─────────┐",
- "│ │",
- "│ ● │",
- "│ │",
- "└─────────┘",
- ),
- 2: (
- "┌─────────┐",
- "│ ● │",
- "│ │",
- "│ ● │",
- "└─────────┘",
- ),
- 3: (
- "┌─────────┐",
- "│ ● │",
- "│ ● │",
- "│ ● │",
- "└─────────┘",
- ),
- 4: (
- "┌─────────┐",
- "│ ● ● │",
- "│ │",
- "│ ● ● │",
- "└─────────┘",
- ),
- 5: (
- "┌─────────┐",
- "│ ● ● │",
- "│ ● │",
- "│ ● ● │",
- "└─────────┘",
- ),
- 6: (
- "┌─────────┐",
- "│ ● ● │",
- "│ ● ● │",
- "│ ● ● │",
- "└─────────┘",
- ),
- }
- # dice height (rows) and separator (sep)
- DICE_ROWS = len(PRETTY_DICE[1])
- DICE_SEP = ' '
- pretty_faces = []
- for n in crew:
- pretty_faces.append(PRETTY_DICE[n])
- dice_faces_rows = []
- for row_id in range(DICE_ROWS):
- row_components = []
- for die in pretty_faces:
- row_components.append(die[row_id])
- row_string = DICE_SEP.join(row_components)
- dice_faces_rows.append(row_string)
- width = len(dice_faces_rows[0])
- diagram_header = " CREW ".center(width, "~")
- dice_faces_diagram = "\n".join([diagram_header] + dice_faces_rows)
- # the result is shown
- print(f"{dice_faces_diagram}\n")
- def scan_threats(crew, scant, deck):
- """
- loops through the 'crew' array to get the number of sixes.
- If 'scant' plus that number is less than 3 the variable 'scant'
- is updated; If it is equal to or greater than 3, it calculates
- the module of that value between 3 and saves it in 'scant'
- and draws a new threat card [new_threat()].
- It will have to do this as many times as the integer part
- indicates by dividing the number of scanners by 3
- Parameters
- ----------
- crew : array
- Result of the dice roll.
- scant : int
- Number of busy scanners. Value between 0 and 2.
- Returns
- -------
- scant : int
- Updated number of busy scanners. Value between 0 and 2.
- """
- # 'n6' will store the number of sixes in 'crew'
- n6 = 0
- for i in crew:
- if i == 6:
- n6 += 1
- # The number of scanners is updated
- scant = scant + n6
- # If 'scant' is equal to or greater than 3,
- # I draw as many threat cards as groups of 3
- # have been obtained and I update scant
- # with the rest of the division
- if scant >= 3:
- rango = scant // 3
- for n in range(rango):
- new_threat(deck)
- scant = scant % 3
- return(scant)
- def commander(crew):
- """
- Actions to perform with each '1' obtained in the roll.
- It must ask if you want to change any crew member or re-roll
- available crew.
- It will need to validate all user input.
- It needs the crew array and returns it modified
- Parameters
- ----------
- crew : array
- Result of the dice roll.
- Returns
- -------
- crew : array
- Dice roll result modified by commander.
- """
- print('Commander available:')
- # The key pressed by the user is stored in 'c'.
- c = 'x'
- oldcrew = ''
- # Prompt until a valid value is obtained
- while c.lower() != 'c' and c.lower() != 'r':
- c = input("Do you want to change any members of your crew (c) \n \
- or re-roll de dice (r)? c/r\n")
- if c.lower() == 'c':
- # At this point 'c' is used to validate that the number is on the
- # 'crew' (an unavailable crew member cannot be changed)
- c = 'n'
- while c == 'n':
- # 'cont' stores the position in the array in which
- # the indicated value is located
- cont = 0
- oldcrew = int(input("Number of the crew member to be replaced:"))
- # If a 6 (scanner) has been indicated, it moves on to the next
- #iteration without doing anything else (continues requesting
- # a crew member until a valid value is received)
- if oldcrew == 6:
- continue
- for i in crew:
- if i == oldcrew:
- # If the entered value is available the new value is requested,
- # making sure that a value between 2 and 5 is indicated.
- newcrew = 1
- while newcrew < 2 or newcrew > 5:
- newcrew = int(input("Value of the new crew member (2-5)"))
- crew[cont] = newcrew
- # 'c' is updated to stop prompting.
- c = ''
- break
- cont += 1
- elif c.lower() == 'r':
- ncrew = []
- cont = 0
- while cont < len(crew):
- if crew[cont] == 1 or crew[cont] == 6:
- ncrew.append(crew[cont])
- else:
- ncrew.append(randint(1,6))
- cont += 1
- # Crew update
- crew = ncrew
- if c == 'c': print(f'Successfully changed {oldcrew} to {newcrew}')
- print(f"Nueva tripulación: {crew}")
- pretty_shown_crew(crew)
- return(crew)
- def assign_crew(crew):
- """
- If there are commanders in the roll, it is responsible for updating the
- crew and, in any case, calls the functions of each crew member with
- the number of them available.
- Parameters
- ----------
- crew : array
- Result of the dice roll.
- Returns
- -------
- None.
- """
- # The new crew
- ncrew = []
- # Number of crew members of each type
- tacticn = 0
- doctorn = 0
- scientistn = 0
- engineern = 0
- # in search of the commander
- for i in range(len(crew)):
- if crew[i] == 1: ncrew = commander(crew)
- # Crew update if modified
- if len(ncrew) != 0 : crew = ncrew
- # For the rest of the tasks, the appropriate functions are called
- # with the number of crew members of each type.
- for i in range(len(crew)):
- if crew[i] == 2: tacticn += 1
- elif crew[i] == 3: doctorn += 1
- elif crew[i] == 4: scientistn += 1
- elif crew[i] == 5: engineern += 1
- if tacticn > 0: tactic(tacticn)
- if doctorn > 0: doctor(doctorn)
- if scientistn > 0: scientist(scientistn)
- if engineern > 0: engineer(engineern)
- # MAIN
- def main_program():
- deck = list(range(0,9))
- print(deck)
- i = 0
- scant = 0
- crewn = 6
- while i < len(deck):
- clear_screen()
- print(f'\n\nRound #{i + 1}')
- print("1:\nHere is the dice roll")
- crew = generate_crew(crewn)
- print(crew)
- pretty_shown_crew(crew)
- scant = scan_threats(crew, scant, deck)
- print(f"2:\nYou have {scant} scanners in use and {crewn} crew\n \
- members available for the next round")
- print("3:")
- assign_crew(crew)
- print(crew)
- print("4:")
- deck = new_threat(deck)
- print(f"{len(deck)} card(s) remaining")
- print("5:")
- activate_threat()
- crewn = 6 - scant
- if crewn < 1:
- print('You lost!')
- return(0)
- print(f"6:\n{crewn} crew members available")
- i += 1
- c = input("Press enter to start the next round")
- print('Won!')
- c = input('Press enter to exit')
- return(0)
- main_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement