黑白棋python代码框架_Python实现黑白棋人机对弈
Python實現黑白棋人機對弈
規則
黑白棋的每顆棋子由黑白兩色組成,一面白,一面黑。每次落子,把本方顏色的棋子放在棋盤的空格上,若在橫、豎、斜八個方向的任一方向上有本方棋子,則被夾在中間的對手棋子全部翻轉為本方棋子顏色;并且,僅在可以翻轉棋子的地方才能落子。如果一方至少有一步合法棋步可下,他就必須落子,不得棄權。棋盤已滿或雙方都沒有棋子可下時棋局結束,以棋子數目來計算勝負,棋子多的一方獲勝。在棋盤還沒有下滿時,如果一方的棋子已經被對方吃光,則棋局也結束,將對手棋子吃光的一方獲勝。
兩位玩家輪流下棋,直到一方沒有符合規則的落子位置,在這種情況下,剩下的一方繼續下棋,直到對手有了可以落子的位置。此時,恢復兩者輪流下棋的順序。如果一方落子在非法位置,則視為放棄本次對弈,對方獲勝。游戲結束的條件:1)整個棋盤滿了;2)一方的棋子已經被對方吃光;3)兩名玩家都沒有可以落子的棋盤格;4)一方落子在非法位置。前3 種情況以棋子數目來計算勝負,棋子多的一方獲勝;第4 種情況判定對方獲勝。
計算機選擇落子位置的策略
選擇落子位置的策略 選擇落子位置的策略對每個可能 的落子 位置,計算 該位置的 該位置的 “分值 ”(可以翻轉的對手棋子數量 可以翻轉的對手棋子數量 可以翻轉的對手棋子數量.同分數選擇行數小的。
實現思路
黑白棋玩家的子的坐標放在集合中,改變集合元素實現下棋。根據集合中元素打印棋盤。
亟待完善的地方
機器下棋策略可以改善,設計遞推方法
不同的持棋方式寫了重復的代碼,可以進一步包裝
無效代碼可以刪除,如cont,
命名
import time
import csv
class HeiBaiPlayer(object):
status = True # 是否有位置可以下
defeat = False # 是否贏了
R_LIST = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
R_LIST.remove((0, 0))
count = 0
def __init__(self, dim, players, _color): # _color只能是hei or bai ,players只能是com or peop
self.hei = {(dim//2+1, dim//2), (dim//2, dim//2+1)}
self.players = players
self.bai = {(dim//2, dim//2), (dim//2+1, dim//2+1)}
self.dim = dim
self._color = _color # dic
def qp_print(self):
'''
根據場中的hei和bai打印棋盤
:return:
'''
for i in range(self.dim + 1):
if i == 0:
print(" ", end='')
print(''.join([str(chr(97 + x)) for x in range(self.dim)]))
else:
for j in range(self.dim + 1):
if j == 0:
print(chr(96 + i), end='')
else:
if (i, j) in self.hei:
print('X', end='')
elif (i, j) in self.bai:
print('O', end='')
else:
# print((i,j))
print('.', end='')
print()
def legal_position(self,players):
'''
:param players:bai或者hei的集合
:return: 字典 {bai玩家或者hei玩家可以落子位置:反轉對手子的位置}
'''
if players == self.hei:
_players = self.bai
else:
_players = self.hei
kong = [(i, j) for i in range(1, self.dim + 1) for j in range(1, self.dim + 1)]
kong = set(kong) - self.hei - self.bai
# print(kong)
p_players_list_in = {} # 如果落在p,會有的夾在中間的反色子集合
for p in kong:
all_r_players_list_in = [] # 所有方向的反色夾在中間子的集合
for r in self.R_LIST:
_players_list_in = [] # 某一方向夾在中間反色子的集合
i = 1
lst = []
while 1:
if (p[0] + i * r[0], p[1] + i * r[1]) in _players:
lst.append(tuple([p[0] + i * r[0], p[1] + i * r[1]]))
i += 1
if (p[0] + i * r[0], p[1] + i * r[1]) in players:
_players_list_in += lst
break
if i > self.dim + 1:
break
else:
break
if _players_list_in: # 如果這個方向有jiazai中間的反色子
all_r_players_list_in += _players_list_in
if all_r_players_list_in: # 如果落在p,會夾在中間的反色子集合【】
p_players_list_in[p] = all_r_players_list_in
# print(p_players_list_in,'這是測試')
return p_players_list_in
def callback(self):
'''
根據對象不同選擇不同的下棋方式
:return: 機器下棋還是人工下棋
'''
if self.players == 'com':
return self.computer_xia
if self.players == 'peop':
return self.players_xia
def color(self):
if self._color == 'hei':
return self.hei
return self.bai
def hefa(self,players, p):
'''
測試某一位置是否合法,如果合法,返回相應反轉的子的位置,不合法返回False
:param p: 位置元組
:return:列表
'''
if p in self.legal_position(players).keys():
return self.legal_position(players)[p]
return False
def defen(self, players, p):
'''
某一位置的得分
:param players:set
:param p:(,)
:return:
'''
return len(self.hefa(players, p))
def computer_xia(self,players): # 要么是黑,要么是白,players類型是集合
if not self.legal_position(players).keys():
print('com no Invalid move\n========')
self.status = False
else:
self.status = True
p_score = {}
for p in self.legal_position(players).keys():
p_score[p] = self.defen(players, p)
score = max(p_score.values())
for p in [(i, j) for i in range(1, self.dim + 1) for j in range(1, self.dim + 1)]:
if self.hefa(players, p):
if p_score[p] == score:
return {p: self.legal_position(players)[p]}
def players_xia(self, players): # 要么是黑,要么是白,players類型是集合
'''
人工下棋,先判斷有無位置可以下,在讓用戶選擇落子位置,如果位置出錯 self.defeat = True
:param players: 人player擁有子位置的集合
:return: {落子位置:反轉對面位置}
'''
if not self.legal_position(players).keys():
print('poeple no Invalid move\n========')
self.status = False
else:
self.status = True
try:
s = input("你的落子位置(例如ab:a行b列):?")
posintion = tuple([ord(s[0]) - 96, ord(s[1]) - 96])
if posintion in self.legal_position(players).keys():
return {posintion:self.legal_position(players=players)[posintion]}
else:
self.defeat = True
except Exception as e:
print('Sth Wrong, Try again',e)
s = input("你的落子位置:?")
posintion = tuple([ord(s[0]) - 96, ord(s[1]) - 96])
if posintion in self.legal_position(players).keys():
return {posintion: self.legal_position(players=players)[posintion]}
else:
self.defeat = True
def change(self, dic):
'''
下棋之后改變hei和bai中的元素
:param dic: {落子位置:反轉對面位置}
:return: 新的hei和bai集合
'''
if self._color == 'hei':
self.hei = self.hei | set(list(dic.keys())) | set(list(dic.values())[0])
self.bai = self.bai - set(list(dic.values())[0])
else:
self.bai = self.bai | set(list(dic.keys())) | set(list(dic.values())[0])
self.hei = self.hei - set(list(dic.values())[0])
def com_turn():
'''
電腦下棋
:return:
'''
com.bai = peop.bai
com.hei = peop.hei
color_set = com.color() # 顏色集合
HeiBaiPlayer_function = com.callback() # 下棋方法傳入集合
dic = HeiBaiPlayer_function(color_set) # 得到下棋位置和反轉位置
if not com.status:
peop.qp_print()
else:
if peop.status == False:
peop.status = True
com.count = 0
print('==' * 5)
print('機器下棋位置:反轉對方位置', dic)
com.change(dic)
# print(com.hei, com.bai)
com.qp_print()
if not peop.status:
peop.status = True
def peop_turn():
'''
人下棋
:return:
'''
peop.bai = com.bai
peop.hei = com.hei
color_set = peop.color()
HeiBaiPlayer_function = peop.callback() # 下棋的函數
dic = HeiBaiPlayer_function(color_set)
if not peop.status:
peop.qp_print()
elif not peop.defeat:
if com.status == False:
com.status = True
peop.count = 0
print('=='*5)
# print('人的下棋位置:反轉對方位置', dic)
peop.change(dic)
# print('黑,白', peop.hei, peop.bai)
peop.qp_print()
else:
peop.qp_print()
peop.defeat = True
# print("人輸了")
t1 = time.time()
begin_time = time.strftime('%Y%m%d %H:%M:%S')
Dimension = eval(input('Dimension:')) # 用戶輸入開始
OX = input('Computer plays (X/O):')
if OX == 'O':
com = HeiBaiPlayer(dim=Dimension, players='com', _color='bai')
peop = HeiBaiPlayer(dim=Dimension, players='peop', _color='hei')
hei_player = 'computer'
if OX == 'X':
com = HeiBaiPlayer(dim=Dimension, players='com', _color='hei')
peop = HeiBaiPlayer(dim=Dimension, players='peop', _color='bai')
hei_player = 'players'
if com._color == 'hei':
count = 0
peop.qp_print()
while 1:
com_turn()
if peop.status == False and com.status == False:
print("Both players have no valid move.")
print("Game Over")
print('com:{}**peop:{}'.format(com.color(), peop.color()))
if len(com.color()) > len(peop.color()):
print('com win!!')
elif len(com.color()) < len(peop.color()):
print('players win!!')
else:
print('0比0')
score = str(len(com.color())) +':'+ str(len(peop.color()))
break
peop_turn()
if peop.defeat:
print('Invalid move.\nGame over.')
print('com win')
score = 'Human give up'
break
if peop.status == False and com.status == False:
print("Both players have no valid move.")
print("Game Over")
print('com:{}**peop:{}'.format(com.color(), peop.color()))
if len(com.color()) > len(peop.color()):
print('com win!!')
elif len(com.color()) < len(peop.color()):
print('players win!!')
else:
print('0比0')
score = str(len(com.color())) +':'+ str(len(peop.color()))
break
peop.qp_print()
if peop._color == 'hei':
count = 0
peop.qp_print()
while 1:
peop_turn()
if peop.defeat:
print('Invalid move.\nGame over.')
print('com win')
score = 'Human give up'
break
if peop.status == False and com.status == False:
print("Both players have no valid move.")
print("Game Over")
print('com:{}**peop:{}'.format(com.color(), peop.color()))
if len(com.color()) > len(peop.color()):
print('com win!!')
elif len(com.color()) < len(peop.color()):
print('players win!!')
else:
print('0比0')
score = str(len(peop.color())) +':'+ str(len(com.color()))
break
com_turn()
if peop.status == False and com.status == False:
print("Both players have no valid move.")
print("Game Over")
print('com:{}**peop:{}'.format(com.color(), peop.color()))
if len(com.color()) > len(peop.color()):
print('com win!!')
elif len(com.color()) < len(peop.color()):
print('players win!!')
else:
print('0比0')
score = str(len(peop.color())) +':'+ str(len(com.color()))
break
peop.qp_print()
t2 = time.time()
time_sep = int(t2 - t1)
if hei_player == 'computer':
bai_player = 'players'
else:
bai_player = 'computer'
def save_info(begin_time, time_sep, dim, hei_player, bai_player, score):
with open('reversi.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([begin_time, time_sep, str(dim)+'*'+str(dim), hei_player, bai_player, score])
save_info(begin_time, time_sep, Dimension, hei_player, bai_player, score)
1544455193(1).jpg
1544455180(1).jpg
總結
以上是生活随笔為你收集整理的黑白棋python代码框架_Python实现黑白棋人机对弈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android高德地图用地址获取经纬度,
- 下一篇: Python实现淘宝爬取——奶粉销售信息