找回密码
  注册[Register]
查看: 1367|回复: 8

[python] 人机对战井字棋

[复制链接]
发表于 2020-12-31 20:21 | 显示全部楼层 |阅读模式
禁止求评分、诱导评分、互刷评分、互刷悬赏值,违规者封号处理。
禁止发布推广、邀请码、邀请链接、二维码或者有利益相关的任何推广行为。
所有非原创软件请发布在【精品软件区】,发帖必须按照本版块版规格式发帖。

用pygame实现交互,程序比较简陋,有不足之处欢迎大家批评指正

AI的移动思想

逐个遍历每个空的格子,

如果某个格子落子后AI能赢就下,

如果对方能赢AI就堵住。

代码如下
  1. __author__ = 'lthero'

  2. import pygame
  3. from pygame import *
  4. import random as ra

  5. pygame.init()

  6. white = (255, 255, 255)
  7. black = (0, 0, 0)
  8. size = width, height = 600, 600
  9. screen = pygame.display.set_mode(size)
  10. points = [[0, 0, 0],
  11.           [0, 0, 0],
  12.           [0, 0, 0]]
  13. x = 0
  14. y = 0
  15. flag = 1
  16. lst = []
  17. lst_mine = []
  18. lst_android = []
  19. count = 0
  20. text = pygame.font.SysFont('宋体', 50)
  21. Play_score = 0
  22. AI_score = 0


  23. def draw_restart():
  24.     steps = [(400, 450), (400, 500), (550, 500), (550, 450)]
  25.     pygame.draw.polygon(screen, black, steps, 1)
  26.     text_x = text.render("AGAIN?", 1, black)
  27.     screen.blit(text_x, (410, 460))


  28. def draw_img(player, x, y):
  29.     # 玩家
  30.     if player == 1:
  31.         pygame.draw.circle(screen, black, (x, y), 40, 1)
  32.     # 机器
  33.     else:
  34.         pygame.draw.rect(screen, black, ((x - 20, y - 20), (50, 50)), 1)


  35. def draw_score():
  36.     text_1 = pygame.font.SysFont('宋体', 30)
  37.     text_player_score = text_1.render('PLAYER SCORE ' + str(Play_score), 1, black)
  38.     text_AI_score = text_1.render('AI SCORE     ' + str(AI_score), 1, black)
  39.     screen.blit(text_player_score, (410, 10))
  40.     screen.blit(text_AI_score, (410, 40))


  41. def draw_back():
  42.     screen.fill(white)
  43.     steps = [(100, 100), (100, 400), (400, 400), (400, 100)]
  44.     pygame.draw.polygon(screen, black, steps, 1)
  45.     pygame.draw.lines(screen, black, False, [(100, 200), (400, 200)])
  46.     pygame.draw.lines(screen, black, False, [(100, 300), (400, 300)])
  47.     pygame.draw.lines(screen, black, False, [(200, 100), (200, 400)])
  48.     pygame.draw.lines(screen, black, False, [(300, 100), (300, 400)])


  49. def check_win(tab):
  50.     return ((points[0][0] == tab and points[0][1] == tab and points[0][2] == tab) or
  51.             (points[1][0] == tab and points[1][1] == tab and points[1][2] == tab) or
  52.             (points[2][0] == tab and points[2][1] == tab and points[2][2] == tab) or
  53.             (points[0][0] == tab and points[1][0] == tab and points[2][0] == tab) or
  54.             (points[0][1] == tab and points[1][1] == tab and points[2][1] == tab) or
  55.             (points[0][2] == tab and points[1][2] == tab and points[2][2] == tab) or
  56.             (points[0][0] == tab and points[1][1] == tab and points[2][2] == tab) or
  57.             (points[0][2] == tab and points[1][1] == tab and points[2][0] == tab)
  58.             )


  59. def winner():
  60.     # AI
  61.     if check_win(100):
  62.         return 100
  63.     elif check_win(1):
  64.         return -100


  65. def is_full():
  66.     fl = 0
  67.     for i in range(3):
  68.         for j in range(3):
  69.             if points[i][j] != 0:
  70.                 fl += 1

  71.     return fl


  72. def AI_move():
  73.     # 一步能赢
  74.     for i in range(3):
  75.         for j in range(3):
  76.             if points[i][j] == 0:
  77.                 points[i][j] = 100
  78.                 if check_win(100):
  79.                     return (i, j)
  80.                 else:
  81.                     points[i][j] = 0
  82.     # 堵上
  83.     for i in range(3):
  84.         for j in range(3):
  85.             if points[i][j] == 0:
  86.                 points[i][j] = 1
  87.                 if check_win(1):
  88.                     return (i, j)
  89.                 else:
  90.                     points[i][j] = 0

  91.     # 占中间
  92.     if points[1][1] == 0:
  93.         return (1, 1)

  94.     # 占四角
  95.     temp = []
  96.     for i in (0, 2):
  97.         for j in (0, 2):
  98.             if points[i][j] == 0:
  99.                 temp.append((i, j))
  100.     if len(temp) != 0:
  101.         return ra.choice(temp)

  102.     # 占四边
  103.     for i in ((0, 1), (1, 0), (1, 2), (2, 1)):
  104.         if points[i[0]][i[1]] == 0:
  105.             temp.append((i[0], i[1]))
  106.     if len(temp) != 0:
  107.         return ra.choice(temp)


  108. def draw_all():
  109.     draw_back()
  110.     draw_score()
  111.     for i in lst:
  112.         draw_img(i[0], i[1], i[2])
  113.     if flag == 100:
  114.         text_conent = text.render("AI win", 1, black)
  115.         screen.blit(text_conent, (220, 50))
  116.     elif flag == -100:
  117.         text_conent = text.render("You win", 1, black)
  118.         screen.blit(text_conent, (220, 50))
  119.     elif flag == 123:
  120.         text_conent = text.render("TIE", 1, black)
  121.         screen.blit(text_conent, (220, 50))
  122.     if flag == 123 or flag == 100 or flag == -100:
  123.         draw_restart()


  124. def play():
  125.     global flag, AI_score, Play_score
  126.     while True:
  127.         for event in pygame.event.get():
  128.             if event.type == pygame.QUIT:
  129.                 exit()
  130.             if event.type == MOUSEBUTTONDOWN:
  131.                 x, y = pygame.mouse.get_pos()
  132.                 if 400 < x < 550 and 450 < y < 500:
  133.                     lst.clear()
  134.                     for i in range(3):
  135.                         for j in range(3):
  136.                             points[i][j] = 0
  137.                     flag = 1
  138.                 if 100 <= x <= 400 and 100 <= y <= 400:
  139.                     x = (x - 100) // 100
  140.                     y = (y - 100) // 100
  141.                     l_x = x * 100 + 150
  142.                     l_y = y * 100 + 150
  143.                     # player
  144.                     if flag == 1:
  145.                         if is_full() != 9:
  146.                             if points[x][y] == 0:
  147.                                 points[x][y] = 1
  148.                                 lst.append((1, l_x, l_y))
  149.                                 if winner() == -100:
  150.                                     flag = -100
  151.                                     Play_score += 1
  152.                                     print('player win')
  153.                                 else:
  154.                                     flag = -1
  155.                         else:
  156.                             flag = 123

  157.             if flag == -1:
  158.                 if is_full() != 9:
  159.                     # 人机动
  160.                     xx, yy = AI_move()
  161.                     l_x = xx * 100 + 150
  162.                     l_y = yy * 100 + 150
  163.                     points[xx][yy] = 100
  164.                     lst.append((2, l_x, l_y))
  165.                     if winner() == 100:
  166.                         flag = 100
  167.                         AI_score += 1
  168.                         print('AI win')
  169.                     else:
  170.                         flag = 1
  171.                 else:
  172.                     flag = 123

  173.         draw_all()
  174.         pygame.display.flip()


  175. if __name__ == '__main__':
  176.     play()
复制代码


如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2020-12-31 20:55 | 显示全部楼层
感谢楼主分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2020-12-31 21:24 | 显示全部楼层

感谢分享,谢谢提供分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2020-12-31 22:30 | 显示全部楼层
6666666666
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2020-12-31 23:01 | 显示全部楼层
谢谢大佬
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2020-12-31 23:38 | 显示全部楼层

支持楼主,谢谢分享。
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-1-1 00:35 | 显示全部楼层
谢谢@Thanks!
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-1-1 07:13 | 显示全部楼层
多谢楼主分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2021-1-1 10:22 | 显示全部楼层
谢谢分享!
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

RSS订阅|手机版|小黑屋|大牛论坛 |我的广告

GMT+8, 2024-5-3 02:42 , Processed in 0.037653 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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