python入门之井字棋小游戏

 更新时间:2020年03月05日 08:06:03   作者:M青年小客  
这篇文章主要为大家详细介绍了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:写完这些也有九十行代码量了,总感觉太多了。

控制台打印:

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

相关文章

  • python列表推导式 经典代码

    python列表推导式 经典代码

    这篇文章主要介绍了python列表推导式,文章内容附有经典的详细代码,感兴趣的小伙按可以参考下面文章具体内容
    2021-10-10
  • Python中lru_cache的使用和实现详解

    Python中lru_cache的使用和实现详解

    这篇文章主要介绍了Python 中 lru_cache 的使用和实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python3.6 之后字典是有序的?

    Python3.6 之后字典是有序的?

    字典数据是有序的, 但是这个序不是由外部控制, 而是内部字典定位机制的序 所以对外来讲, 数据本身是无序的 你每次遍历的顺序一样, 是因为枚举结果是按内部排序输出 而无序则表示在你无法从外部控制最终的输出顺序,下面我们来学习Python字典有序性的相关资料又当怎样吧
    2021-12-12
  • Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

    Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

    本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看看吧
    2025-02-02
  • 10个Python小技巧你值得拥有

    10个Python小技巧你值得拥有

    这篇文章主要介绍了10个Python小技巧,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • python矩阵/字典实现最短路径算法

    python矩阵/字典实现最短路径算法

    这篇文章主要为大家详细介绍了python矩阵/字典实现最短路径算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • python math模块的基本使用教程

    python math模块的基本使用教程

    这篇文章主要介绍了python math模块的基本使用教程,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-01-01
  • tensorflow使用L2 regularization正则化修正overfitting过拟合方式

    tensorflow使用L2 regularization正则化修正overfitting过拟合方式

    这篇文章主要介绍了tensorflow使用L2 regularization正则化修正overfitting过拟合方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python实现多个视频合成一个视频的功能

    Python实现多个视频合成一个视频的功能

    这篇文章主要介绍了可以将多个视频拼接为一个视频的Python工具代码,文中的代码讲解详细,对我们学习Python有一定的帮助,快来跟随小编一起学习一下吧
    2021-12-12
  • python 将json数据提取转化为txt的方法

    python 将json数据提取转化为txt的方法

    今天小编就为大家分享一篇python 将json数据提取转化为txt的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10

最新评论