python井字棋_python之井字棋游戏

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] &gt 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=' ')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值