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抓取网页时字符集转换问题处理方案分享
Jun 19 Python
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
Apr 09 Python
Python的Django框架中设置日期和字段可选的方法
Jul 17 Python
Python中http请求方法库汇总
Jan 06 Python
python 简单的绘图工具turtle使用详解
Jun 21 Python
Python使用openpyxl读写excel文件的方法
Jun 30 Python
详解Python3注释知识点
Feb 19 Python
通过python3实现投票功能代码实例
Sep 26 Python
Python进程,多进程,获取进程id,给子进程传递参数操作示例
Oct 11 Python
python实现大量图片重命名
Mar 23 Python
Python TCPServer 多线程多客户端通信的实现
Dec 31 Python
python爬虫beautifulsoup解析html方法
Dec 07 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
php不用正则采集速度探究总结
2008/03/24 PHP
Drupal读取Excel并导入数据库实例
2014/03/02 PHP
php初始化对象和析构函数的简单实例
2014/03/11 PHP
如何解决phpmyadmin导入数据库文件最大限制2048KB
2015/10/09 PHP
简单了解PHP编程中数组的指针的使用
2015/11/30 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
js打印纸函数代码(递归)
2010/06/18 Javascript
JavaScript高级程序设计(第3版)学习笔记8 js函数(中)
2012/10/11 Javascript
Js中的onblur和onfocus事件应用介绍
2013/08/27 Javascript
JavaScript 垃圾回收机制分析
2013/10/10 Javascript
a标签click和href执行顺序探讨
2014/06/23 Javascript
jquery中ajax跨域方法实例分析
2015/12/18 Javascript
js获取所有checkbox的值的简单实例
2016/05/30 Javascript
最实用的jQuery分页插件
2016/10/09 Javascript
用move.js库实现百叶窗特效
2017/02/08 Javascript
原生javascript实现读写CSS样式的方法详解
2017/02/20 Javascript
20行js代码实现的贪吃蛇小游戏
2017/06/20 Javascript
JavaScript使用FileReader实现图片上传预览效果
2020/03/27 Javascript
Vue中添加手机验证码组件功能操作方法
2017/12/07 Javascript
JavaScript获取用户所在城市及地理位置
2018/04/21 Javascript
JavaScript的Proxy可以做哪些有意思的事儿
2019/06/15 Javascript
ES6 Map结构的应用实例分析
2019/06/26 Javascript
layui 富文本图片上传接口与普通按钮 文件上传接口的例子
2019/09/23 Javascript
基于Web Audio API实现音频可视化效果
2020/06/12 Javascript
[02:35]DOTA2英雄基础教程 狙击手
2014/01/14 DOTA
使用Python的Tornado框架实现一个简单的WebQQ机器人
2015/04/24 Python
Python微医挂号网医生数据抓取
2019/01/24 Python
windows下numpy下载与安装图文教程
2019/04/02 Python
python2.7 安装pip的方法步骤(管用)
2019/05/05 Python
学习Python列表的基础知识汇总
2020/03/10 Python
利用CSS3实现文字折纸效果实例代码
2018/07/10 HTML / CSS
汉森冲浪板:Hansen Surfboards
2018/05/19 全球购物
Laura官网:加拿大女性的顶级时尚目的地
2019/09/20 全球购物
abstract class和interface有什么区别?
2012/01/03 面试题
《鲁班和橹板》教学反思
2014/04/27 职场文书
义诊活动总结
2015/02/04 职场文书