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中利用Scipy包的SIFT方法进行图片识别的实例教程
Jun 03 Python
Python实现二维数组按照某行或列排序的方法【numpy lexsort】
Sep 22 Python
python使用正则表达式替换匹配成功的组并输出替换的次数
Nov 22 Python
python 统计一个列表当中的每一个元素出现了多少次的方法
Nov 14 Python
理想高通滤波实现Python opencv示例
Jan 30 Python
django框架model orM使用字典作为参数,保存数据的方法分析
Jun 24 Python
用Python徒手撸一个股票回测框架搭建【推荐】
Aug 05 Python
python3获取文件中url内容并下载代码实例
Dec 27 Python
tensorflow模型保存、加载之变量重命名实例
Jan 21 Python
Python实现ATM系统
Feb 17 Python
appium+python自动化配置(adk、jdk、node.js)
Nov 17 Python
python面向对象版学生信息管理系统
Jun 24 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
PHP5.3以上版本安装ZendOptimizer扩展
2015/03/27 PHP
php递归实现无限分类的方法
2015/07/28 PHP
基于JQuery+PHP编写砸金蛋中奖程序
2015/09/08 PHP
php单链表实现代码分享
2016/07/04 PHP
php封装的图片(缩略图)处理类完整实例
2016/10/19 PHP
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
JS控制图片翻转示例代码(兼容firefox,ie,chrome)
2013/12/19 Javascript
8个实用的jQuery技巧
2014/03/04 Javascript
JS获取客户端IP地址、MAC和主机名的7个方法汇总
2014/07/21 Javascript
js格式化时间的方法
2015/12/18 Javascript
JS填写银行卡号每隔4位数字加一个空格
2016/12/19 Javascript
babel基本使用详解
2017/02/17 Javascript
全面解析vue中的数据双向绑定
2017/05/10 Javascript
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
2017/06/26 jQuery
JavaScript监听手机物理返回键的两种解决方法
2017/08/14 Javascript
CSS3 动画卡顿性能优化的完美解决方案
2018/09/20 Javascript
es6中使用map简化复杂条件判断操作实例详解
2020/02/19 Javascript
html+vue.js 实现漂亮分页功能可兼容IE
2020/11/07 Javascript
python实现在windows下操作word的方法
2015/04/28 Python
python使用正则表达式提取网页URL的方法
2015/05/26 Python
Python实现购物车功能的方法分析
2017/11/10 Python
Python编程使用NLTK进行自然语言处理详解
2017/11/16 Python
python3+selenium自动化测试框架详解
2019/03/17 Python
网易2016研发工程师编程题 奖学金(python)
2019/06/19 Python
python实现俄罗斯方块游戏(改进版)
2020/03/13 Python
FC-Moto西班牙:摩托车手最大的购物场所之一
2019/04/11 全球购物
商务英语专业应届毕业生求职信
2013/10/28 职场文书
财政局长自荐信范文
2013/12/22 职场文书
syb养殖创业计划书
2014/01/09 职场文书
我为党旗添光彩演讲稿
2014/09/10 职场文书
乡镇2014法制宣传日活动总结
2014/11/01 职场文书
如何书写民事调解协议书?
2019/06/25 职场文书
如何利用js在两个html窗口间通信
2021/04/27 Javascript
Python中OpenCV实现查找轮廓的实例
2021/06/08 Python
python套接字socket通信
2022/04/01 Python
Tomcat安装使用及部署Web项目的3种方法汇总
2022/08/14 Servers