|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本来想写一写比较复杂的黑白棋AI,但是坑太大,暂时不去想了,就用pygame做了一个极度耿直和简陋版的人机黑白棋。
先凑合看,素材的几张图片都在下面,大家可以拿去试着玩。棋力极低,而且因为字体的缘故就没写得分和输赢。
抛砖引玉一下。另求黑白棋的AI设计策略。
- import random
- import pygame
- from pygame.locals import *
- from sys import exit
- import time
- class Point():
- def __init__(self,lst,a,b):
- self.ite = lst
- self.x ,self.y = a,b
- self.step = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)]
- self.dic,self.dic_true = {1:[],2:[]},{1:[],2:[]}
- self.white,self.black = 0,0
-
- def point(self):
- self.dic,self.dic_true = {1:[],2:[]},{1:[],2:[]}
- for i,j in self.step:
- a,b = self.x + i,self.y+j
- if 0 <= a < 8 and 0 <= b <8:
- if self.ite[a][b] != 0:
- sign = self.ite[a][b]
- self.dic[sign].append([a,b])
- self.ext(sign,a,b,i,j)
-
- def ext(self,sign,a,b,i,j):
- while True:
- a += i
- b += j
- if 0<=a<8 and 0<=b<8 and self.ite[a][b]!=0:
- if self.ite[a][b] == sign:
- self.dic[sign].append([a,b])
- else:
- if sign == 1: self.dic_true[2].append(tuple(self.dic[1]))
- else:
- self.dic_true[1].append(tuple(self.dic[2]))
- else: break
- self.dic[sign] = []
- def count(self):
- if len(self.dic_true[1]):
- self.white = 1
- if len(self.dic_true[2]):
- self.black = 1
- return self.white,self.black
- class Reversi():
- def __init__(self):
- self.lst = [[0]*8 for i in range(8)]
- self.lst[3][3],self.lst[3][4] = 1,2
- self.lst[4][3],self.lst[4][4] = 2,1
- self.Man,self.AI = 0,0
- def possiblecoo(self):
- self.Man,self.AI = 0,0
- for i in range(8):
- for j in range(8):
- if self.lst[i][j] == 0:
- if self.cohesion(i,j):
- P = Point(self.lst,i,j)
- P.point()
- P.count()
- if P.white:
- self.Man += 1
- if P.black:
- self.AI += 1
- return self.AI-self.Man
-
- def cohesion(self,poi_x,poi_y):
- around =[(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)]
- for a,b in around:
- a1,b1 = poi_x+a,poi_y+b
- if 0 <= a1 < 8 and 0 <= b1 <8 and self.lst[a1][b1] != 0:
- return True
- def man_play(self,a,b):
- P = Point(self.lst,a,b)
- P.point()
- if len(P.dic_true[1]):
- self.lst[a][b] = 1
- for i in P.dic_true[1]:
- for [j,k] in i:
- self.lst[j][k] = 1
- return True
- else:
- return None
-
- def check(self):
- pos = []
- for i in range(8):
- for j in range(8):
- if self.lst[i][j] == 0:
- if self.cohesion(i,j):
- P = Point(self.lst,i,j)
- P.point()
- if len(P.dic_true[2]):
- pos.append([i,j,P.dic_true[2]])
- return pos
-
- def AI_play_check(self):
- right = {}
- pos = self.check()
- lst_x = [tuple(i) for i in self.lst]
- for [x,y,p] in pos:
- self.lst[x][y] = 2
- for i in p:
- for [a,b] in i:
- self.lst[a][b] = 2
- temp_comp = self.possiblecoo()
- right[temp_comp] = [x,y,p]
- self.lst = [list(i) for i in lst_x]
- temp = right[max(right)]
- self.lst[temp[0]][temp[1]] = 2
- for i in temp[2]:
- for [a,b] in i:
- self.lst[a][b] = 2
- pygame.init()
- pygame.font.init()
- bg = 255,255,255
- screen = pygame.display.set_mode((800,600),RESIZABLE)
- pygame.display.set_caption('黑白棋')
- imgbackground = pygame.image.load('渔舟唱晚.jpg')
- imgchessboard = pygame.image.load('棋盘.jpg').convert()
- imgblack = pygame.image.load('黑.png').convert_alpha()
- imgwhite = pygame.image.load('白.png').convert_alpha()
-
- chessboard_pos = imgchessboard.get_rect()
- chessboard_pos[0],chessboard_pos[1] = 260,160
- whiteamount = []
- blackamount = []
- R = Reversi()
- def sure_point(x,y):
- unit = 35
- i = int((x-260)/unit)
- j = int((y-160)/unit)
- pos_x,pos_y = 260+int(i*unit),160+int(j*unit)
- return pos_x,pos_y
- def sure_lst(i,j):
- unit = 35
- return [260+int(i*unit),160+int(j*unit)]
- def lst_point(a,b):
- unit = 35
- i,j = int((a-260)/unit),int((b-160)/unit)
- return i,j
- def get_chess():
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- chess_x,chess_y = pygame.mouse.get_pos()
- if 260<=chess_x<=540 and 160<=chess_y<=440 and [chess_x,chess_y] not in whiteamount:
- a,b = sure_point(chess_x,chess_y)
- i,j = lst_point(a,b)
- if R.lst[i][j] == 0:
- return a,b,i,j
-
- def show():
- global whiteamount,blackamount
- T = False
- while True:
- temp = get_chess()
- if temp:
- i,j = temp[2],temp[3]
- if R.man_play(i,j):
- T = True
- whiteamount,blackamount = [],[]
- for i in range(8):
- for j in range(8):
- if R.lst[i][j] == 1:
- whiteamount.append(sure_lst(i,j))
- elif R.lst[i][j] == 2:
- blackamount.append(sure_lst(i,j))
-
- screen.fill(bg)
- screen.blit(imgbackground,(0,0))
- screen.blit(imgchessboard,chessboard_pos)
- for i in whiteamount:
- img_w = imgwhite
- img_w_pos = img_w.get_rect()
- img_w_pos[0],img_w_pos[1] = i[0],i[1]
- screen.blit(img_w,img_w_pos)
- for i in blackamount:
- img_b = imgblack
- img_b_pos = img_b.get_rect()
- img_b_pos[0],img_b_pos[1] = i[0],i[1]
- screen.blit(img_b,img_b_pos)
- pygame.display.flip()
- if T:
- time.sleep(0.5)
- R.AI_play_check()
- T = False
- show()
复制代码
|
|