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代码(逐级优化)
May 25 Python
python实现在windows下操作word的方法
Apr 28 Python
python paramiko模块学习分享
Aug 23 Python
Python面向对象之静态属性、类方法与静态方法分析
Aug 24 Python
python 遍历列表提取下标和值的实例
Dec 25 Python
python后端接收前端回传的文件方法
Jan 02 Python
python使用 zip 同时迭代多个序列示例
Jul 06 Python
Python+Pyqt实现简单GUI电子时钟
Feb 22 Python
Python3视频转字符动画的实例代码
Aug 29 Python
python实现人机五子棋
Mar 25 Python
Python 爬虫的原理
Jul 30 Python
Python 添加文件注释和函数注释操作
Aug 09 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中实现进程间通讯
2006/10/09 PHP
初识PHP
2014/09/28 PHP
PHP动态输出JavaScript代码实例
2015/02/12 PHP
PHP实现从上往下打印二叉树的方法
2018/01/18 PHP
javascript检查日期格式的函数[比较全]
2008/10/17 Javascript
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
javascript Array数组对象的扩展函数代码
2010/05/22 Javascript
jcrop基本参数一览
2013/07/16 Javascript
JS预览图像将本地图片显示到浏览器上
2013/08/25 Javascript
jQuery显示和隐藏 常用的状态判断方法
2015/01/29 Javascript
Jquery中offset()和position()的区别分析
2015/02/05 Javascript
jquery移动点击的项目到列表最顶端的方法
2015/06/24 Javascript
js验证真实姓名与身份证号是否匹配
2015/10/13 Javascript
js实现div在页面拖动效果
2016/05/04 Javascript
轻松掌握JavaScript策略模式
2016/08/25 Javascript
Vuejs第一篇之入门教程详解(单向绑定、双向绑定、列表渲染、响应函数)
2016/09/09 Javascript
jQuery监听文件上传实现进度条效果的方法
2016/10/16 Javascript
Angular 常用指令实例总结整理
2016/12/13 Javascript
ES6中参数的默认值语法介绍
2017/05/03 Javascript
vue中手机号,邮箱正则验证以及60s发送验证码的实例
2018/03/16 Javascript
Python自动登录126邮箱的方法
2015/07/10 Python
Python的GUI框架PySide的安装配置教程
2016/02/16 Python
python解决Fedora解压zip时中文乱码的方法
2016/09/18 Python
python的Crypto模块实现AES加密实例代码
2018/01/22 Python
Python机器学习库scikit-learn安装与基本使用教程
2018/06/25 Python
Python3 pickle对象串行化代码实例解析
2020/03/23 Python
python Matplotlib数据可视化(1):简单入门
2020/09/30 Python
计算 s=(x*y)1/2,用两个宏定义来实现
2016/08/11 面试题
超级搞笑检讨书
2014/01/15 职场文书
幼儿园老师辞职信
2014/01/20 职场文书
交通事故赔偿协议书怎么写
2014/10/04 职场文书
简易离婚协议书(范本)
2014/10/25 职场文书
学术会议邀请函
2015/01/30 职场文书
MySQL慢查询的坑
2021/04/28 MySQL
分析JVM源码之Thread.interrupt系统级别线程打断
2021/06/29 Java/Android
CSS+HTML 实现顶部导航栏功能
2021/08/30 HTML / CSS