萝卜头IT论坛

搜索
查看: 1684|回复: 12
收起左侧

Python 五子棋

[复制链接]
发表于 2022-12-1 15:56:01 | 显示全部楼层 |阅读模式
本帖最后由 电脑时间 于 2022-12-2 09:16 编辑

知道最近我怎么没发帖么?
我***,因为人机程序太难编写了


所以我今天给的是五子棋
人机版(高[ruo]级[zhi]版)
源码
人人对战
  1. """五子棋之人人对战"""

  2. import sys
  3. import pygame
  4. from pygame.locals import *
  5. import pygame.gfxdraw
  6. from checkerboard import Checkerboard, BLACK_CHESSMAN, WHITE_CHESSMAN, Point


  7. SIZE = 30
  8. Line_Points = 19
  9. Outer_Width = 20
  10. Border_Width = 4
  11. Inside_Width = 4
  12. Border_Length = SIZE * (Line_Points - 1) + Inside_Width * 2 + Border_Width
  13. Start_X = Start_Y = Outer_Width + int(Border_Width / 2) + Inside_Width
  14. SCREEN_HEIGHT = SIZE * (Line_Points - 1) + Outer_Width * 2 + Border_Width + Inside_Width * 2
  15. SCREEN_WIDTH = SCREEN_HEIGHT + 200

  16. Stone_Radius = SIZE // 2 - 3
  17. Stone_Radius2 = SIZE // 2 + 3
  18. Checkerboard_Color = (0xE3, 0x92, 0x65)
  19. BLACK_COLOR = (0, 0, 0)
  20. WHITE_COLOR = (255, 255, 255)
  21. RED_COLOR = (200, 30, 30)
  22. BLUE_COLOR = (30, 30, 200)
  23. BLACK_STONE_COLOR = (45, 45, 45)
  24. WHITE_STONE_COLOR = (219, 219, 219)

  25. RIGHT_INFO_POS_X = SCREEN_HEIGHT + Stone_Radius2 * 2 + 10


  26. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  27.     imgText = font.render(text, True, fcolor)
  28.     screen.blit(imgText, (x, y))


  29. def main():
  30.     pygame.init()
  31.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  32.     pygame.display.set_caption('五子棋')

  33.     font1 = pygame.font.SysFont('SimHei', 36)
  34.     font2 = pygame.font.SysFont('SimHei', 72)
  35.     fwidth, fheight = font2.size('黑方获胜')

  36.     checkerboard = Checkerboard(Line_Points)
  37.     cur_runner = BLACK_CHESSMAN
  38.     winner = None

  39.     while True:
  40.         for event in pygame.event.get():
  41.             if event.type == QUIT:
  42.                 sys.exit()
  43.             elif event.type == KEYDOWN:
  44.                 if event.key == K_RETURN:
  45.                     if winner is not None:
  46.                         winner = None
  47.                         cur_runner = BLACK_CHESSMAN
  48.                         checkerboard = Checkerboard(Line_Points)
  49.             elif event.type == MOUSEBUTTONDOWN:
  50.                 if winner is None:
  51.                     pressed_array = pygame.mouse.get_pressed()
  52.                     if pressed_array[0]:
  53.                         mouse_pos = pygame.mouse.get_pos()
  54.                         click_point = _get_clickpoint(mouse_pos)
  55.                         if click_point is not None:
  56.                             if checkerboard.can_drop(click_point):
  57.                                 winner = checkerboard.drop(cur_runner, click_point)
  58.                                 if cur_runner == BLACK_CHESSMAN:
  59.                                     cur_runner = WHITE_CHESSMAN
  60.                                 else:
  61.                                     cur_runner = BLACK_CHESSMAN
  62.                         else:
  63.                             print('超出棋盘区域')

  64.         _draw_checkerboard(screen)

  65.         for i, row in enumerate(checkerboard.checkerboard):
  66.             for j, cell in enumerate(row):
  67.                 if cell == BLACK_CHESSMAN.Value:
  68.                     _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color)
  69.                 elif cell == WHITE_CHESSMAN.Value:
  70.                     _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color)

  71.         _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20), BLACK_STONE_COLOR)
  72.         _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + 20 + Stone_Radius2 * 3), WHITE_STONE_COLOR)

  73.         if winner:
  74.             print_text(screen, font2, (SCREEN_WIDTH - fwidth)//2, (SCREEN_HEIGHT - fheight)//2, winner.Name + '获胜', RED_COLOR)

  75.         if cur_runner == BLACK_CHESSMAN:
  76.             print_text(screen, font1, RIGHT_INFO_POS_X, Start_X, '失败' if winner else '落子中', BLUE_COLOR)
  77.         else:
  78.             print_text(screen, font1, RIGHT_INFO_POS_X, Start_X + Stone_Radius2 * 3, '失败' if winner else '落子中', BLUE_COLOR)

  79.         pygame.display.flip()


  80. def _draw_checkerboard(screen):
  81.     screen.fill(Checkerboard_Color)
  82.     pygame.draw.rect(screen, BLACK_COLOR, (Outer_Width, Outer_Width, Border_Length, Border_Length), Border_Width)
  83.     for i in range(Line_Points):
  84.         pygame.draw.line(screen, BLACK_COLOR,
  85.                          (Start_Y, Start_Y + SIZE * i),
  86.                          (Start_Y + SIZE * (Line_Points - 1), Start_Y + SIZE * i),
  87.                          1)
  88.     for j in range(Line_Points):
  89.         pygame.draw.line(screen, BLACK_COLOR,
  90.                          (Start_X + SIZE * j, Start_X),
  91.                          (Start_X + SIZE * j, Start_X + SIZE * (Line_Points - 1)),
  92.                          1)
  93.     for i in (3, 9, 15):
  94.         for j in (3, 9, 15):
  95.             if i == j == 9:
  96.                 radius = 5
  97.             else:
  98.                 radius = 3
  99.             # pygame.draw.circle(screen, BLACK, (Start_X + SIZE * i, Start_Y + SIZE * j), radius)
  100.             pygame.gfxdraw.aacircle(screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)
  101.             pygame.gfxdraw.filled_circle(screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)


  102. def _draw_chessman(screen, point, stone_color):
  103.     # pygame.draw.circle(screen, stone_color, (Start_X + SIZE * point.X, Start_Y + SIZE * point.Y), Stone_Radius)
  104.     pygame.gfxdraw.aacircle(screen, Start_X + SIZE * point.X, Start_Y + SIZE * point.Y, Stone_Radius, stone_color)
  105.     pygame.gfxdraw.filled_circle(screen, Start_X + SIZE * point.X, Start_Y + SIZE * point.Y, Stone_Radius, stone_color)


  106. def _draw_chessman_pos(screen, pos, stone_color):
  107.     pygame.gfxdraw.aacircle(screen, pos[0], pos[1], Stone_Radius2, stone_color)
  108.     pygame.gfxdraw.filled_circle(screen, pos[0], pos[1], Stone_Radius2, stone_color)


  109. def _get_clickpoint(click_pos):
  110.     pos_x = click_pos[0] - Start_X
  111.     pos_y = click_pos[1] - Start_Y
  112.     if pos_x < -Inside_Width or pos_y < -Inside_Width:
  113.         return None
  114.     x = pos_x // SIZE
  115.     y = pos_y // SIZE
  116.     if pos_x % SIZE > Stone_Radius:
  117.         x += 1
  118.     if pos_y % SIZE > Stone_Radius:
  119.         y += 1
  120.     if x >= Line_Points or y >= Line_Points:
  121.         return None

  122.     return Point(x, y)


  123. if __name__ == '__main__':
  124.     main()
复制代码

人机版(不开大一点你们看不见)
  1. """五子棋之人机对战"""

  2. import sys
  3. import random
  4. import pygame
  5. from pygame.locals import *
  6. import pygame.gfxdraw
  7. from checkerboard import Checkerboard, BLACK_CHESSMAN, WHITE_CHESSMAN, offset, Point

  8. SIZE = 30
  9. Line_Points = 19
  10. Outer_Width = 20
  11. Border_Width = 4
  12. Inside_Width = 4
  13. Border_Length = SIZE * (Line_Points - 1) + Inside_Width * 2 + Border_Width
  14. Start_X = Start_Y = Outer_Width + int(Border_Width / 2) + Inside_Width
  15. SCREEN_HEIGHT = SIZE * (Line_Points - 1) + Outer_Width * 2 + Border_Width + Inside_Width * 2
  16. SCREEN_WIDTH = SCREEN_HEIGHT + 200

  17. Stone_Radius = SIZE // 2 - 3
  18. Stone_Radius2 = SIZE // 2 + 3
  19. Checkerboard_Color = (0xE3, 0x92, 0x65)
  20. BLACK_COLOR = (0, 0, 0)
  21. WHITE_COLOR = (255, 255, 255)
  22. RED_COLOR = (200, 30, 30)
  23. BLUE_COLOR = (30, 30, 200)

  24. RIGHT_INFO_POS_X = SCREEN_HEIGHT + Stone_Radius2 * 2 + 10


  25. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  26.     imgText = font.render(text, True, fcolor)
  27.     screen.blit(imgText, (x, y))


  28. def main():
  29.     pygame.init()
  30.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  31.     pygame.display.set_caption('五子棋')

  32.     font1 = pygame.font.SysFont('SimHei', 32)
  33.     font2 = pygame.font.SysFont('SimHei', 72)
  34.     fwidth, fheight = font2.size('黑方获胜')

  35.     checkerboard = Checkerboard(Line_Points)
  36.     cur_runner = BLACK_CHESSMAN
  37.     winner = None
  38.     computer = AI(Line_Points, WHITE_CHESSMAN)

  39.     black_win_count = 0
  40.     white_win_count = 0

  41.     while True:
  42.         for event in pygame.event.get():
  43.             if event.type == QUIT:
  44.                 sys.exit()
  45.             elif event.type == KEYDOWN:
  46.                 if event.key == K_RETURN:
  47.                     if winner is not None:
  48.                         winner = None
  49.                         cur_runner = BLACK_CHESSMAN
  50.                         checkerboard = Checkerboard(Line_Points)
  51.                         computer = AI(Line_Points, WHITE_CHESSMAN)
  52.             elif event.type == MOUSEBUTTONDOWN:
  53.                 if winner is None:
  54.                     pressed_array = pygame.mouse.get_pressed()
  55.                     if pressed_array[0]:
  56.                         mouse_pos = pygame.mouse.get_pos()
  57.                         click_point = _get_clickpoint(mouse_pos)
  58.                         if click_point is not None:
  59.                             if checkerboard.can_drop(click_point):
  60.                                 winner = checkerboard.drop(cur_runner, click_point)
  61.                                 if winner is None:
  62.                                     cur_runner = _get_next(cur_runner)
  63.                                     computer.get_opponent_drop(click_point)
  64.                                     AI_point = computer.AI_drop()
  65.                                     winner = checkerboard.drop(cur_runner, AI_point)
  66.                                     if winner is not None:
  67.                                         white_win_count += 1
  68.                                     cur_runner = _get_next(cur_runner)
  69.                                 else:
  70.                                     black_win_count += 1
  71.                         else:
  72.                             print('超出棋盘区域')

  73.         _draw_checkerboard(screen)

  74.         for i, row in enumerate(checkerboard.checkerboard):
  75.             for j, cell in enumerate(row):
  76.                 if cell == BLACK_CHESSMAN.Value:
  77.                     _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color)
  78.                 elif cell == WHITE_CHESSMAN.Value:
  79.                     _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color)

  80.         _draw_left_info(screen, font1, cur_runner, black_win_count, white_win_count)

  81.         if winner:
  82.             print_text(screen, font2, (SCREEN_WIDTH - fwidth)//2, (SCREEN_HEIGHT - fheight)//2, winner.Name + '获胜', RED_COLOR)

  83.         pygame.display.flip()


  84. def _get_next(cur_runner):
  85.     if cur_runner == BLACK_CHESSMAN:
  86.         return WHITE_CHESSMAN
  87.     else:
  88.         return BLACK_CHESSMAN


  89. def _draw_checkerboard(screen):
  90.     screen.fill(Checkerboard_Color)
  91.     pygame.draw.rect(screen, BLACK_COLOR, (Outer_Width, Outer_Width, Border_Length, Border_Length), Border_Width)
  92.     for i in range(Line_Points):
  93.         pygame.draw.line(screen, BLACK_COLOR,
  94.                          (Start_Y, Start_Y + SIZE * i),
  95.                          (Start_Y + SIZE * (Line_Points - 1), Start_Y + SIZE * i),
  96.                          1)
  97.     for j in range(Line_Points):
  98.         pygame.draw.line(screen, BLACK_COLOR,
  99.                          (Start_X + SIZE * j, Start_X),
  100.                          (Start_X + SIZE * j, Start_X + SIZE * (Line_Points - 1)),
  101.                          1)
  102.     for i in (3, 9, 15):
  103.         for j in (3, 9, 15):
  104.             if i == j == 9:
  105.                 radius = 5
  106.             else:
  107.                 radius = 3
  108.             pygame.gfxdraw.aacircle(screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)
  109.             pygame.gfxdraw.filled_circle(screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)

  110. def _draw_chessman(screen, point, stone_color):
  111.     pygame.gfxdraw.aacircle(screen, Start_X + SIZE * point.X, Start_Y + SIZE * point.Y, Stone_Radius, stone_color)
  112.     pygame.gfxdraw.filled_circle(screen, Start_X + SIZE * point.X, Start_Y + SIZE * point.Y, Stone_Radius, stone_color)


  113. def _draw_left_info(screen, font, cur_runner, black_win_count, white_win_count):
  114.     _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + Stone_Radius2), BLACK_CHESSMAN.Color)
  115.     _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + Stone_Radius2 * 4), WHITE_CHESSMAN.Color)

  116.     print_text(screen, font, RIGHT_INFO_POS_X, Start_X + 3, '玩家', BLUE_COLOR)
  117.     print_text(screen, font, RIGHT_INFO_POS_X, Start_X + Stone_Radius2 * 3 + 3, '电脑', BLUE_COLOR)

  118.     print_text(screen, font, SCREEN_HEIGHT, SCREEN_HEIGHT - Stone_Radius2 * 8, '战况:', BLUE_COLOR)
  119.     _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, SCREEN_HEIGHT - int(Stone_Radius2 * 4.5)), BLACK_CHESSMAN.Color)
  120.     _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, SCREEN_HEIGHT - Stone_Radius2 * 2), WHITE_CHESSMAN.Color)
  121.     print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - int(Stone_Radius2 * 5.5) + 3, f'{black_win_count} 胜', BLUE_COLOR)
  122.     print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - Stone_Radius2 * 3 + 3, f'{white_win_count} 胜', BLUE_COLOR)


  123. def _draw_chessman_pos(screen, pos, stone_color):
  124.     pygame.gfxdraw.aacircle(screen, pos[0], pos[1], Stone_Radius2, stone_color)
  125.     pygame.gfxdraw.filled_circle(screen, pos[0], pos[1], Stone_Radius2, stone_color)


  126. def _get_clickpoint(click_pos):
  127.     pos_x = click_pos[0] - Start_X
  128.     pos_y = click_pos[1] - Start_Y
  129.     if pos_x < -Inside_Width or pos_y < -Inside_Width:
  130.         return None
  131.     x = pos_x // SIZE
  132.     y = pos_y // SIZE
  133.     if pos_x % SIZE > Stone_Radius:
  134.         x += 1
  135.     if pos_y % SIZE > Stone_Radius:
  136.         y += 1
  137.     if x >= Line_Points or y >= Line_Points:
  138.         return None

  139.     return Point(x, y)


  140. class AI:
  141.     def __init__(self, line_points, chessman):
  142.         self._line_points = line_points
  143.         self._my = chessman
  144.         self._opponent = BLACK_CHESSMAN if chessman == WHITE_CHESSMAN else WHITE_CHESSMAN
  145.         self._checkerboard = [[0] * line_points for _ in range(line_points)]

  146.     def get_opponent_drop(self, point):
  147.         self._checkerboard[point.Y][point.X] = self._opponent.Value

  148.     def AI_drop(self):
  149.         point = None
  150.         score = 0
  151.         for i in range(self._line_points):
  152.             for j in range(self._line_points):
  153.                 if self._checkerboard[j][i] == 0:
  154.                     _score = self._get_point_score(Point(i, j))
  155.                     if _score > score:
  156.                         score = _score
  157.                         point = Point(i, j)
  158.                     elif _score == score and _score > 0:
  159.                         r = random.randint(0, 100)
  160.                         if r % 2 == 0:
  161.                             point = Point(i, j)
  162.         self._checkerboard[point.Y][point.X] = self._my.Value
  163.         return point

  164.     def _get_point_score(self, point):
  165.         score = 0
  166.         for os in offset:
  167.             score += self._get_direction_score(point, os[0], os[1])
  168.         return score

  169.     def _get_direction_score(self, point, x_offset, y_offset):
  170.         count = 0  
  171.         _count = 0  
  172.         space = None   
  173.         _space = None  
  174.         both = 0   
  175.         _both = 0   

  176.         flag = self._get_stone_color(point, x_offset, y_offset, True)
  177.         if flag != 0:
  178.             for step in range(1, 6):
  179.                 x = point.X + step * x_offset
  180.                 y = point.Y + step * y_offset
  181.                 if 0 <= x < self._line_points and 0 <= y < self._line_points:
  182.                     if flag == 1:
  183.                         if self._checkerboard[y][x] == self._my.Value:
  184.                             count += 1
  185.                             if space is False:
  186.                                 space = True
  187.                         elif self._checkerboard[y][x] == self._opponent.Value:
  188.                             _both += 1
  189.                             break
  190.                         else:
  191.                             if space is None:
  192.                                 space = False
  193.                             else:
  194.                                 break
  195.                     elif flag == 2:
  196.                         if self._checkerboard[y][x] == self._my.Value:
  197.                             _both += 1
  198.                             break
  199.                         elif self._checkerboard[y][x] == self._opponent.Value:
  200.                             _count += 1
  201.                             if _space is False:
  202.                                 _space = True
  203.                         else:
  204.                             if _space is None:
  205.                                 _space = False
  206.                             else:
  207.                                 break
  208.                 else:
  209.                     if flag == 1:
  210.                         both += 1
  211.                     elif flag == 2:
  212.                         _both += 1

  213.         if space is False:
  214.             space = None
  215.         if _space is False:
  216.             _space = None

  217.         _flag = self._get_stone_color(point, -x_offset, -y_offset, True)
  218.         if _flag != 0:
  219.             for step in range(1, 6):
  220.                 x = point.X - step * x_offset
  221.                 y = point.Y - step * y_offset
  222.                 if 0 <= x < self._line_points and 0 <= y < self._line_points:
  223.                     if _flag == 1:
  224.                         if self._checkerboard[y][x] == self._my.Value:
  225.                             count += 1
  226.                             if space is False:
  227.                                 space = True
  228.                         elif self._checkerboard[y][x] == self._opponent.Value:
  229.                             _both += 1
  230.                             break
  231.                         else:
  232.                             if space is None:
  233.                                 space = False
  234.                             else:
  235.                                 break   
  236.                     elif _flag == 2:
  237.                         if self._checkerboard[y][x] == self._my.Value:
  238.                             _both += 1
  239.                             break
  240.                         elif self._checkerboard[y][x] == self._opponent.Value:
  241.                             _count += 1
  242.                             if _space is False:
  243.                                 _space = True
  244.                         else:
  245.                             if _space is None:
  246.                                 _space = False
  247.                             else:
  248.                                 break
  249.                 else:
  250.                     if _flag == 1:
  251.                         both += 1
  252.                     elif _flag == 2:
  253.                         _both += 1

  254.         score = 0
  255.         if count == 4:
  256.             score = 10000
  257.         elif _count == 4:
  258.             score = 9000
  259.         elif count == 3:
  260.             if both == 0:
  261.                 score = 1000
  262.             elif both == 1:
  263.                 score = 100
  264.             else:
  265.                 score = 0
  266.         elif _count == 3:
  267.             if _both == 0:
  268.                 score = 900
  269.             elif _both == 1:
  270.                 score = 90
  271.             else:
  272.                 score = 0
  273.         elif count == 2:
  274.             if both == 0:
  275.                 score = 100
  276.             elif both == 1:
  277.                 score = 10
  278.             else:
  279.                 score = 0
  280.         elif _count == 2:
  281.             if _both == 0:
  282.                 score = 90
  283.             elif _both == 1:
  284.                 score = 9
  285.             else:
  286.                 score = 0
  287.         elif count == 1:
  288.             score = 10
  289.         elif _count == 1:
  290.             score = 9
  291.         else:
  292.             score = 0

  293.         if space or _space:
  294.             score /= 2

  295.         return score

  296.     def _get_stone_color(self, point, x_offset, y_offset, next):
  297.         x = point.X + x_offset
  298.         y = point.Y + y_offset
  299.         if 0 <= x < self._line_points and 0 <= y < self._line_points:
  300.             if self._checkerboard[y][x] == self._my.Value:
  301.                 return 1
  302.             elif self._checkerboard[y][x] == self._opponent.Value:
  303.                 return 2
  304.             else:
  305.                 if next:
  306.                     return self._get_stone_color(Point(x, y), x_offset, y_offset, False)
  307.                 else:
  308.                     return 0
  309.         else:
  310.             return 0


  311. if __name__ == '__main__':
  312.     main()
复制代码

对了还要有一个chekerboard
  1. from collections import namedtuple

  2. Chessman = namedtuple('Chessman', 'Name Value Color')
  3. Point = namedtuple('Point', 'X Y')

  4. BLACK_CHESSMAN = Chessman('黑子', 1, (45, 45, 45))
  5. WHITE_CHESSMAN = Chessman('白子', 2, (219, 219, 219))

  6. offset = [(1, 0), (0, 1), (1, 1), (1, -1)]


  7. class Checkerboard:
  8.     def __init__(self, line_points):
  9.         self._line_points = line_points
  10.         self._checkerboard = [[0] * line_points for _ in range(line_points)]

  11.     def _get_checkerboard(self):
  12.         return self._checkerboard

  13.     checkerboard = property(_get_checkerboard)

  14.     def can_drop(self, point):
  15.         return self._checkerboard[point.Y][point.X] == 0

  16.     def drop(self, chessman, point):
  17.         print(f'{chessman.Name} ({point.X}, {point.Y})')
  18.         self._checkerboard[point.Y][point.X] = chessman.Value

  19.         if self._win(point):
  20.             print(f'{chessman.Name}获胜')
  21.             return chessman

  22.     def _win(self, point):
  23.         cur_value = self._checkerboard[point.Y][point.X]
  24.         for os in offset:
  25.             if self._get_count_on_direction(point, cur_value, os[0], os[1]):
  26.                 return True

  27.     def _get_count_on_direction(self, point, value, x_offset, y_offset):
  28.         count = 1
  29.         for step in range(1, 5):
  30.             x = point.X + step * x_offset
  31.             y = point.Y + step * y_offset
  32.             if 0 <= x < self._line_points and 0 <= y < self._line_points and self._checkerboard[y][x] == value:
  33.                 count += 1
  34.             else:
  35.                 break
  36.         for step in range(1, 5):
  37.             x = point.X - step * x_offset
  38.             y = point.Y - step * y_offset
  39.             if 0 <= x < self._line_points and 0 <= y < self._line_points and self._checkerboard[y][x] == value:
  40.                 count += 1
  41.             else:
  42.                 break

  43.         return count >= 5
复制代码

五子棋.zip (7.15 KB, 下载次数: 113)
敲了我5个小时

五子棋.zip

7.19 KB, 下载次数: 132

回复

使用道具 举报

发表于 2022-12-1 16:09:02 | 显示全部楼层
可以可以 我要下回来试试
回复

使用道具 举报

发表于 2022-12-1 16:45:35 | 显示全部楼层
这个有兴趣的人可以试一下,谢谢楼主的分享。
回复

使用道具 举报

发表于 2022-12-1 16:54:29 | 显示全部楼层
我服了没装pygame pip都没有 国内curl pip速度感人
回复

使用道具 举报

发表于 2022-12-1 20:08:21 来自手机 | 显示全部楼层
我用Python3.11环境安装pygame报错
回复

使用道具 举报

发表于 2022-12-2 09:20:30 | 显示全部楼层
爱电脑的昕宇 发表于 2022-12-1 20:08
我用Python3.11环境安装pygame报错

我macOS是pip都没装上...
回复

使用道具 举报

发表于 2022-12-2 15:45:53 | 显示全部楼层
Ace_qingfeng 发表于 2022-12-2 09:20
我macOS是pip都没装上...

参见这里的回复:https://bbs.luobotou.org/thread-53765-1-1.html
回复

使用道具 举报

发表于 2022-12-3 10:36:05 | 显示全部楼层
厉害,毅力可真强,如果是我早就废了                        
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-4-25 09:26 , Processed in 0.096021 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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