由于涉及到人工智能算法,中国象棋游戏代码较为复杂,以下为一个简单的实现,供参考。

import random

class ChessBoard:
    def __init__(self):
        self.board = [
            ['車', '馬', '象', '士', '将', '士', '象', '馬', '車'],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', '炮', ' ', ' ', ' ', ' ', ' ', '炮', ' '],
            ['卒', ' ', '卒', ' ', '卒', ' ', '卒', ' ', '卒'],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['兵', ' ', '兵', ' ', '兵', ' ', '兵', ' ', '兵'],
            [' ', '砲', ' ', ' ', ' ', ' ', ' ', '砲', ' '],
            [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
            ['車', '馬', '相', '仕', '帅', '仕', '相', '馬', '車'],
        ]
        self.current_player = 'red'

    def __str__(self):
        display_board = ''
        for i in range(len(self.board)):
            display_board += str(9 - i) + ' ' + ' '.join(self.board[i]) + '\n'
        display_board += '  ' + ' '.join(list('abcdefghi'))
        return display_board

    def get_valid_moves(self, x, y):
        piece = self.board[x][y]
        if piece == ' ':
            return []

        valid_moves = []
        if piece == '卒':
            if x >= 5:
                valid_moves.append((x-1, y))
            else:
                valid_moves.append((x-1, y))
                if y > 0:
                    valid_moves.append((x, y-1))
                if y < 8:
                    valid_moves.append((x, y+1))
        elif piece == '兵':
            if x <= 4:
                valid_moves.append((x+1, y))
            else:
                valid_moves.append((x+1, y))
                if y > 0:
                    valid_moves.append((x, y-1))
                if y < 8:
                    valid_moves.append((x, y+1))
        elif piece == '车':
            for i in range(x-1, -1, -1):
                if self.board[i][y] == ' ':
                    valid_moves.append((i, y))
                elif self.board[i][y].islower() != piece.islower():
                    valid_moves.append((i, y))
                    break
                else:
                    break
            for i in range(x+1, 10):
                if self.board[i][y] == ' ':
                    valid_moves.append((i, y))
                elif self.board[i][y].islower() != piece.islower():
                    valid_moves.append((i, y))
                    break
                else:
                    break
            for i in range(y-1, -1, -1):
                if self.board[x][i] == ' ':
                    valid_moves.append((x, i))
                elif self.board[x][i].islower() != piece.islower():
                    valid_moves.append((x, i))
                    break
                else:
                    break
            for i in range(y+1, 9):
                if self.board[x][i] == ' ':
                    valid_moves.append((x, i))
                elif self.board[x][i].islower() != piece.islower():
                    valid_moves.append((x, i))
                    break
                else:
                    break
        elif piece == '炮':
            count = 0
            for i in range(x-1, -1, -1):
                if self.board[i][y] == ' ':
                    valid_moves.append((i, y))
                elif count == 0:
                    count += 1
                elif count == 1 and self.board[i][y].islower() != piece.islower():
                    valid_moves.append((i, y))
                    break
            count = 0
            for i in range(x+1, 10):
                if self.board[i][y] == ' ':
                    valid_moves.append((i, y))
                elif count == 0:
                    count += 1
                elif count == 1 and self.board[i][y].islower() != piece.islower():
                    valid_moves.append((i, y))
                    break
            count = 0
            for i in range(y-1, -1, -1):
                if self.board[x][i] == ' ':
                    valid_moves.append((x, i))
                elif count == 0:
                    count += 1
                elif count == 1 and self.board[x][i].islower() != piece.islower():
                    valid_moves.append((x, i))
                    break
            count = 0
            for i in range(y+1, 9):
                if self.board[x][i] == ' ':
                    valid_moves.append((x, i))
                elif count == 0:
                    count += 1
                elif count == 1 and self.board[x][i].islower() != piece.islower():
                    valid_moves.append((x, i))
                    break
        elif piece == '馬':
            if x > 1 and self.board[x-1][y] == ' ':
                if y > 0 and self.board[x-2][y-1].islower() != piece.islower():
                    valid_moves.append((x-2, y-1))
                if y < 8 and self.board[x-2][y+1].islower() != piece.islower():
                    valid_moves.append((x-2, y+1))
            if x < 8 and self.board[x+1][y] == ' ':
                if y > 0 and self.board[x+2][y-1].islower() != piece.islower():
                    valid_moves.append((x+2, y-1))
                if y < 8 and self.board[x+2][y+1].islower() != piece.islower():
                    valid_moves.append((x+2, y+1))
            if y > 1 and self.board[x][y-1] == ' ':
                if x > 0 and self.board[x-1][y-2].islower() != piece.islower():
                    valid_moves.append((x-1, y-2))
                if x < 9 and self.board[x+1][y-2].islower() != piece.islower():
                    valid_moves.append((x+1, y-2))
            if y < 7 and self.board[x][y+1] == ' ':
                if x > 0 and self.board[x-1][y+2].islower() != piece.islower():
                    valid_moves.append((x-1, y+2))
                if x < 9 and self.board[x+1][y+2].islower() != piece.islower():
                    valid_moves.append((x+1, y+2))
        elif piece == '象' or piece == '相':
            if piece == '象' and x > 4:
                return []
            if piece == '相' and x < 5:
                return []
            if x > 1 and y > 0 and self.board[x-2][y-1] == ' ':
                valid_moves.append((x-2, y-1))
            if x > 1 and y < 8 and self.board[x-2][y+1] == ' ':
                valid_moves.append((x-2, y+1))
            if x < 8 and y > 0 and self.board[x+2][y-1] == ' ':
                valid_moves.append((x+2, y-1))
            if x < 8 and y < 8 and self.board[x+2][y+1] == ' ':
                valid_moves.append((x+2, y+1))
        elif piece == '仕' or piece == '士':
            if x < 7 or x > 9 or y < 3 or y > 5:
                return []
            if x > 7 and y > 3 and self.board[x-1][y-1].islower() != piece.islower():
                valid_moves.append((x-1, y-1))
            if x > 7 and y < 5 and self.board[x-1][y+1].islower() != piece.islower():
                valid_moves.append((x-1, y+1))
            if x < 9 and y > 3 and self.board[x+1][y-1].islower() != piece.islower():
                valid_moves.append((x+1, y-1))
            if x < 9 and y < 5 and self.board[x+1][y+1].islower() != piece.islower():
                valid_moves.append((x+1, y+1))
        elif piece == '将' or piece == '帅':
            if x < 7 or x > 9 or y < 3 or y > 5:
                return []
            if x > 7 and self.board[x-1][y].islower() != piece.islower():
                valid_moves.append((x-1, y))
            if x < 9 and self.board[x+1][y].islower() != piece.islower():
                valid_moves.append((x+1, y))
            if y > 3 and self.board[x][y-1].islower() != piece.islower():
                valid_moves.append((x, y-1))
            if y < 5 and self.board[x][y+1].islower() != piece.islower():
                valid_moves.append((x, y+1))
        return valid_moves

    def move(self, x1, y1, x2, y2):
        piece = self.board[x1][y1]
        if piece == ' ':
            return False
        if piece.islower() == (self.current_player == 'red'):
            return False
        if (x2, y2) not in self.get_valid_moves(x1, y1):
            return False
        self.board[x1][y1] = ' '
        self.board[x2][y2] = piece
        self.current_player = 'black' if self.current_player == 'red' else 'red'
        return True

    def get_winner(self):
        red_piece = []
        black_piece = []
        for i in range(10):
            for j in range(9):
                piece = self.board[i][j]
                if piece != ' ':
                    if piece.islower():
                        black_piece.append(piece)
                    else:
                        red_piece.append(piece)
        if '帅' not in red_piece:
            return 'black'
        if '将' not in black_piece:
            return 'red'
        return None

class Player:
    def __init__(self, color):
        self.color = color

    def get_move(self, board):
        valid_moves = []
        for i in range(10):
            for j in range(9):
                if board.board[i][j] != ' ' and board.board[i][j].islower() == (self.color == 'red'):
                    moves = board.get_valid_moves(i, j)
                    for move in moves:
                        valid_moves.append((i, j, move[0], move[1]))
        if len(valid_moves) == 0:
            return None
        return random.choice(valid_moves)

def main():
    board = ChessBoard()
    player1 = Player('red')
    player2 = Player('black')
    while True:
        print(board)
        winner = board.get_winner()
        if winner is not None:
            print(winner + ' wins')
            break
        if board.current_player == 'red':
            move = player1.get_move(board)
        else:
            move = player2.get_move(board)
        if move is None:
            print('No valid moves')
            break
        if not board.move(*move):
            print('Invalid move')
            break

if __name__ == '__main__':
    main()

该代码实现了一个简单的中国象棋游戏,包括棋盘、玩家、棋子移动等基本功能。其中,ChessBoard类表示棋盘,Player类表示玩家,main函数是主函数。

在主函数中,不断循环直到有一方胜利或者没有有效的移动。在每个回合中,根据当前玩家获得可行的移动,然后进行移动,判断胜利条件等。

python 中国象棋游戏代码

原文地址: https://www.cveoy.top/t/topic/bSIf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录