鱼C论坛

 找回密码
 立即注册
查看: 1563|回复: 2

[技术交流] 耿直版黑白棋pygame

[复制链接]
发表于 2018-3-22 01:27:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        本来想写一写比较复杂的黑白棋AI,但是坑太大,暂时不去想了,就用pygame做了一个极度耿直和简陋版的人机黑白棋。
先凑合看,素材的几张图片都在下面,大家可以拿去试着玩。棋力极低,而且因为字体的缘故就没写得分和输赢。
抛砖引玉一下。另求黑白棋的AI设计策略。
  1. import random
  2. import pygame
  3. from pygame.locals import *
  4. from sys import exit
  5. import time


  6. class Point():

  7.     def __init__(self,lst,a,b):
  8.         self.ite = lst
  9.         self.x ,self.y = a,b
  10.         self.step = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)]
  11.         self.dic,self.dic_true = {1:[],2:[]},{1:[],2:[]}
  12.         self.white,self.black = 0,0
  13.         
  14.     def point(self):
  15.         self.dic,self.dic_true = {1:[],2:[]},{1:[],2:[]}
  16.         for i,j in self.step:
  17.             a,b = self.x + i,self.y+j
  18.             if 0 <= a < 8 and 0 <= b <8:
  19.                  if self.ite[a][b] != 0:
  20.                     sign = self.ite[a][b]
  21.                     self.dic[sign].append([a,b])
  22.                     self.ext(sign,a,b,i,j)
  23.                     
  24.     def ext(self,sign,a,b,i,j):
  25.         while True:
  26.             a += i
  27.             b += j
  28.             if 0<=a<8 and 0<=b<8 and self.ite[a][b]!=0:
  29.                 if self.ite[a][b] == sign:
  30.                     self.dic[sign].append([a,b])
  31.                 else:
  32.                     if sign == 1: self.dic_true[2].append(tuple(self.dic[1]))
  33.                     else:
  34.                         self.dic_true[1].append(tuple(self.dic[2]))
  35.             else: break
  36.         self.dic[sign] = []

  37.     def count(self):
  38.         if len(self.dic_true[1]):
  39.             self.white = 1
  40.         if len(self.dic_true[2]):
  41.             self.black = 1
  42.         return self.white,self.black

  43. class Reversi():

  44.     def __init__(self):
  45.         self.lst = [[0]*8 for i in range(8)]
  46.         self.lst[3][3],self.lst[3][4] = 1,2
  47.         self.lst[4][3],self.lst[4][4] = 2,1
  48.         self.Man,self.AI = 0,0

  49.     def possiblecoo(self):
  50.         self.Man,self.AI = 0,0
  51.         for i in range(8):
  52.             for j in range(8):
  53.                 if self.lst[i][j] == 0:
  54.                     if self.cohesion(i,j):
  55.                         P = Point(self.lst,i,j)
  56.                         P.point()
  57.                         P.count()
  58.                         if P.white:
  59.                             self.Man += 1
  60.                         if P.black:
  61.                             self.AI += 1
  62.         return self.AI-self.Man
  63.                            
  64.     def cohesion(self,poi_x,poi_y):
  65.         around =[(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)]
  66.         for a,b in around:
  67.             a1,b1 = poi_x+a,poi_y+b
  68.             if 0 <= a1 < 8 and 0 <= b1 <8 and self.lst[a1][b1] != 0:
  69.                 return True

  70.     def man_play(self,a,b):
  71.         P = Point(self.lst,a,b)
  72.         P.point()
  73.         if len(P.dic_true[1]):
  74.             self.lst[a][b] = 1
  75.             for i in P.dic_true[1]:
  76.                 for [j,k] in i:
  77.                     self.lst[j][k] = 1                    
  78.             return True
  79.         else:
  80.             return None
  81.         
  82.     def check(self):
  83.         pos = []
  84.         for i in range(8):
  85.             for j in range(8):
  86.                 if self.lst[i][j] == 0:
  87.                     if self.cohesion(i,j):
  88.                         P = Point(self.lst,i,j)
  89.                         P.point()
  90.                         if len(P.dic_true[2]):
  91.                              pos.append([i,j,P.dic_true[2]])
  92.         return pos
  93.         
  94.     def AI_play_check(self):
  95.         right = {}
  96.         pos = self.check()
  97.         lst_x = [tuple(i) for i in self.lst]
  98.         for [x,y,p] in pos:
  99.             self.lst[x][y] = 2
  100.             for i in p:
  101.                 for [a,b] in i:
  102.                     self.lst[a][b] = 2
  103.             temp_comp = self.possiblecoo()
  104.             right[temp_comp] = [x,y,p]
  105.             self.lst = [list(i) for i in lst_x]
  106.         temp = right[max(right)]
  107.         self.lst[temp[0]][temp[1]] = 2
  108.         for i in temp[2]:
  109.             for [a,b] in i:
  110.                 self.lst[a][b] = 2

  111. pygame.init()
  112. pygame.font.init()
  113. bg = 255,255,255
  114. screen = pygame.display.set_mode((800,600),RESIZABLE)
  115. pygame.display.set_caption('黑白棋')

  116. imgbackground = pygame.image.load('渔舟唱晚.jpg')
  117. imgchessboard = pygame.image.load('棋盘.jpg').convert()
  118. imgblack = pygame.image.load('黑.png').convert_alpha()
  119. imgwhite = pygame.image.load('白.png').convert_alpha()
  120.    
  121. chessboard_pos = imgchessboard.get_rect()
  122. chessboard_pos[0],chessboard_pos[1] = 260,160
  123. whiteamount = []
  124. blackamount = []

  125. R = Reversi()

  126. def sure_point(x,y):
  127.     unit = 35
  128.     i = int((x-260)/unit)
  129.     j = int((y-160)/unit)
  130.     pos_x,pos_y = 260+int(i*unit),160+int(j*unit)
  131.     return pos_x,pos_y

  132. def sure_lst(i,j):
  133.     unit = 35
  134.     return [260+int(i*unit),160+int(j*unit)]

  135. def lst_point(a,b):
  136.     unit = 35
  137.     i,j = int((a-260)/unit),int((b-160)/unit)
  138.     return i,j

  139. def get_chess():
  140.     for event in pygame.event.get():
  141.         if event.type == QUIT:
  142.             pygame.quit()
  143.             exit()
  144.         if event.type == MOUSEBUTTONDOWN:
  145.             if event.button == 1:
  146.                 chess_x,chess_y = pygame.mouse.get_pos()
  147.                 if 260<=chess_x<=540 and 160<=chess_y<=440 and [chess_x,chess_y] not in whiteamount:
  148.                     a,b = sure_point(chess_x,chess_y)
  149.                     i,j = lst_point(a,b)
  150.                     if R.lst[i][j] == 0:
  151.                         return a,b,i,j

  152.         
  153. def show():
  154.     global whiteamount,blackamount
  155.     T = False
  156.     while True:
  157.         temp = get_chess()
  158.         if temp:
  159.             i,j = temp[2],temp[3]
  160.             if R.man_play(i,j):
  161.                 T = True   
  162.         whiteamount,blackamount = [],[]
  163.         for i in range(8):
  164.             for j in range(8):
  165.                 if R.lst[i][j] == 1:
  166.                     whiteamount.append(sure_lst(i,j))        
  167.                 elif R.lst[i][j] == 2:
  168.                     blackamount.append(sure_lst(i,j))
  169.         
  170.         screen.fill(bg)
  171.         screen.blit(imgbackground,(0,0))
  172.         screen.blit(imgchessboard,chessboard_pos)
  173.         for i in whiteamount:
  174.             img_w = imgwhite
  175.             img_w_pos = img_w.get_rect()
  176.             img_w_pos[0],img_w_pos[1] = i[0],i[1]
  177.             screen.blit(img_w,img_w_pos)
  178.         for i in blackamount:
  179.             img_b = imgblack
  180.             img_b_pos = img_b.get_rect()
  181.             img_b_pos[0],img_b_pos[1] = i[0],i[1]
  182.             screen.blit(img_b,img_b_pos)
  183.         pygame.display.flip()
  184.         if T:
  185.             time.sleep(0.5)
  186.             R.AI_play_check()
  187.             T = False
  188. show()

复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-22 01:32:43 | 显示全部楼层
然后大家自己写进上面代码的图片名位置里面
效果图:  
ZI}B8)_N~O`6`0HVL)2GR%H.png
素材图~~~~~~~~~~~~~~~~~~~~
棋盘.jpg
黑.png 白.png
渔舟唱晚.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-22 01:42:25 From FishC Mobile | 显示全部楼层
明天起来写注释
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 17:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表