/
ui.py
177 lines (144 loc) · 5.12 KB
/
ui.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
import re
import os
class UI:
@staticmethod
def input_nick():
nick = input("Enter your nickname: ")
while not UI.is_valid_nickname(nick):
nick = input("Enter your nickname (min. 3 chars): ")
return nick
@classmethod
def print_nick_is_taken(cls):
print('Nick is already taken. Choose another one.')
@staticmethod
def is_valid_nickname(nick):
return bool(re.match("\w{3,}$", nick))
@classmethod
def select_player_from_list(cls, player_list):
cls.print_player_table(player_list)
return cls.universal_number_input(len(player_list))
@classmethod
def print_player_table(cls, player_list):
cls.print_universal_number_list(player_list)
@staticmethod
def print_select_player_kinds():
select_player_kinds = ['New player', 'Player from players list']
UI.print_universal_number_list(select_player_kinds)
@classmethod
def input_select_player_kind(cls):
player_kind = cls.universal_number_input(2)
if player_kind == "1":
return "new"
if player_kind == "2":
return "from list"
@classmethod
def universal_number_input(cls, max_input_number):
number_input = input("Enter number: ").strip()
while not cls.is_valid_input_number(number_input, max_input_number):
number_input = input("Enter number between 1 and {}: ".format(max_input_number))
return number_input
@staticmethod
def is_valid_input_number(number_input, max_input_number):
return number_input.isnumeric() and int(number_input) <= max_input_number
@staticmethod
def print_try_kinds():
try_kinds = ['Enter letter', 'Enter all word']
UI.print_universal_number_list(try_kinds)
@staticmethod
def print_universal_number_list(list_to_print):
for index, text in enumerate(list_to_print):
print('({}) {}'.format(index+1, text))
@classmethod
def input_try_kind(cls):
try_kind = cls.universal_number_input(2)
if try_kind == "1":
return "letter"
if try_kind == "2":
return "word"
@classmethod
def input_letter(cls):
letter = input('Enter letter: ').strip()
while not cls.is_valid_letter(letter):
letter = input('Enter letter: ').strip()
return letter.upper()
@staticmethod
def is_valid_letter(letter):
return bool(re.match(r'^[A-Za-z]$', letter))
@classmethod
def input_all_word(cls):
word = input('Enter word: ').strip()
while not cls.is_valid_word(word):
word = input('Enter word (only letters and spaces): ').strip()
return word.upper()
@staticmethod
def is_valid_word(word):
return bool(re.match(r'[A-Za-z ]+$', word))
@staticmethod
def print_lives(lives):
print("You have {} live{}".format(lives, 's' if lives > 1 else ''))
@staticmethod
def print_dashes(dashes):
print(' '.join(list(dashes)))
@staticmethod
def screen_clear():
os.system('clear')
@staticmethod
def print_used_letters(used_letters):
print('Used letter: {}'.format(', '.join(sorted(list(set(used_letters))))))
@staticmethod
def print_attempts_counts(attempts):
print('Attempt no. {}.'.format(attempts))
@classmethod
def input_is_continue(cls):
to_is_continue = cls.universal_number_input(2)
if to_is_continue == "1":
return "yes"
if to_is_continue == "2":
return "no"
@staticmethod
def print_to_continue_question():
print("Next game?")
to_continue_answers = ['Yes', 'No']
UI.print_universal_number_list(to_continue_answers)
@staticmethod
def print_win_information(correct_word):
print("You won!")
print("{} was word to guess".format(correct_word))
@staticmethod
def print_fail_information(correct_word):
print("You failed!")
print("{} was word to guess".format(correct_word))
@classmethod
def start_screen(cls):
cls.screen_clear()
cls.print_game_title()
cls.print_game_options()
@classmethod
def print_game_title(cls):
print("HANGMAN")
@classmethod
def print_game_options(cls):
# game_options = Game.get_game_options()
game_options = ['Start game', 'Help', 'Score table', 'Credits', 'Exit']
cls.print_universal_number_list(game_options)
@classmethod
def input_game_options(cls):
# game_options = Game.get_game_options()
game_options = ['Start game', 'Help', 'Score table', 'Credits', 'Exit']
return cls.universal_number_input(len(game_options))
@classmethod
def print_help(cls):
cls.screen_clear()
print("Help here!")
cls.press_enter_to_continue()
@classmethod
def print_credits(cls):
cls.screen_clear()
print("Credits_here!")
cls.press_enter_to_continue()
@classmethod
def press_enter_to_continue(cls):
input("Press enter to continue")
@classmethod
def print_goodbye(cls):
print("See you next time!")