class Robot(object):
'''基于五子棋规则写的一个机器人'''
def __init__(self, _board):
self.board = _board
def haveValuePoints(self, player, enemy, board):
"""算出棋盘中所有有价值的点"""
points = []
for x in range(15):
for y in range(15):
list1 = []
list2 = []
list3 = []
list4 = []
if self.board[x][y] == -1:
for tmp in range(9):
i = x + tmp - 4
j = y + tmp - 4
if i < 0 or i > 14:
list1.append(-2)
else:
list1.append(board[i][y])
if j < 0 or j > 14:
list2.append(-2)
else:
list2.append(board[x][j])
if i < 0 or j < 0 or i > 14 or j > 14:
list3.append(-2)
else:
list3.append(board[i][j])
k = y - tmp + 4
if i < 0 or k < 0 or i > 14 or k > 14:
list4.append(-2)
else:
list4.append(board[i][k])
playerValue = self.value_point(player, enemy, list1, list2, list3, list4)
enemyValue = self.value_point(enemy, player, list1, list2, list3, list4)
if enemyValue >= 10000:
enemyValue -= 500
elif enemyValue >= 5000:
enemyValue -= 300
elif enemyValue >= 2000:
enemyValue -= 250
elif enemyValue >= 1500:
enemyValue -= 200
elif enemyValue >= 99:
enemyValue -= 10
elif enemyValue >= 5:
enemyValue -= 1
value = playerValue + enemyValue
if value > 0:
points.append([x, y, value])
return points
def MaxValue_po(self, player, enemy):
"""算出最大价值的点"""
points = self.haveValuePoints(player, enemy, self.board)
flag = 0
_point = []
for p in points:
if p[2] > flag:
_point = p
flag = p[2]
return _point[0], _point[1], _point[2]
def value_point(self, player, enemy, list1, list2, list3, list4):
"""算出点的价值"""
flag = 0
flag += self.willbefive(player, list1)
flag += self.willbefive(player, list2)
flag += self.willbefive(player, list3)
flag += self.willbefive(player, list4)
flag += self.willbealive4(player, list1)
flag += self.willbealive4(player, list2)
flag += self.willbealive4(player, list3)
flag += self.willbealive4(player, list4)
flag += self.willbesleep4(player, enemy, list1)
flag += self.willbesleep4(player, enemy, list2)
flag += self.willbesleep4(player, enemy, list3)
flag += self.willbesleep4(player, enemy, list4)
flag += self.willbealive3(player, list1)
flag += self.willbealive3(player, list2)
flag += self.willbealive3(player, list3)
flag += self.willbealive3(player, list4)
flag += self.willbesleep3(player, enemy, list1)
flag += self.willbesleep3(player, enemy, list2)
flag += self.willbesleep3(player, enemy, list3)
flag += self.willbesleep3(player, enemy, list4)
flag += self.willbealive2(player, enemy, list1)
flag += self.willbealive2(player, enemy, list2)
flag += self.willbealive2(player, enemy, list3)
flag += self.willbealive2(player, enemy, list4)
flag += self.willbesleep2(player, enemy, list1)
flag += self.willbesleep2(player, enemy, list2)
flag += self.willbesleep2(player, enemy, list3)
flag += self.willbesleep2(player, enemy, list4)
return flag
@staticmethod
def willbefive(player, checklist):
"""下在这个点将会得到连无"""
if checklist[0] == player and checklist[1] == player and \
checklist[2] == player and checklist[3] == player:
return 10000
elif checklist[5] == player and checklist[6] == player and \
checklist[7] == player and checklist[8] == player:
return 10000
elif checklist[2] == player and checklist[3] == player and \
checklist[5] == player and checklist[6] == player:
return 10000
elif checklist[1] == player and checklist[2] == player and \
checklist[3] == player and checklist[5] == player:
return 10000
elif checklist[3] == player and checklist[5] == player and \
checklist[6] == player and checklist[7] == player:
return 10000
else:
return 0
@staticmethod
def willbealive4(player, checklist):
"""下在这个点将会形成活四"""
if checklist[0] == -1 and checklist[1] == player and \
checklist[2] == player and checklist[3] == player \
and checklist[5] == -1:
return 5000
elif checklist[3] == -1 and checklist[5] == player and \
checklist[6] == player and checklist[7] == player \
and checklist[8] == -1:
return 5000
elif checklist[1] == -1 and checklist[2] == player and \
checklist[3] == player and checklist[5] == player \
and checklist[6] == -1:
return 5000
elif checklist[2] == -1 and checklist[3] == player and \
checklist[5] == player and checklist[6] == player \
and checklist[7] == -1:
return 5000
else:
return 0
@staticmethod
def willbesleep4(player, enemy, checklist):
"""下在这个点会形成眠四"""
if checklist[0] == enemy and checklist[1] == player and \
checklist[2] == player and checklist[3] == player \
and checklist[5] == -1:
return 1700
elif checklist[1] == enemy and checklist[2] == player and \
checklist[3] == player and checklist[5] == player \
and checklist[6] == -1:
return 1700
elif checklist[2] == enemy and checklist[3] == player and \
checklist[5] == player and checklist[6] == player \
and checklist[7] == -1:
return 1700
elif checklist[3] == enemy and checklist[5] == player and \
checklist[6] == player and checklist[7] == player \
and checklist[8] == -1:
return 1700
elif checklist[0] == -1 and checklist[1] == player and \
checklist[2] == player and checklist[3] == player \
and checklist[5] == enemy:
return 1700
elif checklist[1] == -1 and checklist[2] == player and \
checklist[3] == player and checklist[5] == player \
and checklist[6] == enemy:
return 1700
elif checklist[2] == -1 and checklist[3] == player and \
checklist[5] == player and checklist[6] == player \
and checklist[7] == enemy:
return 1700
elif checklist[3] == -1 and checklist[5] == player and \
checklist[6] == player and checklist[7] == player \
and checklist[8] == enemy:
return 1700
else:
return 0
@staticmetho
python实现AI五子棋
4星 · 超过85%的资源 需积分: 50 82 浏览量
2019-04-14
06:55:29
上传
评论 24
收藏 9KB RAR 举报
Kyda
- 粉丝: 438
- 资源: 3
最新资源
- base.apk
- Vue 结合 D3.js 进行数据可视化开发的练手案例.zip
- 语音模块使用说明_jr6001调试工具.html
- 毕业设计-reat18货物运输后台管理系统
- Qt tcp通信(exe)
- tensorflow-gpu-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.8.0-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.7.4-cp39-cp39-manylinux2010-x86-64.whl
- HTML后台管理系统源49套合集 H5模板源码下载 总有一款适合你 静态页面
- DDColor-PDF文献
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈