-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
59 lines (43 loc) · 1.88 KB
/
tictactoe.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
from games.tictactoe import TicTacToe
from helpers import validate_input
def main(board_obj: TicTacToe):
"""
The main function to play a game of Tic Tac Toe.
:param board_obj: The Board object to run the game on.
:return:
"""
print("Welcome to Tic Tac Toe.")
print("Possible moves are:")
board_obj.print_move_grid()
while board_obj.active:
# Print a newline.
print()
# Get the move the player made.
move = input(f"{board_obj.get_player_icon(board_obj.current_player)} > ")
# Check the validity of the move.
while not board_obj.check_move_validity(move):
print("Invalid move.")
move = input(f"{board_obj.get_player_icon(board_obj.current_player)} > ")
# Convert the move to an int.
move = int(move)
# Make the move.
board_obj.make_move(move)
# Notify the player of the new board.
board_obj.print_grid()
# Print the winner.
print("\nThe game ended!")
print(f"Player {board_obj.get_player_icon(board_obj.winner)} won!")
if __name__ == '__main__':
try:
# Ask the user for parameters.
grid_size = int(validate_input("Size of the grid: ", lambda x: x.isnumeric()))
amount_of_players = int(validate_input("Amount of players: ", lambda x: x.isnumeric()))
player_symbols = validate_input("Enter the symbols for the players, separated by space.\n", lambda x: len(set(x.split())) >= amount_of_players).split()[:amount_of_players]
player_symbols = [x[:1] for x in player_symbols] # Only use the first character of each symbol.
# Create the Board class
board = TicTacToe(grid_size=grid_size, players=amount_of_players, player_symbols=player_symbols)
# Run the main function.
main(board)
except KeyboardInterrupt:
print("\n\nThanks for playing.")
exit(0)