-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.py
95 lines (77 loc) · 2.2 KB
/
tic-tac-toe.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from termcolor import colored
board = list(range(1, 10))
winners = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
moves = ((1, 7, 3, 9), (5,), (2, 4, 6, 8))
def print_board():
j = 1
for i in board:
end = " "
if j % 3 == 0:
end = "\n\n"
if i == "X":
print(colored(f"[{i}]", "red"), end=end)
elif i == "O":
print(colored(f"[{i}]", "blue"), end=end)
else:
print(f"[{i}]", end=end)
j += 1
def has_empty_space():
return board.count("X") + board.count("O") != 9
player, computer = "X", "O"
print("Player: x\nComputer: o")
def can_move(brd, mve):
if mve in range(1, 10) and isinstance(brd[mve - 1], int):
return True
return False
def is_winner(brd, plyr):
win = True
for tup in winners:
win = True
for j in tup:
if brd[j] != plyr:
win = False
break
if win:
break
return win
def make_move(brd, plyr, mve, undo=False):
if can_move(brd, mve):
brd[mve - 1] = plyr
win = is_winner(brd, plyr)
if undo:
brd[mve - 1] = mve
return True, win
return False, False
def computer_move():
mv = -1
# آیا خود کامپیوتر می تواند برنده باشد؟
for i in range(1, 10):
if make_move(board, computer, i, True)[1]:
mv = i
break
if mv == -1:
for j in range(1, 10):
if make_move(board, player, j, True)[1]:
mv = j
break
if mv == -1:
for tup in moves:
for m in tup:
if mv == -1 and can_move(board, m):
mv = m
break
return make_move(board, computer, mv)
while has_empty_space():
print_board()
move = int(input("Choose your move(1-9): "))
moved, won = make_move(board, player, move)
if not moved:
print("Invalid Number! Try again!")
continue
if won:
print(colored("You Won!", "green"))
break
elif computer_move()[1]:
print(colored("You lose!", "yellow"))
break
has_empty_space()