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中设置变量作为默认值时容易遇到的错误
Apr 03 Python
Python实现telnet服务器的方法
Jul 10 Python
python django使用haystack:全文检索的框架(实例讲解)
Sep 27 Python
python的构建工具setup.py的方法使用示例
Oct 23 Python
Python 中包/模块的 `import` 操作代码
Apr 22 Python
python对象与json相互转换的方法
May 07 Python
Opencv实现抠图背景图替换功能
May 21 Python
50行Python代码获取高考志愿信息的实现方法
Jul 23 Python
python 实现查询Neo4j多节点的多层关系
Dec 23 Python
Python图像处理库PIL的ImageEnhance模块使用介绍
Feb 26 Python
Python 循环读取数据内存不足的解决方案
May 25 Python
Python OpenCV超详细讲解读取图像视频和网络摄像头
Apr 02 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 pki加密技术(openssl)详解
2013/07/01 PHP
解析php根据ip查询所在地区(非常有用,赶集网就用到)
2013/07/01 PHP
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
php使用递归计算文件夹大小
2014/12/24 PHP
PHP将二维数组某一个字段相同的数组合并起来的方法
2016/02/26 PHP
分析php://output和php://stdout的区别
2018/05/06 PHP
Yii Framework框架使用PHPExcel组件的方法示例
2019/07/24 PHP
Add Formatted Data to a Spreadsheet
2007/06/12 Javascript
JavaScript 继承的实现
2009/07/09 Javascript
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
js 数据类型转换总结笔记
2011/01/17 Javascript
js给页面加style无效果的解决方法
2014/01/20 Javascript
jquery显示隐藏input对象
2014/07/21 Javascript
AngularJS中update两次出现$promise属性无法识别的解决方法
2017/01/05 Javascript
原生JS与jQuery编写简单选项卡
2017/10/30 jQuery
jQuery 禁止表单用户名、密码自动填充功能
2017/10/30 jQuery
vue + el-form 实现的多层循环表单验证
2020/11/25 Vue.js
[01:12:40]DOTA2-DPC中国联赛 正赛 DLG vs XG BO3 第三场 1月25日
2021/03/11 DOTA
Python中__name__的使用实例
2015/04/14 Python
Python解决N阶台阶走法问题的方法分析
2017/12/28 Python
Python获取当前函数名称方法实例分享
2018/01/18 Python
Selenium 模拟浏览器动态加载页面的实现方法
2018/05/16 Python
python获取中文字符串长度的方法
2018/11/14 Python
python中的RSA加密与解密实例解析
2019/11/18 Python
详解python破解zip文件密码的方法
2020/01/13 Python
pytorch 模拟关系拟合——回归实例
2020/01/14 Python
利用pipenv和pyenv管理多个相互独立的Python虚拟开发环境
2020/11/01 Python
爱游人:Travelliker
2017/09/05 全球购物
三只松鼠官方旗舰店:全网坚果销售第1
2017/11/25 全球购物
美国著名的户外用品品牌:L.L.Bean
2018/01/05 全球购物
有影响力的品牌之家:Our Social Collective
2019/06/08 全球购物
在校生钳工实习自我鉴定
2013/09/19 职场文书
电子信息科学专业自荐信
2014/01/30 职场文书
幼儿园感恩节活动方案2014
2014/10/11 职场文书
管辖权异议上诉状
2015/05/23 职场文书
解决Go gorm踩过的坑
2021/04/30 Golang