猫史档案馆


【Python作品分享】下棋游戏(BUG修复*1)【作品秀】

用户:KIHIOKIHIO查看:3 回复:2 评论:3 创建时间:2021-07-31T11:49:49


【作品展示】

center_image

 

【作品介绍】

修复了判断输赢的BUG

 

【作品源代码】

def jd(x):
    try:
        x = float(x)
    except ValueError:
        return 0
    if x-(float(int(x))) > 0:
        return 0
    x = int(x)
    if x < 1 or x > 10:
        return 0
    return x


def sy(a, b, c, a2, b2, c2, s):
    x = 0
    while x <= a2:
        y = 0
        while y <= b2:
            n = 0
            lxh = 0
            lxb = 0
            while n <= c2:
                if s[x+y+n] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[x+y+n] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += c
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += b
        x += a
    return 1


def xsy(xx, xs, yx, ys, js, js2, s):
    x = xx
    while x <= xs:
        y = yx
        while y <= yx:
            lxb = 0
            lxh = 0
            n = 0
            while n <= 4:
                if s[(y+n*js2)*10+x+n*js] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[(y+n*js2)*10+x+n*js] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += 1
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += 1
        x += 1
    return 1


print("棋盘为10*10。")
s = []
a = 0
while a < 100:
    s.append('无')
    a += 1
print("你告诉我黑棋还是白棋先下?(输入A代表黑,输入B代表白):")
while True:
    hb = input()
    if hb != 'A' and hb != 'B':
        print("输入错误,请重试:")
    else:
        break
b = 0
while True:
    f = 0
    fh = 0
    hqs = 0
    bqs = 0
    if sy(10, 1, 1, 90, 5, 4, s) == 2:
        hqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 2:
        hqs = 1
    if sy(10, 1, 1, 90, 5, 4, s) == 0:
        bqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 0:
        bqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 0:
        bqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 0:
        bqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 0:
        bqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 0:
        bqs = 1
    while f < 100:
        k = 0
        bs = ' '
        while k < 10:
            if k == 0:
                bs = s[f+k]
            else:
                bs = bs+' '+s[f+k]
            k += 1
        print(bs)
        f += 10
    if hqs == 1:
        print("黑棋赢!")
        break
    if bqs == 1:
        print("白棋赢!")
        break
    if hb == 'A':
        if b % 2 == 0:
            jhb = 'A'
        else:
            jhb = 'B'
    else:
        if b % 2 == 0:
            jhb = 'B'
        else:
            jhb = 'A'
    if jhb == 'A':
        print("黑棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("黑棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '黑'
    else:
        print("白棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("白棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '白'
    if fh == 0:
        b += 1

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
KIHIOKIHIO

注意:此棋为五子棋。

点赞0


评论


fkymfkym

神仙变量瞎打谁看得懂

点赞0


评论