-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathttt.py
More file actions
52 lines (40 loc) · 1.39 KB
/
ttt.py
File metadata and controls
52 lines (40 loc) · 1.39 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
def display_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)
def check_win(board, player):
for row in board:
if all(cell == player for cell in row):
return True
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)):
return True
return False
def tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
players = ["X", "O"]
current_player = 0
print("Welcome to Tic-Tac-Toe!")
for _ in range(9):
display_board(board)
print(f"Player {players[current_player]}'s turn")
while True:
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
if board[row][col] == " ":
board[row][col] = players[current_player]
break
else:
print("That position is already taken. Try again.")
if check_win(board, players[current_player]):
display_board(board)
print(f"Player {players[current_player]} wins!")
break
current_player = 1 - current_player
else:
display_board(board)
print("It's a tie!")
if __name__ == "__main__":
tic_tac_toe()