-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
246 lines (189 loc) · 9.98 KB
/
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#NEIL AND ABHINAV WORDLE PROJECT
import random
import string
import math
import sys #used to forcibly quit program
from setting import Setting
from wordbank import WordBank
from wordleword import WordleWord
from wordleplayer import WordlePlayer
from fancyword import FancyWord
from rule import rules
from ranks import Ranks
# testing github
#======
# markGuess - will "mark" the guess and the alphabet according to the word
# word - String of word to be guessed
# guess - WordleWord that have been guessed
# alphabet - WordleWord of the letters a-z that have been marked
#======
#======
# playRound(players, words, all_words, settings)
# Plays one round of Wordle.
# Returns nothing, but modifies the player statistics at end of round
#
# players - List of WordlePlayers
# words - Wordbank of the common words to select from
# all_words - Wordbank of the legal words to guess
# settings - Settings of game test
#======
def markGuess(word, guess, alphabet, hardmode):
if (hardmode == False): #normal mode is colored here(as yellow must be there)
g = guess.getWord()
w = word
a = alphabet.getWord()
for idx in range(len(g)): #using double loop, one for guess and the other for the actual word so
for jdx in range(len(w)):
if (guess.getWord()[idx] == w[idx]): #marks letter green
guess.setCorrect(idx)
alphaidx= alphabet.getWord().find(guess.getWord()[idx]) #gets word from alphabet object and finds the character at position idx
if alphabet.colorAt(alphaidx) != "gray": #makes sure word is not gray before coloring green(conflicts occur otherwise)
alphabet.setCorrect(alphaidx)
break
if (guess.getWord()[idx] == w[jdx]): #makrs letter yellow
guess.setMisplaced(idx)
alphaidx= alphabet.getWord().find(guess.getWord()[idx])
if alphabet.colorAt(alphaidx) != "green": #makes sure its not green before making yellow
alphabet.setMisplaced(alphaidx)
break
if (guess.getWord()[idx] != w[jdx]): #marks gray
alphaidx1= alphabet.getWord().find(guess.getWord()[idx])
if ((alphabet.colorAt(alphaidx1) != "green") and (alphabet.colorAt(alphaidx1) != "yellow")): #makes sure not green or yellow before passing
alphabet.setNotUsed(alphaidx1)
#coloring for hard mode(no yellow)
else:
g = guess.getWord()
w = word
a = alphabet.getWord()
for idx in range(len(g)): #using double loop, one for guess and the other for the actual word so
for jdx in range(len(w)):
if (guess.getWord()[idx] == w[idx]): #marks letter green
guess.setCorrect(idx)
alphaidx= alphabet.getWord().find(guess.getWord()[idx]) #gets word from alphabet object and finds the character at position idx
if alphabet.colorAt(alphaidx) != "gray": #makes sure word is not gray before coloring green
alphabet.setCorrect(alphaidx)
break
if (guess.getWord()[idx] != w[jdx]): #marks gray
alphaidx1= alphabet.getWord().find(guess.getWord()[idx])
if ((alphabet.colorAt(alphaidx1) != "green") and (alphabet.colorAt(alphaidx1) != "yellow")): #makes sure not green or yellow before passing
alphabet.setNotUsed(alphaidx1)
def playRound(player, words, all_words, settings, hardmode):
ranWord = words.getRandom() #getting random word
print(ranWord)
alphaObj = WordleWord("abcdefghijklmnopqrstuvwxyz") #alphabet object
i = 0 #loop variable
guessObjList = []
while i < 6:
player_guess = input("Enter your guess!: ")
if (len(player_guess) or not all_words.contains(player_guess)): #checks if word doesn't have 5 letters or not in file of words
while len(player_guess) != 5 or not all_words.contains(player_guess): #while so that it keeps asking if player hasnt provided a proper word
print("Our system has detected that you have given an incorrect word: ")
player_guess = input("Please guess a valid 5 letter word: ")
i += 0 #making sure incorrect words do not count towards the total six tries
if len(player_guess) == 5 and all_words.contains(player_guess):
player_guess = player_guess
player_guess_obj = WordleWord(player_guess)
guessObjList.append(player_guess_obj)
print("\n")
if player_guess == ranWord: #goes here if players's guess complety matches string-wise(doesn't color yet)
i = i + 1 #this if checks if the player guess string is == to the ranword
markGuess(ranWord, player_guess_obj, alphaObj, hardmode) #colors it al green
indexval = 0
for p in guessObjList: #making sure it prints all the previous guesses
indexval = indexval + 1
print(indexval,":",p,"\n") #index val is used to number guess
print(" Alphabet ",alphaObj,"\n")
print("Congratulations! You have guessed the correct word")
print("")
player[0].updateStats(True, i) #updating stats
player_rank = Ranks(True) #updating rank and trophies
player_rank.trophies_update(i, hardmode)
player_rank.current_rank_update(player_rank.get_score())
print("Trophies: " + str(player_rank.get_score()))
print("Rank: " + player_rank.get_rank())
return #in order to exit out of function
else: #goes here if word doesn't match
markGuess(ranWord, player_guess_obj, alphaObj, hardmode)
indexval = 0#for numbering guess
for p in guessObjList:
indexval = indexval + 1
print(indexval,":",p,"\n")
print(" Alphabet ",alphaObj,"\n")
i+=1 #adds to the number of tries
#comes here if maximum tries has been exceeded
print("Sorry, you weren't able to guess the word within the allowed number of attempts")
print("The correct word was: " + ranWord )
player[0].updateStats(False, 0)
def playWordle():
# initialize WordBanks
all_words = WordBank("words_alpha.txt")
five_letter_words = WordBank("common5letter.txt")
# intialize settings to the baseline settings
settings = Setting()
settings.setSetting('maxguess', 6)
settings.setSetting('numplayers', 1)
settings.setSetting('difficulty', 'normal')
#167-191 is intro code block
print("Let's play the game of Wordle!!")
name = input("Enter your name: ")
ready_to_play = input("Welcome " + name + " Do you wish to play? (y/n) or would you like to know the rules first: ") #part 1 of intro, welcoming
if ready_to_play == "y":
print("Loading You In To The Game . . .")
print("")
print("Ok, you may begin. Please enter your first guess into the following line")
print("")
elif ready_to_play == "n":
print("Exiting You Out Of The Game . . .")
return
elif ready_to_play == "r": #player can request for rules
rules()
else:
new_attempt = ""
while (new_attempt != "y" and new_attempt != "n" and new_attempt != "r"):
new_attempt = input("That is not a valid input, please try again!: ")
if new_attempt == "y":
print("Loading You In To The Game . . .")
elif new_attempt == "n":
print("Exiting You Out Of The Game . . .")
return
elif new_attempt == "r":
rules()
# make the player
#actually created the player object here
player = WordlePlayer(name, 6)
player_list = []
player_list = player_list + [player]
#asks player if they would like to play in hardmode or normal mode
#NOTE-Player cannot revert back, unless program is completely killed
hardmode = input("Would you like to activate hard mode?(y/n) There is no turning back from here ")
if hardmode == "n":
playRound(player_list, five_letter_words, all_words, settings, False)
elif hardmode == "y":
# start playing rounds of Wordle
playRound(player_list, five_letter_words, all_words, settings, True)
else:
while (hardmode!= "y" and hardmode != "n" and hardmode != "r"): #checking if input is not y, n or r
hardmode = input("That is not a valid input, please try again!: ")
if hardmode == "n":
playRound(player_list, five_letter_words, all_words, settings, False)
elif hardmode == "y":
# start playing rounds of Wordle
playRound(player_list, five_letter_words, all_words, settings, True)
# Finding out of another round is being player and end game by displaying player stats
play_again = input("Would you like to play another round?(y/n): ") #asks after first round played by player if more than multiple rounds are played, it shifts to the while loop below
#play_again_bool = True
if play_again.lower() == "n":
player.displayStats()
while play_again.lower() == "y":
if hardmode == "n":
playRound(player_list, five_letter_words, all_words, settings, True)
else:
# start playing rounds of Wordle
playRound(player_list, five_letter_words, all_words, settings, False)
play_again = input("Would you like to play another round?: ")
if play_again.lower() == "n":
player.displayStats()
def main():
playWordle()
if __name__ == "__main__":
main()