代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
A*算法实现五子棋人机对弈
作者:
imcto
/ 发布于
2018/3/26
/
979
A*算法实现五子棋人机对弈
import pygame import numpy as np import os import random import copy import time class GoBang(object): """docstring for GoBang""" def __init__(self, w, h, img: str): pygame.init() pygame.mixer.init() self.width = w self.height = h self.FPS = 30 # 刷新频率 self.row_count = 19 # 格数 self.gridwidth = w // 20 self.steps = [] self.blackSteps = [] self.whiteSteps = [] self.board = [] self.gradeBoard = [] self.screen = pygame.display.set_mode((w, h)) self.bac_img = pygame.image.load(img).convert() pygame.display.set_caption("五子棋") self.Draw_background() self.InitBoard(self.board) self.InitBoard(self.gradeBoard) def InitBoard(self, board): for i in range(19): l = [] for j in range(19): l.append(0) board.append(l) def Set_bacimg(self, filename: str): self.bac_img = pygame.image.load(str).convert() def Draw_background(self): # 加载背景图片 self.screen.blit(self.bac_img, (0, 0)) # 画网格线,棋盘为 19行 19列的 rect_lines = [ ((self.gridwidth, self.gridwidth), (self.gridwidth, self.height - self.gridwidth)), ((self.gridwidth, self.gridwidth), (self.width - self.gridwidth, self.gridwidth)), ((self.gridwidth, self.height - self.gridwidth), (self.width - self.gridwidth, self.height - self.gridwidth)), ((self.width - self.gridwidth, self.gridwidth), (self.width - self.gridwidth, self.height - self.gridwidth)), ] for line in rect_lines: pygame.draw.line(self.screen, (0, 0, 0), line[0], line[1], 2) # 画出中间的网格线 for i in range(17): pygame.draw.line(self.screen, (0, 0, 0), (self.gridwidth * (2 + i), self.gridwidth), (self.gridwidth * (2 + i), self.height - self.gridwidth)) pygame.draw.line(self.screen, (0, 0, 0), (self.gridwidth, self.gridwidth * (2 + i)), (self.height - self.gridwidth, self.gridwidth * (2 + i))) # 画出棋盘中的五个点 circle_center = [ (self.gridwidth * 4, self.gridwidth * 4), (self.width - self.gridwidth * 4, self.gridwidth * 4), (self.width - self.gridwidth * 4, self.height - self.gridwidth * 4), (self.gridwidth * 4, self.height - self.gridwidth * 4), (self.gridwidth * 10, self.gridwidth * 10) ] for cc in circle_center: pygame.draw.circle(self.screen, (0, 0, 0), cc, 5) def AddCoin(self, pos, color): self.steps.append( ((pos[0] * self.gridwidth, pos[1] * self.gridwidth), color)) self.gradeBoard[pos[0] - 1][pos[1] - 1] = 0 if color == (0, 0, 0): self.board[pos[0] - 1][pos[1] - 1] = 1 if color == (255, 255, 255): self.board[pos[0] - 1][pos[1] - 1] = 2 pygame.draw.circle( self.screen, color, (pos[0] * self.gridwidth, pos[1] * self.gridwidth), 16) def DrawSteps(self): for p in self.steps: pygame.draw.circle(self.screen, p[1], p[0], 16) def IsAccess(self, pos): if pos[0] - 1 < 0 or pos[1] - 1 < 0: return False if self.board[pos[0] - 1][pos[1] - 1] == 0: return True else: return False def BestPos(self, a: list, b: list): s = [] if a[1] > b[1]: s = a[0] else: s = b[0] grade = 0 grades = [] gride = self.GrideOf(s) print("gride:", gride) for i in range(len(gride)): if gride[i] != 0: continue start = i - 1 left = 0 right = len(gride) - 1 while start >= left: if gride[start] != 0: grade += i - start start -= 1 start = i + 1 while start <= right: if gride[start] != 0: grade += start - i start += 1 grades.append([grade, i]) print(grade) grade = 0 grades.sort(key=lambda gr: gr[0]) print("grades:", grades) if grades: index = grades[0][1] grades.clear() return s[index] def GameOver(self): if self.GrideOf(self.whiteSteps[0][0]) == [1, 1, 1, 1, 1]: win = pygame.image.load("image/victory.jpg").convert() win = pygame.transform.scale(win, (self.width, self.height)) self.screen.blit(win, (0, 0)) pygame.display.flip() print("黑子胜利") return True elif self.GrideOf(self.blackSteps[0][0]) == [2, 2, 2, 2, 2]: print("白子胜利") over = pygame.image.load("image/gameover.jpg").convert() over = pygame.transform.scale(over, (self.width, self.height)) self.screen.blit(over, (0, 0)) time.sleep(2) pygame.display.flip() return True return False def Split(self, a: list, b, row, col): c = [] d = [] # print("b:",b) grade = [] color = 0 # print(a) if b == 2: color = 1 else: color = 2 for i in range(len(a)): if a[i] != b: if row >= 0 and col < 0: c.append((row + 1, i + 1)) continue if row < 0 and col >= 0: c.append((i + 1, col + 1)) continue if row == col == 1: if self.board[a[i][0] - 1][a[i][1] - 1] != b: c.append(a[i]) else: if len(c) >= 5: for j in range(len(c)): end = j + 5 s = c[j:end] if end <= len(c): d.append(s) d.append(self.GradeOf( self.GrideOf(s), color)) grade.append(copy.deepcopy(d)) d.clear() c.clear() else: if len(c) >= 5: for j in range(len(c)): end = j + 5 s = c[j:end] if end <= len(c): d.append(s) d.append(self.GradeOf(self.GrideOf(s), color)) grade.append(copy.deepcopy(d)) d.clear() c.clear() # print(c) continue if c and len(c) >= 5: for j in range(len(c)): end = j + 5 s = c[j:end] if end <= len(c): d.append(s) d.append(self.GradeOf(self.GrideOf(s), color)) grade.append(copy.deepcopy(d)) d.clear() return grade def GroupOf(self, color, which): which.clear() Color = 0 if color == (0, 0, 0): Color = 2 else: Color = 1 # 逐行扫描 for i in range(len(self.board)): white = self.Split(self.board[i], Color, i, -1) # print("i:", i, '\t', white) if white: which.extend(white) # 逐列扫描 for i in range(len(self.board)): col = [self.board[j][i] for j in range(len(self.board))] white = self.Split(col, Color, -1, i) # print("i:", i, '\t', white) if white: which.extend(white) # 倾斜扫描 x = [i for i in range(19)] for i in range(-14, 15, 1): f = np.poly1d([1, i]) y = [(j + 1, f(j) + 1) for j in range(len(x)) if f(j) >= 0 and f(j) <= 18] # print(y) white = self.Split(y, Color, 1, 1) # print("i:", i, '\t', white) if white: which.extend(white) for i in range(4, 33): f = np.poly1d([-1, i]) y = [(j + 1, f(j) + 1) for j in range(len(x)) if f(j) >= 0 and f(j) <= 18] white = self.Split(y, Color, 1, 1) # print("i:", i, '\t', white) if white: which.extend(white) # print(y) which.sort(key=lambda grade: grade[1], reverse=True) # print(self.whiteSteps) def GrideOf(self, a: list): c = [] for i in range(len(a)): c.append(self.board[a[i][0] - 1][a[i][1] - 1]) # print(c) return c def GradeOf(self, gr: list, color): grade = 0 if gr == [color, 0, 0, 0, 0] or gr == [0, 0, 0, 0, color]: grade = 1 elif gr == [0, color, 0, 0, 0] or gr == [0, 0, 0, color, 0]: grade = 2 elif gr == [0, 0, color, 0, 0]: grade = 3 elif gr == [color, color, 0, 0, 0] or gr == [0, 0, 0, color, color]: grade = 7 elif gr == [color, 0, color, 0, 0] or gr == [0, 0, color, 0, color]: grade = 6 elif gr == [color, 0, 0, color, 0] or gr == [0, color, 0, 0, color]: grade = 5 elif gr == [color, 0, 0, 0, color]: grade = 4 elif gr == [0, color, color, 0, 0] or gr == [0, 0, color, color, 0]: grade = 8 elif gr == [0, color, 0, color, 0]: grade = 13 elif gr == [color, color, color, 0, 0] or gr == [0, 0, color, color, color]: grade = 10 elif gr == [color, color, 0, color, 0] or gr == [0, color, 0, color, color]: grade = 12 elif gr == [color, color, 0, 0, color] or gr == [color, 0, 0, color, color]: grade = 10 elif gr == [color, 0, color, color, 0] or gr == [0, color, color, 0, color]: grade = 12 elif gr == [0, color, color, color, 0]: grade = 14 elif gr == [color, 0, color, 0, color]: grade = 11 elif gr == [color, color, color, color, 0] or gr == [0, color, color, color, color]: grade = 15 elif gr == [color, 0, color, color, color] or gr == [color, color, color, 0, color]: grade = 15 elif gr == [color, color, 0, color, color]: grade = 15 elif gr == [color, color, color, color, color]: grade = 100 # print(grade) return grade def Show(self): run = True clock = pygame.time.Clock() while run: clock.tick(self.FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == pygame.MOUSEBUTTONDOWN: pos = event.pos gridpos = (int(round(event.pos[0] / (self.gridwidth + .0))), int( round(event.pos[1] / (self.gridwidth + .0)))) print(gridpos) if self.IsAccess(gridpos): self.AddCoin(gridpos, (0, 0, 0)) self.Draw_background() self.DrawSteps() pygame.display.update() else: continue self.GroupOf((0, 0, 0), self.whiteSteps) self.GroupOf((255, 255, 255), self.blackSteps) pos1 = self.BestPos(self.whiteSteps[0], self.blackSteps[0]) print(pos1) if self.GameOver(): time.sleep(3) os._exit(0) if pos1: self.AddCoin(pos1, (255, 255, 255)) self.Draw_background() self.DrawSteps() pygame.display.update() if self.GameOver(): time.sleep(3) os._exit(0) self.Draw_background() self.DrawSteps() pygame.display.flip() if __name__ == '__main__': board = GoBang(720, 720, "image/pink2.jpg") board.Show()
试试其它关键字
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
imcto
贡献的其它代码
(
12
)
.
取得某年(year)第几周(weeks)的星期几(weekDay)的日期
.
数字计算工具 NumberUtil
.
A*算法实现五子棋人机对弈
.
获取特定JSON格式数据节点
.
SQL被阻塞查询
.
谷歌地图添加文本标注
.
sql查询含有某列名的所有表
.
Scala调用Java类
.
最大子段和
.
多进程共享变量
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3