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中list列表的高级函数
May 17 Python
python类中super()和__init__()的区别
Oct 18 Python
浅谈python配置与使用OpenCV踩的一些坑
Apr 02 Python
Python之inspect模块实现获取加载模块路径的方法
Oct 16 Python
详解Python:面向对象编程
Apr 10 Python
解决Django生产环境无法加载静态文件问题的解决
Apr 23 Python
Python实现html转换为pdf报告(生成pdf报告)功能示例
May 04 Python
Python实战之制作天气查询软件
May 14 Python
Python在cmd上打印彩色文字实现过程详解
Aug 07 Python
Python 实现PS滤镜的旋涡特效
Dec 03 Python
Python 中如何使用 virtualenv 管理虚拟环境
Jan 21 Python
python turtle绘图
May 04 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/07/30 PHP
PHP中怎样保持SESSION不过期 原理及方案介绍
2013/08/08 PHP
解决PHP curl或file_get_contents下载图片损坏或无法打开的问题
2019/10/11 PHP
jquery tools系列 expose 学习
2009/09/06 Javascript
Mootools 1.2教程 事件处理
2009/09/15 Javascript
C#中TrimStart,TrimEnd,Trim在javascript上的实现
2011/01/17 Javascript
下载文件个别浏览器文件名乱码解决办法
2013/03/19 Javascript
给事件响应函数传参数的四种方式小结
2013/12/05 Javascript
如何设置一定时间内只能发送一次请求
2014/02/28 Javascript
angular.foreach 循环方法使用指南
2015/01/06 Javascript
js的for in循环和java里foreach循环的区别分析
2015/01/28 Javascript
jQuery检测鼠标左键和右键点击的方法
2015/03/17 Javascript
javascript中Date()函数在各浏览器中的显示效果
2015/06/18 Javascript
jQuery+ajax+asp.net获取Json值的方法
2016/06/08 Javascript
Bootstrap媒体对象学习使用
2017/03/07 Javascript
原生js实现放大镜特效
2017/03/08 Javascript
Django+Vue.js搭建前后端分离项目的示例
2017/08/07 Javascript
Postman的下载及安装教程详解
2018/10/16 Javascript
Vue 子组件与数据传递问题及注意事项
2019/07/11 Javascript
基于Vue.js与WordPress Rest API构建单页应用详解
2019/09/16 Javascript
python async with和async for的使用
2019/06/20 Python
From CSV to SQLite3 by python 导入csv到sqlite实例
2020/02/14 Python
简单的Python人脸识别系统
2020/07/14 Python
python的dict判断key是否存在的方法
2020/12/09 Python
基于 HTML5 WebGL 实现的医疗物流系统
2019/10/08 HTML / CSS
Darphin迪梵官网: 来自巴黎,植物和精油调制的护肤品牌
2016/10/11 全球购物
莫斯科购买书籍网站:Book24
2020/01/12 全球购物
大学生收银员求职信分享
2014/01/02 职场文书
文秘个人求职信范文
2014/04/22 职场文书
软件售后服务承诺书
2014/05/21 职场文书
宣传活动总结范文
2014/07/01 职场文书
成本会计实训报告
2014/11/05 职场文书
2016年公司新年寄语
2015/08/17 职场文书
岗位聘任协议书
2015/09/21 职场文书
在Windows下安装配置CPU版的PyTorch的方法
2021/04/02 Python
关于html选择框创建占位符的问题
2021/06/09 HTML / CSS