用python模拟象棋游戏

咱们直接进入代码正文 :



# 这是一个python实现的象棋游戏
class ChessPiece(object):
    def __init__(self, color, role):
        self.color = color
        self.role = role
        if 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_string
 
    def move(self, start, end):
        if start not in self.pieces:
            print("No piece at the starting position.")
            return
        if end in self.pieces:
            print("There is already a piece at the end position.")
            return
        piece = self.pieces.pop(start)
        self.pieces[end] = piece
        print("Moved {} from {} to {}.".format(piece.role, start, end))
 
    def is_checkmate(self, color):
        king_position = None
        for position in self.pieces:
            if self.pieces[position].role == "king" and self.pieces[position].color == color:
                king_position = position
                break
 
        for position in self.pieces:
            if self.pieces[position].color != color:
                if self.valid_move(position, king_position):
                    return True
        return False
 
    def valid_move(self, start, end):
        if start not in self.pieces:
            return False
        piece = self.pieces[start]
        if end in self.pieces and self.pieces[end].color == piece.color:
            return False
        if 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 True
                elif x_diff == 2 and y_diff == 0 and start[0] == 6:
                    return True
                elif x_diff == 1 and y_diff == 1 and end in self.pieces:
                    return True
            elif piece.color == "black":
                if x_diff == 1 and y_diff == 0:
                    return True
                elif x_diff == 2 and y_diff == 0 and start[0] == 1:
                    return True
                elif x_diff == 1 and y_diff == 1 and end in self.pieces:
                    return True
            return False
        elif 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 True
            elif x_diff == 1 and y_diff == 2:
                return True
            else:
                return False
        elif piece.role == "bishop":
            x_diff = abs(start[0] - end[0])
            y_diff = abs(start[1] - end[1])
            if x_diff == y_diff:
                return True
            else:
                return False
        elif 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 True
            else:
                return False
        elif 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 True
            else:
                return False
        elif 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 True
            else:
                return False
 
    def 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)

希望对你有帮助...

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值