python入门之井字棋小游戏


Posted in Python onMarch 05, 2020

引言:

刚学python好几天了,从java到python,基础学起来确实比较容易,语法掌握,基本概念上都比较容易入脑。

唯一比较郁闷的是老想着用java的语法去学python代码,这点还需要后面慢慢掌握吧,相信学多种语言的你们也有这种经历吧。

start:开始上代码了,希望有更好的逻辑思维来写,自己也是用最笨拙的思路去写的,如果有可以优化的代码请各位大神指教

#!/user/bin/python
# -*- coding: utf-8 -*-
import os
import sys
#棋盘模块
def model(dictionary,serial=False):
 if serial:
  print('-(初版)井字棋游戏,输入棋号进行对战,')
  print('对应棋号为第一行:a1-a2-a3',end=',')
  print('对应棋号为第二行:b1-b2-b3',end=',')
  print('对应棋号为第三行:c1-c2-c3')
 print(dictionary['a1'] + ' | '+ dictionary['a2'] +' | '+ dictionary['a3'] +' | ')
 print('- +- +- +-')
 print(dictionary['b1'] + ' | ' + dictionary['b2'] + ' | ' + dictionary['b3'] + ' | ')
 print('- +- +- +-')
 print(dictionary['c1'] + ' | ' + dictionary['c2'] + ' | ' + dictionary['c3'] + ' | ')
#主模块
def main():
 dictionary={'a1':' ','a2':' ','a3':' ','b1':' ','b2':' ','b3':' ','c1':' ','c2':' ','c3':' '}
 model(dictionary, True)
 u1 = 'x' #用户1
 u2 = 'o' #用户2
 stepNumber =1 #记录步数
 break_fang = 0 #获胜者记录
 while(stepNumber<=9):
 fv = True # 判断条件2
 while fv:
  num = input('请用户u1开始下棋:')
  compare=1 #判断条件1
  for x in dictionary:
  if x.find(num)!=-1:compare=0
  if compare ==0:
  fv=False
 dictionary[num] = u1
 model(dictionary)
 # 0:继续 1,用户1胜,2,用户2胜
 break_fang = forResult(dictionary)
 if break_fang > 0: break
 fv =True #清楚状态
 stepNumber+=1
 while fv:
  num1=input('请用户u2开始下棋:')
  compare = 1 # 判断条件1
  for x in dictionary:
  if x.find(num1)!=-1:compare=0
  if compare == 0:
  fv=False
 dictionary[num1] = u2
 model(dictionary)
 break_fang = forResult(dictionary)
 if break_fang > 0: break
 stepNumber+=1
 gameover(break_fang)
#退出下棋
def gameover(break_fang):
 c = input('是否重新开始? yes:no:')
 if c.find('yes')!=-1:
 main()
 else:
 print('-游戏结束-')
 return
#判断获胜情况
#dictionary:棋盘信息
def forResult(dictionary):
 dicts= dict(dictionary)
 if dicts['a1'] == dicts['a2'] and dicts['a2'] == dicts['a3'] and len(dicts['a3'].strip())>0:
 print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
 return 1 if dicts['a1']=='x' else 2
 elif dicts['a1'] == dicts['b2'] and dicts['b2'] == dicts['c3'] and len(dicts['c3'].strip())>0:
 print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
 return 1 if dicts['a1'] == 'x' else 2
 elif dicts['a1'] == dicts['b1'] and dicts['b1'] == dicts['c1'] and len(dicts['c1'].strip())>0:
  print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
  return 1 if dicts['a1'] == 'x' else 2
 elif dicts['a2'] == dicts['b2'] and dicts['b2'] == dicts['c2'] and len(dicts['c2'].strip())>0:
 print('游戏结束,' + '用户1-获胜' if dicts['a2'] == 'x' else '用户2-获胜')
 return 1 if dicts['a2'] == 'x' else 2
 elif dicts['a3'] == dicts['b3'] and dicts['b3'] == dicts['c3'] and len(dicts['c3'].strip())>0:
  print('游戏结束,' + '用户1-获胜' if dicts['a3'] == 'x' else '用户2-获胜')
  return 1 if dicts['a3'] == 'x' else 2
 elif dicts['a3'] == dicts['b2'] and dicts['b3'] == dicts['c1'] and len(dicts['c1'].strip())>0:
  print('游戏结束,' + '用户1-获胜' if dicts['a3'] == 'x' else '用户2-获胜')
  return 1 if dicts['a3'] == 'x' else 2
 elif dicts['b1'] == dicts['b2'] and dicts['b2'] == dicts['b3'] and len(dicts['b3'].strip())>0:
  print('游戏结束,' + '用户1-获胜' if dicts['b1'] == 'x' else '用户2-获胜')
  return 1 if dicts['b1'] == 'x' else 2
 elif dicts['c1'] == dicts['c2'] and dicts['c2'] == dicts['c3'] and len(dicts['c3'].strip())>0:
  print('游戏结束,' + '用户1-获胜' if dicts['c1'] == 'x' else '用户2-获胜')
  return 1 if dicts['c1'] == 'x' else 2
 else:
 return 0
if __name__ =='__main__':
 main()

补一点更改思路:forResult()的另一种实现,compares()函数:少了6行代码量。

def compares(dictionary={'':''},string=''):
 if len(dictionary)>0 | len(string.strip())==0:print('传值为空!')
 else:
 axle =('a1','a3','b2','c1','c3') # 四个角和中间的数特殊判断 条件1
 axle_fang=False #特殊棋号需要多加一种可能性
 for x in axle:
  if string==x:axle_fang=True
 if axle_fang: #条件1
  if dictionary['a1']==dictionary['b2'] and dictionary['b2']==dictionary['c3'] and dictionary['c3'].strip()!=''\
   or dictionary['a3']==dictionary['b2'] and dictionary['b2']==dictionary['c1']and dictionary['c1'].strip()!='':
   print('游戏结束,' + '用户1-获胜' if dictionary[string] == 'x' else '用户2-获胜')
   return 1 if dictionary[string] == 'x' else 2
 # 拆分棋号 splitStr0,splitStr1,普通棋号只需判断俩种a俩种可能,上下-左右间的位置
 splitStr0,splitStr1 = string[0],string[1]
 print(splitStr0+":"+splitStr1)
 if dictionary[splitStr0+'1']==dictionary[splitStr0+'2'] and dictionary[splitStr0+'2']==dictionary[splitStr0+'3']\
  or dictionary['a'+splitStr1]==dictionary['b'+splitStr1] and dictionary['b'+splitStr1]==dictionary['c'+splitStr1]:
  print('游戏结束,' + '用户1-获胜' if dictionary[string] == 'x' else '用户2-获胜')
  return 1 if dictionary[string] == 'x' else 2
 else:return 0

end:写完这些也有九十行代码量了,总感觉太多了。

控制台打印:

python入门之井字棋小游戏

python入门之井字棋小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 环境变量和import模块导入方法(详解)
Jul 11 Python
Python如何抓取天猫商品详细信息及交易记录
Feb 23 Python
Python之pandas读写文件乱码的解决方法
Apr 20 Python
python 实现在Excel末尾增加新行
May 02 Python
查看django执行的sql语句及消耗时间的两种方法
May 29 Python
对python判断是否回文数的实例详解
Feb 08 Python
Python机器学习算法库scikit-learn学习之决策树实现方法详解
Jul 04 Python
Django实现文件上传下载功能
Oct 06 Python
基于Python爬取爱奇艺资源过程解析
Mar 02 Python
pymysql 插入数据 转义处理方式
Mar 02 Python
Windows10+anacond+GPU+pytorch安装详细过程
Mar 24 Python
在主流系统之上安装Pygame的方法
May 20 Python
解决Python图形界面中设置尺寸的问题
Mar 05 #Python
python实现简单井字棋小游戏
Mar 05 #Python
python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例
Mar 04 #Python
Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸实例
Mar 04 #Python
关于Python Tkinter Button控件command传参问题的解决方式
Mar 04 #Python
在python tkinter界面中添加按钮的实例
Mar 04 #Python
Python tkinter布局与按钮间距设置方式
Mar 04 #Python
You might like
攻克CakePHP系列一 连接MySQL数据库
2008/10/22 PHP
基于PHP读取csv文件内容的详解
2013/06/18 PHP
php魔术变量用法实例详解
2014/11/13 PHP
网页自动刷新,不产生嗒嗒声的一个解决方法
2007/03/27 Javascript
通过身份证号得到出生日期和性别的js代码
2009/11/23 Javascript
JavaScript 语言的递归编程
2010/05/18 Javascript
windows下安装nodejs及框架express
2015/08/07 NodeJs
ionic组件ion-tabs选项卡切换效果实例
2016/08/27 Javascript
深入理解Nodejs Global 模块
2017/06/03 NodeJs
bootstrap table方法之expandRow-collapseRow展开或关闭当前行数据
2020/08/09 Javascript
基于vue2实现上拉加载功能
2017/11/28 Javascript
karma+webpack搭建vue单元测试环境的方法示例
2018/05/24 Javascript
vue插件实现v-model功能
2018/09/10 Javascript
微信小程序http连接访问解决方案的示例
2018/11/05 Javascript
vue 实现Web端的定位功能 获取经纬度
2019/08/08 Javascript
vue使用nprogress实现进度条
2019/12/09 Javascript
Vue强制组件重新渲染的方法讨论
2020/02/03 Javascript
[01:02:17]2014 DOTA2华西杯精英邀请赛 5 24 DK VS VG
2014/05/26 DOTA
[42:32]Secret vs Optic 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
[40:01]OG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
python操作sqlite的CRUD实例分析
2015/05/08 Python
快速了解Python中的装饰器
2018/01/11 Python
浅谈Python基础—判断和循环
2019/03/22 Python
django 前端页面如何实现显示前N条数据
2020/03/16 Python
使用python创建生成动态链接库dll的方法
2020/05/09 Python
Python限制内存和CPU使用量的方法(Unix系统适用)
2020/08/04 Python
python+openCV对视频进行截取的实现
2020/11/27 Python
英国袜子店:Sock Shop
2017/01/11 全球购物
Does C# support multiple inheritance? (C#支持多重继承吗)
2012/01/04 面试题
统计学专业毕业生的自我评价分享
2013/11/28 职场文书
家长给幼儿园的表扬信
2014/01/09 职场文书
运动会入场词200字
2014/02/15 职场文书
合作协议书模板2014
2014/09/26 职场文书
2014最新党员违纪检讨书
2014/10/12 职场文书
新年晚会主持词开场白
2015/05/28 职场文书
暑期辅导班宣传单
2015/07/14 职场文书