-
Notifications
You must be signed in to change notification settings - Fork 15
/
words_api.py
52 lines (40 loc) · 1.32 KB
/
words_api.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
import random
class Words:
def __init__(self, size):
self.size = size
self.words_list = []
self.used_words = []
self.word = ""
self.load_words()
self.select_word()
def load_words(self):
if self.size == 3:
file_name = 'three_letters'
elif self.size == 4:
file_name = 'four_letters'
elif self.size == 5:
file_name = 'five_letters'
else:
file_name = 'six_letters'
with open(f"word_files/{file_name}.txt", 'r') as file:
self.words_list = file.readlines()
self.words_list = [word.strip('\n') for word in self.words_list]
def is_at_right_position(self, i, char):
if self.word[i] == char:
return True
return False
def is_in_word(self, char):
if char in self.word:
return True
return False
def is_valid_guess(self, guess):
if guess == self.word:
return True
return False
def select_word(self):
self.word = random.choice(self.words_list).upper()
while self.word in self.used_words:
self.word = random.choice(self.words_list).upper()
self.used_words.append(self.word)
def display_right_word(self):
print("Right word was : ", self.word)