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 相关文章推荐
wxPython框架类和面板类的使用实例
Sep 28 Python
Python写的服务监控程序实例
Jan 31 Python
总结Python编程中三条常用的技巧
May 11 Python
基于python实现的抓取腾讯视频所有电影的爬虫
Apr 22 Python
python实现图书馆研习室自动预约功能
Apr 27 Python
Python 使用PIL中的resize进行缩放的实例讲解
Aug 03 Python
Python 获取中文字拼音首个字母的方法
Nov 28 Python
用Python和WordCloud绘制词云的实现方法(内附让字体清晰的秘笈)
Jan 08 Python
Python定时器线程池原理详解
Feb 26 Python
Python获取excel内容及相关操作代码实例
Aug 10 Python
python 命令行传参方法总结
May 25 Python
Python连续赋值需要注意的一些问题
Jun 03 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 在线翻译函数代码
2009/05/07 PHP
函数中使用require_once问题深入探讨 优雅的配置文件定义方法推荐
2014/07/02 PHP
php获取文件类型和文件信息的方法
2015/07/10 PHP
WordPress中查询文章的循环Loop结构及用法分析
2015/12/17 PHP
php 从一个数组中随机的取出若干个不同的数实例
2016/12/31 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
YUI模块开发原理详解
2013/11/18 Javascript
jquery实现简单合拢与展开网页面板的方法
2015/09/01 Javascript
JS实现带鼠标效果的头像及文章列表代码
2015/09/27 Javascript
javascript函数式编程程序员的工具集
2015/10/11 Javascript
SVG描边动画
2017/02/23 Javascript
微信小程序 聊天室简单实现
2017/04/19 Javascript
javascript 正则表达式分组、断言详解
2017/04/20 Javascript
JS中的数组转变成JSON格式字符串的方法
2017/05/09 Javascript
Vue实现一个返回顶部backToTop组件
2017/07/25 Javascript
Vue项目webpack打包部署到Tomcat刷新报404错误问题的解决方案
2018/05/15 Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
2019/03/21 Javascript
vue + typescript + 极验登录验证的实现方法
2019/06/27 Javascript
jQuery实现的解析本地 XML 文档操作示例
2020/04/30 jQuery
浅谈vue中document.getElementById()拿到的是原值的问题
2020/07/26 Javascript
JQuery通过键盘控制键盘按下与松开触发事件
2020/08/07 jQuery
Element-ui树形控件el-tree自定义增删改和局部刷新及懒加载操作
2020/08/31 Javascript
vue treeselect获取当前选中项的label实例
2020/08/31 Javascript
Python使用Flask框架获取当前查询参数的方法
2015/03/21 Python
python脚本实现数据导出excel格式的简单方法(推荐)
2016/12/30 Python
Python Excel处理库openpyxl使用详解
2019/05/09 Python
Python集中化管理平台Ansible介绍与YAML简介
2019/06/12 Python
python绘制已知点的坐标的直线实例
2019/07/04 Python
关于python字符串方法分类详解
2019/08/20 Python
网络技术专业求职信
2014/02/18 职场文书
房产买卖委托公证书
2014/04/04 职场文书
暑假打工感想
2015/08/07 职场文书
感恩教师节主题班会
2015/08/12 职场文书
Python的flask接收前台的ajax的post数据和get数据的方法
2021/04/12 Python
react如何快速设置文件路径别名
2021/04/28 Javascript
springcloud之Feign超时问题的解决
2021/06/24 Java/Android