Advertisement
Guest User

NEA_Task2Annotated_KamraanQureshi

a guest
May 12th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.17 KB | None | 0 0
  1. """
  2. Kamraan H. Qureshi
  3. Final Program
  4. NEA; Task 2 - Dice Game
  5. 10M 10YB
  6. 10MS2 - Ms Grover
  7. Started 10/02/19
  8. Finished 20/02/19
  9. """
  10.  
  11. """START GAME"""
  12.  
  13. """Import's Modules"""
  14. #The Random library is used to generate a random integer for the randomly selected dice
  15. from random import randint
  16. #The Datetime library is used to display the date and time for the player's login
  17. import datetime
  18. #The time library is used to add pauses in the program
  19. import time
  20. #The sys library shuts down the program at the end
  21. import sys
  22. #The csv library is used to open and close .csv files and to manipulate their data by changing, adding or removing it
  23. import csv
  24. #----------------------------------------------------------------------------------------------------
  25. """Authentication"""
  26. userpass = {}#Set's 'userpass' equal to an empty dictionary
  27. with open("annotated_passwords.csv", "r") as csvfile: #Open's .csv file of passwords
  28.     #.csv file is and opened by the computer and stored in a variable called 'reader'
  29.     reader = csv.reader(csvfile)
  30.     for i in reader:
  31.         userpass[i[0]] = i[1] #Put's csv file contents into a dictionary
  32.  
  33. access1 = False
  34.  
  35. #Authenticate's Player1
  36. while access1 == False:
  37.     #Ask's Player1 to enter in their username
  38.     username1 = input("Player 1 - Input your username: ")
  39.  
  40.     if username1 in userpass: #Check's if username exists
  41.         #Ask's Player1 to enter in their password
  42.         password1 = input("Player 1 - Input your password: ")
  43.  
  44.         if password1 == userpass[username1]: #Check's if the password is correct
  45.             #Display's message to tell user1 that they're now in the game
  46.             print("Access Granted")
  47.             #Divider
  48.             print(" ")
  49.             access1 = True
  50.         else:
  51.             #Display's message to the user if their password is wrong, and needs to be typed in again
  52.             print("Invalid password.")
  53.     else:
  54.         #Display's message to the user if their username is wrong, and needs to be typed in again
  55.         print("Invalid username.")
  56.  
  57. access2 = False
  58.  
  59. #Authenticate's Player2
  60. while access2 == False:
  61.     #Ask's Player2 to type in their username
  62.     username2 = input("Player 2 - Input your username: ")
  63.  
  64.     if username2 == username1:
  65.         print("That user is already logged in")#Check's if the username is already logged in    
  66.     elif username2 in userpass: #Check's if the username exists
  67.         #Ask's Player2 to type in their password
  68.         password2 = input("Player 2 - Input your password: ")
  69.  
  70.         if password2 == userpass[username2]: #Check's if the password is correct
  71.             #Display's message to tell user2 that they're now in the game
  72.             print("Access Granted")
  73.             #Divider
  74.             print("--------------------")
  75.             print(" ")
  76.             access2 = True
  77.         else:
  78.             #Display's message to user2 if their password is wrong, and needs to be typed in again
  79.             print("Invalid password.")
  80.     else:
  81.         #Display's message to user2 if their username is wrong, and needs to be typed in again
  82.         print("Invalid username.") 
  83. #----------------------------------------------------------------------------------------------------  
  84. """Prompt's the user to play"""
  85. while True:
  86.     shouldplay = False
  87.    
  88.     while True:
  89.                 #Divider
  90.         print(" ")
  91.         play = input("Would you like to play the game? (Y/N): ")#Ask's the Players if they want to play
  92.  
  93.                 #Test's for different, but the same, responses to the question, to see if they're valid
  94.         if play.lower() == "y":
  95.             shouldplay = True
  96.             break
  97.         elif play.lower() == "n":
  98.             break
  99.         else:
  100.                         #If the user input isn't valid
  101.             print("That is not a valid option")
  102.    
  103.     if not shouldplay:#If the user doesn't want to play, the program skips to the end and terminate's
  104.         break
  105.    
  106.     """Introduce's Players to the game"""
  107.     #Divider
  108.     print(" ")
  109.     print("Welcome, " + username1 + " and " + username2 + "!")
  110.     time.sleep(1)
  111.     #Set's the current date and time on a variable called currentDT
  112.     currentDT = datetime.datetime.now()
  113.     #Display's the current date and time
  114.     print("You've logged in on the " + currentDT.strftime("%d-%m-%Y") + " @ " + currentDT.strftime("%H:%M:%S %p"))#Show's date & time of login
  115.     #Divider
  116.     print(" ")
  117.     time.sleep(1)
  118.        
  119.     """Print's Rules"""
  120.     """Goes at the user's pace, to make sure that they understood each rule and how to play the game"""
  121.    
  122.     print("The Rules are simple:")
  123.     time.sleep(1)
  124.     input("Press enter to continue to scroll through the rules: ")
  125.     print(" ")
  126.     input(" 1)Each player roles 2 6-sided dice.")
  127.     input(" 2)The points rolled on each players dice are added to their score.")
  128.     input(" *If the total is an even number, an additional 10 points are added to your score*")
  129.     input(" *If the total is an odd number, 5 points are subtracted from your score*")
  130.     input(" *A player cannot go below 0 points at any time*")
  131.     print(" ")
  132.     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")
  133.     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")
  134.     input("The person with the highest score at the end of 5 rounds wins!")
  135.     #Divider
  136.     print("--------------------")
  137.     print(" ")
  138.     input("Press enter to continue: ")
  139.     #Divider
  140.     print(" ")
  141.     print("--------------------")
  142.     print(" ")
  143. #----------------------------------------------------------------------------------------------------
  144.     """Set's the players' score to 0 in a variable called player1_total_score and player2_total_score"""
  145.     player1_total_score = 0
  146.     player2_total_score = 0
  147.        
  148.     """Main Game"""
  149.     #Repeat's all code in "for" loop 5 times
  150.     for i in range(5):#For loop makes everything inside of the indentation happed five times
  151.         print("Round " + str(i + 1))#Display's the round number to the player
  152.  
  153.         #Player 1
  154.         time.sleep(1)
  155.         print(username1 + "'s turn")
  156.         #Prompt's the user to roll their two dice
  157.         input(username1 + ", press enter to roll your first pair of dice: ")
  158.  
  159.                 #Retro-style game text to simulate the rolling of actual dice
  160.         print("Rolling....")
  161.         time.sleep(1)
  162.         print("Rolling....")
  163.         time.sleep(1)
  164.  
  165.                 #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
  166.         dice1 = randint(1, 6)
  167.         dice2 = randint(1, 6)
  168.  
  169.                 #Display's the two dice that the player rolled
  170.         print("Your first dice is " + str(dice1))
  171.         time.sleep(1)
  172.         print("Your second dice is " + str(dice2))
  173.         time.sleep(1)
  174.  
  175.                 #Updates player1's score to include the points won in dice1 and dice 2, from the player1_total_score variable
  176.         player1_total_score = player1_total_score + dice1 + dice2
  177.  
  178.         if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player1 roll's a double
  179.                         #Display's to player1 that they've rolled a double
  180.             print(" ")
  181.             print("Wow! You rolled a double!")
  182.             time.sleep(1)
  183.             #Tells player1 what happens next, now that they've roleld a double
  184.             print("Now you get to roll one extra die")
  185.             time.sleep(1)
  186.             #Prompt's player1 to roll their die
  187.             input("Press enter to roll your third die: ")
  188.  
  189.                         #Retro-style game text to simulate the rolling of actual dice
  190.             print("Rolling....")
  191.             time.sleep(1)
  192.             print("Rolling....")
  193.             time.sleep(1)
  194.  
  195.                         #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it in a variable called dice3
  196.             dice3 = randint(1, 6)
  197.  
  198.                         #Display's to player1 what their third dice was
  199.             print("Your third die is " + str(dice3))
  200.             time.sleep(1)
  201.  
  202.                         #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
  203.             player1_total_score = player1_total_score + dice3 + 10
  204.             #Verifies that if the player's score is below 0, their score it set to 0
  205.             if player1_total_score < 0:
  206.                 player1_total_score = 0#Score is set to 0
  207.         #Check's that the player's score isn't below 0, otherwise their total would be 0
  208.         elif player1_total_score < 0:
  209.             player1_total_score = 0#Score is set to 0
  210.         elif (dice1 + dice2) % 2 == 0:
  211.                 #If the sum of the player's roll is even, then an additional 10 points are added to their variable 'player1_total_score'
  212.             player1_total_score = player1_total_score + 10
  213.             #Check's that the player's score isn't below 0, otherwise their total would be 0
  214.             if player1_total_score < 0:
  215.                 player1_total_score = 0#Set's score equal to 0
  216.         elif (dice1 + dice2) % 2 != 0:
  217.                 #If the sum of the player's roll is even, then an additional 10 points are added to their variable 'player1_total_score'
  218.             player1_total_score = player1_total_score - 5
  219.             #Check's that the player's score isn't below 0, otherwise their total would be 0
  220.             if player1_total_score < 0:
  221.                 player1_total_score = 0#Set's score to 0
  222.  
  223.         print(username1 + ", your total so far is " + str(player1_total_score))#Display's Player1 score
  224.  
  225.                 #Divider
  226.         time.sleep(1)
  227.         print("----------")
  228.  
  229.         #Player 2
  230.         #Display's the turn of player2
  231.         print(username2 + "'s turn")
  232.         #Prompt's player2 to roll their dice
  233.         input(username2 + ", press enter to roll your first pair of dice: ")
  234.  
  235.                 #Retro-style game text to simulate the rolling of actual dice
  236.         print("Rolling....")
  237.         time.sleep(1)
  238.         print("Rolling....")
  239.         time.sleep(1)
  240.  
  241.                 #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
  242.         dice1 = randint(1, 6)
  243.         dice2 = randint(1, 6)
  244.        
  245.                 #Display's to the player the two dice they've rolled
  246.         print("Your first dice is " + str(dice1))
  247.         time.sleep(1)
  248.         print("Your second dice is " + str(dice2))
  249.         time.sleep(1)
  250.  
  251.                 #Updates player2's score using the variable player2_total_score by adding the sum of the two dice that they've rolled
  252.         player2_total_score = player2_total_score + dice1 + dice2
  253.  
  254.         if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player2 roll's a double
  255.             print(" ")
  256.             #Display's that player2 rolled a double
  257.             print("Wow! You rolled a double!")
  258.             time.sleep(1)
  259.             #Display's to user2 what they have to do, now that they've rolled a double
  260.             print("Now you get to roll one extra die")
  261.             time.sleep(1)
  262.             #Prompts user2 to roll their third die
  263.             input("Press enter to roll your third die: ")
  264.  
  265.                         #Retro-style game text to simulate the rolling of actual dice
  266.             print("Rolling....")
  267.             time.sleep(1)
  268.             print("Rolling....")
  269.             time.sleep(1)
  270.  
  271.                         #Use's the 'random' library to geenrate 1 number between 1 and 6 (inclusive) and set's it in a variable called dice3
  272.             dice3 = randint(1, 6)
  273.  
  274.                         #Diplay's to player2 what their third dice is
  275.             print("Your third die is " + str(dice3))
  276.             time.sleep(1)
  277.  
  278.                         #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
  279.             player2_total_score = player2_total_score + dice3 + 10
  280.  
  281.             #Verifies that if the player's score is below 0, their score it set to 0
  282.             if player2_total_score < 0:
  283.                 player2_total_score = 0#Set's the score equal to 0
  284.         #Verifies that if the player's score is below 0, their score it set to 0
  285.         elif player2_total_score < 0:
  286.             player2_total_score = 0#Set's the score to 0
  287.         elif (dice1 + dice2) % 2 == 0:#Checks if the sum of the dice rolled is even
  288.             player2_total_score = player2_total_score + 10#Add's 10 points to player2's score ot the variable 'player2_total_score'
  289.             if player2_total_score < 0:#Check's if the player's score is below 0 after that operation
  290.                 player2_total_score = 0#Set's player2's score to 0
  291.         elif (dice1 + dice2) % 2 != 0:#Checks if the sum of the two dice rolled is odd
  292.             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
  293.             if player2_total_score < 0:#Verifies of the score after the mathematical operations is blow 0
  294.                 player2_total_score = 0#Set's score equal to 0
  295.        
  296.         print(username2 + ", your total so far is " + str(player2_total_score))#Display's Player2 score
  297.  
  298.                 #Divider
  299.         time.sleep(1)
  300.         print(" ")
  301.  
  302.         """Results per Round"""
  303.         if player1_total_score > player2_total_score:#Checks if player1's score is bigger than player2's score
  304.                         #Set's the difference of these two score in a variable called 'difference'
  305.             difference = player1_total_score - player2_total_score
  306.             #1 is added to the difference to tell player2 how many points they need to be first but not equal to player1
  307.             difference = difference + 1
  308.             #Display who is in the lead so far (player1) and by how mnay points
  309.             print("At the end of round " + str(i + 1) + ", " + username1 + " is now in the lead with " + str(player1_total_score) + " points!")
  310.             time.sleep(1)
  311.             #Tell player2 how many points they need to be first
  312.             print(username2 + ", you need " + str(difference) + " points to be first!")
  313.         elif player1_total_score < player2_total_score:#Check's if player2's score is larger than player1's
  314.                         #Set's the difference of these two score in a variable called 'difference'
  315.             difference = player2_total_score - player1_total_score
  316.             #1 is added to the difference to tell player1 how many points they need to be first but not equal to player2
  317.             difference = difference + 1
  318.             #Display who is in the lead so far (player2) and by how mnay points
  319.             print("At the end of round " + str(i + 1) + ", " + username2 + " is now in the lead with " + str(player2_total_score) + " points!")
  320.             time.sleep(1)
  321.             #Tell player1 how many points they need to be first
  322.             print(username1 + ", you need " + str(difference) + " points to be first!")
  323.         elif player1_total_score == player2_total_score:#Check's if the scores are equal to each other
  324.                         #Display's a message saying that both users are tied
  325.             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
  326.  
  327.         #Divider
  328.         time.sleep(1)
  329.         print(" ")
  330.         print("--------------------")
  331.         print(" ")
  332.  
  333.     """Final, End Results"""
  334.     print("The Results")
  335.     time.sleep(1)
  336.     player1 = input("Press enter to find out the results: ")#Prompt's the user to find out the results by pressing enter
  337.    
  338.     print("Calculating....")
  339.     time.sleep(1)
  340.     print("Calculating....")
  341.     time.sleep(1)
  342.     #Display's player1's score
  343.     print(" ")
  344.     print(username1 + ": " + str(player1_total_score) + " points")
  345.     time.sleep(1)
  346.     #Display's player2's score
  347.     print(username2 + ": " + str(player2_total_score) + " points")
  348.     time.sleep(1)
  349.     print(" ")
  350.  
  351.     #Check's if player1's final score if bigger than player2's
  352.     if player1_total_score > player2_total_score:
  353.                 #Display's message congratulating player1
  354.         print("Congratulations, " + username1 + "! You have won my game!")
  355.         time.sleep(1)
  356.         #Display's message to player2
  357.         print("Better luck next time, " + username2 + "!")
  358.         time.sleep(1)
  359.     elif player1_total_score < player2_total_score:#If player2's score is bigger than player1, then player2 wins
  360.                 #Display's message congratulating player2
  361.         print("Congratulations, " + username2 + "! You have won my game!")
  362.         time.sleep(1)
  363.                 #Display's message to player1
  364.         print("Better luck next time, " + username1 + "!")
  365.         time.sleep(1)
  366.    
  367.     """If both players' score are the same"""
  368.     while player1_total_score == player2_total_score:#Check's if the player's scores are equal
  369.                 #Set's the scores equal to 0 again, for a new round
  370.         player1_total_score = 0
  371.         player2_total_score = 0
  372.  
  373.         #Display's message to both users telling them what happens now
  374.         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")
  375.         time.sleep(1)
  376.         #Prompts user1 to roll their die  
  377.         input(username1 + ", press enter to roll your first die: ")
  378.  
  379.                 #Retro-style game text to simulate the rolling of actual die
  380.         print("Rolling....")
  381.         time.sleep(1)
  382.         print("Rolling....")
  383.         time.sleep(1)
  384.  
  385.         #Set's the variable 'dice1' equal to a random integer, inclusive between 1 and 6
  386.         dice1 = randint(1, 6)
  387.  
  388.         #Set's player1_total_score variable to the numbe rolled on the variable 'dice1'
  389.         player1_total_score = player1_total_score + dice1
  390.        
  391.         print("Your first die is " + str(dice1) + ".")
  392.         time.sleep(1)
  393.         print(username1 + ", your total score is " + str(player1_total_score) + ".")#Display's Player1 score
  394.        
  395.         time.sleep(1)
  396.         print("----------")
  397.                
  398.         #Player2's turn
  399.                 #Prompt's user2 to roll their die
  400.         input(username2 + ", press enter to roll your first die: ")
  401.  
  402.         #Retro-style game text to simulate the rolling of actual die
  403.         print("Rolling....")
  404.         time.sleep(1)
  405.         print("Rolling....")
  406.         time.sleep(1)
  407.  
  408.         #Set's the variable 'dice2' equal to a random integer, inclusive between 1 and 6
  409.         dice2 = randint(1, 6)
  410.  
  411.         #Set's player2's score to the die rolled in the variable 'player2_total_score'
  412.         player2_total_score = player2_total_score + dice2
  413.  
  414.         #Display's what player2's number was for his die
  415.         print("Your first die is " + str(dice2) + ".")
  416.         time.sleep(1)
  417.         #Display's player's final score
  418.         print(username2, ", Your total score is " + str(player2_total_score))#Display's Player2 score
  419.  
  420.         #Check's if player's score is greater than players's
  421.         if player1_total_score > player2_total_score:
  422.                         #Display's congratulations to player 1
  423.             print("Congratulation " + username1 + ", you have won my game!")
  424.             time.sleep(1)
  425.             #Display's Player2's concession
  426.             print("Better luck next time " + username2 + "!")
  427.         #Check's if player1's score is less than player2's score
  428.         elif player1_total_score < player2_total_score:
  429.                         #Display's congratulations to player 2
  430.             print("Congratulations " + username2 + ", you have won my game!")
  431.             time.sleep(1)
  432.             #Display's Player1 concession
  433.             print("Better luck next time " + username1 + "!")
  434. #----------------------------------------------------------------------------------------------------
  435.     """High scores"""
  436.     #Set's the 'scores' equal to an empty dictionary
  437.     scores = {}
  438.     """Stores the winner's score, & their name, in an external  .csv file"""
  439.     with open("annotated_highscores.csv", "r", newline='') as csvfile: #Open's highscores.csv to read and get the high scores
  440.         reader = csv.reader(csvfile)
  441.         for i in reader:
  442.             scores[i[0]] = int(i[1]) #Put's all data into a dictionary called "scores"
  443.  
  444.     #Check's if the scores are higher than highscore.csv stored scores
  445.     #Check's if the list of username1 is greater than their score
  446.     if scores[username1] < player1_total_score:
  447.         scores[username1] = player1_total_score#The username is set to the corresponding playerscore for user1
  448.     #Check's if the list of username2 is greater than their score
  449.     if scores[username2] < player2_total_score:
  450.         scores[username2] = player2_total_score#The username is set to the corresponding playerscore for user2
  451.  
  452.     with open("annotated_highscores.csv", "w", newline='') as csvfile: #Open's .csv file of highscores
  453.         writer = csv.writer(csvfile)
  454.         for name, score in scores.items():
  455.             writer.writerow([name, score]) #Update's all highscores with new ones from this game
  456.  
  457.  
  458.     """Display's the score & player name of the top 5 winning scores from the external .csv file"""
  459.     #Divider
  460.     print("--------------------")
  461.     #Prompt's the user to press enter to view the top 5 scores
  462.     input("Press enter to view the top 5 Scores: ")
  463.     print(" ")
  464.  
  465.         #Set's highestnames equal to an empty list for it to be changed
  466.     highestnames = []
  467.     #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
  468.     highestscores = []
  469.  
  470.     #Sort's all highscores and picks the first five
  471.     for i in range(5):#Repeats follwing code in the #for# loop indentation five times, to get the top five scores
  472.         highestscore = 0#Set's highscore equal to 0
  473.         highestname = ""#Set's highest name equal to an empty string
  474.        
  475.         for name, score in scores.items():
  476.             if score > highestscore:#Check's if the score is greater than the highest score
  477.                 highestscore = score#Set's highscore equal to the score
  478.                 highestname = name#Set's highestname equal to the player's username
  479.  
  480.         highestnames.append(highestname)
  481.         highestscores.append(highestscore)
  482.  
  483.         del scores[highestname]
  484.  
  485.     for index, name in enumerate(highestnames): #Use's enumerate to get the index while iterating
  486.         print(name + ": " + str(highestscores[index])) #Print's the users' score
  487.  
  488. """Quits Program"""
  489. input("Press Enter to close the program: ") #Make's sure the program doesn't close at the end
  490. #Divider
  491. print(" ")
  492. print("Terminating Program")
  493. time.sleep(1)
  494. print("3....")
  495. time.sleep(1)
  496. print("2....")
  497. time.sleep(1)
  498. print("1....")
  499. time.sleep(1)
  500. print("Goodbye")
  501.  
  502. """END GAME"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement