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 相关文章推荐
教你如何在Django 1.6中正确使用 Signal
Jun 22 Python
利用Django框架中select_related和prefetch_related函数对数据库查询优化
Apr 01 Python
Python3连接MySQL(pymysql)模拟转账实现代码
May 24 Python
详解Python nose单元测试框架的安装与使用
Dec 20 Python
Python爬虫实战之12306抢票开源
Jan 24 Python
python解析yaml文件过程详解
Aug 30 Python
给我一面国旗 python帮你实现
Sep 30 Python
WxPython实现无边框界面
Nov 18 Python
详解Python设计模式之策略模式
Jun 15 Python
没编程基础可以学python吗
Jun 17 Python
Python+OpenCV图像处理——实现直线检测
Oct 23 Python
python区块链实现简版工作量证明
May 25 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 求质素(素数) 的实现代码
2011/04/12 PHP
PHP数组常用函数实例小结
2018/08/20 PHP
ExtJs默认的字体大小改变的几种方法(自己整理)
2013/04/18 Javascript
javaScript array(数组)使用字符串作为数组下标的方法
2013/11/19 Javascript
JS中的数组的sort方法使用示例
2014/01/22 Javascript
js或jquery实现页面打印可局部打印
2014/03/27 Javascript
javascript+ajax实现产品页面加载信息
2015/07/09 Javascript
javascript顺序加载图片的方法
2015/07/18 Javascript
JavaScript实现的圆形浮动标签云效果实例
2015/08/06 Javascript
JS三级可折叠菜单实现方法
2016/02/29 Javascript
JS基于递归实现网页版计算器的方法分析
2017/12/20 Javascript
Vue中的字符串模板的使用
2018/05/17 Javascript
详解Vue.js自定义tipOnce指令用法实例
2018/12/19 Javascript
vue 集成jTopo 处理方法
2019/08/07 Javascript
node静态服务器实现静态读取文件或文件夹
2019/12/03 Javascript
vue动态渲染svg、添加点击事件的实现
2020/03/13 Javascript
[02:09:59]火猫TV国士无双dota2 6.82版本详解(下)
2014/09/29 DOTA
Python列表推导式的使用方法
2013/11/21 Python
Python读取sqlite数据库文件的方法分析
2017/08/07 Python
python+mongodb数据抓取详细介绍
2017/10/25 Python
python编写朴素贝叶斯用于文本分类
2017/12/21 Python
python 查找文件名包含指定字符串的方法
2018/06/05 Python
python获取时间及时间格式转换问题实例代码详解
2018/12/06 Python
python设置随机种子实例讲解
2019/09/12 Python
django admin后管定制-显示字段的实例
2020/03/11 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
Python 使用xlwt模块将多行多列数据循环写入excel文档的操作
2020/11/10 Python
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
Yahoo-PHP面试题4
2012/05/05 面试题
老公给老婆的道歉信
2014/01/10 职场文书
黄继光的英雄事迹材料
2014/02/13 职场文书
爱岗敬业演讲稿
2014/05/05 职场文书
八一建军节营销活动方案
2014/08/31 职场文书
会议简报格式范文
2015/07/20 职场文书
纯CSS实现酷炫的霓虹灯效果
2021/04/13 HTML / CSS
MongoDB连接数据库并创建数据等使用方法
2021/11/27 MongoDB