python实现井字棋游戏

 更新时间:2020年03月30日 11:57:39   作者:Nick_Aaron  
这篇文章主要为大家详细介绍了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 selenium 三种等待方式解读

    Python selenium 三种等待方式解读

    这篇文章主要介绍了Python selenium一定要会用selenium的等待,三种等待方式解读的相关资料,需要的朋友可以参考下
    2016-09-09
  • Tensorflow中TFRecord生成与读取的实现

    Tensorflow中TFRecord生成与读取的实现

    TFRecord格式的文件存储形式会很合理的帮我们存储数据,本文主要介绍了Tensorflow中TFRecord生成与读取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • python切片的步进、添加、连接简单操作示例

    python切片的步进、添加、连接简单操作示例

    这篇文章主要介绍了python切片的步进、添加、连接简单操作,结合实例形式分析了Python切片运算的常见操作技巧,需要的朋友可以参考下
    2019-07-07
  • Python catplot函数自定义颜色的方法

    Python catplot函数自定义颜色的方法

    catplot() 函数是 Seaborn 中一个非常有用的函数,它可以绘制分类变量的图形,并可以根据另一个或多个变量进行分组,这篇文章主要介绍了Python catplot函数自定义颜色的方法,需要的朋友可以参考下
    2023-03-03
  • Selenium(Python web测试工具)基本用法详解

    Selenium(Python web测试工具)基本用法详解

    这篇文章主要介绍了Selenium(Python web测试工具)基本用法,结合实例形式分析了Selenium的基本安装、简单使用方法及相关操作技巧,需要的朋友可以参考下
    2018-08-08
  • matplotlib中legend位置调整解析

    matplotlib中legend位置调整解析

    这篇文章主要介绍了matplotlib中legend位置调整解析,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • mac下如何将python2.7改为python3

    mac下如何将python2.7改为python3

    这篇文章主要介绍了mac下如何将python2.7改为python3,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • python生成指定长度的随机数密码

    python生成指定长度的随机数密码

    这篇文章主要介绍了python生成指定长度的随机密码示例,密码使用数字和字母组合,大家参考使用吧
    2014-01-01
  • python pexpect ssh 远程登录服务器的方法

    python pexpect ssh 远程登录服务器的方法

    今天小编就为大家分享一篇python pexpect ssh 远程登录服务器的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • 详解Python3 pickle模块用法

    详解Python3 pickle模块用法

    pickle模块中的两个主要函数是dump()和load(),这篇文章主要介绍了Python3 pickle模块用法,需要的朋友可以参考下
    2019-09-09

最新评论