python实现井字棋小游戏,简单易学

发布于:2022-11-09 ⋅ 阅读:(1600) ⋅ 点赞:(1)

---利用启发式搜索策略模拟机器人进行井字棋对弈---

文章目录


前言

实现井字棋人机对弈小游戏编程:

例如:python作为一种简单过程化的高级语言,具备很好的性能,用python做一个小游戏既能提升自己对python的掌握能力,又能为大家的学习带来更多乐趣。


以下是本篇文章正文内容

一、怎样实现井字棋的人机对弈,原理,玩法?

人机对弈,简单来说就是电脑通过分析棋局,得到胜算最大的落棋点,然后与人对弈,此程序采用的是类似迪杰斯特拉算法求解最短路径来求得最优解,也类似于人工智能方向的启发式搜索策略。在面对任何一种棋盘形势时,通过寻找棋子的可落脚点的各种可能,并通过每种可能状态下的可赢的可能性来寻找棋子最好的落脚点。

原理:井字棋一共有八种赢的方式,分别是横三种竖三种和对角线两种,定义一个列表可以用来储存棋盘上的这八种路线,用line来表示,那么每发现line中的一条边不可能行得通,line中就会少一条边,通过分析棋子在不同落脚点时line中能赢的可能情况总和即可得出最优解,在对弈中除了要考虑最优解以外还需分析最优解中的最优解,以及两种情况:一步即赢时优先一步即赢,在不能一步即赢的情况下,要优先考虑拦截对手,其次再考虑其他情况中的最优解。

玩法:利用电脑键盘右方小键盘0-9输入即可,与棋盘上一一对应

此程序仅需要用到内置库:time,copy.

二、使用步骤

1.引入库,创建一个类,并构造初始函数;

代码如下:

import copy
import time
class Tic_toc_tae():
    def __init__(self):
        self.chessboard=[[],[],[]]#存放棋盘
        self.line=[[],[],[],[],[],[],[],[]]#存放各条边,包括斜着的边
        self.player_plot='x'#玩家用x号
        self.rebot_plot='o'#电脑用o号
        self.n=0
        self.STOP=False#决定整个游戏是否继续

2.向类中增加初始化自定义函数

代码如下:

def initialation(self):
    for i in range(len(self.chessboard)):#初始化棋盘
        self.chessboard[i]=[' ',' ',' ']
    for j in range(3):#初始化记录各条能赢的边
        self.line[j]=self.chessboard[j]
        self.line[j+3]=[self.chessboard[j][0],self.chessboard[j][1],self.chessboard[j][2]]
    self.line[6]=[self.chessboard[0][0],self.chessboard[1][1],self.chessboard[2][2]]
    self.line[7]=[self.chessboard[2][0],self.chessboard[1][1],self.chessboard[2][2]]
    return self.chessboard
def get_now_line(self,chessboard):#方便后续调用
    line=[[],[],[],[],[],[],[],[]]
    for j in range(3):
        line[j]=copy.deepcopy(chessboard[j])
        line[j+3]=copy.deepcopy([chessboard[0][j],chessboard[1][j],chessboard[2][j]])
    line[6]=copy.deepcopy([chessboard[0][0],chessboard[1][1],chessboard[2][2]])
    line[7]=copy.deepcopy([chessboard[2][0],chessboard[1][1],chessboard[0][2]])
    return line

3.向类中增加玩家输入功能和显示功能:

代码如下(注意到copy内置函数的使用,由于列表直接赋值会导致地址错误,所以不得不使用copy函数让一个列表可以多次使用):

def change_chessboard(self,keys1):#这里是主要的下棋部分,后面都要引用到
    plot=self.player_plot
    chessboard=copy.deepcopy(self.chessboard)#得到目前的棋盘
    STOP=0
    n=0
    body=True
    key=False
    while(True and STOP==0):
        if keys1==0:
            try:
                n=int(input("从小键盘输入对应位置\n"))
            except(ValueError):
                print("请重新输入")
            else:
                STOP=1
        if keys1!=0:
            n=keys1
            body=False
        if n>0 and n<=3:
            if chessboard[2][n-1]==plot or chessboard[2][n-1]==self.rebot_plot:
                print("这里不能再下")
                continue
            if body==False:
                chessboard[2][n-1]=self.rebot_plot
            else:
                chessboard[2][n-1]=plot
            key=True
            STOP=1
        elif n>=4 and n<=6:
            if chessboard[1][n-4]==plot or chessboard[1][n-4]==self.rebot_plot:
                print("这里不能再下")
                continue
            if body==False:
                chessboard[1][n-4]=self.rebot_plot
            else:
                chessboard[1][n-4]=plot
            key=True
            STOP=1
        elif n>=7 and n<=9:
            if chessboard[0][n-7]==plot or chessboard[0][n-7]==self.rebot_plot:
                print("这里不能再下")
                continue
            if body==False:
                chessboard[0][n-7]=self.rebot_plot
            else:
                chessboard[0][n-7]=plot
            key=True
            STOP=1
    #if key==True:
    #    print("操作成功")
    return chessboard
def show_chessboard(self):#输出功能,把棋盘可视化出来
    for i in self.chessboard:
        for j in i:
            print("|"+j,end='|')
        print()

4.向类中增加机器人功能:

机器人这块主要分三部分:

1,主函数

2,搜索寻求最优可能

3,分析得出最优解

def rebot(self):#主函数
    if self.STOP==None:#STOP约束,方便停止游戏
        try:
            x,y=self.rebot_analyze()
            self.chessboard[x][y] = self.rebot_plot
        except(TypeError,UnboundLocalError):#输入非0-9时报错情况处理
            print("-------")
def rebot_get_line(self,Chessboard):#分析一个落棋点的可行度
    self.line=copy.deepcopy(self.get_now_line(Chessboard))
    line=0
    for i in self.line:
        n=0
        prevent=0
        for j in i:
           if j==self.player_plot:
               #self.line.remove(i)
               prevent+=1
           if j==self.rebot_plot:
               n+=1
        if n==3:#在一步能赢时,优先一步取得胜利
            line+=300
        if prevent==2 and n==1:#在一步不能赢时,优先拦截对方
            line+=200
            n=0
        if n==1:
            line+=1
        if n==2:
            line+=50
    return line
def rebot_analyze(self):#分析得出最优解,可以说是主程序
    all_situation=[]
    Chessboard=copy.deepcopy(self.chessboard)
    for i in range(len(Chessboard)):
        for j in range(len(Chessboard[i])):
            if Chessboard[i][j]!=self.player_plot and Chessboard[i][j]!=self.rebot_plot:
                Chessboard=self.change_chessboard(3*(2-i)+j+1)
                n=self.rebot_get_line(Chessboard)
                all_situation.append([n,i,j])
    if all_situation==[]:
        print("游戏结束,平局")
        self.STOP=True
        return self.STOP
    all_situation2=[]
    for i in all_situation:
        all_situation2.append(i[0])
    max_n=0
    for j in range(len(all_situation2)):
        if all_situation2[j]==max(all_situation2):
            max_n=j
    x=all_situation[max_n][1]
    y=all_situation[max_n][2]
    return x,y

5.向类中增加分析棋盘的功能

代码如下(及时分析棋盘,得出胜负):

def referee(self):
    if self.STOP==None:
        line=copy.deepcopy(self.get_now_line(self.chessboard))
        rebot=player=0
        n=0
        for i in line:
            rebot=player=0
            for k in i:
                if k==self.rebot_plot:
                    rebot+=1
                if k==self.player_plot:
                    player+=1
            if rebot==3:
                print("------机器人获胜------")
                return True
            if player==3:
                print("------玩家获胜------")
                return True

6,改善整个类,并封装,从外部调用

代码如下:

def run(self):#使用run函数作为主函数,调用整个程序的自定义函数
    print("---|井字棋|---")
    print("->人机对弈-1.0版本-503<-")
    print("[x 为玩家,o 为电脑]")
    print()
    while(self.STOP==False):
        self.initialation()
        self.show_chessboard()
        while(self.STOP!=True):
            self.chessboard=self.change_chessboard(0)
            self.STOP=self.referee()
            print("机器思考中")
            time.sleep(1)
            self.rebot()
            if self.STOP!=True:
                self.STOP=self.referee()
            self.show_chessboard()
        print("->是否再来一局?")
        anything=input("->按0退出,按其他键再来一局\n")
        if anything=='0':
            self.STOP=True
        else:
            self.STOP=False
tic_toc_tae=Tic_toc_tae()
tic_toc_tae.run()

游戏界面:

 

 

总结

希望大家玩的开心,由于作者水平有限,文章可能会出现谬误,欢迎大佬指正。
------------------------------------------------------------

本文含有隐藏内容,请 开通VIP 后查看