Added full word guessing.
This commit is contained in:
parent
95b7899648
commit
37928df153
46
hangfish.py
46
hangfish.py
@ -1,7 +1,7 @@
|
|||||||
alphabet = "abcdefghijklmnopqrstuvwxyzABCDKFGHLJKLMJOPQRSFUVWXYZ"
|
class Hangfish():
|
||||||
class HangFish():
|
discord_instances = {}
|
||||||
|
|
||||||
def __init__(self, channel, word):
|
def __init__(self, word):
|
||||||
# Word to be guessed
|
# Word to be guessed
|
||||||
self.word = word
|
self.word = word
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ class HangFish():
|
|||||||
for i in range(len(word)):
|
for i in range(len(word)):
|
||||||
self.graphical_progress_string += "_"
|
self.graphical_progress_string += "_"
|
||||||
|
|
||||||
self.guesses = 10
|
self.guesses = 26
|
||||||
self.letters_guessed = 0
|
self.letters_guessed = 0
|
||||||
|
|
||||||
# Game state
|
# Game state
|
||||||
@ -24,18 +24,20 @@ class HangFish():
|
|||||||
self.status_message = ""
|
self.status_message = ""
|
||||||
|
|
||||||
def guess(self, c):
|
def guess(self, c):
|
||||||
for i in range(len(self.word)):
|
if not self.running: return
|
||||||
if self.word[i] == c:
|
if len(c) == 1:
|
||||||
if not self.guessed_indices[i]:
|
for i in range(len(self.word)):
|
||||||
self.letters_guessed += 1
|
if self.word[i] == c:
|
||||||
temp_list = list(self.graphical_progress_string)
|
if not self.guessed_indices[i]:
|
||||||
temp_list[i] = c
|
self.letters_guessed += 1
|
||||||
self.graphical_progress_string = ''.join(temp_list)
|
temp_list = list(self.graphical_progress_string)
|
||||||
self.guessed_indices[i] = True
|
temp_list[i] = c
|
||||||
|
self.graphical_progress_string = ''.join(temp_list)
|
||||||
|
self.guessed_indices[i] = True
|
||||||
|
|
||||||
self.guesses -= 1
|
self.guesses -= 1
|
||||||
|
|
||||||
word_guessed = self.letters_guessed == len(self.word)
|
word_guessed = self.letters_guessed == len(self.word) or c == self.word
|
||||||
out_of_guesses = self.guesses == 0
|
out_of_guesses = self.guesses == 0
|
||||||
|
|
||||||
if word_guessed or out_of_guesses:
|
if word_guessed or out_of_guesses:
|
||||||
@ -46,21 +48,23 @@ class HangFish():
|
|||||||
self.status_message = "Out of Guesses."
|
self.status_message = "Out of Guesses."
|
||||||
|
|
||||||
def getString(self):
|
def getString(self):
|
||||||
return self.graphical_string + 'guesses left = {}\n'.format(self.guesses) + self.graphical_progress_string
|
string = ""
|
||||||
|
string += self.graphical_string + 'guesses left = {}\n'.format(self.guesses) + self.graphical_progress_string + '\n'
|
||||||
|
if not self.running: string += self.status_message
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
hangFish = HangFish(None, "Gen")
|
hangfish = Hangfish("Gen")
|
||||||
|
|
||||||
print(hangFish.getString())
|
print(hangfish.getString())
|
||||||
while (hangFish.running):
|
while (hangfish.running):
|
||||||
try:
|
try:
|
||||||
letter = str(input("Enter Letter\n"))[0]
|
letter = str(input("Enter Letter\n"))[0]
|
||||||
hangFish.guess(letter)
|
hangfish.guess(letter)
|
||||||
print(hangFish.getString())
|
print(hangfish.getString())
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("Please enter a letter.")
|
print("Please enter a letter.")
|
||||||
|
|
||||||
print (hangFish.status_message)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user