php井字游戏,python实现井字棋游戏
#本游戏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
以上就是本文的详细内容,希望对大家的学习有所帮助。
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:php中文网
php井字游戏,python实现井字棋游戏相关推荐
- python井字棋游戏代码_python实现井字棋游戏
python实现井字棋游戏 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: python实现井字棋游戏.txt ] (友情提示:右键点上行txt文档名->目标 ...
- python实现井字棋
参考学习:Python实现井字棋游戏 闲扯 井字棋(Tic-Tac-Toe),初高中进行打发时间的一种画x画o的游戏,3*3的格子组成,一方把行列斜行连成相同的就算获胜. 那么怎么利用进行人机对弈这种 ...
- python编写井字棋_编写井字游戏
python编写井字棋 Programming computer games may be the most technically challenging (and possibly the bes ...
- python井字棋ai_[Python100行系列]-井字棋游戏
博客:Hzy的博客 | Hzy Bloghzeyuan.cn一些学习python的小项目,小游戏.python小项目github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...
- python井字棋_[Python100行系列]-井字棋游戏
博客:Hzy的博客 | Hzy Bloghzeyuan.cn一些学习python的小项目,小游戏.python小项目github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...
- Python基础编程案例:简单的井字棋游戏设计与制作
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 前言 python井字棋游戏虽然看上去非常简陋,但是却非常值得学习. 先看怎么玩 ...
- python井字棋游戏大作业实验报告_Part 1.2 - 实现一个井字棋游戏的gym环境
上文已经描述了怎么创建和注册一个自定义的gym环境.但是环境类中的4个函数都是空的,本文将描述怎么实现那4个函数,实现一个完整的井字棋游戏的环境. 游戏规则:两个玩家在3x3的棋盘上,一方执X,一方执 ...
- python写井字棋_python 游戏(井字棋)
1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠. 2. 使用模块和游戏提示 impor ...
- php井字游戏代码_JS实现井字棋游戏步骤详解
这次给大家带来JS实现井字棋游戏步骤详解,JS实现井字棋游戏的注意事项有哪些,下面就是实战案例,一起来看一下. 最近有一门课结束了,需要做一个井字棋的游戏,我用JavaScript写了一个.首先界面应 ...
最新文章
- [2010山东ACM省赛] Greatest Number(数的组合+二分搜索)
- Linux中的top命令查看内存和cpu信息
- 成为一名优秀数据分析师的必经之路
- easyUI 学习网站
- unity webgl读写txt文件_VB 读写txt文件
- 计算机网络之物理层:7、物理层设备(中继器、集线器)
- C++语言基础 —— 控制结构
- psutil python库
- 大数据分析——零售业
- 让自己慢下来(2)-朋友们的回复
- 20211010 PHP笔记
- 4周第4次课 压缩打包介绍 gzip bzip2 xz压缩工具
- Java多线程之JUC包:CountDownLatch源码学习笔记
- 一.微信公众号迁移流程 openid转换
- Serekh简介:新资产包,埋藏的回忆,第2卷
- Steam安装游戏时,无法选择C盘(无法选择其他盘(盘符))
- Just For Fun
- C# MemoryStream
- kali 插耳机没声音
- Unity 游戏的String interning优化
热门文章
- 医学影像阅读/分析软件FSLeyes安装避坑+核磁共振影像数据处理
- GBDT 和 AdaBoost区别?
- Android中 TextView的使用
- Java学习笔记(必看经典)
- 面向对象一类与对象的概念与特性
- 从网页的控制台登录云服务器ECS中的Ubuntu系统
- java 运算顺序 从左到右_java – 表达式与运算符优先级的从左到右的评估.为什么从左到右的评估似乎胜出了?...
- 【文本分类】基于BERT预训练模型的灾害推文分类方法、基于BERT和RNN的新闻文本分类对比
- 【文本分类】Attention Is All You Need
- 计算机上的应用商城,Windows 应用商店帐户将应用安装在多达五台电脑上