用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

井字棋1.0

2020-12-05 作者: 一条咸鱼举报

[python]代码库

import random
print(" 1 | 2 | 3 ")
print("-----------")
print(" 4 | 5 | 6 ")
print("-----------")
print(" 7 | 8 | 9 ")


coin = random.randint(0,1)
chessList = list(range(10))
count = 0
while True:
    count += 1
    if coin == 0:
        while True:
            user = input("玩家出子:")
            idx = random.randint(0,len(chessList) - 1)
            if int(user) == chessList[int(user)]:
                chessList[int(user)] = "o"
                coin = 1
                break
            else:
                print("此处有子,请重新出子!")
    elif coin == 1:
        print("电脑出子:")
        while True:
            idx = random.randint(0,len(chessList) - 1)
            
            if idx == chessList[idx]:
                chessList[idx] = "x"
                coin = 0
                break

    print(" " + str(chessList[1]) + " | " + str(chessList[2]) + " | " + str(chessList[3]) + " ")
    print("-----------")
    print(" " + str(chessList[4]) + " | " + str(chessList[5]) + " | " + str(chessList[6]) + " ")
    print("-----------")
    print(" " + str(chessList[7]) + " | " + str(chessList[8]) + " | " + str(chessList[9]) + " ")

    win1 = chessList[1] == chessList[2] == chessList[3]
    win2 = chessList[4] == chessList[5] == chessList[6]
    win3 = chessList[7] == chessList[8] == chessList[9]
    win4 = chessList[1] == chessList[4] == chessList[7]
    win5 = chessList[2] == chessList[5] == chessList[8]
    win6 = chessList[3] == chessList[6] == chessList[9]
    win7 = chessList[1] == chessList[5] == chessList[9]

    if win1 or win2 or win3 or win4 or win5 or win6 or win7 or win8:
        if coin == 1:
            print("玩家获胜")
        else:
            print("电脑获胜")
        break
    elif count == 9:
        print("和棋")
        break


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...