广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python实现井字棋游戏
  • 930
分享到

python实现井字棋游戏

游戏python井字棋 2022-06-04 19:06:01 930人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下 windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与linux不兼容,无法在Linux下运行。 游戏就

本文实例介绍了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

以上就是本文的详细内容,希望对大家的学习有所帮助。

--结束END--

本文标题: python实现井字棋游戏

本文链接: https://www.lsjlt.com/news/15369.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python实现井字棋游戏
    本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下 windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。 游戏就...
    99+
    2022-06-04
    游戏 python 井字棋
  • JavaScript Canvas实现井字棋游戏
    本文实例为大家分享了JavaScript Canvas实现井字棋游戏的具体代码,供大家参考,具体内容如下 index.html <!DOCTYPE html> &l...
    99+
    2022-11-12
  • python实现简单的井字棋小游戏
    Python做三子棋游戏,这个是我刚开始了解做Python小游戏的时候第一个项目,因为简单好入手,实现它的过程是我开始摸索Python的GUI界面的入门之路。这个设计也都是按照自己对...
    99+
    2022-11-10
  • Python+Tkinter实现经典井字棋小游戏
    目录演示介绍官方文档tkinter.messagebox源码演示 介绍 首先来介绍一下GUI库Tkinter 主要模块: tkinter Main Tkinter module....
    99+
    2022-11-13
  • C语言实现井字棋游戏
    本文实例为大家分享了C语言实现井字棋游戏的具体代码,供大家参考,具体内容如下 首先,我们需要一个大体的思路,先进行宏观规划,再对细节进行实现。 比如: 1、首先需要一个菜单面板作以修...
    99+
    2022-11-12
  • python实现人机对战的井字棋游戏
    本文实例为大家分享了python实现人机对战井字棋的具体代码,供大家参考,具体内容如下 游戏简介:在九宫格内进行,如果一方抢先于另一方向(横、竖、斜)连成3子,则获得胜利。游戏中输入...
    99+
    2022-11-10
  • python实现带界面的井字棋小游戏
    目录1、首先安装tkinter2、初始化窗口3、定义按钮4、检查获胜的条件今天我们用python+tkinter安装带界面的井字棋,效果如图所示。 Tkinter 是 Python...
    99+
    2022-11-12
  • C语言实现井字棋小游戏
    C语言实现简单的“井字棋游戏”,供大家参考,具体内容如下 总体构造: 1.游戏菜单的逻辑实现 2.游戏本体的代码实现 part 1:游戏菜单的整体逻辑 ①简单的通过一个输入0和1的s...
    99+
    2022-11-12
  • python怎么实现简单的井字棋小游戏
    这篇文章主要讲解了“python怎么实现简单的井字棋小游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python怎么实现简单的井字棋小游戏”吧!窗口万能的窗口,实现窗口都可以进行简单的修...
    99+
    2023-06-30
  • python实现井字棋游戏的代码怎么写
    python实现井字棋游戏的代码怎么写,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。井字棋简介井字棋又称三子棋,英文名为Tic Tac Toe。具体玩法为在一个3x3的棋盘上,...
    99+
    2023-06-25
  • 使用python怎么实现一个井字棋游戏
    这篇文章将为大家详细讲解有关使用python怎么实现一个井字棋游戏,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。使用python实现井字棋游戏,没有具体算法,只是用随机下棋简单实现:impo...
    99+
    2023-06-15
  • C语言代码实现井字棋游戏
    井字棋是一个很常见的小游戏。要求对战双方在一个“井”形的棋盘里分别下“*”棋子或者“#”棋子,谁先连成3个,谁就赢。 本次使用C语言来实现这个小游戏。 由于使用函数较多,所以采用多文...
    99+
    2022-11-12
  • 纯CSS3实现井字棋游戏的示例
    这篇文章给大家分享的是有关纯CSS3实现井字棋游戏的示例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。运行效果:html<div class="tic-tac-toe">...
    99+
    2023-06-08
  • C语言实现简易井字棋游戏
    井子棋承载了每个人孩童时的美好时光,小到书本、纸张,大到课桌、墙壁,总能找到井字棋盘的痕迹。今天我们就来实际操作一番,用C语言完成一个简单的井字棋游戏,让我们一起重温美好。 棋盘如下...
    99+
    2022-11-12
  • C语言实现简单井字棋游戏
    本文实例为大家分享了C语言实现简单井字棋游戏的具体代码,供大家参考,具体内容如下 游戏截图 源代码 person.h //玩家对战 void person() { int...
    99+
    2022-11-12
  • 基于C语言实现井字棋游戏
    井字棋游戏要求在3乘3棋盘上,每行都相同或者每列都相同再或者对角线相同,则胜出.因此我们可以使用一个二维数组来表示棋盘,判断胜负只需要判断数组元素是否相同即可.具体我们可以分为以下几...
    99+
    2022-11-12
  • Java Websocket Canvas实现井字棋网络游戏
    本文实例为大家分享了Java Websocket Canvas实现井字棋网络游戏的具体代码,供大家参考,具体内容如下 TicTacToeGame.java  impo...
    99+
    2022-11-12
  • C语言如何实现井字棋游戏
    这篇文章主要介绍了C语言如何实现井字棋游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。C语言是什么C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发,使用...
    99+
    2023-06-14
  • React中井字棋游戏的实现示例
    目录需求分析实现分析涉及的组件涉及的状态编码实现项目初始化定义各个组件的props/stateSquare组件propsBoard组件propsGame组件state各组件代码Squ...
    99+
    2022-11-13
    React 井字棋游戏
  • 微信小程序实现井字棋游戏
    本文实例为大家分享了微信小程序实现井字棋游戏的具体代码,供大家参考,具体内容如下 效果图 .wxml <view class="title">   <view w...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作