python 井字棋

又是一年开学日,

写一点点代码缅怀一下童年。

当然,

代码水平也是幼稚的,

很配。

说明:1、输入O或X选择棋子,并且O设定为先手

           2、有0、1、2三个难度:

                      0:程序随机走子

                      1:在难度0的基础上,程序优先下获胜点,没有获胜点则优先抢占玩家的获胜点。

                      2:在难度1的的基础上,程序固定先手,并内置了几步固定的走法

                        (其实是我小学三年级时的走法)

          3、走子坐标:

                       00 | 01 | 02

                       10 | 11 | 12

                       20 | 21 | 22

          4、一局结束后可选择Y重玩该局(初始设定不变)或N结束程序或R重新开始

import re
import random

qp0 = ([' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '])


#棋盘显示
def qpf(qp):
    print(f' {qp[0][0]} | {qp[0][1]} | {qp[0][2]} ')
    print('--- --- ---')
    print(f' {qp[1][0]} | {qp[1][1]} | {qp[1][2]} ')
    print('--- --- ---')
    print(f' {qp[2][0]} | {qp[2][1]} | {qp[2][2]} ')
    print('\n')


#随机走
def suiji0(qp):
    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[i][j] == ' ':
                m = m + 1
            j = j + 1
        i = i + 1
    n = random.randint(1, m)
    i = 0
    m = 0
    i1 = 0
    j1 = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[i][j] == ' ':
                m = m + 1
                if m == n:
                    i1 = i
                    j1 = j
            j = j + 1
        i = i + 1
    return i1, j1


#判定仅有两个X连成一线的第三个点
def xpd(qp):
    i = 0
    i1, j1 = 8, 8
    while i < 3:
        j = i
        if qp[i][1] == qp[i][2] == 'X' and qp[i][0] == ' ':
            i1 = i
            j1 = 0
        if qp[i][0] == qp[i][2] == 'X' and qp[i][1] == ' ':
            i1 = i
            j1 = 1
        if qp[i][0] == qp[i][1] == 'X' and qp[i][2] == ' ':
            i1 = i
            j1 = 2
        if qp[1][j] == qp[2][j] == 'X' and qp[0][j] == ' ':
            i1 = 0
            j1 = j
        if qp[0][j] == qp[2][j] == 'X' and qp[1][j] == ' ':
            i1 = 1
            j1 = j
        if qp[0][j] == qp[1][j] == 'X' and qp[2][j] == ' ':
            i1 = 2
            j1 = j
        i = i + 1
    if qp[1][1] == qp[2][2] == 'X' and qp[0][0] == ' ':
        i1 = 0
        j1 = 0
    if qp[0][0] == qp[2][2] == 'X' and qp[1][1] == ' ':
        i1 = 1
        j1 = 1
    if qp[0][0] == qp[1][1] == 'X' and qp[2][2] == ' ':
        i1 = 2
        j1 = 2
    if qp[1][1] == qp[2][0] == 'X' and qp[0][2] == ' ':
        i1 = 0
        j1 = 2
    if qp[0][2] == qp[2][0] == 'X' and qp[1][1] == ' ':
        i1 = 1
        j1 = 1
    if qp[0][2] == qp[1][1] == 'X' and qp[2][0] == ' ':
        i1 = 2
        j1 = 0
    return i1, j1


#判定仅有两个O连成一线的第三个点
def opd(qp):
    i = 0
    i1, j1 = 8, 8
    while i < 3:
        j = i
        if qp[i][1] == qp[i][2] == 'O' and qp[i][0] == ' ':
            i1 = i
            j1 = 0
        if qp[i][0] == qp[i][2] == 'O' and qp[i][1] == ' ':
            i1 = i
            j1 = 1
        if qp[i][0] == qp[i][1] == 'O' and qp[i][2] == ' ':
            i1 = i
            j1 = 2
        if qp[1][j] == qp[2][j] == 'O' and qp[0][j] == ' ':
            i1 = 0
            j1 = j
        if qp[0][j] == qp[2][j] == 'O' and qp[1][j] == ' ':
            i1 = 1
            j1 = j
        if qp[0][j] == qp[1][j] == 'O' and qp[2][j] == ' ':
            i1 = 2
            j1 = j
        i = i + 1
    if qp[1][1] == qp[2][2] == 'O' and qp[0][0] == ' ':
        i1 = 0
        j1 = 0
    if qp[0][0] == qp[2][2] == 'O' and qp[1][1] == ' ':
        i1 = 1
        j1 = 1
    if qp[0][0] == qp[1][1] == 'O' and qp[2][2] == ' ':
        i1 = 2
        j1 = 2
    if qp[1][1] == qp[2][0] == 'O' and qp[0][2] == ' ':
        i1 = 0
        j1 = 2
    if qp[0][2] == qp[2][0] == 'O' and qp[1][1] == ' ':
        i1 = 1
        j1 = 1
    if qp[0][2] == qp[1][1] == 'O' and qp[2][0] == ' ':
        i1 = 2
        j1 = 0
    return i1, j1


#难度1走法
def suiji1(qp, p):
    i1, j1 = suiji0(qp)
    if p == 1:
        i1, j1 = opd(qp)
        if i1 == 8:
            i1, j1 = xpd(qp)
            if i1 == 8:
                i1, j1 = suiji0(qp)
    if p == 2:
        i1, j1 = xpd(qp)
        if i1 == 8:
            i1, j1 = opd(qp)
            if i1 == 8:
                i1, j1 = suiji0(qp)
    return i1, j1


bz0 = [0]
bz1 = [0]


#难度2走法
def my3(qp, p, b):
    i1, j1 = suiji1(qp, p)
    if b == 0:
        i1 = 0
        j1 = 0
    if b == 2:
        if qp[0][2] == 'X' or qp[2][0] == 'X':
            i1 = 2
            j1 = 2
        elif qp[1][2] == 'X' or qp[2][1] == 'X':
            i1 = 1
            j1 = 1
        elif qp[0][1] == 'X' or qp[1][0] == 'X':
            i1 = 1
            j1 = 1
            bz0[0] = 1
        elif qp[2][2] == 'X':
            i1 = 2
            j1 = 0
            bz1[0] = 1
    if b == 4:
        if bz0[0] == 1 and qp[2][2] == 'X':
            if qp[0][1] == 'X':
                i1 = 1
                j1 = 0
            if qp[1][0] == 'X':
                i1 = 0
                j1 = 1
        elif bz1[0] == 1 and qp[1][0] == 'X':
            i1 = 0
            j1 = 2

    return i1, j1

#不同难度采取的走法处理
def ai(qp, p, level, b):
    i1, j1 = suiji0(qp)
    if level == 1:
        i1, j1 = suiji1(qp, p)
    if level == 2:
        i1, j1 = my3(qp, p, b)
    return i1, j1


#胜利判断
def win(qp, q):
    t = 1

    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[i][j] == 'X':
                m = m + 1
                if m == 3:
                    if q == 1:
                        print('You Win')
                    else:
                        print('You Lose')
                    t = 0

            j = j + 1
        m = 0
        i = i + 1

    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[j][i] == 'X':
                m = m + 1
                if m == 3:
                    if q == 1:
                        print('You Win')
                    else:
                        print('You Lose')
                    t = 0

            j = j + 1
        m = 0
        i = i + 1
    if qp[0][0] == qp[1][1] == qp[2][2] == 'X':
        if q == 1:
            print('You Win')
        else:
            print('You Lose')
        t = 0
    if qp[0][2] == qp[1][1] == qp[2][0] == 'X':
        if q == 1:
            print('You Win')
        else:
            print('You Lose')
        t = 0

    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[i][j] == 'O':
                m = m + 1
                if m == 3:
                    if q == 2:
                        print('You Win')
                    else:
                        print('You Lose')
                    t = 0

            j = j + 1
        m = 0
        i = i + 1

    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[j][i] == 'O':
                m = m + 1
                if m == 3:
                    if q == 2:
                        print('You Win')
                    else:
                        print('You Lose')
                    t = 0
            j = j + 1
        m = 0
        i = i + 1
    if qp[0][0] == qp[1][1] == qp[2][2] == 'O':
        if q == 2:
            print('You Win')
        else:
            print('You Lose')
        t = 0
    if qp[0][2] == qp[1][1] == qp[2][0] == 'O':
        if q == 2:
            print('You Win')
        else:
            print('You Lose')
        t = 0

    i = 0
    m = 0
    while i < 3:
        j = 0
        while j < 3:
            if qp[i][j] == ' ':
                m = m + 1
            j = j + 1
        i = i + 1
    if m == 0 and t != 0:
        t = 0
        print('no winner')
    return t


#玩家落子处理
def shuru():
    da = input("Enter your input: ")
    da1 = re.findall(r"\d", da)
    mx, my = da1
    mx = int(mx)
    my = int(my)
    return mx, my


#开始选项
def sta():
    i = 1
    j = 1
    while i:
        da = input("X or O: ")
        if da == 'X':
            j = 1
            i = 0
        elif da == 'O':
            j = 2
            i = 0
        else:
            print('error')

    level = 0
    i = 1

    while i:
        da = input("choose your level(0 or 1 or 2) ")
        if da == '0':
            level = 0
            i = 0
        elif da == '1':
            level = 1
            i = 0
        elif da == '2':
            level = 2
            i = 0
        else:
            print('error')
    return j, level


#主程序
game = 1
gr = 0
qz, le = sta()
while game:
    qp0 = ([' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '])
    bz0 = [0]
    bz1 = [0]
    f = 1
    if gr == 1:
        qz, le = sta()
    bs = 0
    if le == 2 and qz == 2:
        qz = 1
        print('\nI am level2.\nI need O.\n')
    while f:
        if qz == 1:
            ix, iy = ai(qp0, qz, le, bs)
            qp0[ix][iy] = 'O'
            bs = bs + 1

            qpf(qp0)
            f = win(qp0, qz)

            if f == 0:
                break

            f1 = 1
            while f1:
                mx1, my1 = shuru()
                if 0 <= mx1 < 3 and 0 <= my1 < 3:
                    if qp0[mx1][my1] == ' ':
                        qp0[mx1][my1] = 'X'
                        f1 = 0
                    else:
                        print('error')
                else:
                    print('error')

            qpf(qp0)
            f = win(qp0, qz)
            bs = bs + 1
        else:

            f1 = 1
            while f1:
                mx1, my1 = shuru()
                if 0 <= mx1 < 3 and 0 <= my1 < 3:
                    if qp0[mx1][my1] == ' ':
                        qp0[mx1][my1] = 'O'
                        f1 = 0
                    else:
                        print('error')
                else:
                    print('error')

            qpf(qp0)
            f = win(qp0, qz)

            if f == 0:
                break

            ix, iy = ai(qp0, qz, le, bs)
            qp0[ix][iy] = 'X'

            qpf(qp0)
            f = win(qp0, qz)
    f1 = 1
    while f1:
        sr = input("replay? \nY:replay\nN:stop\nR:restart\n")
        if sr == 'Y':
            f1 = 0
            gr = 0
        elif sr == 'N':
            f1 = 0
            game = 0
        elif sr == 'R':
            f1 = 0
            gr = 1
        else:
            print('error')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值