欢迎来到资源库(www.zyku.net)

Python教程

当前位置:首页 > 网络编程 > Python教程 > 井字棋

python实现简单的井字棋

时间:2022-03-31|栏目:Python教程|点击:|我要投稿

本文实例为大家分享了python实现简单的井字棋的具体代码,供大家参考,具体内容如下

使用python实现井字棋游戏,没有具体算法,只是用随机下棋简单实现:

import random
board = [['+','+','+'],['+','+','+'],['+','+','+']]
def ma(board):
    if isempty(board):
        a = random.randint(0, 2)
        b = random.randint(0, 2)
        if board[a][b] != 'X' and board[a][b] != 'O':
            print("机器走:")
            board[a][b] = 'O'
            oput(board)
        else:
            ma(board)
    else:
        print("平局")
def oput(board):
    print("  0 1 2")
    for i in range(3):
        print(i, end=' ')
        for j in range(3):
            print(board[i][j], end=" ")
        print("")
def winput(i,j):
    if board[i][j] == 'X':
        print("human win")
    else:
        print("machine win")
    return 1

def test(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] != '+':
                if j == 0:
                    if board[i][j] == board[i][j + 1] == board[i][j + 2]:
                        return winput(i,j)
                if i == 0:
                    if board[i][j] == board[i + 1][j] == board[i + 2][j]:
                        return winput(i,j)
                if i == 0 and j == 0:
                    if board[i][j] == board[i + 1][j + 1] == board[i + 2][j + 2]:
                        return winput(i,j)
                if i == 2 and j == 0:
                    if board[i][j] == board[i - 1][j + 1] == board[i - 2][j + 2]:
                        return winput(i,j)

def isempty(board):
    for i in range(3):
        for j in range(3):
            if board[i][j] == '+':
                return True
    return False
def main():
    print("初始棋盘:")
    oput(board)
    flag = 0
    t = input("human first? Y/N  human for X, machine for O\n")
    if t == 'Y':
        while isempty(board):
            print("人走:  ")
            a, b = map(int, input("输入落子纵横坐标: a,b \n").split(','))
            if board[a][b] == '+':
               board[a][b] = 'X'
               oput(board)
               flag = test(board)
               if flag == 1:
                  break
            else:
                print("落子位置不对")
                continue
            ma(board)
            flag = test(board)
            if flag == 1:
                break
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
    elif t == 'N':
        while isempty(board):
            ma(board)
            flag = test(board)
            if isempty(board) == 0 and flag == 0:
                print("平局")
                break
            if flag == 1:
                break
            print("人走:  ")
            a, b = map(int, input("输入落子纵横坐标: a,b \n").split(','))
            if board[a][b] == '+':
                board[a][b] = 'X'
                oput(board)
                flag = test(board)
                if flag == 1:
                    break
            else:
                print("落子位置不对")
                continue

if __name__ == "__main__":
    main()

结果:

(资源库 www.zyku.net)

原文链接:https://blog.csdn.net/weixin_43871677/article/details/117234869

上一篇:用 Python 元类的特性实现 ORM 框架

栏    目:Python教程

下一篇:Python基础之元编程知识总结

本文标题:python实现简单的井字棋

本文地址:https://www.zyku.net/python/11148.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务