|
发表于 2019-4-27 19:09:17
|
显示全部楼层
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('井字棋游戏开始了 x代表人类 o代表电脑')
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('---------------------------------')
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 movman(): ###人走棋
print('小飞鱼该你走棋了')
while True:
try:
mov=input('请按这样的格式走棋 如:a1,a3,b2,c1:')
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
return
except:
print('您的输入有误!!!',end=' ')
pass
def isFinished():####得分判断
# 横向
if [1, 1, 1] in board:
print('小飞鱼你太棒了 ,你赢了,!')
return True
if [2, 2, 2] in board:
print('电脑赢!')
return True
# 竖向
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] == 1:
print('小飞鱼你太棒了 ,你赢了,!')
return True
if board[0][i] == board[1][i] == board[2][i] == 2:
print('电脑赢!')
return True
# 交叉
if (board[0][0] == board[1][1] == board[2][2] == 1) or (
board[2][0] == board[1][1] == board[0][2] == 1):
print('小飞鱼你太棒了 ,你赢了,!')
return True
if (board[0][0] == board[1][1] == board[2][2] == 2) or (
board[2][0] == board[1][1] == board[0][2] == 2):
print('电脑赢!')
return True
# 平棋
draw = True
for i in range(3):
if 0 in board[i]:
draw = False
if draw:
print('竟然打成平局')
return True
return False
yourturn = True
showBoard()
while not isFinished():
if yourturn:
movman()
else:
movi()
showBoard()
yourturn = not yourturn |
|