python做本地双人井字旗游戏(不需要pygame,网络连接)

简单,111行代码。

就是打印区,判断区,debug区,输入区和初始化。

废话不多说,代码!

d = open('debug.dat', 'rb').read().decode()
debug = bool(d)
if d != 'True' and d != 'False':
    debug = False
list = [[None, None, None],[None, None, None],[None, None, None]]
def __init__():
    global list
    global debug
    global d
    d = open('debug.dat', 'rb').read().decode()
    debug = bool(d)
    if d != 'True' and d != 'False':
        debug = False
    list = [[None, None, None], [None, None, None], [None, None, None]]
def ying(b):
    if b:
        name = 'X'
    elif b is False:
        name = 'O'
    else:
        if debug:
            print('debug::错误!\n')
        return 0
    print(f'{name}赢了!')
    print('下一局...\n\n')
    __init__()
def main():
    while True:
        # 行列输入
        for i in range(2):
            while True:
                ni = None
                if i == 0:
                    ni = 'O'
                elif i == 1:
                    ni = 'X'
                else:
                    if debug:
                        print('debug::错误!')
                a_ = input(ni + " 输入行/列:")
                a = a_.split('/')
                try:
                    n = int(a[0]) - 1, int(a[1]) - 1
                    if list[n[0]][n[1]] is not False and not list[n[0]][n[1]]:
                        if n[0] >= 0 and n[1] >= 0:
                            list[n[0]][n[1]] = bool(i)
                        else:
                            print('超出范围!')
                            continue
                    else:
                        print('已有子')
                        continue
                    break
                except IndexError:
                    print('超出范围!')
                    continue
                except ValueError:
                    print('字符错误!应为digit/digit,而不是' + a_ + '.')
                    continue
        # 判断是否赢
        for i in range(len(list)):
            # 横
            if list[i][0] == list[i][1] == list[i][2] and list[i][0] is not None and list[i][1] is not None and list[i][2] is not None:
                ying(list[i][0])
                if debug:
                    print('debug::横')
                continue
            # 竖
            elif list[0][i] == list[1][i] == list[2][i] and list[0][i] is not None and list[1][i] is not None and list[2][i] is not None:
                ying(list[0][i])
                if debug:
                    print('debug::竖')
                continue
            # 右下斜
            elif list[0][0] == list[1][1] == list[2][2] and list[0][0] is not None and list[1][1] is not None and list[2][2] is not None:
                ying(list[0][0])
                if debug:
                    print('debug::右下')
                continue
            # 左上斜
            elif list[0][2] == list[1][1] == list[2][0] and list[0][2] is not None and list[1][1] is not None and list[0][2] is not None:
                ying(list[0][2])
                if debug:
                    print('debug::左上')
                continue
        if debug:
            print('debug::' + list)
        # 打印井字旗
        print('-------')
        for i in list:
            print('|', end = '')
            for i2 in i:
                if i2:
                    print('X', end = '|')
                elif i2 is False:
                    print('O', end = '|')
                else:
                    print(' ', end = '|')
            print('\n-------')
        # 判断是否平局
        p = True
        for i in list:
            for i2 in i:
                if not i2 or i2 is not False:
                    p = False
                    break
        if p:
            print('平局!')
            __init__()
if __name__ == '__main__':
    main()

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值