咱们直接进入代码正文 :

# 这是一个python实现的象棋游戏
class ChessPiece(object):def __init__(self, color, role):self.color = colorself.role = roleif self.color == "black":self.abbreviation = role[0].lower()else:self.abbreviation = role[0].upper()class ChessBoard(object):def __init__(self):self.pieces = {(0, 0): ChessPiece("black", "rook"),(0, 1): ChessPiece("black", "knight"),(0, 2): ChessPiece("black", "bishop"),(0, 3): ChessPiece("black", "queen"),(0, 4): ChessPiece("black", "king"),(0, 5): ChessPiece("black", "bishop"),(0, 6): ChessPiece("black", "knight"),(0, 7): ChessPiece("black", "rook"),(7, 0): ChessPiece("white", "rook"),(7, 1): ChessPiece("white", "knight"),(7, 2): ChessPiece("white", "bishop"),(7, 3): ChessPiece("white", "queen"),(7, 4): ChessPiece("white", "king"),(7, 5): ChessPiece("white", "bishop"),(7, 6): ChessPiece("white", "knight"),(7, 7): ChessPiece("white", "rook"),}for x in range(8):self.pieces[(1, x)] = ChessPiece("black", "pawn")self.pieces[(6, x)] = ChessPiece("white", "pawn")def __str__(self):board_string = ""for row in range(7, -1, -1):board_string += str(row + 1) + " |"for column in range(8):if (row, column) in self.pieces:board_string += self.pieces[(row, column)].abbreviation + "|"else:board_string += " |"board_string += "\n"board_string += "   ---------------\n"board_string += "    A B C D E F G H"return board_stringdef move(self, start, end):if start not in self.pieces:print("No piece at the starting position.")returnif end in self.pieces:print("There is already a piece at the end position.")returnpiece = self.pieces.pop(start)self.pieces[end] = pieceprint("Moved {} from {} to {}.".format(piece.role, start, end))def is_checkmate(self, color):king_position = Nonefor position in self.pieces:if self.pieces[position].role == "king" and self.pieces[position].color == color:king_position = positionbreakfor position in self.pieces:if self.pieces[position].color != color:if self.valid_move(position, king_position):return Truereturn Falsedef valid_move(self, start, end):if start not in self.pieces:return Falsepiece = self.pieces[start]if end in self.pieces and self.pieces[end].color == piece.color:return Falseif piece.role == "pawn":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if piece.color == "white":if x_diff == 1 and y_diff == 0:return Trueelif x_diff == 2 and y_diff == 0 and start[0] == 6:return Trueelif x_diff == 1 and y_diff == 1 and end in self.pieces:return Trueelif piece.color == "black":if x_diff == 1 and y_diff == 0:return Trueelif x_diff == 2 and y_diff == 0 and start[0] == 1:return Trueelif x_diff == 1 and y_diff == 1 and end in self.pieces:return Truereturn Falseelif piece.role == "knight":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if x_diff == 2 and y_diff == 1:return Trueelif x_diff == 1 and y_diff == 2:return Trueelse:return Falseelif piece.role == "bishop":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if x_diff == y_diff:return Trueelse:return Falseelif piece.role == "rook":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if x_diff == 0 or y_diff == 0:return Trueelse:return Falseelif piece.role == "queen":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if x_diff == y_diff or x_diff == 0 or y_diff == 0:return Trueelse:return Falseelif piece.role == "king":x_diff = abs(start[0] - end[0])y_diff = abs(start[1] - end[1])if x_diff <= 1 and y_diff <= 1:return Trueelse:return Falsedef game_over(self):black_in_check = self.is_checkmate("black")white_in_check = self.is_checkmate("white")if black_in_check and white_in_check:print("The game is a draw.")elif black_in_check:print("White wins.")elif white_in_check:print("Black wins.")else:print("The game is still in progress.")chess_board = ChessBoard()
print(chess_board)while not chess_board.game_over():start = input("Enter starting position: ")start = (int(start[1]) - 1, ord(start[0]) - 97)end = input("Enter end position: ")end = (int(end[1]) - 1, ord(end[0]) - 97)chess_board.move(start, end)print(chess_board)

希望对你有帮助...

用python模拟象棋游戏相关推荐

  1. python模拟报数游戏有n个人围成一圈,从0到n-1按顺序编号,从第一个人开始从1到k报数,报到k的人推出圈子,然后圈子缩小,从下个人继续游戏,问最后留下来的是几号?

    python模拟报数游戏 有n个人围成一圈,从0到n-1按顺序编号,从第一个人开始从1到k报数,报到k的人推出圈子,然后圈子缩小,从下个人继续游戏,问最后留下来的是几号? from itertools ...

  2. python模拟报数游戏

    题目:编写程序,模拟报数游戏.有n个人围成一圈,顺序编号,从第一个人开始从1到k(假设k=3)报数,报到k的人退出圈子,然后圈子缩小,从下一个人继续游戏,问最后留下的是原来的第几号. 思路: 初步实现 ...

  3. java通用象棋游戏_在通用国际象棋界面周围模拟GraphQL包装器

    java通用象棋游戏 The Universal Chess Interface (UCI) has been around a long time and used by many chess en ...

  4. 使用Python模拟蒙蒂霍尔悖论游戏

    假设你正参加一个有奖游戏节目,并且有3道门可选:其中一个后面是汽车,另外两个后面是山羊.你选择一个门,比如说1号门,主持人当然知道每个门后面是什么并且打开了另一个门,比如说3号门,后面是一只山羊.然后 ...

  5. 基于Python的人机博弈象棋游戏的设计与实现

    源码获取:https://www.bilibili.com/video/BV1Ne4y1g7dC/ 基于Python的人机博弈象棋游戏的设计与实现

  6. python做的游戏放到微信_【1、 创建一个python的文件,运行后,文字版方式模拟微信游戏“跳一跳?】...

    python脚本学习过程推荐 学习流程: 一:打础 1.找到合适的书籍(推荐Python核心编程2Dive into Python),大致读一次,循环啊判,常用类啊,搞懂(太难的跳过) 2.勤练习py ...

  7. python快速实现简易中国象棋游戏

    游戏所需所有图片资源如下: 游戏完整代码如下(依然主要依赖于pygame库): 如果运行报错就只可能是由于你的项目放置在其它文件夹下或IDE内部原因导致部分图片资源路径问题,将多个pygame.ima ...

  8. 基于python的游戏设计与实现-基于Python的网络中国象棋游戏设计与实现

    基于Python的网络中国象棋游戏设计与实现 摘要中国象棋是一种家喻户晓的棋类游戏,随着互联网时代的到来,人们的娱乐方式也逐渐向PC端和移动端上发展.本文将传统的中国象棋游戏和当下的互联网技术结合作为 ...

  9. 如何用Python开发象棋小游戏

    如何用Python开发象棋小游戏,源代码版本 人生苦短,我用Python! hello 大家好!我是Mark,一个姓马名克的中国人. 最近,我在CSDN当中看到了这一篇文章: 传送门 他详细地讲解了如 ...

最新文章

  1. Heritrix源码分析(六) Heritrix的文件结构分析
  2. Hadoop通过路径和和链接访问HDFS
  3. 用Python让单片机“行动”起来——MicroPython实战入门篇
  4. Java加密算法 AES
  5. maven+tomcat8.0+eclipse远程部署项目
  6. STM32通用定时器(原理、结构体、库函数、定时器中断每秒闪烁一次灯) —— 时钟源、分频值、重装载值
  7. 想成领袖?先瞄准老板身边的位置
  8. SQL基础E-R图画法(一)
  9. xwpython aui 子窗口-Python/wxPython:AUI管理器,防止面板离开Fram
  10. php自带excel,基于php中使用excel的简单介绍_PHP
  11. 【11】MINST数据集的分类与效果验证
  12. 深圳市及各区人才补贴
  13. 大数据培训:Hadoop生态系统圈
  14. Python小项目-烤地瓜
  15. react-router-dom ^6.0.2使用过程中报错 Error: A <Route> is only ever to be used as the child of <Routes>
  16. HEVC学习笔记(二)整体介绍
  17. 如何用java股票量化交易接口读取股票数据?
  18. 人工智能ai写作系统,ai智能写作机器人
  19. ActiveX控件缺失解决方法
  20. 鼠标单击经常被当做双击的问题

热门文章

  1. Java--基本数据类型拓展 转义字符
  2. 网心科技CEO李浩:边缘计算的当下以及未来
  3. 程序员眼里IE浏览器是什么样的
  4. 传统语音增强——最小均方(LMS)自适应滤波算法
  5. SSL虚拟专用网络通信分析
  6. GetModuleHandle(NULL) 和 HINSTANCE的区别
  7. this指向和改变其指向的方法
  8. pythonic风格_函数式编程的pythonic风格
  9. So easy Spring事务回滚机制
  10. 域名解析--详细讲解