-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayTTT.py
125 lines (105 loc) · 2.19 KB
/
PlayTTT.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import TTTAI
import copy
import random
end = False
b = TTTAI.ini_board()
X = "X"
O = "O"
M = None
def display(board):
i=0
j=0
while i<3:
j=0
while j<3:
if board[i][j] == M:
board[i][j] = " "
j += 1
i += 1
print(board[0])
print(board[1])
print(board[2])
def conv(x):
inp=""
if x=="0":
inp="00"
if x=="1":
inp="01"
if x=="2":
inp="02"
if x=="3":
inp="10"
if x=="4":
inp="11"
if x=="5":
inp="12"
if x=="6":
inp="20"
if x=="7":
inp="21"
if x=="8":
inp="22"
if len(x) == 2:
inp=x
return inp
if __name__ == "__main__":
while not end:
user = input("Enter Move")
inp = conv(user)
p=int(inp[0])
q=int(inp[1])
b[p][q] = X
end = TTTAI.terminal(copy.deepcopy(b))
if end:
display(copy.deepcopy(b))
if TTTAI.winner(b) == X:
print("X wins")
elif TTTAI.winner(b) == O:
print("O wins")
else:
print("Tie")
break
AI = random.choice(TTTAI.minimax(copy.deepcopy(b)))
p=AI[0]
q=AI[1]
b[p][q] = O
display(copy.deepcopy(b))
end = TTTAI.terminal(copy.deepcopy(b))
if end:
display(copy.deepcopy(b))
if TTTAI.winner(b) == X:
print("X wins")
elif TTTAI.winner(b) == O:
print("O wins")
else:
print("Tie")
break
def dispdiscy(board):
i=0
j=0
output =""
while i < 3:
j = 0
while j < 3:
if board[i][j] == M:
board[i][j] = "N"
j += 1
i += 1
i=0
j=0
while i < 3:
j = 0
while j < 3:
if j<2:
output = output + board[i][j] + "|"
else:
output = output + board[i][j]
j += 1
output = output + "\n"
i += 1
return output
def canplay(board, p, q):
if board[p][q] == M:
return True
else:
return False