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实现选择排序
Jun 04 Python
Python实现获取照片拍摄日期并重命名的方法
Sep 30 Python
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
Jul 13 Python
Python get获取页面cookie代码实例
Sep 12 Python
PyQt5笔记之弹出窗口大全
Jun 20 Python
django组合搜索实现过程详解(附代码)
Aug 06 Python
pytorch索引查找 index_select的例子
Aug 18 Python
Python3 main函数使用sys.argv传入多个参数的实现
Dec 25 Python
pytorch下使用LSTM神经网络写诗实例
Jan 14 Python
Pycharm debug调试时带参数过程解析
Feb 03 Python
python GUI库图形界面开发之PyQt5复选框控件QCheckBox详细使用方法与实例
Feb 28 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
May 10 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
WML,Apache,和 PHP 的介绍
2006/10/09 PHP
PHP+Memcache实现wordpress访问总数统计(非插件)
2014/07/04 PHP
[原创]PHP简单开启curl的方法(测试可行)
2016/01/11 PHP
PHP实现的分页类定义与用法示例
2017/07/05 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
javascript编程起步(第七课)
2007/01/10 Javascript
jQuery对象和DOM对象使用说明
2010/06/25 Javascript
js操作CheckBoxList实现全选/反选(在客服端完成)
2013/02/02 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
javascript格式化指定日期对象的方法
2015/04/21 Javascript
js简单判断移动端系统的方法
2016/02/25 Javascript
JavaScript使用Ajax上传文件的示例代码
2017/08/10 Javascript
基于webpack 实用配置方法总结
2017/09/28 Javascript
H5实现仿flash效果的实现代码
2017/09/29 Javascript
浅谈微信小程序之官方UI框架we-ui使用教程
2018/08/20 Javascript
vue插件draggable实现拖拽移动图片顺序
2018/12/01 Javascript
浅谈JS中几种轻松处理'this'指向方式
2019/09/16 Javascript
vue-quill-editor插入图片路径太长问题解决方法
2021/01/08 Vue.js
python数据结构之二叉树的遍历实例
2014/04/29 Python
python在windows下实现ping操作并接收返回信息的方法
2015/03/20 Python
Python中有趣在__call__函数
2015/06/21 Python
Python面向对象之静态属性、类方法与静态方法分析
2018/08/24 Python
详解Python用三种方式统计词频的方法
2019/07/29 Python
浅谈python累加求和+奇偶数求和_break_continue
2020/02/25 Python
python实现全排列代码(回溯、深度优先搜索)
2020/02/26 Python
Keras 切换后端方式(Theano和TensorFlow)
2020/06/19 Python
CSS3改变浏览器滚动条样式
2019/01/04 HTML / CSS
Canvas环形饼图与手势控制的实现代码
2019/11/08 HTML / CSS
丝芙兰新加坡官网:Sephora新加坡
2018/12/04 全球购物
犹他州最古老的体育用品公司:Al’s
2020/12/18 全球购物
2014年社会实践活动总结范文
2014/04/29 职场文书
酒店端午节活动方案
2014/08/26 职场文书
党员民主评议总结
2014/10/20 职场文书
2015年小学校长工作总结
2015/05/19 职场文书
2015年小学远程教育工作总结
2015/07/28 职场文书
浅谈JavaScript作用域
2021/12/06 Javascript