Python-井字棋游戏

x33g5p2x  于2021-11-09 转载在 Python  
字(2.6k)|赞(0)|评价(0)|浏览(469)

1.先进行数据建模

# 游戏棋盘
    theBoard = {'top-L': '', 'top-M': '', 'top-R': '',
                'mid-L': '', 'mid-M': '', 'mid-R': '',
                'low-L': '', 'low-M': '', 'low-R': ''
                }
    # 棋盘坐标
    print("----------棋盘坐标---------------")
    print('top-L|top-M|top-R')
    print('mid-L|mid-M|mid-R')
    print('low-L|low-M|low-R')

    # 打印棋盘
    print("----------实际棋盘---------------")
    print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
    print('-+-+-')
    print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
    print('-+-+-')
    print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])

效果如下:

有了棋盘建模了,那么我们就需要写逻辑了,让棋盘动起来

2.使用逻辑让数据动起来

完整代码如下:

def printBoard(theBoard):
    pd = 1  # 我方和敌方
    while True:
        # 效验棋盘是否满了
        checkerboard_count = 9  # 井字游戏棋盘棋子的个数是9
        for v in theBoard.values():
            v = v.strip()  # 去除空格
            if v != '' and len(v) > 0:
                checkerboard_count -= 1
        if checkerboard_count == 0:
            print("棋盘已满游戏结束---平局")
            return None

        print("请" + str(pd) + "号选手输入下棋位置:")
        str_v = input()
        # 判断是否有这个棋子的坐标
        if str_v not in theBoard.keys():
            print("----对不起没有此棋子坐标请从新输入:")
            continue

        # 判断棋子是否已经存在
        if theBoard[str_v].strip()!='' or len(theBoard[str_v].strip())>0:
            print("已下过次棋-请从新选择下棋位置")
            continue

        if pd == 1:
            theBoard[str_v] = 'O'
        else:
            theBoard[str_v] = 'X'
        print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
        print('-+-+-')
        print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
        print('-+-+-')
        print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])
        win = pd_win(theBoard)
        if win == 'X':
            print("2号选手胜利-游戏结束")
            return None
        elif win == 'O':
            print("1号选手胜利-游戏结束")
            return None
        # 转换为2|1号选手
        if pd == 1:
            pd = 2
        elif pd == 2:
            pd = 1

def pd_win(theBoard):

    # 效验是否胜利
    win_chess = [[0, 4, 8], [2, 4, 6], [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8]]
    for i in win_chess:  # 每次执行一次可能性
        pd_board_3 = 0  # 记录子循环次数
        record1 = 0  # X成绩记录
        record2 = 0  # O成绩记录
        for k, v in theBoard.items():
            if pd_board_3 in i:
                if v == 'X':
                    record1 += 1
                if v == 'O':
                    record2 += 1
            pd_board_3 += 1
        if record1 == 3:
            return 'X'
        elif record2 == 3:
            return 'O'

if __name__ == '__main__':
    # 游戏棋盘
    theBoard = {'top-L': '', 'top-M': '', 'top-R': '',
                'mid-L': '', 'mid-M': '', 'mid-R': '',
                'low-L': '', 'low-M': '', 'low-R': ''
                }
    # 棋盘坐标
    print("----------棋盘坐标---------------")
    print('top-L|top-M|top-R')
    print('mid-L|mid-M|mid-R')
    print('low-L|low-M|low-R')

    # 打印棋盘
    print("----------实际棋盘---------------")
    print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
    print('-+-+-')
    print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
    print('-+-+-')
    print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])

    # 提示
    print("井字棋规则: 选手分为1号选手和二号选手, 一号选手先下棋(自行分配)----游戏即将开始")
    # 用户输入
    printBoard(theBoard)

效果演示

至于其他可能性我都写了,这里就不一一展示了
比如: 棋盘满了----(ok),重复下棋----(ok)
纯手写----给点鼓励(点赞 -收藏-关注)有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章