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"""
- from random import randint
- import datetime
- import time
- import sys
- import csv
- """Authentication"""
- userpass = {}
- with open("passwords.csv", "r") as csvfile: #Open's .csv file of passwords
- 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:
- username1 = input("Player 1 - Input your username: ")
- if username1 in userpass: #Check's if username exists
- password1 = input("Player 1 - Input your password: ")
- if password1 == userpass[username1]: #Check's if the password is correct
- print("Access Granted")
- print(" ")
- access1 = True
- else:
- print("Invalid password.")
- else:
- print("Invalid username.")
- access2 = False
- #Authenticate's Player2
- while access2 == False:
- 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
- password2 = input("Player 2 - Input your password: ")
- if password2 == userpass[username2]: #Check's if the password is correct
- print("Access Granted")
- print("--------------------")
- print(" ")
- access2 = True
- else:
- print("Invalid password.")
- else:
- print("Invalid username.")
- #----------------------------------------------------------------------------------------------------
- """Prompt's the user to play"""
- while True:
- shouldplay = False
- while True:
- print(" ")
- play = input("Would you like to play the game? (Y/N): ")#Ask's the Players if they want to play
- if play.lower() == "y":
- shouldplay = True
- break
- elif play.lower() == "n":
- break
- else:
- 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"""
- print(" ")
- print("Welcome, " + username1 + " and " + username2 + "!")
- time.sleep(1)
- currentDT = datetime.datetime.now()
- print("You've logged in on the " + currentDT.strftime("%d-%m-%Y") + " @ " + currentDT.strftime("%H:%M:%S %p"))#Show's date & time of login
- print(" ")
- time.sleep(1)
- """Print's Rules"""
- 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!")
- print("--------------------")
- print(" ")
- input("Press enter to continue: ")
- print(" ")
- print("--------------------")
- print(" ")
- #----------------------------------------------------------------------------------------------------
- """Set's the players' score to 0"""
- player1_total_score = 0
- player2_total_score = 0
- """Main Game"""
- #Repeat's all code in "for" loop 5 times
- for i in range(5):
- print("Round " + str(i + 1))
- #Player 1
- time.sleep(1)
- print(username1 + "'s turn")
- input(username1 + ", press enter to roll your first pair of dice: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice1 = randint(1, 6)
- dice2 = randint(1, 6)
- print("Your first dice is " + str(dice1))
- time.sleep(1)
- print("Your second dice is " + str(dice2))
- time.sleep(1)
- player1_total_score = player1_total_score + dice1 + dice2
- if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player1 roll's a double
- print(" ")
- print("Wow! You rolled a double!")
- time.sleep(1)
- print("Now you get to roll one extra die")
- time.sleep(1)
- input("Press enter to roll your third die: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice3 = randint(1, 6)
- print("Your third die is " + str(dice3))
- time.sleep(1)
- player1_total_score = player1_total_score + dice3 + 10
- if player1_total_score < 0:
- player1_total_score = 0
- elif player1_total_score < 0:
- player1_total_score = 0
- elif (dice1 + dice2) % 2 == 0:
- player1_total_score = player1_total_score + 10
- if player1_total_score < 0:
- player1_total_score = 0
- elif (dice1 + dice2) % 2 != 0:
- player1_total_score = player1_total_score - 5
- if player1_total_score < 0:
- player1_total_score = 0
- print(username1 + ", your total so far is " + str(player1_total_score))#Display's Player1 score
- time.sleep(1)
- print("----------")
- #Player 2
- print(username2 + "'s turn")
- input(username2 + ", press enter to roll your first pair of dice: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice1 = randint(1, 6)
- dice2 = randint(1, 6)
- print("Your first dice is " + str(dice1))
- time.sleep(1)
- print("Your second dice is " + str(dice2))
- time.sleep(1)
- player2_total_score = player2_total_score + dice1 + dice2
- if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player2 roll's a double
- print(" ")
- print("Wow! You rolled a double!")
- time.sleep(1)
- print("Now you get to roll one extra die")
- time.sleep(1)
- input("Press enter to roll your third die: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice3 = randint(1, 6)
- print("Your third die is " + str(dice3))
- time.sleep(1)
- player2_total_score = player2_total_score + dice3 + 10
- if player2_total_score < 0:
- player2_total_score = 0
- elif player2_total_score < 0:
- player2_total_score = 0
- elif (dice1 + dice2) % 2 == 0:
- player2_total_score = player2_total_score + 10
- if player2_total_score < 0:
- player2_total_score = 0
- elif (dice1 + dice2) % 2 != 0:
- player2_total_score = player2_total_score - 5
- if player2_total_score < 0:
- player2_total_score = 0
- print(username2 + ", your total so far is " + str(player2_total_score))#Display's Player2 score
- time.sleep(1)
- print(" ")
- print(" ")
- """Results per Round"""
- if player1_total_score > player2_total_score:
- difference = player1_total_score - player2_total_score
- difference = difference + 1
- print("At the end of round " + str(i + 1) + ", " + username1 + " is now in the lead with " + str(player1_total_score) + " points!")
- time.sleep(1)
- print(username2 + ", you need " + str(difference) + " points to be first!")
- elif player1_total_score < player2_total_score:
- difference = player2_total_score - player1_total_score
- difference = difference + 1
- print("At the end of round " + str(i + 1) + ", " + username2 + " is now in the lead with " + str(player2_total_score) + " points!")
- time.sleep(1)
- print(username1 + ", you need " + str(difference) + " points to be first!")
- elif player1_total_score == player2_total_score:
- print("At the end of round " + str(i + 1) + ", you're both at " + str(player1_total_score) + " points each! What are the chances?")
- time.sleep(1)
- print(" ")
- print("--------------------")
- print(" ")
- """Final, End Results"""
- print("The Results")
- time.sleep(1)
- player1 = input("Press enter to find out the results: ")
- print("Calculating....")
- time.sleep(1)
- print("Calculating....")
- time.sleep(1)
- print(" ")
- print(username1 + ": " + str(player1_total_score) + " points")
- time.sleep(1)
- print(username2 + ": " + str(player2_total_score) + " points")
- time.sleep(1)
- print(" ")
- if player1_total_score > player2_total_score:
- print("Congratulations, " + username1 + "! You have won my game!")
- time.sleep(1)
- print("Better luck next time, " + username2 + "!")
- time.sleep(1)
- elif player1_total_score < player2_total_score:
- print("Congratulations, " + username2 + "! You have won my game!")
- time.sleep(1)
- print("Better luck next time, " + username1 + "!")
- time.sleep(1)
- """If both players' score are the same"""
- while player1_total_score == player2_total_score:
- player1_total_score = 0
- player2_total_score = 0
- 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)
- input(username1 + ", press enter to roll your first die: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice1 = randint(1, 6)
- 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)
- dice2 = randint(1, 6)
- input(username2 + ", press enter to roll your first die: ")
- print("Rolling....")
- time.sleep(1)
- print("Rolling....")
- time.sleep(1)
- dice2 = randint(1, 6)
- player2_total_score = player2_total_score + dice2
- print("Your first die is " + str(dice2) + ".")
- time.sleep(1)
- print(username2, ", Your total score is " + str(player2_total_score))#Display's Player2 score
- if player1_total_score > player2_total_score:
- print("Congratulation " + username1 + ", you have won my game!")
- time.sleep(1)
- print("Better luck next time " + username2 + "!")
- elif player1_total_score < player2_total_score:
- print("Congratulations " + username2 + ", you have won my game!")
- #----------------------------------------------------------------------------------------------------
- """High scores"""
- scores = {}
- """Stores the winner's score, & their name, in an external .csv file"""
- with open("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
- if scores[username1] < player1_total_score:
- scores[username1] = player1_total_score
- if scores[username2] < player2_total_score:
- scores[username2] = player2_total_score
- with open("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"""
- print("--------------------")
- input("Press enter to view the top 5 Scores: ")
- print(" ")
- highestnames = []
- highestscores = []
- #Sort's all highscores and picks the first five
- for i in range(5):
- highestscore = 0
- highestname = ""
- for name, score in scores.items():
- if score > highestscore:
- highestscore = score
- highestname = name
- 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
- 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