-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe-numpy.py
More file actions
64 lines (47 loc) · 1.64 KB
/
tictactoe-numpy.py
File metadata and controls
64 lines (47 loc) · 1.64 KB
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
import numpy as np
def create_board():
return np.zeros((3, 3), dtype=int)
def print_board(board):
symbols = {0: " ", 1: "X", 2: "O"}
for row in board:
print(" | ".join(symbols[val] for val in row))
print("---------")
def is_winner(board, player):
# Check rows, columns, and diagonals for a win
return (
np.any(np.all(board == player, axis=1))
or np.any(np.all(board == player, axis=0)) # rows
or np.all(np.diag(board) == player) # columns
or np.all(np.diag(np.fliplr(board)) == player) # main diagonal
) # anti-diagonal
def is_board_full(board):
return not any(0 in row for row in board)
def take_turn(board, player):
while True:
try:
row = int(input(f"Player {player}, enter the row (0, 1, 2): "))
col = int(input(f"Player {player}, enter the column (0, 1, 2): "))
if 0 <= row <= 2 and 0 <= col <= 2 and board[row][col] == 0:
board[row][col] = player
break
else:
print("Invalid move. Try again.")
except ValueError:
print("Invalid input. Please enter a number.")
def play_game():
board = create_board()
player = 1
while True:
print_board(board)
take_turn(board, player)
if is_winner(board, player):
print_board(board)
print(f"Player {player} wins!")
break
elif is_board_full(board):
print_board(board)
print("It's a tie!")
break
player = 3 - player # Switch player (1 -> 2 or 2 -> 1)
if __name__ == "__main__":
play_game()