Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Programming Project - A1
- from math import *
- from random import *
- from time import sleep
- ##File Directory Verification - class used as functions cannot keep local static variables in python
- class checkDirBase():
- def __init__(self,direct="highscore.txt"):
- self.direct,mode,self.data=direct,"r",[]
- while True:
- if direct=="":
- self.direct,mode="highscore.txt","w"
- break
- try:
- with open(self.direct,mode) as f: pass
- break
- except Exception:
- self.direct = input("If it exists, enter the name of the new directory for highscore, else just press enter: ")
- self.getData()
- def __call__(self):
- return data
- def getData(self):
- with open(self.direct,"r") as f:
- for i in f:
- self.data.append(i[:-1].split("|"))
- def setData(self):
- with open(self.direct,"w") as f:
- for i in self.data:
- f.write(f"{i[0]}|{i[1]}|{i[2]}")
- ###End of Class
- def showLeaders(direct):
- direct.data.sort(key=lambda i:i[1])
- print(direct.data)
- def highscore(score,direct):#account to verify user that has obtained the highscore
- if input("Would you like to record your score on the leaderboard? ").lower()=="yes":
- if input("Do you have an account? ").lower()=="yes":
- details = list(map(input, ["Enter your username: ","Enter your password: "]))
- for i in direct.data:
- if i[0]==details[0]:
- if i[2]==details[1]:
- i[1]=score
- createAccount(score,direct)
- def createAccount(score,direct):
- while True:
- details = list(map(input, ["Enter a username: ","Enter a password: "]))
- for i in direct.data:
- if details[0] in i:
- print(f"You must choose another username, {details[0]} is already taken")
- createAccount()
- direct.data.append([details[0],score,details[1]])
- break
- def generateSequence():
- p,r = "abcd",""
- while len(r) < 4:
- order=randint(0,3)
- if not(p[order] in r):
- r+=p[order]
- return r
- def generateComment(score):
- if score==1:
- return ("You guessed everything right first go!")
- elif score<5:
- return (f"Good try, you guessed correct in {score} goes")
- else:
- return (f"You took {score} goes to guess this?, your BAD")
- def mastermind():
- direct=checkDirBase()
- tGuess = generateSequence()
- print(tGuess)
- attempts =1
- while True:
- nCorrect = 0
- Guess = input("Guess the sequence: ")
- for n in range(0,4):
- if Guess[n]==tGuess[n]:
- nCorrect+=1
- if nCorrect==4:
- print(generateComment(attempts))
- highscore(attempts,direct)
- return
- attempts+=1
- mastermind()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement