-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe-cli2.py
55 lines (43 loc) · 1.42 KB
/
tic-tac-toe-cli2.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):
print(" " + board[0] + " | " + board[1] + " | " + board[2])
print("---|---|---")
print(" " + board[3] + " | " + board[4] + " | " + board[5])
print("---|---|---")
print(" " + board[6] + " | " + board[7] + " | " + board[8])
def check_win(board):
# Check rows
for i in range(0, 9, 3):
if board[i] == board[i+1] == board[i+2] != " ":
return True
# Check columns
for i in range(3):
if board[i] == board[i+3] == board[i+6] != " ":
return True
# Check diagonals
if board[0] == board[4] == board[8] != " ":
return True
if board[2] == board[4] == board[6] != " ":
return True
return False
def tic_tac_toe():
board = [" "] * 9
players = ["X", "O"]
current_player = 0
while True:
print_board(board)
print("Player", players[current_player], "turn:")
move = int(input()) - 1
if board[move] != " ":
print("Invalid move! Try again.")
continue
board[move] = players[current_player]
if check_win(board):
print_board(board)
print("Player", players[current_player], "wins!")
break
if " " not in board:
print_board(board)
print("Tie!")
break
current_player = (current_player + 1) % 2
tic_tac_toe()