-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
55 lines (45 loc) · 1.56 KB
/
index.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
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def check_winner(board):
# Check rows
for row in board:
if row[0] == row[1] == row[2] != ' ':
return row[0]
# Check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] != ' ':
return board[0][col]
# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != ' ':
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] != ' ':
return board[0][2]
return None
def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
players = ['X', 'O']
current_player = 0
print("Let's play Tic-Tac-Toe!")
while True:
print_board(board)
print(f"Player {players[current_player]}'s turn")
row = int(input("Enter row (0, 1, or 2): "))
col = int(input("Enter column (0, 1, or 2): "))
if board[row][col] == ' ':
board[row][col] = players[current_player]
winner = check_winner(board)
if winner:
print_board(board)
print(f"Player {winner} wins!")
break
elif all(' ' not in row for row in board):
print_board(board)
print("It's a tie!")
break
current_player = (current_player + 1) % 2
else:
print("That cell is already taken. Try again.")
if __name__ == "__main__":
tic_tac_toe()