python井字棋游戏代码_python实现井字棋游戏
python实现井字棋游戏
来源:中文源码网 浏览: 次 日期:2018年9月2日
【下载文档: python实现井字棋游戏.txt 】
(友情提示:右键点上行txt文档名->目标另存为)
python实现井字棋游戏 本文实例介绍了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之穿越火线游戏代码_Python 大作业之五子棋游戏(附代码)
Python 大作业--五子棋游戏 姓名:吴欣 学号: 姓名:张雨清 学号: 一 游戏介绍: 我们设计的是五子棋游戏,支持两人一个鼠标对下,黑方用左 键单击,白方用右键单击,谁先下均可,落子无悔,下过 ...
- python塔防小游戏代码_Python制作塔防小游戏
开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 原理介绍 游戏规则简介: 玩家通过建造箭塔抵御敌人的进攻. 每隔一段时间,将会有一波敌人从 ...
- python井字棋小游戏代码_python实现井字棋小游戏
本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前 ...
- python之穿越火线游戏代码_Python实现拼字游戏与代码重构
有位文豪说得好:"看一个作家的水平,不是看他发表了多少文字,而要看他的废纸篓里扔掉了多少." 我觉得同样的理论适用于编程.好的程序员,他们删掉的代码,比留下来的还要多很多.如果你看 ...
- python十点半游戏代码_Python实现Pig Latin小游戏实例代码
前言: 本文研究的主要是Python实现pig Latin小游戏的简单代码,具体介绍如下. Pig Latin是一个语言游戏. 步骤: 1.让用户输入一个英文单词 2.确保用户输入一个有效单词 3.将 ...
- python接水果游戏代码_Python开发接水果小游戏编程
我研发的Python游戏引擎Pylash已经更新到1.4了.现在我们就来使用它完成一个极其简单的小游戏:接水果.以下是游戏截图: vc/yvPy/2NbGyMvO79LGtq+jrMq5yMvO79P ...
- python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏
学了一天pygame,用python和pygame写一个简单的挡板弹球游戏 GitHub: # -*- coding:utf-8 -*- from sys import exit import pyg ...
- python成语游戏代码_Python基础,猜成语小游戏
猜成语 闲的无事,无聊的写bug,突然觉得可以随便写个猜成语小游戏,正好可以解闷 Python随机库,random random是Python的随机库,有这样几个简单的用法 在使用random前要用i ...
- python制作猜拳游戏代码_python实现猜拳游戏项目
本文实例为大家分享了python实现猜拳游戏的具体代码,供大家参考,具体内容如下 项目功能: 1.系统生成随机的石头剪刀布,玩家输入石头剪刀布 2.因为玩家可能会输入shitou st这样的输入,需要 ...
最新文章
- 中秋将至,联合几个号主送出价值500元的中秋大礼包
- FLASHCS3多文件上传源代码(类似uccenter社区)
- Redis使用及工具类
- head first Design Pattern State
- 生成configDataContextRefres失败:Error creating bean with name ‘configDataContextRefresher‘
- 使用JUnitParams进行参数化的JUnit测试
- fx5u以太网通讯设置_操作示例 | 实现S7300和FX5U的数据交换
- C++/C--set常见用法详解【转载】
- 前端新人如何有效地提高自己
- Java+selenium 自动化测试--自动化测试模型介绍
- Ubuntu18.04安装“迅雷“
- C3P0与DBCP数据库连接池的区别,阿里的Druid数据源配置入门
- RNN基本原理以及基于Pytorch实践
- vue项目手机端适配
- 炉石传说 疯狂爆破者空场炸死2个精灵龙的概率
- Java面试题!深度解析跳槽从开始到结束完整流程,吊打面试官
- ubuntu 16.04 成功安装网卡驱动
- 一条SQL语句查询所有任务分数都在60以上的用户名字
- css3实现旋转魔方
- 广西英拓网络,7*24昼夜无差别 提供G空间,首月送产权服务器租用托管!
热门文章
- 1_Java概述_JDK配置
- 银行计算机房应急预案,银行计算机应急预案行计算机应急预案.doc
- 四级作文模板【题目关于私立学校】
- python数据透视表计数去除重复_excel透视表计数去重_Excel数据透视表中的唯一计数...
- python arduino i2c1602_Arduino通过I2C控制1602LCD显示屏
- Simotion之应用故障整理
- Arcsoft人脸识别算法_Camera1、Camera2、CameraX_API的使用
- 提供曲库、评分、修音功能的K歌SDK-Android版本
- flask 的 jsonify 自动排序问题
- Spring Boot上传文件出错,Required request part fileis not present