Advertisement
Guest User

NEA_Task2_KamraanQureshi

a guest
May 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.13 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. from random import randint
  15. import datetime
  16. import time
  17. import sys
  18. import csv
  19.  
  20. """Authentication"""
  21. userpass = {}
  22. with open("passwords.csv", "r") as csvfile: #Open's .csv file of passwords
  23.     reader = csv.reader(csvfile)
  24.     for i in reader:
  25.         userpass[i[0]] = i[1] #Put's csv file contents into a dictionary
  26.  
  27. access1 = False
  28.  
  29. #Authenticate's Player1
  30. while access1 == False:
  31.     username1 = input("Player 1 - Input your username: ")
  32.  
  33.     if username1 in userpass: #Check's if username exists
  34.         password1 = input("Player 1 - Input your password: ")
  35.  
  36.         if password1 == userpass[username1]: #Check's if the password is correct
  37.             print("Access Granted")
  38.             print(" ")
  39.             access1 = True
  40.         else:
  41.             print("Invalid password.")
  42.     else:
  43.         print("Invalid username.")
  44.  
  45. access2 = False
  46.  
  47. #Authenticate's Player2
  48. while access2 == False:
  49.     username2 = input("Player 2 - Input your username: ")
  50.  
  51.     if username2 == username1:
  52.         print("That user is already logged in")#Check's if the username is already logged in    
  53.     elif username2 in userpass: #Check's if the username exists
  54.         password2 = input("Player 2 - Input your password: ")
  55.  
  56.         if password2 == userpass[username2]: #Check's if the password is correct
  57.             print("Access Granted")
  58.             print("--------------------")
  59.             print(" ")
  60.             access2 = True
  61.         else:
  62.             print("Invalid password.")
  63.     else:
  64.         print("Invalid username.") 
  65. #----------------------------------------------------------------------------------------------------  
  66. """Prompt's the user to play"""
  67. while True:
  68.     shouldplay = False
  69.    
  70.     while True:
  71.         print(" ")
  72.         play = input("Would you like to play the game? (Y/N): ")#Ask's the Players if they want to play
  73.  
  74.         if play.lower() == "y":
  75.             shouldplay = True
  76.             break
  77.         elif play.lower() == "n":
  78.             break
  79.         else:
  80.             print("That is not a valid option")
  81.    
  82.     if not shouldplay:#If the user doesn't want to play, the program skips to the end and terminate's
  83.         break
  84.    
  85.     """Introduce's Players to the game"""
  86.     print(" ")
  87.     print("Welcome, " + username1 + " and " + username2 + "!")
  88.     time.sleep(1)
  89.     currentDT = datetime.datetime.now()
  90.     print("You've logged in on the " + currentDT.strftime("%d-%m-%Y") + " @ " + currentDT.strftime("%H:%M:%S %p"))#Show's date & time of login
  91.     print(" ")
  92.     time.sleep(1)
  93.        
  94.     """Print's Rules"""
  95.     print("The Rules are simple:")
  96.     time.sleep(1)
  97.     input("Press enter to continue to scroll through the rules: ")
  98.     print(" ")
  99.     input(" 1)Each player roles 2 6-sided dice.")
  100.     input(" 2)The points rolled on each players dice are added to their score.")
  101.     input(" *If the total is an even number, an additional 10 points are added to your score*")
  102.     input(" *If the total is an odd number, 5 points are subtracted from your score*")
  103.     input(" *A player cannot go below 0 points at any time*")
  104.     print(" ")
  105.     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")
  106.     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")
  107.     input("The person with the highest score at the end of 5 rounds wins!")
  108.     print("--------------------")
  109.     print(" ")
  110.     input("Press enter to continue: ")
  111.     print(" ")
  112.     print("--------------------")
  113.     print(" ")
  114. #----------------------------------------------------------------------------------------------------
  115.     """Set's the players' score to 0"""
  116.     player1_total_score = 0
  117.     player2_total_score = 0
  118.        
  119.     """Main Game"""
  120.     #Repeat's all code in "for" loop 5 times
  121.     for i in range(5):
  122.         print("Round " + str(i + 1))
  123.  
  124.         #Player 1
  125.         time.sleep(1)
  126.         print(username1 + "'s turn")
  127.         input(username1 + ", press enter to roll your first pair of dice: ")
  128.  
  129.         print("Rolling....")
  130.         time.sleep(1)
  131.         print("Rolling....")
  132.         time.sleep(1)
  133.  
  134.         dice1 = randint(1, 6)
  135.         dice2 = randint(1, 6)
  136.  
  137.         print("Your first dice is " + str(dice1))
  138.         time.sleep(1)
  139.         print("Your second dice is " + str(dice2))
  140.         time.sleep(1)
  141.  
  142.         player1_total_score = player1_total_score + dice1 + dice2
  143.  
  144.         if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player1 roll's a double
  145.             print(" ")
  146.             print("Wow! You rolled a double!")
  147.             time.sleep(1)
  148.             print("Now you get to roll one extra die")
  149.             time.sleep(1)
  150.             input("Press enter to roll your third die: ")
  151.  
  152.             print("Rolling....")
  153.             time.sleep(1)
  154.             print("Rolling....")
  155.             time.sleep(1)
  156.  
  157.             dice3 = randint(1, 6)
  158.  
  159.             print("Your third die is " + str(dice3))
  160.             time.sleep(1)
  161.  
  162.             player1_total_score = player1_total_score + dice3 + 10
  163.             if player1_total_score < 0:
  164.                 player1_total_score = 0
  165.         elif player1_total_score < 0:
  166.             player1_total_score = 0
  167.         elif (dice1 + dice2) % 2 == 0:
  168.             player1_total_score = player1_total_score + 10
  169.             if player1_total_score < 0:
  170.                 player1_total_score = 0
  171.         elif (dice1 + dice2) % 2 != 0:
  172.             player1_total_score = player1_total_score - 5
  173.             if player1_total_score < 0:
  174.                 player1_total_score = 0
  175.  
  176.         print(username1 + ", your total so far is " + str(player1_total_score))#Display's Player1 score
  177.  
  178.         time.sleep(1)
  179.         print("----------")
  180.  
  181.         #Player 2
  182.         print(username2 + "'s turn")
  183.         input(username2 + ", press enter to roll your first pair of dice: ")
  184.  
  185.         print("Rolling....")
  186.         time.sleep(1)
  187.         print("Rolling....")
  188.         time.sleep(1)
  189.  
  190.         dice1 = randint(1, 6)
  191.         dice2 = randint(1, 6)
  192.  
  193.         print("Your first dice is " + str(dice1))
  194.         time.sleep(1)
  195.         print("Your second dice is " + str(dice2))
  196.         time.sleep(1)
  197.  
  198.         player2_total_score = player2_total_score + dice1 + dice2
  199.  
  200.         if (dice1 == dice2) and ((dice1 + dice2) % 2 == 0):#If Player2 roll's a double
  201.             print(" ")
  202.             print("Wow! You rolled a double!")
  203.             time.sleep(1)
  204.             print("Now you get to roll one extra die")
  205.             time.sleep(1)
  206.             input("Press enter to roll your third die: ")
  207.  
  208.             print("Rolling....")
  209.             time.sleep(1)
  210.             print("Rolling....")
  211.             time.sleep(1)
  212.  
  213.             dice3 = randint(1, 6)
  214.  
  215.             print("Your third die is " + str(dice3))
  216.             time.sleep(1)
  217.  
  218.             player2_total_score = player2_total_score + dice3 + 10
  219.            
  220.             if player2_total_score < 0:
  221.                 player2_total_score = 0
  222.         elif player2_total_score < 0:
  223.             player2_total_score = 0
  224.         elif (dice1 + dice2) % 2 == 0:
  225.             player2_total_score = player2_total_score + 10
  226.             if player2_total_score < 0:
  227.                 player2_total_score = 0
  228.         elif (dice1 + dice2) % 2 != 0:
  229.             player2_total_score = player2_total_score - 5
  230.             if player2_total_score < 0:
  231.                 player2_total_score = 0
  232.        
  233.         print(username2 + ", your total so far is " + str(player2_total_score))#Display's Player2 score
  234.  
  235.         time.sleep(1)
  236.        
  237.         print(" ")
  238.         print(" ")
  239.        
  240.         """Results per Round"""
  241.         if player1_total_score > player2_total_score:
  242.             difference = player1_total_score - player2_total_score
  243.             difference = difference + 1
  244.             print("At the end of round " + str(i + 1) + ", " + username1 + " is now in the lead with " + str(player1_total_score) + " points!")
  245.             time.sleep(1)
  246.             print(username2 + ", you need " + str(difference) + " points to be first!")
  247.         elif player1_total_score < player2_total_score:
  248.             difference = player2_total_score - player1_total_score
  249.             difference = difference + 1
  250.             print("At the end of round " + str(i + 1) + ", " + username2 + " is now in the lead with " + str(player2_total_score) + " points!")
  251.             time.sleep(1)
  252.             print(username1 + ", you need " + str(difference) + " points to be first!")
  253.         elif player1_total_score == player2_total_score:
  254.             print("At the end of round " + str(i + 1) + ", you're both at " + str(player1_total_score) + " points each! What are the chances?")
  255.            
  256.         time.sleep(1)
  257.         print(" ")
  258.         print("--------------------")
  259.         print(" ")
  260.  
  261.     """Final, End Results"""
  262.     print("The Results")
  263.     time.sleep(1)
  264.     player1 = input("Press enter to find out the results: ")
  265.    
  266.     print("Calculating....")
  267.     time.sleep(1)
  268.     print("Calculating....")
  269.     time.sleep(1)
  270.    
  271.     print(" ")
  272.     print(username1 + ": " + str(player1_total_score) + " points")
  273.     time.sleep(1)
  274.     print(username2 + ": " + str(player2_total_score) + " points")
  275.     time.sleep(1)
  276.     print(" ")
  277.    
  278.     if player1_total_score > player2_total_score:
  279.         print("Congratulations, " + username1 + "! You have won my game!")
  280.         time.sleep(1)
  281.         print("Better luck next time, " + username2 + "!")
  282.         time.sleep(1)
  283.     elif player1_total_score < player2_total_score:
  284.         print("Congratulations, " + username2 + "! You have won my game!")
  285.         time.sleep(1)
  286.         print("Better luck next time, " + username1 + "!")
  287.         time.sleep(1)
  288.    
  289.     """If both players' score are the same"""
  290.     while player1_total_score == player2_total_score:
  291.         player1_total_score = 0
  292.         player2_total_score = 0
  293.        
  294.         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")
  295.         time.sleep(1)
  296.         input(username1 + ", press enter to roll your first die: ")
  297.  
  298.         print("Rolling....")
  299.         time.sleep(1)
  300.         print("Rolling....")
  301.         time.sleep(1)
  302.        
  303.         dice1 = randint(1, 6)
  304.        
  305.         player1_total_score = player1_total_score + dice1
  306.        
  307.         print("Your first die is " + str(dice1) + ".")
  308.         time.sleep(1)
  309.         print(username1 + ", your total score is " + str(player1_total_score) + ".")#Display's Player1 score
  310.        
  311.         time.sleep(1)
  312.         dice2 = randint(1, 6)
  313.         input(username2 + ", press enter to roll your first die: ")
  314.        
  315.         print("Rolling....")
  316.         time.sleep(1)
  317.         print("Rolling....")
  318.         time.sleep(1)
  319.        
  320.         dice2 = randint(1, 6)
  321.        
  322.         player2_total_score = player2_total_score + dice2
  323.        
  324.         print("Your first die is " + str(dice2) + ".")
  325.         time.sleep(1)
  326.         print(username2, ", Your total score is " + str(player2_total_score))#Display's Player2 score
  327.        
  328.         if player1_total_score > player2_total_score:
  329.             print("Congratulation " + username1 + ", you have won my game!")
  330.             time.sleep(1)
  331.             print("Better luck next time " + username2 + "!")
  332.         elif player1_total_score < player2_total_score:
  333.             print("Congratulations " + username2 + ", you have won my game!")      
  334. #----------------------------------------------------------------------------------------------------
  335.     """High scores"""
  336.     scores = {}
  337.     """Stores the winner's score, & their name, in an external  .csv file"""
  338.     with open("highscores.csv", "r", newline='') as csvfile: #Open's highscores.csv to read and get the high scores
  339.         reader = csv.reader(csvfile)
  340.         for i in reader:
  341.             scores[i[0]] = int(i[1]) #Put's all data into a dictionary called "scores"
  342.  
  343.     #Check's if the scores are higher than highscore.csv stored scores
  344.     if scores[username1] < player1_total_score:
  345.         scores[username1] = player1_total_score
  346.     if scores[username2] < player2_total_score:
  347.         scores[username2] = player2_total_score
  348.  
  349.     with open("highscores.csv", "w", newline='') as csvfile: #Open's .csv file of highscores
  350.         writer = csv.writer(csvfile)
  351.         for name, score in scores.items():
  352.             writer.writerow([name, score]) #Update's all highscores with new ones from this game
  353.  
  354.  
  355.     """Display's the score & player name of the top 5 winning scores from the external .csv file"""
  356.     print("--------------------")
  357.     input("Press enter to view the top 5 Scores: ")
  358.     print(" ")
  359.  
  360.     highestnames = []
  361.     highestscores = []
  362.  
  363.     #Sort's all highscores and picks the first five
  364.     for i in range(5):
  365.         highestscore = 0
  366.         highestname = ""
  367.        
  368.         for name, score in scores.items():
  369.             if score > highestscore:
  370.                 highestscore = score
  371.                 highestname = name
  372.  
  373.         highestnames.append(highestname)
  374.         highestscores.append(highestscore)
  375.  
  376.         del scores[highestname]
  377.  
  378.     for index, name in enumerate(highestnames): #Use's enumerate to get the index while iterating
  379.         print(name + ": " + str(highestscores[index])) #Print's the users' score
  380.  
  381. """Quits Program"""
  382. input("Press Enter to close the program: ") #Make's sure the program doesn't close at the end
  383. print(" ")
  384. print("Terminating Program")
  385. time.sleep(1)
  386. print("3....")
  387. time.sleep(1)
  388. print("2....")
  389. time.sleep(1)
  390. print("1....")
  391. time.sleep(1)
  392. print("Goodbye")
  393.  
  394. """END GAME"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement