python入门之井字棋小游戏
作者:M青年小客 发布时间:2021-12-11 11:35:36
标签:python,井字棋
引言:
刚学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:写完这些也有九十行代码量了,总感觉太多了。
控制台打印:
来源:https://blog.csdn.net/msdengxw/article/details/81872321
0
投稿
猜你喜欢
- ThinkPHP提供的视图查询应用功能十分强大,用户利用视图查询功能可以将多个数据表的字段内容按需要进行指定和筛选,组织成一个基于这些数据表
- 从网上找了很多django单元测试的案例,感觉不是很好用,于是自己写了一套测试方法,在测试环境我们只需要传uri 、请求方式、参数即可
- TensorFlow提供了一种统一的格式来存储数据,就是TFRecord,它可以统一不同的原始数据格式,并且更加有效地管理不同的属性。TFR
- 本文实例讲述了PHP实现获取第一个中文首字母并进行排序的方法。分享给大家供大家参考,具体如下:最近在做储值结算,需求里结算首页需要按门店的首
- 从最基础的说起。本教程中,所有IE 均指 WindowXP + IE 6.0, 所有 FF 均指 FF 1.5。1. 不用编程的部分1.1
- IF Exists(Select 1 From sysobjects Where Name='sp_search' And
- 很早就听说韩国网站的设计师们很会利用空间,来创造更多的信息承载量.最近浏览了几个韩国SHOPPING网站果不其然,就拿小小的广告轮播来说,非
- 目录1、简单循环 Simple loops2、简单循环但是使用了线程Simple loops but threaded3、定时调度库 Sch
- 背景对接多个外部接口,需要保存请求参数以及返回参数,方便消息的补偿,因为多个外部接口,多个接口字段都不统一,整体使用一个大字段(longte
- 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如果想汉字不进行转码,这里提
- 本文实例讲述了Python实现字符串逆序输出功能。分享给大家供大家参考,具体如下:1、有时候我们可能想让字符串倒序输出,下面给出几种方法方法
- 在oracle中有很多关于日期的函数,如:1、add_months()用于从一个日期值增加或减少一些月份date_value:=add_mo
- 搞一个图形化界面还是挺酷的,是吧 安装库什么的应该不用多说了吧。。一般来说会让你把 designer.exe(编辑图形化界面的东西,跟vb差
- 回想自己从事Web方面的开发已经有6-7年,对于各种Web技术都已经非常熟悉.可是,身为程序员的我对于制作Web表单界面的事着实心痛。心痛1
- sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdat
- python-redis-lock官方文档不错的博文可参考问题背景在使用celery执行我们的异步任务时,为了提高效率,celery可以开启
- 本文实例讲述了python实现堆栈与队列的方法。分享给大家供大家参考。具体分析如下:1、python实现堆栈,可先将Stack类写入文件st
- clear()方法将删除字典中的所有项目(清空字典)语法以下是clear()方法的语法:dict.clear()参数
- 每次调用内部的方法时,方法前面加 self.举例:例子参考百度知道里面的回答class MyClass: def __init_
- 1. 数组数组是 Golang 中的一种基本数据类型,用于存储固定数量的同类型元素。在 Golang 中,数组的长度是固定的,并且必须在定义