-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.txt
148 lines (122 loc) · 4.03 KB
/
code.txt
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import csv
import random
#Function check if a letter is in the word
def checkWord (letter, word):
if letter in word:
return True
else:
return False
#combine previous with current array
def combineArray (array1, array2):
comboArray=[]
for item in range (len(array1)):
if array1[item] != '_ ':
comboArray.append(array1[item])
elif array2[item] != '_ ':
comboArray.append(array2[item])
else:
comboArray.append('_ ')
return comboArray
#changing an array into a string
def arrayToString (array):
string=""
for item in array:
string+=item;
return string
#string to array
def stringToArray (string):
array=[]
for letter in string:
array.append(letter)
return array
#validate input is a letter within the remaining letters
def validateGuess (letter, array):
while (letter not in array):
print("Invalid guess.")
letter=input('Guess a letter: ').lower()
return letter
#ask if user wants to guess; validation
def askGuessWord():
guess=input('Do you want to guess the word?(Y/N) ').lower()
while ((guess != "y") and (guess != "n")):
guess=input("Invalid input. Enter (y/n) only: ").lower()
return guess
#checking if the word is completed and out of "_ "
def checkCompletion(array):
for char in array:
if (char=="_ "):
return False
return True
def getWord():
wordBank=[]
count=0
#open file and store words in an array
with open('word_list.txt') as csvFile:
csvReader=csv.reader(csvFile, delimiter=',')
for row in csvReader:
wordBank.append(row[0])
count+=1
#generate a random number
selection=random.randint(0, count-1)
wordChosen=wordBank[selection]
return wordChosen
#starting the game
answer=getWord()
lives=6
alphabetArray=['a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z']
print('Lives: '+ str(lives), end='\n\n')
answerArray=stringToArray(answer)
#starting off with dashes
initialProgress=[]
for item in answerArray:
initialProgress.append('_ ')
correct=False
complete=False
#playing the game
while ((lives > 0) and (not correct) and (not complete)):
print ('Remaining Letters: ', end='')
print (alphabetArray)
letterGuess=input('Guess a letter: ').lower()
letterGuess=validateGuess(letterGuess, alphabetArray)
newProgress=[]
#if letter is in word, append
if checkWord(letterGuess, answer):
for letter in answerArray:
if (letterGuess==letter):
newProgress.append(letterGuess)
else:
newProgress.append('_ ')
#combined array
initialProgress = combineArray(initialProgress, newProgress)
print(arrayToString(initialProgress))
#if not in word, minus 1 life display life
else:
lives-=1
print('The letter you entered was not part of the answer.')
print('You just lost a life.')
print('Now you have ' + str(lives) + ' lives left.')
#removed the guessed letter
alphabetArray.remove(letterGuess)
#check if word is completed
complete=checkCompletion(initialProgress)
#ask "do you want to guess word"
if(not complete):
boolGuessWord=askGuessWord()
if boolGuessWord=='y':
guessWord=input('What\'s your guess? ').lower()
if guessWord==answer:
print('Correct! You won!')
correct=True
#do not display negative life count from guessing word wrong
elif (lives>=1):
print('You lost a life for guessing the word incorrectly.')
lives-=1
print('You now have '+ str(lives) + ' lives remaining.')
print(end='\n\n\n')
print('******************************************************************************')
if (lives==0):
print('Game Over. You lost.')
if (complete):
print('You won!')