python实现井字棋游戏


Posted in Python onMarch 30, 2020

本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下

windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。
游戏就是井字棋,小键盘上的数字位置对应棋盘位置。

#本游戏python3.4.0下编写调试,只能在windows下运行。
import random
import subprocess
import time
#定义函数
def draw_board(the_board):
 subprocess.call("cls", shell = True)
 print(' -------\n' + ' |' + the_board[9] + '|' + the_board[8] + '|' + the_board[7] + '|\n' + ' -------\n' + ' |' + the_board[6] + '|' + the_board[5] + '|' + the_board[4] + '|\n' + ' -------\n' + ' |' + the_board[3] + '|' + the_board[2] + '|' + the_board[1] + '|\n' + ' -------')
def input_player_letter():
 letter = ' '
 while not (letter == 'X' or letter == 'O'):
 print('请选择X或O作棋子:', end = '')
 letter = input().upper()
 if letter == 'X':
 return ['X', 'O']
 else:
 return ['O', 'X']
def who_first():
 if 1 == random.randint(1, 2):
 return 'computer'
 else:
 return 'player'
def is_again():
 print('再一次?(Yes or No)')
 return input().lower().startswith('y')
def is_space_free(the_board, move):
 return the_board[move] == ' '
def choose_random_from_list(the_board, move_from_list):
 possible_moves = []
 for i in move_from_list:
 if is_space_free(the_board, i):
 possible_moves.append(i)
 if len(possible_moves) != 0:
 return random.choice(possible_moves)
 else:
 return None
def make_move(the_board, the_letter, the_move):
 the_board[the_move] = the_letter
def get_board_copy(the_board):
 duplicated_board = []
 for i in board:
 duplicated_board.append(i)
 return duplicated_board
def is_board_full(the_board):
 for i in range(1, 9):
 if is_space_free(the_board, i):
 return False
 else:
 return True
def get_player_move(the_board):
 the_move = 0
 while the_move not in list(range(1, 9)) or not is_space_free(the_board, the_move):
 print('请输入走步:', end = '')
 the_move = int(input())
 return the_move
def is_winner(the_board, the_letter):
 return (the_board[1] == the_letter and the_board[2] == the_letter and the_board[3] == the_letter) or (the_board[4] == the_letter and the_board[5] == the_letter and the_board[6] == the_letter) or (the_board[7] == the_letter and the_board[8] == the_letter and the_board[9] == the_letter) or (the_board[1] == the_letter and the_board[5] == the_letter and the_board[9] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[5] == the_letter and the_board[7] == the_letter) or (the_board[1] == the_letter and the_board[4] == the_letter and the_board[7] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[6] == the_letter and the_board[9] == the_letter)
def get_computer_move(the_board, computer_letter):
 global player_letter
 global move
 if player_letter == 'X':
 computer_letter = 'O'
 else:
 player_letter = 'O'
 computer_letter = 'X'
 #虚拟棋盘查看是否自己可一步得胜
 for i in range(1,9):
 copy = get_board_copy(board)
 if is_space_free(board, i):
 make_move(copy, computer_letter, i)
 if is_winner(copy, computer_letter):
 return i
 #虚拟棋盘查看是否对手可一步得胜
 for i in range(1,9):
 if is_space_free(board, i):
 copy = get_board_copy(board)
 make_move(copy, player_letter, i)
 if is_winner(copy, player_letter):
 return i
 move = choose_random_from_list(board, [1, 3, 7, 9])
 if move != 0:
 return move
 if is_space_free(board, 5):
 return 5
 return choose_random_from_list(board, [2, 4, 6, 8, 7])
print('欢迎玩 井字棋 游戏!')
time.sleep(1)
print('''???????????????????????????????
???????????????????????????????
???? ??????????????????????????
?????????????????????????????
?????? ????????????????????????
???????????????????????????????
???????????????????????????????
???????????????????????????????
??????????? ???????????????????
???????????? ???????? ?????????
???????????????????????????????
?????????????? ????????????????
??????????????? ???????????????
???????????????? ??????????????
???????????????????????????????
???????????????????????????????
???????????????????????????????
????????????????????
?????????
????????????????????? ?????????
???????????????????????????????
???????????????????????????????
???????????????????????????????
?????????????????????????? ????
???????????????????????????????
???????????????????????????? ??
????????????????????????????? ?
???????????????????????????????''')
time.sleep(2)
subprocess.call("cls", shell = True)
while True:
 board = [' '] * 10
 player_letter, computer_letter = input_player_letter()
 turn = who_first()
 print(turn + '先走')
 time.sleep(1)
 game_is_playing = True
 while game_is_playing:
 if turn == 'player':
 draw_board(board)
 move = get_player_move(board)
 make_move(board, player_letter, move)
 if is_winner(board, player_letter):
 draw_board(board)
 print('恭喜!你赢了。')
 game_is_playinig = False
 else:
 if is_board_full(board):
  draw_board(board)
  print('平局!')
  break
 else:
  turn = 'computer'
 else:
 move = get_computer_move(board, computer_letter)
 make_move(board, computer_letter, move)
 if is_winner(board, computer_letter):
 draw_board(board)
 print('电脑胜利,你挂了!')
 game_is_playing = False
 else:
 if is_board_full(board):
  draw_board(board)
  print('平局!')
  break
 else:
  turn = 'player'
 if not is_again():
 break

更多关于python游戏的精彩文章请点击查看以下专题:

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

Python 相关文章推荐
Python获取单个程序CPU使用情况趋势图
Mar 10 Python
Python调用C语言开发的共享库方法实例
Mar 18 Python
在Python3中初学者应会的一些基本的提升效率的小技巧
Mar 31 Python
python爬虫_实现校园网自动重连脚本的教程
Apr 22 Python
Python访问MongoDB,并且转换成Dataframe的方法
Oct 15 Python
解决pycharm安装后代码区不能编辑的问题
Oct 28 Python
Python Pexpect库的简单使用方法
Jan 29 Python
python之当你发现QTimer不能用时的解决方法
Jun 21 Python
Django对models里的objects的使用详解
Aug 17 Python
python sklearn常用分类算法模型的调用
Oct 16 Python
python 实现多维数组转向量
Nov 30 Python
python中pycryto实现数据加密
Apr 29 Python
python搭建微信公众平台
Feb 09 #Python
Python实例一个类背后发生了什么
Feb 09 #Python
Python中的条件判断语句基础学习教程
Feb 07 #Python
Python模拟登录验证码(代码简单)
Feb 06 #Python
Python上传package到Pypi(代码简单)
Feb 06 #Python
深入讲解Java编程中类的生命周期
Feb 05 #Python
python&MongoDB爬取图书馆借阅记录
Feb 05 #Python
You might like
对盗链说再见...
2006/10/09 PHP
php实现水印文字和缩略图的方法示例
2016/12/29 PHP
基于jquery的Repeater实现代码
2010/07/17 Javascript
Javascript new关键字的玄机 以及其它
2010/08/25 Javascript
JavaScript关闭当前页面(窗口)不带任何提示
2014/03/26 Javascript
用js读、写、删除Cookie代码续篇
2014/12/03 Javascript
详解JavaScript的Polymer框架中的通知交互
2015/07/29 Javascript
将页面table内容与样式另存成excel文件的方法
2015/08/05 Javascript
JavaScript数据类型学习笔记
2016/01/25 Javascript
javascript实现不同颜色Tab标签切换效果
2016/04/27 Javascript
Bootstrap对话框使用实例讲解
2016/09/24 Javascript
基于百度地图实现产品销售的单位位置查看功能设计与实现
2016/10/21 Javascript
js时间戳格式化成日期格式的多种方法介绍
2017/02/16 Javascript
详解Angular之constructor和ngOnInit差异及适用场景
2017/06/22 Javascript
详解如何构建Angular项目目录结构
2017/07/13 Javascript
js实现通过开始结束控制的计时器
2019/02/25 Javascript
JS XMLHttpRequest原理与使用方法深入详解
2020/04/30 Javascript
python中文分词,使用结巴分词对python进行分词(实例讲解)
2017/11/14 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
Python shelve模块实现解析
2019/08/28 Python
python模块hashlib(加密服务)知识点讲解
2019/11/25 Python
django haystack实现全文检索的示例代码
2020/06/24 Python
纯CSS3实现图片无间断轮播效果
2016/08/25 HTML / CSS
英国口碑最好的的维他命胶囊品牌:Myvitamins(有中文站)
2016/12/03 全球购物
DeinDesign德国:设计自己的手机壳
2019/12/14 全球购物
园长自我鉴定
2013/10/06 职场文书
行政助理的职责
2013/11/14 职场文书
个性婚礼策划方案
2014/05/17 职场文书
导师就业推荐信范文
2014/05/22 职场文书
亚运会口号
2014/06/20 职场文书
2014个人四风对照检查材料思想汇报
2014/09/18 职场文书
2014年幼师工作总结
2014/11/22 职场文书
2014年小学工作总结
2014/11/26 职场文书
只需要这一行代码就能让python计算速度提高十倍
2021/05/24 Python
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js
《LOL》“克隆大作战”久违归来 幻灵战队皮肤上线
2022/04/03 其他游戏