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 内置字符串处理函数的使用方法
Jun 11 Python
python监控linux内存并写入mongodb(推荐)
Sep 11 Python
将字典转换为DataFrame并进行频次统计的方法
Apr 08 Python
python多进程使用及线程池的使用方法代码详解
Oct 24 Python
python 随机打乱 图片和对应的标签方法
Dec 14 Python
使用Python 正则匹配两个特定字符之间的字符方法
Dec 24 Python
Python3 安装PyQt5及exe打包图文教程
Jan 08 Python
python xlwt如何设置单元格的自定义背景颜色
Sep 03 Python
使用Python画出小人发射爱心的代码
Nov 23 Python
Python如何给你的程序做性能测试
Jul 29 Python
如何利用python之wxpy模块玩转微信
Aug 17 Python
Python3中最常用的5种线程锁实例总结
Jul 07 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
Terran剧情介绍
2020/03/14 星际争霸
php知道与问问的采集插件代码
2010/10/12 PHP
php中全局变量global的使用演示代码
2011/05/18 PHP
php mysql获取表字段名称和字段信息的三种方法
2016/11/13 PHP
PHP二维索引数组的遍历实例分析【2种方式】
2019/06/24 PHP
使用laravel和ECharts实现折线图效果的例子
2019/10/09 PHP
Javascript的闭包
2009/12/31 Javascript
jquery合并表格中相同文本的相邻单元格
2015/07/17 Javascript
canvas实现十二星座星空图
2017/02/14 Javascript
JavaScript解析任意形式的json树型结构展示
2017/07/23 Javascript
如何解决React官方脚手架不支持Less的问题(小结)
2018/09/12 Javascript
详解react阻止无效重渲染的多种方式
2018/12/11 Javascript
vue实现弹幕功能
2019/10/25 Javascript
Javascript查看大图功能代码实现
2020/05/07 Javascript
JavaScript本地储存:localStorage、sessionStorage、cookie的使用
2020/10/13 Javascript
详解vue中在父组件点击按钮触发子组件的事件
2020/11/13 Javascript
python基础教程之常用运算符
2014/08/29 Python
编程语言Python的发展史
2014/09/26 Python
在Python的Flask框架下使用sqlalchemy库的简单教程
2015/04/09 Python
Python文件读取的3种方法及路径转义
2015/06/21 Python
python利用高阶函数实现剪枝函数
2018/03/20 Python
Python多线程及其基本使用方法实例分析
2019/10/29 Python
Flask框架请求钩子与request请求对象用法实例分析
2019/11/07 Python
python实现12306登录并保存cookie的方法示例
2019/12/17 Python
浅谈python中频繁的print到底能浪费多长时间
2020/02/21 Python
Python如何脚本过滤文件中的注释
2020/05/27 Python
python小白学习包管理器pip安装
2020/06/09 Python
使用pyplot.matshow()函数添加绘图标题
2020/06/16 Python
Pycharm 2020.1 版配置优化的详细教程
2020/08/07 Python
解决python 输出到csv 出现多空行的情况
2021/03/24 Python
父亲生日宴会答谢词
2014/01/10 职场文书
新闻报道策划方案
2014/06/11 职场文书
社团个人总结范文
2015/03/05 职场文书
教师“一帮一”结对子活动总结
2015/05/07 职场文书
社区干部培训心得体会
2016/01/06 职场文书
详解Java线程池是如何重复利用空闲线程的
2021/06/26 Java/Android