五一福利 —— Python 代码之井字棋

杨承翰 328 0

五一劳动节到了,既然是节日,就要给点“福利”[aru_14]。这次的福利是两段 Python 代码![aru_3]

所需 Python 版本

Python 3.x

Python 安装方法

Windows

戳这里获取安装包:

戳我下载

Linux

Linux 通常自带 Python。可以使用命令:

Python3

来验证是否安装 Python 3。如果没有,CentOS 可以这样:

yum -y install python3

Ubuntu 可以这样:

sudo apt install python3

所需 Python 模块

  • pygame
  • easygui
  • winsound(自带)
  • time(自带)
  • random(自带)
  • sys(自带)

模块安装方法

这个不用多讲吧?[aru_31] Python 3.x 的通用方法就是 pip。以上模块可以通过以下命令安装:

pip install pygame && pip install easygui

代码

代码来喽![aru_17] 请看下方。

#--------------------------------
# Coding by www.ych-template.com
#--------------------------------
import pygame, random, winsound, easygui, sys, time

a = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
test = a[:][:]
tmp = 0
tmparr = 0
sys.setrecursionlimit(100000)
mark = [0, 0]

def cal():
    r = 0
    for i in range(3):
        for j in range(3):
            if a[i][j] != 0:
                r = r + 1
    return r

# 判断游戏是否结束
def win():
    global tmp
    tmp = 0
    for i in range(3):
        if a[0][i] == a[1][i] and a[1][i] == a[2][i]:
            return a[1][i]
        if a[i][0] == a[i][1] and a[i][1] == a[i][2]:
            return a[i][1]
    if a[0][0] == a[1][1] and a[1][1] == a[2][2]:
        return a[1][1]
    if a[0][2] == a[1][1] and a[1][1] == a[2][0]:
        return a[1][1]
    for i in range(3):
        for j in range(3):
            if a[i][j] == 0:
                tmp = 1
                break
    if tmp == 0:
        return 3
    return 0

def trans(n):
    if n == 1:
        return 0, 0
    elif n == 2:
        return 0, 1
    elif n == 3:
        return 0, 2
    elif n == 4:
        return 1, 0
    elif n == 5:
        return 1, 1
    elif n == 6:
        return 1, 2
    elif n == 7:
        return 2, 0
    elif n == 8:
        return 2, 1
    elif n == 9:
        return 2, 2
    else:
        return None

# AI 的思想部分
def think(com):
    global tmparr, a
    l = [-1, -1]
    tmplist = []
    #if cal() == 2 and a[1][1] == 1 and com == 1 and a[0][2] == 0 and a[2][2] == 0 and a[0][0] == 0 and a[2][0] == 0:
    #    return random.randint(0, 1) * 2, random.randint(0, 1) * 2
    if cal() == 1 and a[1][1] == 1 and com == 2:
        return random.randint(0, 1) * 2, random.randint(0, 1) * 2
    for i in range(3):
        for j in range(3):
            if a[i][j] == 0:
                tmparr = a[i][j]
                a[i][j] = com
                if win() == com:
                    a[i][j] = tmparr
                    return i, j
                a[i][j] = 3 - com
                if win() == 3 - com:
                    a[i][j] = tmparr
                    l = [i, j]
                    continue
                a[i][j] = tmparr
    if l != [-1, -1]:
        return l[0], l[1]
    for x in range(3):
        for y in range(3):
            if a[x][y] != 0:
                continue
            tmparr = a[x][y]
            a[x][y] = com
            for i in range(3):
                for j in range(3):
                    if a[i][j] == 0:
                        tmparr2 = a[i][j]
                        a[i][j] = com
                        if win() == com:
                            for k in tmplist:
                                if k == [x, y]:
                                    a[i][j] = tmparr2
                                    a[x][y] = tmparr
                                    return x, y
                            tmplist.append([x, y])
                        a[i][j] = tmparr2
            a[x][y] = tmparr
    return trans(random.randint(1, 9))

def go(com):
    if win() == 0:
        x, y = think(com)
        if a[x][y] != 0:
            go(com)
            return
        a[x][y] = com
        return

# 制作出动画
def animate(com):
    screen.fill([255, 255, 255])
    pygame.draw.rect(screen, [0, 0, 0], [0, 195, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 395, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [195, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [395, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 590, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 0, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [590, 0, 10, 600], 0)
    for i in range(3):
        for j in range(3):
            if a[i][j] == com:
                screen.blit(pic2, [j * 200 - 10, i * 200 - 120])
            elif a[i][j] == 3 - com:
                screen.blit(pic1, [j * 200 - 10, i * 200 - 120])
    pygame.display.flip()

# 主体部分
pygame.init()
disp = pygame.font.Font("cour.ttf", 100)
screen = pygame.display.set_mode([600, 600])
pygame.display.set_caption("井字棋游戏 - By 杨承翰")
icon = pygame.image.load("favicon.png").convert_alpha()
pygame.display.set_icon(icon)
pic1_font = pygame.font.Font("cour.ttf", 355)
pic1 = pic1_font.render("o", 1, (0, 0, 255))
pic2_font = pygame.font.Font("cour.ttf", 390)
pic2 = pic2_font.render("×", 1, (0, 0, 255))
running = True

for looper in range(5):
    if not running:
        break
    screen.fill([255, 255, 255])
    pygame.draw.rect(screen, [0, 0, 0], [0, 195, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 395, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [195, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [395, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 590, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 0, 600, 10], 0)
    pygame.draw.rect(screen, [0, 0, 0], [0, 0, 10, 600], 0)
    pygame.draw.rect(screen, [0, 0, 0], [590, 0, 10, 600], 0)
    pygame.display.flip()

    a = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    test = a[:][:]
    tmp = 0
    tmparr = 0
    if int(time.time() + 1) % 2:
        easygui.msgbox(msg = "抽签得出,您是先手。", title = "抽签", ok_button = "知道了")
        pygame.time.delay(700)
        usr = 1
        com = 2
    else:
        easygui.msgbox(msg = "抽签得出,您是后手。", title = "抽签", ok_button = "知道了")
        pygame.time.delay(700)
        usr = 2
        com = 1

    if usr == 2:
        time.sleep(1)
        go(com)
        animate(com)
        winsound.Beep(2000, 700)

    while running:
        e = 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.pos[0] < 200 and event.pos[1] < 200 and e and a[0][0] == 0:
                    a[0][0] = usr
                    e = 0
                elif event.pos[0] > 200 and event.pos[0] < 400 and event.pos[1] < 200 and e and a[0][1] == 0:
                    a[0][1] = usr
                    e = 0
                elif event.pos[0] > 400 and event.pos[1] < 200 and e and a[0][2] == 0:
                    a[0][2] = usr
                    e = 0
                elif event.pos[0] < 200 and event.pos[1] > 200 and event.pos[1] < 400 and e and a[1][0] == 0:
                    a[1][0] = usr
                    e = 0
                elif event.pos[0] > 200 and event.pos[0] < 400 and event.pos[1] > 200 and event.pos[1] < 400 and e and a[1][1] == 0:
                    a[1][1] = usr
                    e = 0
                elif event.pos[0] > 400 and event.pos[1] > 200 and event.pos[1] < 400 and e and a[1][2] == 0:
                    a[1][2] = usr
                    e = 0
                elif event.pos[0] < 200 and event.pos[1] > 400 and event.pos[1] < 600 and e and a[2][0] == 0:
                    a[2][0] = usr
                    e = 0
                elif event.pos[0] > 200 and event.pos[0] < 400 and event.pos[1] > 400 and event.pos[1] < 600 and e and a[2][1] == 0:
                    a[2][1] = usr
                    e = 0
                elif event.pos[0] > 400 and event.pos[1] > 400 and event.pos[1] < 600 and e and a[2][2] == 0:
                    a[2][2] = usr
                    e = 0
            break
        if e:
            continue
        animate(com)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        time.sleep(1)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        go(com)
        animate(com)
        w = win()
        if w == 0:
            winsound.Beep(2000, 700)
            continue
        winsound.Beep(1200, 700)
        if looper < 4:
            if w == 3:
                easygui.msgbox(msg = "哎哟,平局了!\n\n目前比分:%d:%d" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break
            elif w == usr:
                mark[1] += 1
                easygui.msgbox(msg = "恭喜你,你赢了!\n\n目前比分:%d:%d" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break
            elif w == com:
                mark[0] += 1
                easygui.msgbox(msg = "对不起,你输了!\n\n目前比分:%d:%d" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break

        else:
            if w == 3:
                if mark[1] < mark[0]:
                    easygui.msgbox(msg = "哎哟,平局了!\n\n比分为:%d:%d ,你输了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                elif mark[1] > mark[0]:
                    easygui.msgbox(msg = "哎哟,平局了!\n\n比分为:%d:%d ,你赢了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                else:
                    easygui.msgbox(msg = "哎哟,平局了!\n\n比分为:%d:%d ,平局了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break
            elif w == usr:
                mark[1] += 1
                if mark[1] < mark[0]:
                    easygui.msgbox(msg = "恭喜你,你赢了!\n\n比分为:%d:%d ,你输了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                elif mark[1] > mark[0]:
                    easygui.msgbox(msg = "恭喜你,你赢了!\n\n比分为:%d:%d ,你赢了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                else:
                    easygui.msgbox(msg = "恭喜你,你赢了!\n\n比分为:%d:%d ,平局了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break
            elif w == com:
                mark[0] += 1
                if mark[1] < mark[0]:
                    easygui.msgbox(msg = "对不起,你输了!\n\n比分为:%d:%d ,你输了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                elif mark[1] > mark[0]:
                    easygui.msgbox(msg = "对不起,你输了!\n\n比分为:%d:%d ,你赢了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                else:
                    easygui.msgbox(msg = "对不起,你输了!\n\n比分为:%d:%d ,平局了!" % (mark[1], mark[0]), title = "结果", ok_button = "好的")
                pygame.time.delay(200)
                break

pygame.quit()

代码没加太多注释,应该 …… 看得懂吧 …… [aru_34]

我们还需要一个字体文件,下载地址:

蓝奏云

还有,我们的图标在这里下载:

请注意,只有 Chrome / 火狐 可以使用下载功能,其它浏览器的用户请换浏览器或点击下方按钮,手动保存!

 戳我下载

运行方法

新建一个 chess.py 的文件(可以先建立一个文本文档,再 修改其后缀名 为 py),然后将以上代码键入并保存,把之前下载的字体 ttf 文件放在同一目录。[aru_42] 然后使用命令运行:

python chess.py
温馨提示

Python 2 和 Python 3 不兼容,请卸载其中一个!
Python 2 和 Python 3 不兼容,请卸载其中一个!
Python 2 和 Python 3 不兼容,请卸载其中一个!
重要的事情说三遍![aru_43]

打包方法

代码敲完之后,我们需要将 py 文件打包。这里举出 Windows 的例子,其它系统请 百度搜索 。[aru_33]

首先,我们需要安装 pyinstaller。安装命令:

pip install pyinstaller

接着,使用两行命令

pyi-makespec chess.py
pyinstaller -i 【ico 图标路径】-w chess.py

来进行打包。然后文件夹中会出现一个 dist 目录,打开里面会有“chess”目录(也就是 py 文件的前缀),其中有着我们要的 chess.exe。这时还打不开,记住,要把 ttf 文件和下载的图片放到此目录![aru_59]

发表评论 取消回复
表情 图片 链接 代码

分享