1 Star 0 Fork 0

张哲瑞/tic-tac-toe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
play.py 5.36 KB
一键复制 编辑 原始数据 按行查看 历史
张哲瑞 提交于 2024-07-31 09:55 . 2024/7/31
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('获胜')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangzherui666/tic-tac-toe.git
git@gitee.com:zhangzherui666/tic-tac-toe.git
zhangzherui666
tic-tac-toe
tic-tac-toe
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385