Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. #Programming Project - A1
  2. from math import *
  3. from random import *
  4. from time import sleep
  5.  
  6. ##File Directory Verification - class used as functions cannot keep local static variables in python
  7.  
  8. class checkDirBase():
  9.   def __init__(self,direct="highscore.txt"):
  10.     self.direct,mode,self.data=direct,"r",[]
  11.     while True:
  12.       if direct=="":
  13.         self.direct,mode="highscore.txt","w"
  14.         break
  15.       try:
  16.         with open(self.direct,mode) as f: pass
  17.         break
  18.       except Exception:
  19.         self.direct = input("If it exists, enter the name of the new directory for highscore, else just press enter: ")
  20.     self.getData()
  21.  
  22.   def __call__(self):
  23.     return data
  24.  
  25.   def getData(self):
  26.     with open(self.direct,"r") as f:
  27.       for i in f:
  28.         self.data.append(i[:-1].split("|"))
  29.  
  30.   def setData(self):
  31.     with open(self.direct,"w") as f:
  32.       for i in self.data:
  33.         f.write(f"{i[0]}|{i[1]}|{i[2]}")
  34. ###End of Class
  35.  
  36. def showLeaders(direct):
  37.   direct.data.sort(key=lambda i:i[1])
  38.   print(direct.data)
  39.  
  40. def highscore(score,direct):#account to verify user that has obtained the highscore
  41.   if input("Would you like to record your score on the leaderboard? ").lower()=="yes":
  42.     if input("Do you have an account? ").lower()=="yes":
  43.       details = list(map(input, ["Enter your username: ","Enter your password: "]))
  44.       for i in direct.data:
  45.         if i[0]==details[0]:
  46.           if i[2]==details[1]:
  47.             i[1]=score
  48.  
  49.     createAccount(score,direct)
  50.  
  51. def createAccount(score,direct):
  52.   while True:
  53.     details = list(map(input, ["Enter a username: ","Enter a password: "]))
  54.     for i in direct.data:
  55.       if details[0] in i:
  56.         print(f"You must choose another username, {details[0]} is already taken")
  57.         createAccount()
  58.     direct.data.append([details[0],score,details[1]])
  59.     break
  60.  
  61.  
  62. def generateSequence():
  63.   p,r = "abcd",""
  64.   while len(r) < 4:
  65.     order=randint(0,3)
  66.     if not(p[order] in r):
  67.       r+=p[order]
  68.   return r
  69.  
  70. def generateComment(score):
  71.   if score==1:
  72.     return ("You guessed everything right first go!")
  73.   elif score<5:
  74.     return (f"Good try, you guessed correct in {score} goes")
  75.   else:
  76.     return (f"You took {score} goes to guess this?, your BAD")
  77.  
  78. def mastermind():
  79.   direct=checkDirBase()
  80.   tGuess = generateSequence()
  81.   print(tGuess)
  82.   attempts =1
  83.   while True:
  84.     nCorrect = 0
  85.     Guess = input("Guess the sequence: ")
  86.     for n in range(0,4):
  87.       if Guess[n]==tGuess[n]:
  88.         nCorrect+=1
  89.         if nCorrect==4:
  90.           print(generateComment(attempts))
  91.           highscore(attempts,direct)
  92.           return
  93.     attempts+=1
  94.  
  95. mastermind()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement