-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
93 lines (85 loc) · 3.17 KB
/
tic_tac_toe.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
# @TODO: can create game plate with 3 cols or greater
# @TODO: Add computer turn
def tic_tac_toe(player: str):
"""
:param player:
:return: [win, points, winner, game]
"""
points = 0
win = 0
player_turn = 1
game_plate = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
print("You are player 1\n")
while win == 0 and win != "tie":
empty_cell = 0
print(f"Player {player_turn}'s turn")
display_game_plate(game_plate)
player_coordinate_x = input('Please type coordinate for the row you choose\n')
player_coordinate_y = input('Please type coordinate for the column you choose\n')
coordinates = verify_coordinate(player_coordinate_x, player_coordinate_y, game_plate)
if coordinates:
x = coordinates[0]
y = coordinates[1]
game_plate[x][y] = player_turn
display_game_plate(game_plate)
win = check_win_condition(game_plate)
if win != 0:
print(f"Player {player_turn} wins!")
if player_turn == 1:
points = 1
break
player_turn *= -1
else:
print("Invalid move. Try again.")
# check tie
for i in range(3):
for j in range(3):
if game_plate[i][j] == game_plate[i][j] == game_plate[i][j] == 0:
empty_cell += 1
if empty_cell == 0:
print("it's a tie")
win = "tie"
return [win, points, player, "tic tac toe"]
def verify_coordinate(player_coordinate_x: str, player_coordinate_y: str, game_plate):
"""
This function checks the user's input and returns it if valid.
:param player_coordinate_x: The x-coordinate input by the player.
:param player_coordinate_y: The y-coordinate input by the player.
:param game_plate: The current game board.
:return: Tuple of coordinates if valid, otherwise False.
"""
if player_coordinate_x.isdigit() and player_coordinate_y.isdigit():
x = int(player_coordinate_x)
y = int(player_coordinate_y)
if x in (0, 1, 2) and y in (0, 1, 2):
if game_plate[x][y] == 0:
return x, y
else:
print("Cell already taken. Choose another coordinate.")
return False
def display_game_plate(game_plate):
for row in game_plate:
print(" | ".join(str(cell) if cell != 0 else " " for cell in row))
print("-" * 10)
def check_win_condition(game_plate):
"""
Check the game board for a win condition.
:param game_plate: The current game board.
:return: The winning player number, or 0 if no winner.
"""
# Check rows and columns
for i in range(3):
if game_plate[i][0] == game_plate[i][1] == game_plate[i][2] != 0:
return game_plate[i][0]
if game_plate[0][i] == game_plate[1][i] == game_plate[2][i] != 0:
return game_plate[0][i]
# Check diagonals
if game_plate[0][0] == game_plate[1][1] == game_plate[2][2] != 0:
return game_plate[0][0]
if game_plate[0][2] == game_plate[1][1] == game_plate[2][0] != 0:
return game_plate[0][2]
return 0