import random
board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
board_x={'1':0,'2':1,'3':2}
board_y={'a':0,'b':1,'c':2}
CHESS = [' ','x','o']
print('井字棋游戏开始了 ')
def showBoard():##### 绘制棋谱
print (' a b c ')
for i in range(3):
print(' |---|---|---|')
print(i+1, '|',end=' ')
for j in range(3):
print( '%s |' %CHESS[board[i][j]], end=' ')
print()
print (' |---|---|---|')
print('【x代表:%s】【 o代表:电脑】' %name)
print('---------------------------------')
###############################################################
def movi():### 电脑随机走棋
print('电脑走棋')
while True:
r = random.randint(0, 2)
c = random.randint(0, 2)
if board[r][c] == 0:
board[r][c]=2
return board[r][c]
###############################################################
def moveAI(): #####电脑计算走棋
print ('电脑走棋:')
point = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
max_point = -1
position = (0, 0)
for i in range(3):
for j in range(3):
if board[i][j] != 0:
point[i][j] = -1
continue
# row
point[i][j] += calcPoint(board[i])
# col
line = [board[k][j] for k in range(3)]
point[i][j] += calcPoint(line)
# left-top to right-bottom
if i == j:
line = [board[k][k] for k in range(3)]
point[i][j] += calcPoint(line)
# right-top to left-bottom
if i + j == 2:
line = [board[k][2 - k] for k in range(3)]
point[i][j] += calcPoint(line)
# center
if i == 1:
point[i][j] += 1
if j == 1:
point[i][j] += 1
if point[i][j] > max_point:
max_point = point[i][j]
position = (i, j)
# print(point)
board[position[0]][position[1]] = 2
def calcPoint(line): ####计算得分
point = 0
if line.count(2) == 2:
point += 1000
if line.count(1) == 2:
point += 900
if line.count(2) == 1 and line.count(1) == 0:
point += 100
if line.count(2) == 0 and line.count(1) == 1:
point += 90
if line.count(0) == 3:
point += 10
return point
def movman(): ###人走棋
print('%s 该你走棋了'%name ,end=' ' )
while True:
try:
mov=input('请按这样的格式走棋 (如:a1,a3,b2,c1)输完后请按回车键:')
print(' ' )
pos_x=board_x[mov[1]]
pos_y=board_y[mov[0]]
if board[pos_x][pos_y]==0:
board[pos_x][pos_y]=1
else:
print('此处已有棋子 请从下棋',end=' ')