本文实例介绍了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实现井字棋游戏相关推荐

  1. python套接字编程_Python套接字编程(1)——socket模块与套接字编程

    在Python网络编程系列,我们主要学习以下内容: 5. 常见的Python异步编程框架 6. 协程在Python网络编程中的使用 本文介绍Python下的基本套接字编程,主要基于 socket 模块 ...

  2. python井字棋_python实现井字棋小游戏

    本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前 ...

  3. python大作业数独_python做一个数独小游戏

    最近看了下python的一些知识,在这里记载一下. 1.首先是安装,在官网下载最新的版本3.6,安装的时候要注意在下面勾选上ADD TO PATH,安装的时候会自动写入到环境变量里面,如果没有勾选,可 ...

  4. python画圆形螺旋线_Python写的弹球小游戏

    Python 的功能强大应用广泛,从爬虫到 Web 开发,从科学计算到人工智能,都能见到它的身影.当然,Python 还可以编写游戏代码,虽然不是主流,但却十分有趣,既能提升代码能力,又能自娱自乐.敲 ...

  5. python编程剪刀石头布思路_Python制作简单的剪刀石头布游戏

    关于程序相关的 您可以反复玩游戏,直到选择停止为止. 该程序跟踪获胜情况. 大小写无关紧要(即ROCK与Rock相同). 如果您输入的内容无效,程序会一直提示您,直到您输入有效的内容. 对项目进行编码 ...

  6. python 苹果试玩_python实现吃苹果小游戏

    本文实例为大家分享了python实现吃苹果小游戏的具体代码,供大家参考,具体内容如下 1.公共类模块 import pygame from pygame.rect import Rect def pr ...

  7. python身份证的秘密_Python算法之旅字符串游戏之身份证号的秘密

    最近在力扣(LeetCode)网闲逛,发现很多题目的官方题解都是用Python语言来描述的,这说明大家已经逐渐认识到Python语言描述算法的优越性:它语法简明,内置函数丰富,表述直截了当,可以用最简 ...

  8. python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例

    本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...

  9. 圈叉棋、套娃圈叉棋、嵌套圈叉棋、九个井字棋

    目录 一,圈叉棋(Tic-Tac-Toe) 二,套娃圈叉棋 1,规则 2,解空间分析 1,节点 2,解空间结构 3,复杂度分析 1,总节点数目 2,思路一--关键节点 3,思路二--对称性 4,数据结 ...

  10. python编程示例_Python套接字编程–服务器,客户端示例

    python编程示例 Good Day Learners! In our previous tutorial, we discussed about Python unittest module. T ...

最新文章

  1. tf里面InteractivateSession()与Session()的区别
  2. 华为云 AI 实战营计划,带你迈上 AI 之路
  3. 学java的就业方向_学习Java的就业前景怎么样
  4. ubuntu安装软件
  5. linux之ssh-keygen命令
  6. python两个dataframe求差集_spark计算两个DataFrame的差集、交集、合集
  7. pku2060 Taxi Cab Scheme
  8. #读取json某一节点数据_06596.2.0Hive处理JSON格式数据
  9. visa卡号生成器 在线_作为一名程序员,我都收集了哪些好玩的生成器?
  10. VC2008中影响exe大小和速度的全部编译选项
  11. rtl驱动 ubuntu 禁用_Ubuntu下成功安装台式机网卡realtek的rtl8188eu芯片驱动并实现AP功能...
  12. Uncode、ASCII、UTF-8之前的转换函数
  13. 和月薪5W的华为程序员聊过后,才知道自己一直在打杂...
  14. WPF 海康威视网络摄像头回调方式实现断连提示,降低时延
  15. 用cobar搭建分布式数据库
  16. 15. Perl 正则表达式-正则捕获
  17. unable to access ‘https://sys-gitlab.n.com.cn/IPSC/web/Bumblebee/platform.w/platform.git/‘: SSL cert
  18. 【安全知识分享】PPTX|生产安全事故案例(54页)(附下载)
  19. 【C语言三种自定义类型】
  20. framebuffer驱动全篇

热门文章

  1. The Art Of Code-Beta
  2. MFC对磁盘测速工具CrystalDiskMark6_0_0的工厂方式控制源代码 同时控制 8个窗口,界面同步显示,比较完整点的版本
  3. 《非暴力沟通》阅读总结
  4. Intel连遭打击,未来或回归wintel联盟
  5. Android 设备接入扫码枪,Android 设备接入扫码枪
  6. 想更快成长更应该关注的博客
  7. 最全的期货交易术语在这里
  8. 第7周 项目6—停车场模拟
  9. js中操作cookie
  10. SAP S4 OP/Cloud大乱斗(转载)