I'm trying to get a game about The Royal family that consists of three mini games. Separately my mini games work but when I put them together I can't seem to get them to flow from beginning to end. My knowledge isn't the best and am trying hard to become good at programming, so apologies for the mess it is in.
My Code
#Welcome and Name
name = input("Welcome to Save the Monarchy! What is your name?")
print ("Hello,", name)
#Explanation
print ("""
You will have to complete 3 mini game Challenges and a final mission in
order to save The Queen!
Every mini game has a prince that needs to be saved!
You must win all games to save the Queen!
""")
print(name,)
#start game or quit
response = input("Are you willing to save the Monarchy? Y/N \n")
if response == "Y" or response == "y":
print("Starting game...")
elif response == "N" or response == "n":
print("You have let Great Britain down, GOODBYE!")
quit()
else:
print("You did not input a valid response!")
input("""\n\n
Press the Y key to continue
Press the N key to exit
""")
#Game 1
#Noughts and Crosses
#Noughts and Crosses
def instructions():
"""Display game instructions."""
print("Here are the instructions to Noughts and Crosses:")
instructions()
print(
"""
Welcome to Noughts and Crosses!
Your mission in this game is to win or draw with the computer to save Prince Harry.
To win games you will need to make your move known by entering a number, 0-8.
The number will correspond to the board position as illustrated:
0|1|2
-----
3|4|5
-----
6|7|8
Take your time and make sure you win. GOOD LUCK!!! \n
"""
)
from random import randint
from time import sleep
from copy import deepcopy
#Setup game
def main():
#Set default board values
global board
board = [[0, " 0 "], [1, " 1 "], [2," 2 "], [3," 3 "], [4," 4 "], [5," 5 "], [6," 6 "], [7," 7 "], [8," 8 "]]
#Sets default counter value:
global counter
counter = 0
#Calls function to draw board
drawBoard()
#Draw game board
def drawBoard():
#Draws game board with proper variables
print(board[0][1] + "|"+ board[1][1] +"|" + board[2][1] + "\n---+---+--- \n" + board[3][1] +"|" + board[4][1] + "|"
+ board[5][1] + "\n---+---+--- \n" + board[6][1] + "|" + board[7][1] + "|" + board[8][1] + "\n")
#After board has been updated, initiates next turn
nextTurn(counter)
#Execute the next turn
def nextTurn(count):
#If count is divisible by two, let the player make a move and increase turn counter
if count % 2 == 0:
global counter
counter += 1
playerMove()
#If count is not divisible by two, let the cmputer make a move and increase turn counter
else:
counter += 1
computerMove()
#Code for the player's move
def playerMove():
#Get player input and try to convert it to an integer. If it cannot be, prompt user to enter again
try:
boardSpot = int(input("It's your turn! Please select an empty space on the board by typing a number 1-9\n"))
except ValueError:
playerMove()
#If the space is empty, proceed
if board[boardSpot][1] != " X " and board[boardSpot][1] != " O " :
#Set board spot to X and redraw the board
board[boardSpot][1] = " X "
if checkWinner(board, " X ") == True:
print("You have won and saved Prince Harry! \n")
endOfGame()
checkTie(board)
drawBoard()
#Else, let them input a new number by recalling playerMove
else:
playerMove()
def computerMove():
#Display some dialouge alerting the user that the computer is making its move
print("It's the computer's Turn!")
sleep(1)
print("Thinking...")
sleep(1)
#For loop iterating through every board spot. Checks to see if bot can win. If it can, make that winning move!
for boardSpot in range (0,9):
#Makes a copy of the board
boardCopy = deepcopy(board)
#If boardspot is empty, continue with the check. Else, move onto the next spot!
if boardCopy[boardSpot][1] != " X " and boardCopy[boardSpot][1] != " O ":
boardCopy[boardSpot][1] = " O "
if checkWinner(boardCopy, " O "):
board[boardSpot][1] = " O "
print("You have lost to the Computer! \n")
endOfGame()
drawBoard()
return
#For loop iterating through every board spot. Checks to see if player can win. If it can, prevent him from winning!
for boardSpot in range(0,9):
#Makes a copy of the board
boardCopy = deepcopy(board)
#If boardspot is empty, continue with the check. Else, move onto the next spot!
if boardCopy[boardSpot][1] != " X " and boardCopy[boardSpot][1] != " O ":
boardCopy[boardSpot][1] = " X "
if checkWinner(boardCopy, " X "):
board[boardSpot][1] = " O "
drawBoard()
return
#If code has progressed this far, simply chose a random empty space
randomMove = True
while randomMove:
boardSpot = randint(0,8)
if board[boardSpot][1] != " X " and board[boardSpot][1] != " O ":
board[boardSpot][1] = " O "
randomMove = False
drawBoard()
#Check if someone has won
def checkWinner(brd, lttr):
return ((brd[6][1] == lttr and brd[7][1] == lttr and brd[8][1] == lttr) or # across bottom
(brd[3][1] == lttr and brd[4][1] == lttr and brd[5][1] == lttr) or # across middle
(brd[0][1] == lttr and brd[1][1] == lttr and brd[2][1] == lttr) or # across top
(brd[6][1] == lttr and brd[3][1] == lttr and brd[0][1] == lttr) or # down left side
(brd[7][1] == lttr and brd[4][1] == lttr and brd[1][1] == lttr) or # down middle
(brd[8][1] == lttr and brd[5][1] == lttr and brd[2][1] == lttr) or # down right side
(brd[6][1] == lttr and brd[4][1] == lttr and brd[2][1] == lttr) or # diagonal
(brd[8][1] == lttr and brd[4][1] == lttr and brd[0][1] == lttr)) # diagonal
#Check for tie
def checkTie(brd):
emptySpaces = 0
#Loop through all the board spaces. If the board space is not X or O, then it is free.
for x in brd:
if x[1] != " O " and x[1] != " X ":
emptySpaces += 1
#Full board
if emptySpaces == 0:
print("The board is full! You have tied! Well Done, Prince Harry is saved \n")
endOfGame()
#Either start a new game or quit the program depending on player input
def endOfGame():
response = input("Would you like to continue? Y/N \n")
if response == "Y" or response == "y":
input("\n\nPress enter to continue")
elif response == "N" or response == "n":
################################################################################################################################
#Game 2
#Word Jumble mini game
################################################################################################################################
#The computer picks a random word and it's up to the player to unscamble
import random
#random words
WORDS = ("MONARCH", "PRINCE", "QUEEN", "ROYALTY","HER ROYAL HIGHNESS", "HIS ROYAL HIGHNESS", "KING", "BRITAIN", "WILLIAM", "HARRY", "DUKE OF ENDINBURGH", "MA'AM", "SIR", "CORGI", "PRINCESS", "MAJESTY")
#Setting the score to zero
score = 0
score = int(score) #Converts the 0 into a number so we can add scores
#Start Game
print ("""Welcome to Word Jumble!
You must unscramble the 2 out of 3 words.
If you fail to unscramble the words Prince William will die
""")
print ('Important : AS THIS IS A VERY IMPORTANT CHALLENGE, PRINCE WILLIAM WILL BE OUR KING IN YEARS TO COME, ALL ANSWERS MUST BE IN CAPITAL LETTERS!!!')
print ('\n-----------------------------------------------------------\n')
# pick one word randomly from the sequence
jumble = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
#Question 1 ----------------------------------------------------
print ("The jumbled word is:", jumble)
#guessing
guess = input("\nYour guess:")
while guess != correct and guess !="":
print("Sorry, that is not right, try again")
#While loop
guess = input("\nYour guess:")
count = 0
while count<10:
if guess == correct:
print("Correct!\n")
score = score + 1
print ('Your current score is ' + str(score) + ' out of 3')
print ('\n-----------------------------------------------------------\n')
break
if guess == incorrect:
print("Wrong!Prince William's life is in your hands!")
count = count +1
#Question 2 ----------------------------------------------------
import random
#random words
WORDS = ("MONARCH", "PRINCE", "QUEEN", "ROYALTY","HER ROYAL HIGHNESS", "HIS ROYAL HIGHNESS", "KING", "BRITAIN", "WILLIAM", "HARRY", "DUKE OF ENDINBURGH", "MA'AM", "SIR", "CORGI", "PRINCESS", "MAJESTY")
# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print ("The jumbled word is:", jumble)
#guessing
guess = input("\nYour guess:")
while guess != correct and guess !="":
print("Sorry, that is not right, try again")
#While loop
guess = input("\nYour guess:")
count = 0
while count<10:
if guess == correct:
print("Correct!\n")
score = score + 1
print ('Your current score is ' + str(score) + ' out of 3')
print ('\n-----------------------------------------------------------\n')
break
if guess == incorrect:
print("Wrong!Prince William is dead!")
count = count +1
#Question 3 ----------------------------------------------------
import random
#random words
WORDS = ("MONARCH", "PRINCE", "QUEEN", "ROYALTY","HER ROYAL HIGHNESS", "HIS ROYAL HIGHNESS", "KING", "BRITAIN", "WILLIAM", "HARRY", "DUKE OF ENDINBURGH", "MA'AM", "SIR", "CORGI", "PRINCESS", "MAJESTY")
# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print ("The jumbled word is:", jumble)
#guessing
guess = input("\nYour guess:")
while guess != correct and guess !="":
print("Sorry, that is not right, try again")
#While loop
guess = input("\nYour guess:")
count = 0
while count<10:
if guess == correct:
print("Correct!You have saved Prince William's life! CONGRATULATIONS\n!")
score = score + 1
print ('Your current score is ' + str(score) + ' out of 3')
print ('\n-----------------------------------------------------------\n')
break
if guess == incorrect:
print("You failed! Prince William is dead and it's your fault!")
count = count +1
###################################################################
#Game 3
# Quiz
###################################################################
# Defining Score variables
x = 0
score = x
#Introduction
print("""
Your final challenge is to get at least 50% correct to save Prince Charles (the prince next in line for the throne).
By getting this far you would have already saved Prince William and Prince Harry.
If you succeed this mini quiz not only will you have saved all three Prince's but you'll be one step away from saving the queen!
""")
# Question One
print("When was the Queen crowned")
answer_1 = input("a)1901\nb)1950\nc)1942\nd)1953\n:")
if answer_1.lower() == "d" or answer_1.lower() == "1953":
print("Correct")
x = x + 1
else:
print("Incorrect, Her Royal Highness was crowned in 1953")
# Question Two
print("Who was Prince Charles's first wife?")
answer_2 = input("a)Camilla, Duchess of Cornwall\nb)Princess Diana\nc)Lady Amanda Knatchbull\nd)Anna Wallace\n:")
if answer_2.lower() == "b" or answer_2.lower() == "Princess Diana" or answer_2.lower () == "Diana" :
print("Correct")
x = x + 1
else:
print("Incorrect, Princess Diana was Prince Charles's first wife")
# Question Three
print("True or False... The Queen was never meant to be on the throne")
answer_3 = input(":")
if answer_3.lower() == "true" or answer_3.lower() == "t":
print("Correct")
x = x + 1
else:
print("Incorrect, The Queens father,George VI, only became King as his older brother was abdicated")
# Question Four
print("When did the Duke of Endinburgh,Prince Phillip, stop flying?")
answer_4 = input("a)11.08.1997\nb)12.09.1999\nc)11.08.1979\nd)15.10.1998\n:")
if answer_4.lower() == "a" or answer_4 == "1997" or answer_4 == "11.08.1997":
print("Correct")
x = x + 1
else:
print("Incorrect, The Duke's final flight was on 11th August 1997 from Carlisle to Islay")
# Question Five
print("""
True or False... Lord Louis Mountbatten once said to Prince Charles,
'You can’t possibly be king with ears like that!'
""")
answer_5 = input(":")
if answer_5.lower() == "true" or answer_5.lower() == "t":
print("Correct")
x = x + 1
else:
print("""Incorrect, The statement is quoted in the official biography of Prince Charles by Jonathan Dimbleby,
from Lord Louis Mountbatten, Philip’s uncle.
""")
# Question Six
print("How many gifts did Elizabeth and Philip receive when they married in 1947?")
answer_6 = input ("a)2,500\nb)4,600\nc)10,750\nd)7,200\n//:")
if answer_6.lower () == "a" or answer_6 == "2,500" or answer_6 == "2500":
print ("Correct")
x=x+1
else:
print("""
Incorrect, 2,500. Gifts included jewels, lace made with yarn personally spun by Mahatma Gandhi,
two pairs of bed socks and a hand-knitted tea cosy
""")
# Question seven
print("True or False... The Queens first corgi was called Sarah")
answer_7 = input(":")
if answer_7.lower() == "false" or answer_7.lower() == "f":
print ("Correct")
x=x+1
else:
print("Incorrect, The first corgi to be owned by the Queen was Susan. She was given Susan on her 18th birthday.")
# Question eight
print ("The Queen famously referred to a difficult year of her reign as her “annus horribilis.” What year was she referring to?")
answer_8 = input ("""
a)1993, the year she started paying taxes\nb)1992, the year Charles and Diana separated
\nc)1997, the year Diana died\nd)2005, the year Prince Charles,her son, remarried to Camilla
""")
if answer_8.lower ()== "1992, the year Charles and Diana separated" or answer_8.lower == "1992" or answer_8.lower == "b":
print("Correct")
x=x+1
else:
print("""Incorrect,1992. It was also the year Andrew and Sarah split,
the year Princess Anne divorced her first husband and the year of a major fire at Windsor Castle.
""")
# Question nine
print (" True or False... The Queen owns all the sturgeons, porpoises and whales in the waters around the U.K.")
answer_9 = input (":")
if answer_9.lower () == "true" or answer_9.lower () == "t":
print("Correct")
x=x+1
else:
print("Incorrect, The Queen owns all the sturgeons, porpoises and whales in the waters around the U.K.")
# Question 10
print("Which of the following is the Queen’s private residence,")
answer_10 = input("a)Balmoral Castle\nb)Windsor Castle\nc)Buckingham Palace\nd)Kensington Palace")
if answer_10.lower () == "a" or answer_10.lower == "Balmoral Castle":
print("Correct")
x=x+1
else:
print("Incorrect, Balmoral Castle in the Scottish Highlands was built by Queen Victoria and is a private residence of the monarch.")
#Total Score
score = float(x / 10) * 100
print(x,"out of 10, that is",score, "%")
I'm hoping you get the idea of how it's meant to go
Aucun commentaire:
Enregistrer un commentaire