代码拉取完成,页面将自动刷新
from datetime import time
from move import bestmove, send_message, init_pos
import globals
man_color = 'black' if globals.dobot_color == 'white' else 'white' # 计算机使用的颜色
def checkman():
# 创建一个字典,用于存储每个棋子的初始位置
initial_positions = {pos: state for pos, state in globals.board.items() if state != 'empty'}
# 创建一个字典,用于存储每个棋子的当前位置信息
current_positions = {pos: state for pos, state in globals.board_ca.items() if state != 'empty'}
# 查找初始位置和当前位置信息的区别
moved_piece = None
original_pos = None
new_pos = None
for pos, piece in initial_positions.items():
if pos in current_positions and current_positions[pos] != piece:
moved_piece = piece
original_pos = pos
new_pos = list(current_positions.keys())[list(current_positions.values()).index(piece)]
break
elif pos not in current_positions:
moved_piece = piece
original_pos = pos
new_pos = -1
break
# 返回结果
if moved_piece:
return original_pos, new_pos
else:
num1 = -1
return num1, num1
def checkWin():
global board
if board[1] == board[2] and board[1] == board[3] and board[1] != ' ':
return True
elif board[4] == board[5] and board[4] == board[6] and board[4] != ' ':
return True
elif board[7] == board[8] and board[7] == board[9] and board[7] != ' ':
return True
elif board[1] == board[4] and board[1] == board[7] and board[1] != ' ':
return True
elif board[2] == board[5] and board[2] == board[8] and board[2] != ' ':
return True
elif board[3] == board[6] and board[3] == board[9] and board[3] != ' ':
return True
elif board[1] == board[5] and board[1] == board[9] and board[1] != ' ':
return True
elif board[7] == board[5] and board[7] == board[3] and board[7] != ' ':
return True
else:
return False
def checkWhichMarkWon(mark):
global board
if board[1] == board[2] and board[1] == board[3] and board[1] == mark:
return True
elif board[4] == board[5] and board[4] == board[6] and board[4] == mark:
return True
elif board[7] == board[8] and board[7] == board[9] and board[7] == mark:
return True
elif board[1] == board[4] and board[1] == board[7] and board[1] == mark:
return True
elif board[2] == board[5] and board[2] == board[8] and board[2] == mark:
return True
elif board[3] == board[6] and board[3] == board[9] and board[3] == mark:
return True
elif board[1] == board[5] and board[1] == board[9] and board[1] == mark:
return True
elif board[7] == board[5] and board[7] == board[3] and board[7] == mark:
return True
else:
return False
def checkDraw():
for key in globals.board.keys():
if globals.board[key] == ' ':
return False
return True
def compMove():
bestScore = -800
bestMove = 0
for key in globals.board.keys():
if globals.board[key] == 'empty':
globals.board[key] = globals.dobot_color
score = minimax(globals.board, False)
globals.board[key] = ' '
if score > bestScore:
bestScore = score
bestMove = key
return bestMove
def minimax(board, isMaximizing):
if checkWhichMarkWon(globals.dobot_color):
return 1
elif checkWhichMarkWon(man_color):
return -1
elif checkDraw():
return 0
if isMaximizing:
bestScore = -800
for key in board.keys():
if board[key] == ' ':
board[key] = globals.dobot_color
score = minimax(board, False)
board[key] = ' '
if score > bestScore:
bestScore = score
return bestScore
else:
bestScore = 800
for key in board.keys():
if board[key] == ' ':
board[key] = man_color
score = minimax(board, True)
board[key] = ' '
if score < bestScore:
bestScore = score
return bestScore
def play():
round = 1
if globals.dobot_color == 'black':
bestmove(5)
globals.board[5] = 'white'
round = round + 1
else:
time.sleep(15)
num_from, num_to = checkman()
if (num_from != -1):
x1, y1 = globals.best_move_to_position.get(num_from, None)
x2, y2 = globals.best_move_to_position.get(num_to, None)
send_message(x1, y1, True)
send_message(x2, y2, True)
init_pos()
round = round + 1
while checkWin() == False and round <= 9:
if round % 2 == 1:
if globals.dobot_color == 'black':
bestMove = compMove()
bestmove(bestMove)
round = round + 1
else:
time.sleep(15)
round = round + 1
else:
if globals.dobot_color == 'black':
time.sleep(15)
round = round + 1
else:
bestMove = compMove()
bestmove(bestMove)
round = round + 1
if (checkWin() == False):
print('和局')
else:
print('获胜')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。