吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2548|回复: 6
收起左侧

[Python 转载] 井字棋游戏源码

[复制链接]
云无器 发表于 2020-11-22 22:01
看你们全是发布关于爬虫的代码,我这个都不好意思发了。但是又想分享给别人索性还是发了。不求点赞,只求大佬们如果有好的意见可以给出。
代码有我学的教程上的,有我查资料选用的,也有我自己想到的,如果涉及侵权,请联系我删帖。
最后上代码:
#画棋盘,定义棋子
tbp={'a1':' ','a2':' ','a3':' ','b1':' ','b2':' ','b3':' ','c1':' ','c2':' ','c3':' '}
def cb(board):
    print(board['a1']+'|'+board['a2']+'|'+board['a3'])
    print('------')
    print(board['b1']+'|'+board['b2']+'|'+board['b3'])
    print('------')
    print(board['c1']+'|'+board['c2']+'|'+board['c3'])
#判断输赢
def aqw(tbp):
    #dicts=dict(tbp)
    if(tbp['a1'] == tbp['a2'] and tbp['a2'] == tbp['a3'] and tbp['a3']!=' '):
        if(tbp['a1']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['a1'] == tbp['b2'] and tbp['b2'] == tbp['c3'] and tbp['c3']!=' '):
        if(tbp['a1']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['a1'] == tbp['b1'] and tbp['b1'] == tbp['c1'] and tbp['c1']!=' '):
        if(tbp['a1']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['a2'] == tbp['b2'] and tbp['b2'] == tbp['c2'] and tbp['c2']!=' '):
        if(tbp['a2']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['a3'] == tbp['b3'] and tbp['b3'] == tbp['c3'] and tbp['c3']!=' '):
        if(tbp['a3']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['a3'] == tbp['b2'] and tbp['b3'] == tbp['c1'] and tbp['c1']!=' '):
        if(tbp['a3']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['b1'] == tbp['b2'] and tbp['b2'] == tbp['b3'] and tbp['b3']!=' '):
        if(tbp['b1']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
        else:
            print('游戏结束,玩家o获胜')
            return 2
    elif (tbp['c1'] == tbp['c2'] and tbp['c2'] == tbp['c3'] and tbp['c3']!=' '):
         if(tbp['c1']=='x'):
            print('游戏结束,玩家x获胜')
            return 1
         else:
            print('游戏结束,玩家o获胜')
            return 2
    else:
        return 0
#下棋操作
turn='x'
num=-1
for i in range(9):
    print('玩家:'+turn+'请下棋:',end='')
    move=input()
    if(move not in tbp):#误落子落错子处理
         print('没有该棋子请玩家:'+turn+'重新落子:',end='')
         move=input()
    if(tbp[move]!=' '):
         print('此处已落子请玩家:'+turn+'重新落子:',end='')
         move=input()
    num+=1
    tbp[move]=turn
    cb(tbp)
    awd=aqw(tbp)
#判断是否为平局
    if(awd==0):
        if(num==8):
            print('游戏结束,该局为平局')
    if(awd>0):
        break
#换人操作
    if(turn=='x'):
        turn='o'
    else:
        turn='x'

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
回忆扣心弦 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cszmwll666 发表于 2020-11-22 22:59
好的  谢谢楼主
Newzzz 发表于 2020-11-22 23:07
義夫当官 发表于 2020-11-22 23:09
回忆扣心弦 发表于 2020-11-23 00:46
哈哈哈,经典游戏,顶一个
rxxcy 发表于 2020-11-23 01:32
你这if快赶上百度自动驾驶了
168qn 发表于 2020-11-23 08:27
支持原创,感谢分享!!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 21:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表