holiday_bingo/generate_card.py

182 lines
5.0 KiB
Python

# This is a comment. Comments begin with a '#'
# To run this program, call:
# python3 generate_card.py
# To save the output, call:'
# python3 generate_card.py > card.txt
# This will save the output in a file named 'card.txt' that you can print.
# You can name the file anything you want.
# print a 5 x 5 bingo grid with words
# text file format
#-----------
#| | | | | |
#-----------
#| | | | | |
#-----------
#| | |x| | |
#-----------
#| | | | | |
#-----------
#| | | | | |
#-----------
# This is an import. This is how we can pull in code from modules
# that have already been written
import random
import textwrap
# We will set some constant values that will not change but which multiple
# functions will use.
# They are called constants because their value will not change. Using
# all caps is how we name constants.
SQUARE_HEIGHT = 5
SQUARE_LENGTH = 14
SQUARES_PER_ROW = 5
ROWS = 5
# This is assigning an array of "strings" to the variable squares.
# We will pull values for the bingo squares from here.
# A 'string' is a series of characters enclosed in quotes
squares = [
"Santa vists",
"Lead is royalty",
"Gingerbread",
"Empty coffee cups",
"Hot chocolate",
"Confession of a lie or mistake",
"Evil developers",
"Mistletoe!",
"Saving a landmark",
"Interrupted smooch",
"Fake relationship",
"Lead who works in real estate",
"Big romantic gesture",
"Secret identity",
"Unexpected visitor",
"Not a couple mistaken as a couple",
"Untrustworthy person in a suit",
"US Flag",
"Literal fall into someone's arms",
"Someone returns to hometown",
"Christmas market",
"Snowstorm",
"Friends to lovers",
"Christmas wedding",
"Pregnant!",
"Family heirloom",
"Living in home you can't afford",
"Charity event",
"Homemade decorations",
"Someone gets a promotion",
"Unreasonable deadline",
"Flirty outdoor winter activity",
"Prevented from attending or staying at event",
"Breakup",
"Failed romantic gesture",
"Lead who owns a business",
"Real snow",
"Contract or business deal",
"Red coat",
"A misunderstanding",
"Giant, shiny big city offices",
"Change of heart",
"Empty gift boxes or bags",
"Snow falling",
"Empty coffee cups",
"Precocious child",
]
# This is a function. Functions are defined with the 'def' keyword.
# def 'function_name' ('parameters')
# If there are no parameters, use an empty ()
# functions make it easier to do the same thing over again.
def make_dash_line (): # Python uses indentation to organize code
output = "+"
for i in range(SQUARES_PER_ROW):
for j in range(SQUARE_LENGTH):
output += "="
output += "+"
output += "\n" # Here we are still inside the function, but outside
return output # the loop
# This function will take the row of the square, and the string and
# print out the padded section of the string for this line
def get_square_text(row, s):
wrapped_strings = textwrap.wrap(str(s),width=SQUARE_LENGTH)
output = ""
if row < len(wrapped_strings):
string_section = wrapped_strings[row]
else:
string_section = " "
return output + string_section.center(SQUARE_LENGTH)
# The function print_row will take 5 strings and print a bingo row
def make_row (row_strings):
# each square will be 5 rows high and 12 chars across. We can adjust
# this to make the layout better
output = ""
for i in range(SQUARE_HEIGHT):
#For each line in the row, get the part of the string
#Which will be printed on this line
for j in range(SQUARES_PER_ROW):
output += "|"
output += get_square_text(i, row_strings[j])
output += "|\n"
return output
def print_card():
# At random, pull values from "squares" and print out 5 rows of bingo card
selected = [-1]
output = ""
for i in range(ROWS):
output += make_dash_line()
# get 5 new picks
picks = []
for j in range( SQUARES_PER_ROW ):
# If we have the center square, set "Free"
if i == 2 and j == 2:
picks.append("Free")
continue
# Keep picking a number until we get one we haven't picked yet
pick = -1
while pick in selected:
pick = random.randrange(0,len(squares))
# Record that we have picked this item
selected.append(pick)
picks.append(squares[pick])
# Now print the row with those picks
output += make_row(picks)
# Print last bottom line
output += make_dash_line()
return output
card = print_card()
with open("card.txt", "w") as f:
f.write(card)
print("Bingo card has been created as card.txt")
input("Enter to exit...")