人机对战井字棋
用pygame实现交互,程序比较简陋,有不足之处欢迎大家批评指正AI的移动思想
逐个遍历每个空的格子,
如果某个格子落子后AI能赢就下,
如果对方能赢AI就堵住。
代码如下
__author__ = 'lthero'
import pygame
from pygame import *
import random as ra
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
size = width, height = 600, 600
screen = pygame.display.set_mode(size)
points = [,
,
]
x = 0
y = 0
flag = 1
lst = []
lst_mine = []
lst_android = []
count = 0
text = pygame.font.SysFont('宋体', 50)
Play_score = 0
AI_score = 0
def draw_restart():
steps = [(400, 450), (400, 500), (550, 500), (550, 450)]
pygame.draw.polygon(screen, black, steps, 1)
text_x = text.render("AGAIN?", 1, black)
screen.blit(text_x, (410, 460))
def draw_img(player, x, y):
# 玩家
if player == 1:
pygame.draw.circle(screen, black, (x, y), 40, 1)
# 机器
else:
pygame.draw.rect(screen, black, ((x - 20, y - 20), (50, 50)), 1)
def draw_score():
text_1 = pygame.font.SysFont('宋体', 30)
text_player_score = text_1.render('PLAYER SCORE ' + str(Play_score), 1, black)
text_AI_score = text_1.render('AI SCORE ' + str(AI_score), 1, black)
screen.blit(text_player_score, (410, 10))
screen.blit(text_AI_score, (410, 40))
def draw_back():
screen.fill(white)
steps = [(100, 100), (100, 400), (400, 400), (400, 100)]
pygame.draw.polygon(screen, black, steps, 1)
pygame.draw.lines(screen, black, False, [(100, 200), (400, 200)])
pygame.draw.lines(screen, black, False, [(100, 300), (400, 300)])
pygame.draw.lines(screen, black, False, [(200, 100), (200, 400)])
pygame.draw.lines(screen, black, False, [(300, 100), (300, 400)])
def check_win(tab):
return ((points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab) or
(points == tab and points == tab and points == tab)
)
def winner():
# AI
if check_win(100):
return 100
elif check_win(1):
return -100
def is_full():
fl = 0
for i in range(3):
for j in range(3):
if points != 0:
fl += 1
return fl
def AI_move():
# 一步能赢
for i in range(3):
for j in range(3):
if points == 0:
points = 100
if check_win(100):
return (i, j)
else:
points = 0
# 堵上
for i in range(3):
for j in range(3):
if points == 0:
points = 1
if check_win(1):
return (i, j)
else:
points = 0
# 占中间
if points == 0:
return (1, 1)
# 占四角
temp = []
for i in (0, 2):
for j in (0, 2):
if points == 0:
temp.append((i, j))
if len(temp) != 0:
return ra.choice(temp)
# 占四边
for i in ((0, 1), (1, 0), (1, 2), (2, 1)):
if points]] == 0:
temp.append((i, i))
if len(temp) != 0:
return ra.choice(temp)
def draw_all():
draw_back()
draw_score()
for i in lst:
draw_img(i, i, i)
if flag == 100:
text_conent = text.render("AI win", 1, black)
screen.blit(text_conent, (220, 50))
elif flag == -100:
text_conent = text.render("You win", 1, black)
screen.blit(text_conent, (220, 50))
elif flag == 123:
text_conent = text.render("TIE", 1, black)
screen.blit(text_conent, (220, 50))
if flag == 123 or flag == 100 or flag == -100:
draw_restart()
def play():
global flag, AI_score, Play_score
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if 400 < x < 550 and 450 < y < 500:
lst.clear()
for i in range(3):
for j in range(3):
points = 0
flag = 1
if 100 <= x <= 400 and 100 <= y <= 400:
x = (x - 100) // 100
y = (y - 100) // 100
l_x = x * 100 + 150
l_y = y * 100 + 150
# player
if flag == 1:
if is_full() != 9:
if points == 0:
points = 1
lst.append((1, l_x, l_y))
if winner() == -100:
flag = -100
Play_score += 1
print('player win')
else:
flag = -1
else:
flag = 123
if flag == -1:
if is_full() != 9:
# 人机动
xx, yy = AI_move()
l_x = xx * 100 + 150
l_y = yy * 100 + 150
points = 100
lst.append((2, l_x, l_y))
if winner() == 100:
flag = 100
AI_score += 1
print('AI win')
else:
flag = 1
else:
flag = 123
draw_all()
pygame.display.flip()
if __name__ == '__main__':
play()
我赢了
一步一念一顾 发表于 2020-12-31 17:06
请问有象棋的人机对战AI算法吗
象棋。。应该有的,不过实现起来复杂
象棋百科全书,有兴趣可以学习下
http://www.xqbase.com/computer/stepbystep1.htm 学习试试 lthero,干得不错 请问有象棋的人机对战AI算法吗
谢谢分享...收藏了
这个没玩过呢尝鲜一下{:301_1001:} 那这样的话。。那不就没赢的希望了吗{:1_925:} 没玩过,不过可以 尝鲜一下 谢谢分享。
先收藏,慢慢学习。
页:
[1]
2