Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Changes from version 2
- # ----------------------------------------
- # - Added instructions
- # - Renamed text files
- # - Restructured code to make it more readable
- # - Added .lower() on yes or no questions
- # Import random and time modules
- import random, time
- # Define procedure cls()
- def cls():
- # Clears the screen by printing 100 newlines
- print("\n"*100)
- # Define function grid_maker()
- def grid_maker(array):
- # Takes array and converts it into a square grid
- # Check length of array
- if len(array) == 9:
- sidelen = 3
- elif len(array) == 16:
- sidelen = 4
- else:
- # Array will not form square grid, is too large or is too small
- # Throw an error and exit program
- raise ValueError("Array passed to grid_maker() is wrong length")
- # Form grid
- grid = []
- # Set index to 0
- index = 0
- # Iterate through array
- for i in range(sidelen):
- # Insert an empty list into grid
- grid.append([])
- # Iterate through grid
- for j in range(sidelen):
- # Add word to array in grid
- grid[i].append(array[index])
- # Increment the index
- index += 1
- # Return the grid
- return grid
- # Define procedure display_grid()
- def display_grid(grid):
- # Takes a two dimensional array and displays it cleanly
- # Iterate through the list
- for i in grid:
- # Create an empty string
- line = ""
- # Iterate through list i
- for j in i:
- # Append list item j to line
- line += j
- # Add a space to line
- line += " "
- # Print line
- print(line)
- # Define function play()
- def play(filename, grid_size, delay):
- # Play game
- # Open text file containing easy words
- file = open(filename, "r")
- # Read text file
- read_file = file.read()
- # Close file
- file.close()
- # Split file into an array
- words = read_file.split('\n')
- # Remove empty string from end of words
- words = words[:grid_size+1]
- # Shuffle words
- random.shuffle(words)
- # Select the first grid_size words for the grid
- grid_words = words[:grid_size]
- # Convert grid_words into a n x n grid
- grid = grid_maker(grid_words)
- # Insert new line for aesthetics
- print("\n")
- # Call display_grid() with grid
- display_grid(grid)
- # Wait for delay seconds
- time.sleep(delay)
- # Clear the screen
- cls()
- # Store question answers
- replaced = grid_words[0]
- replacement = words[-1]
- # Replace the first word in the grid with the last word in words
- grid_words[0] = replacement
- # Shuffle grid of words
- random.shuffle(grid_words)
- # Convert grid_words into grid
- grid = grid_maker(grid_words)
- # Insert new line for aesthetics
- print("\n")
- # Call display_grid() with grid
- display_grid(grid)
- # Set the number of guesses made by the user to zero
- guesses = 0
- # Loop until guesses reach 3
- while guesses < 3:
- # Ask user the question
- user_guess = input("\n\nWhich word has been replaced? ").upper()
- # Increment the guess counter
- guesses += 1
- # Check if user's guess is correct
- if user_guess == replaced:
- # User guessed correctly
- # Print congratulations message
- print("Correct answer!")
- correct = True
- # Break out of while loop
- break
- else:
- # User guessed incorrectly
- # Print message
- print("Incorrect answer")
- # Has user made three incorrect answers?
- if guesses == 3:
- print("\nYou have made three incorrect guesses: game over")
- # Give user the chance to play again
- again = input("\n\nDo you wish to play again? (y/n) ").lower()
- # Has user input "y" or "n"?
- if again == "y":
- # User chose to play again
- # Call play() function
- play(filename, grid_size, delay)
- else:
- # User chose to exit
- # Print farewell message
- print("\nGoodbye and thanks for playing")
- # End function
- return 1
- # Exit out of function
- # Reset the number of guesses made by the user to zero
- guesses = 0
- # Loop until guesses reach 3
- while guesses < 3:
- # Ask user the question
- user_guess = input("\n\nWhich word is the replacement? ").upper()
- # Increment the guess counter
- guesses += 1
- # Check if user's guess is correct
- if user_guess == replacement:
- # User guessed correctly
- # Print congratulations message
- print("Correct answer!")
- # Break out of while loop
- break
- else:
- # User guessed incorrectly
- # Print message
- print("Incorrect answer")
- # Has user made three incorrect answers?
- if guesses == 3:
- print("\nYou have made three incorrect guesses: game over")
- # Give user the chance to play again
- again = input("\n\nDo you wish to play again? (y/n) ")
- # Has user input "y" or "n"?
- if again == "y":
- # User chose to play again
- # Call play() function
- play(filename, grid_size, delay)
- else:
- # User chose to exit
- # Print farewell message
- print("Goodbye and thanks for playing")
- # End function
- return 1
- # Exit out of function
- # Congratulate the user
- print("\nCongratulations, you've won!")
- # Ask user if they want to play again or not
- again = input("\n\nDo you wish to play again? (y/n) ").lower()
- # Has user input "y" or "n"?
- if again == "y":
- # User chose to play again
- # Call play() function
- play(filename, grid_size, delay)
- else:
- # User chose to exit
- # Print farewell message
- print("\nGoodbye and thanks for playing")
- # End function
- return 1
- def instructions():
- # Print instructions for the user
- # Print instructions for the easy mode
- print("""
- Instructions:
- == EASY MODE ==
- If you choose to play in easy mode,
- you will be shown a 3 by 3 grid of
- nine words. After a 30 second pause
- the screen will clear and another
- grid will be shown. This new grid
- will be the same as the one before
- except one of the words will have
- been replaced.
- The aim of the game is to guess
- which word has been replaced and
- which word is the replacement. You
- have three guesses per question.""")
- # Time delay of 30 seconds to give user time to read the instructions
- time.sleep(30)
- # Print instructions for the hard mode
- print("""
- == HARD MODE ==
- This is just the same as easy mode
- with just a few differences:
- - The grid is a 4 by 4 grid of 16
- words
- - There is a 50 second pause before
- the screen clears""")
- # Start
- # Ask user if they wants to see the instructions or not
- instruction_choice = input("Do you want to see the instructions? (y/n) ").lower()
- # Check user's choice
- if instruction_choice == "y":
- # User chose to see instructions
- # Call function instructions()
- instructions()
- else:
- # Do nothing
- pass
- # Get user's choice
- mode_choice = input("\n\nDo you wish to run 'easy' mode or 'hard' mode? ")
- # Interpret user's choice
- if mode_choice == "easy":
- # Set easy parameters
- # Set filename containing easy words
- filename = "Words.txt"
- # Set grid size
- grid_size = 9
- # Set delay
- delay = 30
- elif mode_choice == "hard":
- # Set hard parameters
- # Set filename containing hard words
- filename = "WordsExt.txt"
- # Set grid size
- grid_size = 16
- # Set delay
- delay = 50
- else:
- # Invalid input
- print("Invalid input")
- # Exit game
- exit()
- # Start game
- play(filename, grid_size, delay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement