-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_game.py
110 lines (95 loc) · 3.24 KB
/
target_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
"""
Module of game about making words
"""
from typing import List
import random
def generate_grid() -> List[List[str]]:
"""
Generates list of lists of letters - i.e. grid for the game.
e.g. [['I', 'G', 'E'], ['P', 'I', 'S'], ['W', 'M', 'G']]
>>> random.seed(2190)
>>> generate_grid()
[['S', 'L', 'B'], ['H', 'X', 'V'], ['S', 'Q', 'R']]
"""
grid = []
for i in range(3):
grid.append([])
for j in range(3):
j += 0
grid[i].append(chr(random.randint(65, 90)))
return grid
def get_words(path: str, letters: List[str]) -> List[str]:
"""
Reads the file f. Checks the words with rules and returns a list of words.
>>> get_words("en.txt", ['f', 'r', 'l', 'h', 'k', 'd', 'z', 'g', 'i'])
['dirk', 'firk', 'khir']
"""
good_words = []
main_letter = letters[4]
with open(path, "r", encoding="utf-8") as dictionary:
lines = dictionary.readlines()
for i in range(3, len(lines)):
lines[i] = lines[i].lower().strip()
if (main_letter in lines[i]) and len(lines[i]) >= 4:
check = 1
for letterl in lines[i]:
if (letterl not in letters) or lines[i].count(letterl) > letters.count(letterl):
check = 0
if check == 1:
good_words.append(lines[i])
return good_words
def get_user_words() -> List[str]:
"""
Gets words from user input and returns a list with these words.
Usage: enter a word or press ctrl+d to finish.
Don't know, how to write this doctest, how to use shortcuts.
"""
words = []
while True:
try:
words.append(input())
except EOFError:
break
return words
def get_pure_user_words(user_words: List[str], letters: List[str],\
words_from_dict: List[str]) -> List[str]:
"""
(list, list, list) -> list
Checks user words with the rules and returns list of those words
that are not in dictionary.
>>> get_pure_user_words(['dirk', 'firk', 'frlhk'],\
['f', 'r', 'l', 'h', 'k', 'd', 'z', 'g', 'i'],\
['dirk', 'firk', 'khir'])
['frlhk']
"""
pure_words = []
main_letter = letters[4]
for i, _ in enumerate(user_words):
user_words[i] = user_words[i].lower().strip()
if (main_letter in user_words[i]) and len(user_words[i]) >= 4\
and (user_words[i] not in words_from_dict):
check = 1
for letterl in user_words[i]:
if (letterl not in letters) or \
user_words[i].count(letterl) > letters.count(letterl):
check = 0
if check == 1:
pure_words.append(user_words[i])
return pure_words
def results():
"""
Prints results
>>> print("some results")
some results
"""
letters_init = generate_grid()
letters_end = []
for i in range(3):
letters_end += letters_init[i]
for i, letter in enumerate(letters_end):
letters_end[i] = letter.lower()
dictionarym = get_words("en.txt", letters_end)
user_words_list = get_user_words()
get_pure_user_words(user_words_list, letters_end, dictionarym)
if __name__ == "__main__":
results()