-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
60 lines (49 loc) · 1.38 KB
/
Copy pathtictactoe.py
File metadata and controls
60 lines (49 loc) · 1.38 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
import sys
board = [
"1", "2", "3",
"4", "5", "6",
"7", "8", "9"
]
players = ["X", "O"]
def print_board() -> None:
print(f" {board[0]} ┃ {board[1]} ┃ {board[2]}")
print("━━━╋━━━╋━━━")
print(f" {board[3]} ┃ {board[4]} ┃ {board[5]}")
print("━━━╋━━━╋━━━")
print(f" {board[6]} ┃ {board[7]} ┃ {board[8]}")
def check_win():
for player in players:
for i in range(3):
if (
board[0 + i * 3] == board[1 + i * 3] == board[2 + i * 3] == player
or board[i + 0] == board[i + 3] == board[i + 6] == player
or board[0] == board[4] == board[8] == player
or board[2] == board[4] == board[6] == player
):
return player
return False
def check_stale():
for i in board:
if i not in players:
return False
return True
print_board()
while True:
for player in players:
while True:
try:
chosen_pos = int(input("Wähle ein Feld ")) - 1
if board[chosen_pos] not in players:
board[chosen_pos] = player
break
except (ValueError, IndexError):
pass
print("Invalid position!")
print_board()
winstatus = check_win()
if winstatus:
print(f"Player {winstatus} won!")
sys.exit()
elif check_stale():
print("Stalemate!")
sys.exit()