-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
71 lines (63 loc) · 3.31 KB
/
gui.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
import os
from fancy_print import FancyPrint
class GUI:
def __init__(self) -> None:
# for better visualization, an instance of "FancyPrint" is needed
self.__fancy_print = FancyPrint()
# this method simply prints the text; this makes the GUI more modular and exchangeable
def text(self, message, line_end: str = '\n'):
print(message, end=line_end)
# use "fancy_print" for displaying colorful text (works only in PyCharm)
def text_blue(self, message: str, line_end: str = '\n') -> None:
if "PYCHARM_HOSTED" in os.environ:
self.__fancy_print.text_blue(message, line_end)
else:
print(message, end=line_end)
def text_red(self, message: str, line_end: str = '\n') -> None:
if "PYCHARM_HOSTED" in os.environ:
self.__fancy_print.text_red(message, line_end)
else:
print(message, end=line_end)
def text_yellow(self, message: str, line_end: str = '\n') -> None:
if "PYCHARM_HOSTED" in os.environ:
self.__fancy_print.text_yellow(message, line_end)
else:
print(message, end=line_end)
def clear_display(self):
if "PYCHARM_HOSTED" in os.environ:
# in PyCharm, the console is "cleared" by printing many line breaks
print('\n' * 100)
else:
# depending on the operating system, different commands are executed
os.system("cls" if os.name == "nt" else "clear")
def show_board(self, board: dict, width: int, height: int, id_to_symbol: dict, winning_line: list = None) -> None:
# firstly, print column names (usually from 1 to 7)
self.text(' ' + ' '.join(map(str, range(1, width + 1))))
# if a player has won, highlight the winning line
if winning_line is None:
# because (0, 0) is the bottom-left corner of board, y has to iterate backwards through rows
for y in range(height - 1, -1, -1):
# display left barrier
self.text("", '|')
# print player symbols instead of player id (unoccupied fields are represented as spaces) for each field
# columns are separated using the pipe symbol
for x in range(width):
self.text(id_to_symbol[board[(x, y)]], '|')
# add a line break so that next row starts in next line
self.text("")
else:
# because (0, 0) is the bottom-left corner of board, y has to iterate backwards through rows
for y in range(height - 1, -1, -1):
# display left barrier
self.text("", '|')
# print player symbols instead of player id (unoccupied fields are represented as spaces) for each field
# columns are separated using the pipe symbol
for x in range(width):
if (x, y) in winning_line:
# highlight the symbol on this field to show part of the winning line
self.text_yellow(id_to_symbol[board[(x, y)]], '|')
else:
# a standard field is printed with default font style
self.text(id_to_symbol[board[(x, y)]], '|')
# add a line break so that next row starts in next line
self.text("")