-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_game.py
102 lines (64 loc) · 3.02 KB
/
class_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import randomword
import win_rate_manager
class game:
def __init__(self, lives, difficulty):
self.lives = lives
self.gameWord = randomword.chooseWord() #Using randonword.py to obtain a randomword.
self.guessProgress = "_" * len(self.gameWord)
self.guessedLetters = []
self.difficulty = difficulty
#Functions to change the variables of the game
def loseLife(self):
self.lives -= 1
def setGuessProgress(self, guessProgress):
self.guessProgress = guessProgress
#Check to see if the guess is [A-Z|a-z] and is a single character
@staticmethod
def guessIsValid(guess):
if (guess.isalpha() and len(guess) == 1):
return True
else:
False
#Make a guess and update variables accordingly
#@staticmethod
def makeGuess(self, guess):
guess = guess.lower()
#Check if the guess is just a single letter of alphabet
if (self.guessIsValid(guess)):
if (guess in self.guessedLetters):
return ("Lives: " + str(self.lives)), self.guessProgress, "Guess already made!"
elif (guess not in self.gameWord):
self.guessedLetters.append(guess)
self.loseLife()
return ("Lives: " + str(self.lives)) , self.guessProgress, "Wrong guess!"
else:
self.guessedLetters.append(guess)
#Treat as a list and perform list comprehension to gather all indices of a correct guess to update
asList = list(self.guessProgress)
indices = [i for i, letter in enumerate(self.gameWord) if letter == guess]
for index in indices:
asList[index] = guess
#Updates the guess progress
guessProgress = "".join(asList)
self.setGuessProgress(guessProgress)
return ("Lives: " + str(self.lives)), self.guessProgress, "You guessed a letter correctly!"
else:
return ("Lives: " + str(self.lives)), self.guessProgress, "Input not valid, Enter a letter A-Z|a-z"
#Check that the game is won/lost or still going returning an appropiate message
def checkGameStatus(self):
if (self.guessProgress == self.gameWord):
#Could do this better, instead of having 2 constructors.
manager = win_rate_manager.win_rate_manager(self.difficulty, True)
manager.wonGame = True
manager.updateWinRate()
return True, "You Won!, you have a use for something after all...."
elif (self.lives <= 0):
manager = win_rate_manager.win_rate_manager(self.difficulty, False)
manager.updateWinRate()
return True, ("You Lost, the word your feeble mind failed to compute was: " + self.gameWord)
else:
False, ""
#newgame = game(9)
#print("Gameword: " + newgame.gameWord)
#print("Guess Progress: " + newgame.guessProgress)
#print("Lives: " + str(newgame.lives))