-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.py
117 lines (102 loc) · 3.26 KB
/
view.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
"""Handles the user interface for the game."""
import os
class GameView:
""" Handles the produced output"""
@staticmethod
def display_start_screen():
"""
Display the start screen of the game.
"""
print(r"""
_______ _ _______ _______
|__ __(_) |__ __| |__ __|
| | _ ___| | __ _ ___| | ___ ___
| | | |/ __| |/ _` |/ __| |/ _ \ / _\
| | | | (__| | (_| | (__| | (_) | __/
|_| |_|\___|_|\__,_|\___|_|\___/ \___|
by Christof & Manuel
""")
@staticmethod
def display_menu():
"""
Display the main menu of the game.
"""
print("""
Welcome to Tic Tac Toe
1) New game
2) Load a saved game
3) Quit
""")
@staticmethod
def display_message(message):
"""
Display a message to the user.
Args:
message: The message to display.
"""
print(message)
@staticmethod
def clear_screen():
"""
Clear the terminal screen.
"""
os.system('cls' if os.name == 'nt' else 'clear')
@staticmethod
def input_prompt(prompt):
"""
Prompt the user for input.
Args:
prompt: The prompt to display to the user.
Returns:
The user's input as a string.
"""
return input(prompt)
@staticmethod
def choose_move(board, player):
"""
Prompt the user to choose a move.
Args:
board: The current state of the game board.
player: The current player.
Returns:
The user's chosen move as a tuple of (row, column),
or 'save' if the user chooses to save the game.
"""
print("To save the game, type 'save' at any time. \n")
print(f"{player.player_type}'s turn.")
while True:
row = input('Enter row [1-3]: ')
if row.lower() == 'save':
return 'save'
if not row.isdigit() or not 1 <= int(row) <= 3:
print("Invalid input. Please enter a number between 1 to 3.")
continue
column = input('Enter column [1-3]: ')
if column.lower() == 'save':
return 'save'
if not column.isdigit() or not 1 <= int(column) <= 3:
print("Invalid input. Please enter a number between 1 to 3.")
continue
index = (int(row) - 1) * 3 + (int(column) - 1)
if board.board[index] != board.BOARD_EMPTY:
print('Space already taken. Try again.')
else:
break
return row, column
@staticmethod
def print_board(board):
"""
Print the current state of the game board.
Args:
board: The current state of the game board.
"""
symbols = {board.BOARD_PLAYER_X: 'X', board.BOARD_PLAYER_O: 'O',
board.BOARD_EMPTY: ' '}
print('\n+---+---+---+')
for i in range(0, 9, 3):
row = []
for j in range(3):
# Fallback for unexpected values
row.append(symbols.get(board.board[i+j], '?'))
print('| ' + ' | '.join(row) + ' |')
print('+---+---+---+')