Advertisement
Guest User

Beta Decay Puzzle

a guest
Aug 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.09 KB | None | 0 0
  1. # Changes from version 2
  2. # ----------------------------------------
  3. # - Added instructions
  4. # - Renamed text files
  5. # - Restructured code to make it more readable
  6. # - Added .lower() on yes or no questions
  7.  
  8. # Import random and time modules
  9. import random, time
  10.  
  11. # Define procedure cls()
  12. def cls():
  13.     # Clears the screen by printing 100 newlines
  14.     print("\n"*100)
  15.  
  16. # Define function grid_maker()
  17. def grid_maker(array):
  18.     # Takes array and converts it into a square grid
  19.     # Check length of array
  20.     if len(array) == 9:
  21.         sidelen = 3
  22.     elif len(array) == 16:
  23.         sidelen = 4
  24.     else:
  25.         # Array will not form square grid, is too large or is too small
  26.         # Throw an error and exit program
  27.         raise ValueError("Array passed to grid_maker() is wrong length")
  28.  
  29.     # Form grid
  30.     grid = []
  31.  
  32.     #  Set index to 0
  33.     index = 0
  34.  
  35.     # Iterate through array
  36.     for i in range(sidelen):
  37.         # Insert an empty list into grid
  38.         grid.append([])
  39.  
  40.         # Iterate through grid
  41.         for j in range(sidelen):
  42.             # Add word to array in grid
  43.             grid[i].append(array[index])
  44.  
  45.             # Increment the index
  46.             index += 1
  47.  
  48.     # Return the grid
  49.     return grid
  50.  
  51. # Define procedure display_grid()
  52. def display_grid(grid):
  53.     # Takes a two dimensional array and displays it cleanly
  54.     # Iterate through the list
  55.     for i in grid:
  56.         # Create an empty string
  57.         line = ""
  58.  
  59.         # Iterate through list i
  60.         for j in i:
  61.             # Append list item j to line
  62.             line += j
  63.  
  64.             # Add a space to line
  65.             line += " "
  66.  
  67.         # Print line
  68.         print(line)
  69.  
  70. # Define function play()
  71. def play(filename, grid_size, delay):
  72.     # Play game
  73.     # Open text file containing easy words
  74.     file = open(filename, "r")
  75.  
  76.     # Read text file
  77.     read_file = file.read()
  78.  
  79.     # Close file
  80.     file.close()
  81.  
  82.     # Split file into an array
  83.     words = read_file.split('\n')
  84.  
  85.     # Remove empty string from end of words
  86.     words = words[:grid_size+1]
  87.    
  88.     # Shuffle words
  89.     random.shuffle(words)
  90.  
  91.     # Select the first grid_size words for the grid
  92.     grid_words = words[:grid_size]
  93.  
  94.     # Convert grid_words into a n x n grid
  95.     grid = grid_maker(grid_words)
  96.  
  97.     # Insert new line for aesthetics
  98.     print("\n")
  99.    
  100.     # Call display_grid() with grid
  101.     display_grid(grid)
  102.  
  103.     # Wait for delay seconds
  104.     time.sleep(delay)
  105.  
  106.     # Clear the screen
  107.     cls()
  108.  
  109.     # Store question answers
  110.     replaced = grid_words[0]
  111.     replacement = words[-1]
  112.    
  113.     # Replace the first word in the grid with the last word in words
  114.     grid_words[0] = replacement
  115.  
  116.     # Shuffle grid of words
  117.     random.shuffle(grid_words)
  118.  
  119.     # Convert grid_words into grid
  120.     grid = grid_maker(grid_words)
  121.  
  122.     # Insert new line for aesthetics
  123.     print("\n")
  124.    
  125.     # Call display_grid() with grid
  126.     display_grid(grid)
  127.  
  128.     # Set the number of guesses made by the user to zero
  129.     guesses = 0
  130.  
  131.     # Loop until guesses reach 3
  132.     while guesses < 3:
  133.         # Ask user the question
  134.         user_guess = input("\n\nWhich word has been replaced? ").upper()
  135.  
  136.         # Increment the guess counter
  137.         guesses += 1
  138.  
  139.         # Check if user's guess is correct
  140.         if user_guess == replaced:
  141.             # User guessed correctly
  142.             # Print congratulations message
  143.             print("Correct answer!")
  144.  
  145.             correct = True
  146.             # Break out of while loop
  147.             break
  148.  
  149.         else:
  150.             # User guessed incorrectly
  151.             # Print message
  152.             print("Incorrect answer")
  153.  
  154.     # Has user made three incorrect answers?
  155.     if guesses == 3:
  156.         print("\nYou have made three incorrect guesses: game over")
  157.  
  158.         # Give user the chance to play again
  159.         again = input("\n\nDo you wish to play again? (y/n) ").lower()
  160.  
  161.         # Has user input "y" or "n"?
  162.         if again == "y":
  163.             # User chose to play again
  164.             # Call play() function
  165.             play(filename, grid_size, delay)
  166.  
  167.         else:
  168.             # User chose to exit
  169.             # Print farewell message
  170.             print("\nGoodbye and thanks for playing")
  171.  
  172.             # End function
  173.             return 1
  174.  
  175.         # Exit out of function
  176.  
  177.     # Reset the number of guesses made by the user to zero
  178.     guesses = 0
  179.  
  180.     # Loop until guesses reach 3
  181.     while guesses < 3:
  182.         # Ask user the question
  183.         user_guess = input("\n\nWhich word is the replacement? ").upper()
  184.  
  185.         # Increment the guess counter
  186.         guesses += 1
  187.  
  188.         # Check if user's guess is correct
  189.         if user_guess == replacement:
  190.             # User guessed correctly
  191.             # Print congratulations message
  192.             print("Correct answer!")
  193.  
  194.             # Break out of while loop
  195.             break
  196.  
  197.         else:
  198.             # User guessed incorrectly
  199.             # Print message
  200.             print("Incorrect answer")
  201.  
  202.     # Has user made three incorrect answers?
  203.     if guesses == 3:
  204.         print("\nYou have made three incorrect guesses: game over")
  205.  
  206.         # Give user the chance to play again
  207.         again = input("\n\nDo you wish to play again? (y/n) ")
  208.  
  209.         # Has user input "y" or "n"?
  210.         if again == "y":
  211.             # User chose to play again
  212.             # Call play() function
  213.             play(filename, grid_size, delay)
  214.  
  215.         else:
  216.             # User chose to exit
  217.             # Print farewell message
  218.             print("Goodbye and thanks for playing")
  219.  
  220.             # End function
  221.             return 1
  222.  
  223.         # Exit out of function
  224.  
  225.     # Congratulate the user
  226.     print("\nCongratulations, you've won!")
  227.    
  228.     # Ask user if they want to play again or not
  229.     again = input("\n\nDo you wish to play again? (y/n) ").lower()
  230.  
  231.     # Has user input "y" or "n"?
  232.     if again == "y":
  233.         # User chose to play again
  234.         # Call play() function
  235.         play(filename, grid_size, delay)
  236.  
  237.     else:
  238.         # User chose to exit
  239.         # Print farewell message
  240.         print("\nGoodbye and thanks for playing")
  241.  
  242.         # End function
  243.         return 1
  244.  
  245. def instructions():
  246.     # Print instructions for the user
  247.     # Print instructions for the easy mode
  248.     print("""
  249. Instructions:
  250.  
  251. == EASY MODE ==
  252.  
  253. If you choose to play in easy mode,
  254. you will be shown a 3 by 3 grid of
  255. nine words. After a 30 second pause
  256. the screen will clear and another
  257. grid will be shown. This new grid
  258. will be the same as the one before
  259. except one of the words will have
  260. been replaced.
  261.  
  262. The aim of the game is to guess
  263. which word has been replaced and
  264. which word is the replacement. You
  265. have three guesses per question.""")
  266.  
  267.     # Time delay of 30 seconds to give user time to read the instructions
  268.     time.sleep(30)
  269.  
  270.     # Print instructions for the hard mode
  271.     print("""
  272. == HARD MODE ==
  273.  
  274. This is just the same as easy mode
  275. with just a few differences:
  276.  
  277. - The grid is a 4 by 4 grid of 16
  278.   words
  279.  
  280. - There is a 50 second pause before
  281.   the screen clears""")
  282.    
  283. # Start
  284.  
  285. # Ask user if they wants to see the instructions or not
  286. instruction_choice = input("Do you want to see the instructions? (y/n) ").lower()
  287.  
  288. # Check user's choice
  289. if instruction_choice == "y":
  290.     # User chose to see instructions
  291.     # Call function instructions()
  292.     instructions()
  293.  
  294. else:
  295.     # Do nothing
  296.     pass
  297.  
  298. # Get user's choice
  299. mode_choice = input("\n\nDo you wish to run 'easy' mode or 'hard' mode? ")
  300.  
  301. # Interpret user's choice
  302. if mode_choice == "easy":
  303.     # Set easy parameters
  304.     # Set filename containing easy words
  305.     filename = "Words.txt"
  306.  
  307.     # Set grid size
  308.     grid_size = 9
  309.  
  310.     # Set delay
  311.     delay = 30
  312.  
  313. elif mode_choice == "hard":
  314.     # Set hard parameters
  315.     # Set filename containing hard words
  316.     filename = "WordsExt.txt"
  317.  
  318.     # Set grid size
  319.     grid_size = 16
  320.  
  321.     # Set delay
  322.     delay = 50
  323.  
  324. else:
  325.     # Invalid input
  326.     print("Invalid input")
  327.  
  328.     # Exit game
  329.     exit()
  330.  
  331. # Start game
  332. play(filename, grid_size, delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement