import time
class jingziqi():
def __init__(self,lst):
self.jin = lst
self.che = []
self.sho = {}
self.jud = 0
def coo(self):
for ver in range(3):
acr = []
shu = []
for each in range(3):
acr.append([ver,each])
shu.append([each,ver])
self.che.append(acr)
self.che.append(shu)
xie1 = []
xie2 = []
a,b,c,d = 0,0,0,2
for t in range(3):
xie1.append([a,b])
xie2.append([c,d])
a += 1
b += 1
c += 1
d -= 1
self.che.append(xie1)
self.che.append(xie2)
def check(self):
for i in range(5):
self.sho[i] = 0
self.eva_2()
for line in self.che:
cot_d = {}
cot_d[0],cot_d[1],cot_d[2] = 0,0,0
for each in line:
temp = self.jin[each[0]][each[1]]
if temp == 0:
cot_d[0] += 1
elif temp == 1:
cot_d[1] += 1
else:
cot_d[2] += 1
self.eva_1(cot_d,line)
def eva_1(self,dic,lst):
if dic[1] == 3:
self.jud = 1
elif dic[0] == 1 and dic[2] == 2:
for i in lst:
if not self.jin[i[0]][i[1]]:
self.sho[0] = i
elif dic[0] == 1 and dic[1] == 2:
for i in lst:
if not self.jin[i[0]][i[1]]:
self.sho[1] = i
def eva_2(self):
if self.jin[1][1] == 0:
self.sho[2] = [1,1]
for s in [[0,0],[0,2],[2,0],[2,2]]:
if not self.jin[s[0]][s[1]]:
self.sho[3] = s
break
for c in [[0,1],[1,2],[2,1],[1,0]]:
if not self.jin[c[0]][c[1]]:
self.sho[4] = c
break
def ai_play(self):
for dec in range(5):
if self.sho[dec]:
c = self.sho[dec]
self.jin[c[0]][c[1]] = 2
if dec == 0:
self.jud = 2
break
def man_play(self):
while True:
temp = input('you:').split(',')
a,b = int(temp[0]),int(temp[1])
if not self.jin[a][b]:
self.jin[a][b] = 1
break
def show(self):
sign = 0
for line in self.jin:
print(' ',end='')
for each in line:
print(each,end=' ')
if not each: sign = 1
print()
print('')
if not sign:
self.jud = 3
if self.jud:
if self.jud == 1: print('You Win~')
elif self.jud == 2: print('AI Win~')
else: print('Dogfall')
return 1
def ini(self):
for line in range(3):
for each in range(3):
self.jin[line][each] = 0
def chess():
board = [[0,0,0] for i in range(3)]
ai = jingziqi(board)
ai.coo()
while True:
if ai.show() == 1:
temp = input('continue?y or n: ')
if temp == 'n':
break
else:
print('')
ai.ini()
ai.show()
ai.man_play()
time.sleep(0.5)
ai.check()
ai.ai_play()
chess()
|