我需要为Connect 4开发板创建一个minimax函数(不使用alpha-beta修剪)。该功能需要以木板和深度作为参数,并以元组的形式返回最佳移动和得分。我的算法还需要准确地识别终端位置,并为其分配适当的值常数。
这是我的启发式代码:
P1_WIN_SCORE = 15
P2_WIN_SCORE = -15
DRAW_SCORE = 0
def heuristic(self, board):
cnt1 = 0 #p1 score counter
cnt2 = 0 #p2 score counter
for column in range(board.WIDTH):
for row in range(board.HEIGHT):
# horizontal checks
# 3 in a row checks
try:
if board.board[column][row] == board.board[column][row+1] == board.board[column][row+2]:
if [column][row] == 0:
cnt1 += 3
if [column][row] == 1:
cnt2 -= 3
except IndexError:
pass
# 2 in a row checks
try:
if board.board[column][row] == board.board[column][row+1]:
if [column][row] == 0:
cnt1 += 2
if [column][row] == 1:
cnt2 -= 2
except IndexError:
pass
# 1 in a row checks (works for every direction)
try:
if board.board[column][row] == 0:
cnt1 += 1
elif board.board[column][row] == 1:
cnt2 -= 1
except IndexError:
pass
# vertical checks
# 3 in a row checks
try:
if board.board[column][row] == board.board[column+1][row] == board.board[column+2][row]:
if [column][row] == 0:
cnt1 += 3
if [column][row] == 1:
cnt2 -= 3
except IndexError:
pass
# 2 in a row checks
try:
if board.board[column][row] == board.board[column+1][row]:
if [column][row] == 0:
cnt1 += 2
if [column][row] == 1:
cnt2 -= 2
except IndexError:
pass
# right diagonal up
# 3 in a row checks
try:
if board.board[column][row] == board.board[column+1][row+1] == board.board[column+2][row+2]:
if [column][row] == 0:
cnt1 += 3
if [column][row] == 1:
cnt2 -= 3
except IndexError:
pass
# 2 in a row checks
try:
if board.board[column][row] == board.board[column+1][row+1]:
if [column][row] == 0:
cnt1 += 2
if [column][row] == 1:
cnt2 -= 2
except IndexError:
pass
# right diagonal down
# 3 in a row
try:
if row >= 3 and board.board[column][row] == board.board[column+1][row-1] == board.board[column+2][row-2]:
if [column][row] == 0:
cnt1 += 3
if [column][row] == 1:
cnt2 -= 3
except IndexError:
pass
# 2 in a row
try:
if row >= 3 and board.board[column][row] == board.board[column+1][row-1]:
if [column][row] == 0:
cnt1 += 2
if [column][row] == 1:
cnt2 -= 2
except IndexError:
pass
return cnt1 + cnt2
这是我查找接线板的功能:
def isTerminal(self):
for column in range(self.WIDTH):
for row in range(self.HEIGHT):
try: #horizontal checks
if self.board[column][row] == self.board[column][row+1] == self.board[column][row+2] == self.board[column][row+3]:
return self.board[column][row]
except IndexError:
pass
try: #vertical checks
if self.board[column][row] == self.board[column+1][row] == self.board[column+2][row] == self.board[column+3][row]:
return self.board[column][row]
except IndexError:
pass
try: #right diagonal up
if self.board[column][row] == self.board[column+1][row+1] == self.board[column+2][row+2] == self.board[column+3][row+3]:
return self.board[column][row]
except IndexError:
pass
try: #right diagonal down
if row >= 3 and self.board[column][row] == self.board[column+1][row-1] == self.board[column+2][row-2] == self.board[column+3][row-3]:
return self.board[column][row]
except IndexError:
pass
if self.isFull():
return -1
return None
这是我到目前为止的代码,我知道我需要一种方法来找出玩家可以选择的最佳栏目,而且我不确定如何实现...
def minimax(self, board, depth):
b = board
#score = 0
#move = depth
while depth > 0:
if board.isTerminal() == True or depth == 0:
return heurisitic(b)
else:
if BasePlayer.heuristic(self, b) > score:
score == BasePlayer.heuristic(self, b)
depth -= 1
self.minimax(board, depth)
return (move, score)