当前位置:   article > 正文

pycharm 五子棋 (后附源码)_pycharm五子棋

pycharm五子棋

五子棋在计算机科学中有很多应用,主要包括以下几个方面:

  1. 人工智能:五子棋是一种复杂的思维游戏,对于计算机人工智能的发展有很大的帮助。许多人工智能算法都可以基于五子棋的规则进行学习和优化,比如深度学习、强化学习、遗传算法等。

  2. 计算机博弈:五子棋是一种人机博弈,对于计算机博弈的研究和发展也提供了很多参考和借鉴。五子棋人机对弈的结果能够评估机器智能水平和人机分析能力。

  3. 算法研究:五子棋的规则比较简单,但是却有很高的复杂度和难度,能够对计算机算法的优化和研究提供很好的平台。比如,通过五子棋的对弈分析,可以得到很多构造性算法,比如长连、活三等。

  4. 游戏开发:五子棋是一种受欢迎的棋类游戏,在游戏开发中被广泛应用。通过五子棋的规则和算法,可以开发出基于人工智能的智能对战游戏,提高游戏的趣味性和挑战性。

前文

如果大家想自学python的话给大家一些资料

链接: https://pan.baidu.com/s/1M7wEE4_s6Hh1MBQKIZClAg

提取码: 3851 

正文

今天我们就用pycharm来做一个五子棋游戏

准备工具:pycharm2023 、pygame

初始化和画棋盘

  1. # -*- coding=utf-8 -*-
  2. import random
  3. import pygame
  4. from pygame.locals import MOUSEBUTTONUP
  5. pygame.init()
  6. space = 60 # 四周留下的边距
  7. cell_size = 40 # 每个格子大小
  8. cell_num = 15
  9. grid_size = cell_size * (cell_num - 1) + space * 2 # 棋盘的大小
  10. screencaption = pygame.display.set_caption('FIR')
  11. screen = pygame.display.set_mode((grid_size, grid_size)) # 设置窗口长宽
  12. chess_arr = []
  13. flag = 1 # 1黑 2白
  14. game_state = 1 # 游戏状态1.表示正常进行 2.表示黑胜 3.表示白胜

运行内容

  1. def get_one_dire_num(lx, ly, dx, dy, m):
  2. tx = lx
  3. ty = ly
  4. s = 0
  5. while True:
  6. tx += dx
  7. ty += dy
  8. if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
  9. s += 1
  10. def check_win(chess_arr, flag):
  11. m = [[0] * cell_num for i in range(cell_num)] # 先定义一个15*15的全0的数组,不能用[[0]*cell_num]*cell_num的方式去定义因为一位数组会被重复引用
  12. for x, y, c in chess_arr:
  13. if c == flag:
  14. m[y][x] = 1 # 上面有棋则标1
  15. lx = chess_arr[-1][0] # 最后一个子的x
  16. ly = chess_arr[-1][1] # 最后一个子的y
  17. dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
  18. [(-1, 1), (1, -1)]] # 4个方向数组,往左+往右、往上+往下、往左上+往右下、往左下+往右上,4组判断方向
  19. for dire1, dire2 in dire_arr:
  20. dx, dy = dire1
  21. num1 = get_one_dire_num(lx, ly, dx, dy, m)
  22. dx, dy = dire2
  23. num2 = get_one_dire_num(lx, ly, dx, dy, m)
  24. if num1 + num2 + 1 >= 5: return True
  25. return False
  26. while True:
  27. for event in pygame.event.get():
  28. if event.type == pygame.QUIT:
  29. pygame.quit()
  30. exit()
  31. if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: # 鼠标弹起
  32. x, y = pygame.mouse.get_pos() # 获取鼠标位置
  33. xi = int(round((x - space) * 1.0 / cell_size)) # 获取到x方向上取整的序号
  34. yi = int(round((y - space) * 1.0 / cell_size)) # 获取到y方向上取整的序号
  35. if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi, 1) not in chess_arr and (
  36. xi, yi, 2) not in chess_arr:
  37. chess_arr.append((xi, yi, flag))
  38. if check_win(chess_arr, flag):
  39. game_state = 2 if flag == 1 else 3
  40. else:
  41. flag = 2 if flag == 1 else 1
  42. screen.fill((205,186,150))
  43. for x in range(0, cell_size * cell_num, cell_size):
  44. pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space),
  45. (x + space, cell_size * (cell_num - 1) + space), 1)
  46. for y in range(0, cell_size * cell_num, cell_size):
  47. pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space),
  48. (cell_size * (cell_num - 1) + space, y + space), 1)
  49. for x, y, c in chess_arr:
  50. chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
  51. pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
  52. if game_state != 1:
  53. myfont = pygame.font.Font(None, 60)
  54. white = 210, 210, 0
  55. win_text = "%s win" % ('black' if game_state == 2 else 'white')
  56. textImage = myfont.render(win_text, True, white)
  57. screen.blit(textImage, (260, 320))
  58. pygame.display.update() # 必须调用update才能看到绘图显示

上效果图

 总体来说还是非常简单的

如果要打包成exe请看:PY文件转exe教程_奔跑的编程者的博客-CSDN博客

完整源码

  1. # -*- coding=utf-8 -*-
  2. import random
  3. import pygame
  4. from pygame.locals import MOUSEBUTTONUP
  5. pygame.init()
  6. space = 60 # 四周留下的边距
  7. cell_size = 40 # 每个格子大小
  8. cell_num = 15
  9. grid_size = cell_size * (cell_num - 1) + space * 2 # 棋盘的大小
  10. screencaption = pygame.display.set_caption('FIR')
  11. screen = pygame.display.set_mode((grid_size, grid_size)) # 设置窗口长宽
  12. chess_arr = []
  13. flag = 1 # 1黑 2白
  14. game_state = 1 # 游戏状态1.表示正常进行 2.表示黑胜 3.表示白胜
  15. def get_one_dire_num(lx, ly, dx, dy, m):
  16. tx = lx
  17. ty = ly
  18. s = 0
  19. while True:
  20. tx += dx
  21. ty += dy
  22. if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
  23. s += 1
  24. def check_win(chess_arr, flag):
  25. m = [[0] * cell_num for i in range(cell_num)] # 先定义一个15*15的全0的数组,不能用[[0]*cell_num]*cell_num的方式去定义因为一位数组会被重复引用
  26. for x, y, c in chess_arr:
  27. if c == flag:
  28. m[y][x] = 1 # 上面有棋则标1
  29. lx = chess_arr[-1][0] # 最后一个子的x
  30. ly = chess_arr[-1][1] # 最后一个子的y
  31. dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
  32. [(-1, 1), (1, -1)]] # 4个方向数组,往左+往右、往上+往下、往左上+往右下、往左下+往右上,4组判断方向
  33. for dire1, dire2 in dire_arr:
  34. dx, dy = dire1
  35. num1 = get_one_dire_num(lx, ly, dx, dy, m)
  36. dx, dy = dire2
  37. num2 = get_one_dire_num(lx, ly, dx, dy, m)
  38. if num1 + num2 + 1 >= 5: return True
  39. return False
  40. while True:
  41. for event in pygame.event.get():
  42. if event.type == pygame.QUIT:
  43. pygame.quit()
  44. exit()
  45. if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: # 鼠标弹起
  46. x, y = pygame.mouse.get_pos() # 获取鼠标位置
  47. xi = int(round((x - space) * 1.0 / cell_size)) # 获取到x方向上取整的序号
  48. yi = int(round((y - space) * 1.0 / cell_size)) # 获取到y方向上取整的序号
  49. if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi, 1) not in chess_arr and (
  50. xi, yi, 2) not in chess_arr:
  51. chess_arr.append((xi, yi, flag))
  52. if check_win(chess_arr, flag):
  53. game_state = 2 if flag == 1 else 3
  54. else:
  55. flag = 2 if flag == 1 else 1
  56. screen.fill((205,186,150))
  57. for x in range(0, cell_size * cell_num, cell_size):
  58. pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space),
  59. (x + space, cell_size * (cell_num - 1) + space), 1)
  60. for y in range(0, cell_size * cell_num, cell_size):
  61. pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space),
  62. (cell_size * (cell_num - 1) + space, y + space), 1)
  63. for x, y, c in chess_arr:
  64. chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
  65. pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
  66. if game_state != 1:
  67. myfont = pygame.font.Font(None, 60)
  68. white = 210, 210, 0
  69. win_text = "%s win" % ('black' if game_state == 2 else 'white')
  70. textImage = myfont.render(win_text, True, white)
  71. screen.blit(textImage, (260, 320))
  72. pygame.display.update() # 必须调用update才能看到绘图显示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/937585
推荐阅读
相关标签
  

闽ICP备14008679号