Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Kamraan H. Qureshi
- Final Program
- NEA; Task 2 - Dice Game
- 10M 10YB
- 10MS2 - Ms Grover
- Started 10/02/19
- Finished 20/02/19
- """
- """START GAME"""
- """Import's Modules"""
- #The Random library is used to generate a random integer for the randomly selected dice
- from random import randint
- #The Datetime library is used to display the date and time for the player's login
- import datetime
- #The time library is used to add pauses in the program
- import time
- #The sys library shuts down the program at the end
- import sys
- #The csv library is used to open and close .csv files and to manipulate their data by changing, adding or removing it
- import csv
- #----------------------------------------------------------------------------------------------------
- """Authentication"""
- userpass = {}#Set's 'userpass' equal to an empty dictionary
- with open("annotated_passwords.csv", "r") as csvfile: #Open's .csv file of passwords
- #.csv file is and opened by the computer and stored in a variable called 'reader'
- reader = csv.reader(csvfile)
- for i in reader:
- userpass[i[0]] = i[1] #Put's csv file contents into a dictionary
- access1 = False
- #Authenticate's Player1
- while access1 == False:
- #Ask's Player1 to enter in their username
- username1 = input("Player 1 - Input your username: ")
- if username1 in userpass: #Check's if username exists
- #Ask's Player1 to enter in their password
- password1 = input("Player 1 - Input your password: ")
- if password1 == userpass[username1]: #Check's if the password is correct
- #Display's message to tell user1 that they're now in the game
- print("Access Granted")
- #Divider
- print(" ")
- access1 = True
- else:
- #Display's message to the user if their password is wrong, and needs to be typed in again
- print("Invalid password.")
- else:
- #Display's message to the user if their username is wrong, and needs to be typed in again
- print("Invalid username.")
- access2 = False
- #Authenticate's Player2
- while access2 == False:
- #Ask's Player2 to type in their username
- username2 = input("Player 2 - Input your username: ")
- if username2 == username1:
- print("That user is already logged in")#Check's if the username is already logged in
- elif username2 in userpass: #Check's if the username exists
- #Ask's Player2 to type in their password
- password2 = input("Player 2 - Input your password: ")
- if password2 == userpass[username2]: #Check's if the password is correct
- #Display's message to tell user2 that they're now in the game
- print("Access Granted")
- #Divider
- print("--------------------")
- print(" ")
- access2 = True
- else:
- #Display's message to user2 if their password is wrong, and needs to be typed in again
- print("Invalid password.")
- else:
- #Display's message to user2 if their username is wrong, and needs to be typed in again
- print("Invalid username.")
- #----------------------------------------------------------------------------------------------------
- """Prompt's the user to play"""
- while True:
- shouldplay = False
- while True:
- #Divider
- print(" ")
- play = input("Would you like to play the game? (Y/N): ")#Ask's the Players if they want to play
- #Test's for different, but the same, responses to the question, to see if they're valid
- if play.lower() == "y":
- shouldplay = True
- break
- elif play.lower() == "n":
- break
- else:
- #If the user input isn't valid
- print("That is not a valid option")
- if not shouldplay:#If the user doesn't want to play, the program skips to the end and terminate's
- break
- """Introduce's Players to the game"""
- #Divider
- print(" ")
- print("Welcome, " + username1 + " and " + username2 + "!")
- time.sleep(1)
- #Set's the current date and time on a variable called currentDT
- currentDT = datetime.datetime.now()
- #Display's the current date and time
- print("You've logged in on the " + currentDT.strftime("%d-%m-%Y") + " @ " + currentDT.strftime("%H:%M:%S %p"))#Show's date & time of login
- #Divider
- print(" ")
- time.sleep(1)
- """Print's Rules"""
- """Goes at the user's pace, to make sure that they understood each rule and how to play the game"""
- print("The Rules are simple:")
- time.sleep(1)
- input("Press enter to continue to scroll through the rules: ")
- print(" ")
- input(" 1)Each player roles 2 6-sided dice.")
- input(" 2)The points rolled on each players dice are added to their score.")
- input(" *If the total is an even number, an additional 10 points are added to your score*")
- input(" *If the total is an odd number, 5 points are subtracted from your score*")
- input(" *A player cannot go below 0 points at any time*")
- print(" ")
- input("If you roll a double, you'll get to roll one extra die and get the number of points rolled added to your score")
- input("If you both get the same score at the end of the game, you're score will go back to 0 and you'll each roll one die until someone wins")
- input("The person with the highest score at the end of 5 rounds wins!")
- #Divider
- print("--------------------")
- print(" ")
- input("Press enter to continue: ")
- #Divider
- print(" ")
- print("--------------------")
- print(" ")
- #----------------------------------------------------------------------------------------------------
- """Set's the players' score to 0 in a variable called player1_total_score and player2_total_score"""
- player1_total_score = 0
- player2_total_score = 0
- """Main Game"""
- #Repeat's all code in "for" loop 5 times
- for i in range(5):#For loop makes everything inside of the indentation happed five times
- print("Round " + str(i + 1))#Display's the round number to the player
- #Player 1
- time.sleep(1)
- print(username1 + "'s turn")
- #Prompt's the user to roll their two dice
- input(username1 + ", press enter to roll your first pair of dice: ")
- #Retro-style game text to simulate the rolling of actual dice
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it to two variables called dice1 and dice2
- dice1 = randint(1, 6)
- dice2 = randint(1, 6)
- #Display's the two dice that the player rolled
- print("Your first dice is " + str(dice1))
- time.sleep(1)
- print("Your second dice is " + str(dice2))
- time.sleep(1)
- #Updates player1's score to include the points won in dice1 and dice 2, from the player1_total_score variable
- player1_total_score = player1_total_score + dice1 + dice2
- if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player1 roll's a double
- #Display's to player1 that they've rolled a double
- print(" ")
- print("Wow! You rolled a double!")
- time.sleep(1)
- #Tells player1 what happens next, now that they've roleld a double
- print("Now you get to roll one extra die")
- time.sleep(1)
- #Prompt's player1 to roll their die
- input("Press enter to roll your third die: ")
- #Retro-style game text to simulate the rolling of actual dice
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it in a variable called dice3
- dice3 = randint(1, 6)
- #Display's to player1 what their third dice was
- print("Your third die is " + str(dice3))
- time.sleep(1)
- #Updates the player1's score so that 10 points are added (because they rolled an even numbered sum) and to add dice3 to their total
- player1_total_score = player1_total_score + dice3 + 10
- #Verifies that if the player's score is below 0, their score it set to 0
- if player1_total_score < 0:
- player1_total_score = 0#Score is set to 0
- #Check's that the player's score isn't below 0, otherwise their total would be 0
- elif player1_total_score < 0:
- player1_total_score = 0#Score is set to 0
- elif (dice1 + dice2) % 2 == 0:
- #If the sum of the player's roll is even, then an additional 10 points are added to their variable 'player1_total_score'
- player1_total_score = player1_total_score + 10
- #Check's that the player's score isn't below 0, otherwise their total would be 0
- if player1_total_score < 0:
- player1_total_score = 0#Set's score equal to 0
- elif (dice1 + dice2) % 2 != 0:
- #If the sum of the player's roll is even, then an additional 10 points are added to their variable 'player1_total_score'
- player1_total_score = player1_total_score - 5
- #Check's that the player's score isn't below 0, otherwise their total would be 0
- if player1_total_score < 0:
- player1_total_score = 0#Set's score to 0
- print(username1 + ", your total so far is " + str(player1_total_score))#Display's Player1 score
- #Divider
- time.sleep(1)
- print("----------")
- #Player 2
- #Display's the turn of player2
- print(username2 + "'s turn")
- #Prompt's player2 to roll their dice
- input(username2 + ", press enter to roll your first pair of dice: ")
- #Retro-style game text to simulate the rolling of actual dice
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it to two variables called dice1 and dice2
- dice1 = randint(1, 6)
- dice2 = randint(1, 6)
- #Display's to the player the two dice they've rolled
- print("Your first dice is " + str(dice1))
- time.sleep(1)
- print("Your second dice is " + str(dice2))
- time.sleep(1)
- #Updates player2's score using the variable player2_total_score by adding the sum of the two dice that they've rolled
- player2_total_score = player2_total_score + dice1 + dice2
- if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player2 roll's a double
- print(" ")
- #Display's that player2 rolled a double
- print("Wow! You rolled a double!")
- time.sleep(1)
- #Display's to user2 what they have to do, now that they've rolled a double
- print("Now you get to roll one extra die")
- time.sleep(1)
- #Prompts user2 to roll their third die
- input("Press enter to roll your third die: ")
- #Retro-style game text to simulate the rolling of actual dice
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it in a variable called dice3
- dice3 = randint(1, 6)
- #Diplay's to player2 what their third dice is
- print("Your third die is " + str(dice3))
- time.sleep(1)
- #Updates their score, by adding their third dice into the variable player2_total_score, and adds 10 points because the sum of their two original dice is even
- player2_total_score = player2_total_score + dice3 + 10
- #Verifies that if the player's score is below 0, their score it set to 0
- if player2_total_score < 0:
- player2_total_score = 0#Set's the score equal to 0
- #Verifies that if the player's score is below 0, their score it set to 0
- elif player2_total_score < 0:
- player2_total_score = 0#Set's the score to 0
- elif (dice1 + dice2) % 2 == 0:#Checks if the sum of the dice rolled is even
- player2_total_score = player2_total_score + 10#Add's 10 points to player2's score ot the variable 'player2_total_score'
- if player2_total_score < 0:#Check's if the player's score is below 0 after that operation
- player2_total_score = 0#Set's player2's score to 0
- elif (dice1 + dice2) % 2 != 0:#Checks if the sum of the two dice rolled is odd
- player2_total_score = player2_total_score - 5#5 points is taken away from the player's score if their sum of the two dice rolled is odd
- if player2_total_score < 0:#Verifies of the score after the mathematical operations is blow 0
- player2_total_score = 0#Set's score equal to 0
- print(username2 + ", your total so far is " + str(player2_total_score))#Display's Player2 score
- #Divider
- time.sleep(1)
- print(" ")
- """Results per Round"""
- if player1_total_score > player2_total_score:#Checks if player1's score is bigger than player2's score
- #Set's the difference of these two score in a variable called 'difference'
- difference = player1_total_score - player2_total_score
- #1 is added to the difference to tell player2 how many points they need to be first but not equal to player1
- difference = difference + 1
- #Display who is in the lead so far (player1) and by how mnay points
- print("At the end of round " + str(i + 1) + ", " + username1 + " is now in the lead with " + str(player1_total_score) + " points!")
- time.sleep(1)
- #Tell player2 how many points they need to be first
- print(username2 + ", you need " + str(difference) + " points to be first!")
- elif player1_total_score < player2_total_score:#Check's if player2's score is larger than player1's
- #Set's the difference of these two score in a variable called 'difference'
- difference = player2_total_score - player1_total_score
- #1 is added to the difference to tell player1 how many points they need to be first but not equal to player2
- difference = difference + 1
- #Display who is in the lead so far (player2) and by how mnay points
- print("At the end of round " + str(i + 1) + ", " + username2 + " is now in the lead with " + str(player2_total_score) + " points!")
- time.sleep(1)
- #Tell player1 how many points they need to be first
- print(username1 + ", you need " + str(difference) + " points to be first!")
- elif player1_total_score == player2_total_score:#Check's if the scores are equal to each other
- #Display's a message saying that both users are tied
- print("At the end of round " + str(i + 1) + ", you're both at " + str(player1_total_score) + " points each! What are the chances?")#Tells the users by how much their tied by
- #Divider
- time.sleep(1)
- print(" ")
- print("--------------------")
- print(" ")
- """Final, End Results"""
- print("The Results")
- time.sleep(1)
- player1 = input("Press enter to find out the results: ")#Prompt's the user to find out the results by pressing enter
- print("Calculating....")
- time.sleep(1)
- print("Calculating....")
- time.sleep(1)
- #Display's player1's score
- print(" ")
- print(username1 + ": " + str(player1_total_score) + " points")
- time.sleep(1)
- #Display's player2's score
- print(username2 + ": " + str(player2_total_score) + " points")
- time.sleep(1)
- print(" ")
- #Check's if player1's final score if bigger than player2's
- if player1_total_score > player2_total_score:
- #Display's message congratulating player1
- print("Congratulations, " + username1 + "! You have won my game!")
- time.sleep(1)
- #Display's message to player2
- print("Better luck next time, " + username2 + "!")
- time.sleep(1)
- elif player1_total_score < player2_total_score:#If player2's score is bigger than player1, then player2 wins
- #Display's message congratulating player2
- print("Congratulations, " + username2 + "! You have won my game!")
- time.sleep(1)
- #Display's message to player1
- print("Better luck next time, " + username1 + "!")
- time.sleep(1)
- """If both players' score are the same"""
- while player1_total_score == player2_total_score:#Check's if the player's scores are equal
- #Set's the scores equal to 0 again, for a new round
- player1_total_score = 0
- player2_total_score = 0
- #Display's message to both users telling them what happens now
- print("You both have the same score! What are the chances? Now, you're both going to roll 1 die each until one of you gets a higher score")
- time.sleep(1)
- #Prompts user1 to roll their die
- input(username1 + ", press enter to roll your first die: ")
- #Retro-style game text to simulate the rolling of actual die
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Set's the variable 'dice1' equal to a random integer, inclusive between 1 and 6
- dice1 = randint(1, 6)
- #Set's player1_total_score variable to the numbe rolled on the variable 'dice1'
- player1_total_score = player1_total_score + dice1
- print("Your first die is " + str(dice1) + ".")
- time.sleep(1)
- print(username1 + ", your total score is " + str(player1_total_score) + ".")#Display's Player1 score
- time.sleep(1)
- print("----------")
- #Player2's turn
- #Prompt's user2 to roll their die
- input(username2 + ", press enter to roll your first die: ")
- #Retro-style game text to simulate the rolling of actual die
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- #Set's the variable 'dice2' equal to a random integer, inclusive between 1 and 6
- dice2 = randint(1, 6)
- #Set's player2's score to the die rolled in the variable 'player2_total_score'
- player2_total_score = player2_total_score + dice2
- #Display's what player2's number was for his die
- print("Your first die is " + str(dice2) + ".")
- time.sleep(1)
- #Display's player's final score
- print(username2, ", Your total score is " + str(player2_total_score))#Display's Player2 score
- #Check's if player's score is greater than players's
- if player1_total_score > player2_total_score:
- #Display's congratulations to player 1
- print("Congratulation " + username1 + ", you have won my game!")
- time.sleep(1)
- #Display's Player2's concession
- print("Better luck next time " + username2 + "!")
- #Check's if player1's score is less than player2's score
- elif player1_total_score < player2_total_score:
- #Display's congratulations to player 2
- print("Congratulations " + username2 + ", you have won my game!")
- time.sleep(1)
- #Display's Player1 concession
- print("Better luck next time " + username1 + "!")
- #----------------------------------------------------------------------------------------------------
- """High scores"""
- #Set's the 'scores' equal to an empty dictionary
- scores = {}
- """Stores the winner's score, & their name, in an external .csv file"""
- with open("annotated_highscores.csv", "r", newline='') as csvfile: #Open's highscores.csv to read and get the high scores
- reader = csv.reader(csvfile)
- for i in reader:
- scores[i[0]] = int(i[1]) #Put's all data into a dictionary called "scores"
- #Check's if the scores are higher than highscore.csv stored scores
- #Check's if the list of username1 is greater than their score
- if scores[username1] < player1_total_score:
- scores[username1] = player1_total_score#The username is set to the corresponding playerscore for user1
- #Check's if the list of username2 is greater than their score
- if scores[username2] < player2_total_score:
- scores[username2] = player2_total_score#The username is set to the corresponding playerscore for user2
- with open("annotated_highscores.csv", "w", newline='') as csvfile: #Open's .csv file of highscores
- writer = csv.writer(csvfile)
- for name, score in scores.items():
- writer.writerow([name, score]) #Update's all highscores with new ones from this game
- """Display's the score & player name of the top 5 winning scores from the external .csv file"""
- #Divider
- print("--------------------")
- #Prompt's the user to press enter to view the top 5 scores
- input("Press enter to view the top 5 Scores: ")
- print(" ")
- #Set's highestnames equal to an empty list for it to be changed
- highestnames = []
- #Set's highestscores equal to an empty list for it to be changed, so the highest scores are added in in the top 5 order
- highestscores = []
- #Sort's all highscores and picks the first five
- for i in range(5):#Repeats follwing code in the #for# loop indentation five times, to get the top five scores
- highestscore = 0#Set's highscore equal to 0
- highestname = ""#Set's highest name equal to an empty string
- for name, score in scores.items():
- if score > highestscore:#Check's if the score is greater than the highest score
- highestscore = score#Set's highscore equal to the score
- highestname = name#Set's highestname equal to the player's username
- highestnames.append(highestname)
- highestscores.append(highestscore)
- del scores[highestname]
- for index, name in enumerate(highestnames): #Use's enumerate to get the index while iterating
- print(name + ": " + str(highestscores[index])) #Print's the users' score
- """Quits Program"""
- input("Press Enter to close the program: ") #Make's sure the program doesn't close at the end
- #Divider
- print(" ")
- print("Terminating Program")
- time.sleep(1)
- print("3....")
- time.sleep(1)
- print("2....")
- time.sleep(1)
- print("1....")
- time.sleep(1)
- print("Goodbye")
- """END GAME"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement