-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
115 lines (96 loc) · 2.87 KB
/
application.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
from os import startfile
import time
matrix=[[0,0,0],[0,0,0],[0,0,0]]
def pmat(file=None):
for line in matrix:
print(*line,file=file)
def screen():
for line in matrix:
for item in line:
if item==1:
print(' X',end=' |')
elif item==2:
print(' O',end=' |')
else:
print(' ',end='|')
print()
print()
def run():
with open('input.txt','w') as fw:
pmat(file=fw)
print(botid,file=fw)
startfile('bot_tictactoe.exe')
time.sleep(.5)
with open('output.txt') as fr:
data=fr.read()
x,y=map(int,data.split())
matrix[x][y]=botid
def draw():
if not any([True for i in range(3) for j in range(3) if matrix[i][j]==0]):
return True
return False
def check(id):
if matrix[0][0]==id and matrix[0][1]==id and matrix[0][2]==id:
return id
if matrix[1][0]==id and matrix[1][1]==id and matrix[1][2]==id:
return id
if matrix[2][0]==id and matrix[2][1]==id and matrix[2][2]==id:
return id
if matrix[0][0]==id and matrix[1][1]==id and matrix[2][2]==id:
return id
if matrix[0][0]==id and matrix[1][0]==id and matrix[2][0]==id:
return id
if matrix[0][1]==id and matrix[1][1]==id and matrix[2][1]==id:
return id
if matrix[0][2]==id and matrix[1][2]==id and matrix[2][2]==id:
return id
if matrix[0][2]==id and matrix[1][1]==id and matrix[2][0]==id:
return id
return None
print(''' ********* TIC TAC TOE *********
With A BOT
''')
playerid=None
while True:
raw=input('\t\t\tChoose a player from X or O: ')
print()
if raw=='O' or raw=='o' or raw=='0' or raw=='2':
playerid=2
botid=1
run()
break
elif raw=='X' or raw=='x' or raw=='1':
playerid=1
botid=2
break
else:
print('Invalid Choice')
while True:
screen()
while True:
raw=input('Enter your move : ')
print()
if not raw.isdigit():
print('Invalid Move')
continue
raw=int(raw)-1
if 0<=raw<=9 and not matrix[raw//3][raw%3] :
matrix[raw//3][raw%3]=playerid
break
else:
print('Invalid Move')
if check(playerid):
screen()
print("\t\t\t** YOU WIN ** : Wanna Play Again\n\n")
break
elif draw():
print('\t\t\t** Draw ** : Try Something New\n\n')
break
run()
if check(botid):
screen()
print('\t\t\t** GAME OVER ** : Better Luck Next Time\n\n')
break
elif draw():
print('\t\t\t** Draw ** : Try Something New\n\n')
break