鱼C论坛

 找回密码
 立即注册
查看: 301|回复: 6

[技术交流] 五子棋人机对战

[复制链接]
发表于 2025-1-18 21:05:32 | 显示全部楼层 |阅读模式

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

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

x
用python cocos2d写的五子棋人机对战。

  1. import cocos
  2. from cocos import draw
  3. from cocos.scene import Scene
  4. from cocos.director import director
  5. from cocos.particle_systems import Fireworks
  6. from pyglet.gl import *
  7. import pyglet

  8. SIZE = 40
  9. WIDTH = SIZE*15
  10. HEIGHT = SIZE*15
  11. N = '0'
  12. W = 'w'
  13. B = 'b'
  14. S = '0'
  15. cdata=[
  16.        [N,N,N,S,B],[B,S,N,N,N],[N,N,N,S,B],[N,B,S,N,N],
  17.        [N,N,S,B,N],[N,N,B,S,N],[N,N,N,S,W],[W,S,N,N,N],
  18.        [N,N,N,S,W],[N,W,S,N,N],[N,N,S,W,N],[N,N,W,S,N],
  19.        [B,B,S,N,N],[N,N,S,B,B],[B,S,B,N,N],[N,N,B,S,B],
  20.        [N,B,S,B,N],[N,B,B,S,N],[N,S,B,B,N],[W,W,S,N,N],
  21.        [N,N,S,W,W],[W,S,W,N,N],[N,N,W,S,W],[N,W,S,W,N],
  22.        [N,W,W,S,N],[N,S,W,W,N],
  23.        [N,S,B,B,B],[B,B,B,S,N],[N,B,B,B,S],[N,B,S,B,B],
  24.        [B,B,S,B,N],[N,S,W,W,W],[W,W,W,S,N],[N,W,W,W,S],
  25.        [N,W,S,W,W],[W,W,S,W,N],
  26.        [S,B,B,B,B],[B,S,B,B,B],[B,B,S,B,B],[B,B,B,S,B],
  27.        [B,B,B,B,S],[S,W,W,W,W],[W,S,W,W,W],[W,W,S,W,W],
  28.        [W,W,W,S,W],[W,W,W,W,S]]
  29. AI_x = -1
  30. AI_y = -1
  31. max_level = -1
  32. board = [['0' for i in range(15)] for j in range(15)]
  33. turn = 'b'
  34. last_turn = 'w'

  35. class GameLayer(cocos.layer.ColorLayer):
  36.     def __init__(self):
  37.         super().__init__(245, 214, 122, 255)
  38.         cocos.layer.Layer.is_event_handler = True
  39.         self.label1 = cocos.text.Label('你赢了!', font_name='kaiti', font_size=100, color=(255, 0, 0, 255),
  40.                                       anchor_x="center", anchor_y="center")
  41.         self.label1.position = (300, 300)

  42.         self.label2 = cocos.text.Label('电脑赢了!', font_name='kaiti', font_size=95, color=(255, 0, 0, 255),
  43.                                        anchor_x="center", anchor_y="center")
  44.         self.label2.position = (300, 300)

  45.         for i in range(0, 15):
  46.             line1 = draw.Line((20, SIZE*i+20), (580, SIZE*i+20), (0, 0, 0, 255), 1)
  47.             self.add(line1)
  48.             line2 = draw.Line((SIZE*i+20, 20), (SIZE*i+20, 580), (0, 0, 0, 255), 1)
  49.             self.add(line2)

  50.     def change_side(self):
  51.         global turn
  52.         if turn == 'b':
  53.             turn = 'w'
  54.         else:
  55.             turn = 'b'

  56.     def check_win(self):
  57.         a = turn
  58.         for i in range(11):
  59.             for j in range(11):
  60.                 if board[i][j] == a and board[i + 1][j + 1] == a and board[i + 2][j + 2] == a \
  61.                         and board[i + 3][j + 3] == a and board[i + 4][j + 4] == a:
  62.                     return True
  63.         for i in range(11):
  64.             for j in range(4, 15):
  65.                 if board[i][j] == a and board[i + 1][j - 1] == a and board[i + 2][j - 2] == a \
  66.                         and board[i + 3][j - 3] == a and board[i + 4][j - 4] == a:
  67.                     return True
  68.         for i in range(15):
  69.             for j in range(11):
  70.                 if board[i][j] == a and board[i][j + 1] == a and board[i][j + 2] == a \
  71.                         and board[i][j + 3] == a and board[i][j + 4] == a:
  72.                     return True
  73.         for i in range(11):
  74.             for j in range(15):
  75.                 if board[i][j] == a and board[i + 1][j] == a and board[i + 2][j] == a \
  76.                         and board[i + 3][j] == a and board[i + 4][j] == a:
  77.                     return True
  78.         return False

  79.     def on_mouse_press(self, x, y, button, _):
  80.         global turn, AI_x, AI_y
  81.         if button == pyglet.window.mouse.LEFT:
  82.             x = round((x - 20) / SIZE)
  83.             y = round((y - 20) / SIZE)
  84.             if turn == 'b':
  85.                 if board[y][x] != '0':
  86.                     return None
  87.                 elif x < 0 or y < 0 or x > 14 or y > 14:
  88.                     return None
  89.                 blk0 = cocos.sprite.Sprite('black.png')
  90.                 blk0.position = (SIZE * x + 20, SIZE * y + 20)
  91.                 self.add(blk0)
  92.                 board[y][x] = 'b'
  93.                 if self.check_win():
  94.                     self.add(self.label1)
  95.                     width, height = director.get_window_size()
  96.                     pe = Fireworks()
  97.                     pe.position = (width // 2, 80)
  98.                     pe.duration = 6
  99.                     pe.speed = 260
  100.                     pe.size = 10.0
  101.                     pe.auto_remove_on_finish = True
  102.                     self.add(pe)
  103.                     director.window.remove_handlers(self)
  104.                 self.change_side()

  105.                 self.AI_play()
  106.                 wht0 = cocos.sprite.Sprite('white.png')
  107.                 wht0.position = (SIZE * AI_x + 20, SIZE * AI_y + 20)
  108.                 self.add(wht0)
  109.                 wht0.do(cocos.actions.Blink(2, 0.5))
  110.                 if self.check_win():
  111.                     self.add(self.label2)
  112.                     director.window.remove_handlers(self)
  113.                 self.change_side()

  114.     def AI_play(self):
  115.         global AI_x, AI_y, max_level
  116.         AI_x = -1
  117.         AI_y = -1
  118.         max_level = -1

  119.         for y in range(15):
  120.             for x in range(15):
  121.                 for level in range(len(cdata) - 1, -1, -1):
  122.                     if level <= max_level:
  123.                         break
  124.                     if x + 4 < 15:
  125.                         if self.auto_match(x, y, level, 1, 0):
  126.                             break
  127.                     if y + 4 < 15:
  128.                         if self.auto_match(x, y,  level, 0, 1):
  129.                             break
  130.                     if x - 4 >= 0 and y + 4 < 15:
  131.                         if self.auto_match(x, y,  level, -1, 1):
  132.                             break
  133.                     if x + 4 < 15 and y + 4 < 15:
  134.                         if self.auto_match(x, y,  level, 1, 1):
  135.                             break
  136.         if AI_x != -1 and AI_y != -1:
  137.             if board[AI_y][AI_x] != '0':
  138.                 return None
  139.             else:
  140.                 board[AI_y][AI_x] = 'w'
  141.                 return True

  142.     def auto_match(self, x, y, level, dx, dy):
  143.         global AI_x, AI_y, max_level
  144.         x_sel = -1
  145.         y_sel = -1
  146.         isfind = True
  147.         for j in range(5):
  148.             cs = board[y + j * dy][x + j * dx]
  149.             if cs == '0':
  150.                 if cdata[level][j] == S:
  151.                     x_sel = x + j * dx
  152.                     y_sel = y + j * dy
  153.                 elif cdata[level][j] != '0':
  154.                     isfind = False
  155.                     break

  156.             elif cs == 'b' and cdata[level][j] != 'b':
  157.                 isfind = False
  158.                 break
  159.             elif cs == 'w' and cdata[level][j] != 'w':
  160.                 isfind = False
  161.                 break

  162.         if isfind:
  163.             max_level = level
  164.             AI_x = x_sel
  165.             AI_y = y_sel
  166.             return True
  167.         return False

  168. director.init(WIDTH, HEIGHT, caption="五子棋")
  169. display = director.window.display
  170. screen_width, screen_height = display.get_default_screen().width, display.get_default_screen().height
  171. window_x = (screen_width - WIDTH) // 2
  172. window_y = (screen_height - HEIGHT) // 2
  173. director.window.set_location(window_x, window_y)
  174. mainscene = Scene()
  175. mainscene.add(GameLayer(), z=1)
  176. director.run(mainscene)
复制代码
black.png
white.png

点评

我很赞同!: 5.0
我很赞同!: 5
太好了!  发表于 2025-1-19 10:02

评分

参与人数 4荣誉 +18 鱼币 +11 贡献 +12 收起 理由
liuhongrun2022 + 5 + 3
foxiangzun + 5 + 5 + 3
zhangjinxuan + 6 + 3 + 3 鱼C有你更精彩^_^
不二如是 + 2 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-18 21:09:27 | 显示全部楼层
加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-18 21:11:52 | 显示全部楼层
支持
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-18 23:12:54 | 显示全部楼层
厉害了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-1-19 10:00:48 | 显示全部楼层
厉害!

点评

发表于 2025-1-19 10:01
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-6 05:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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