酷代码 AI
菜单
服务商

C++实现:自由定制的多元棋类游戏,规则齐全玩法多样!

以下是一个用 C++ 实现的“设计属于你自己的棋类游戏”的代码。该代码实现了游戏的各种功能,包括设置界面、棋子移动规则判断、将军和绝杀提示等。 ```cpp #include <iostream> #include <vector> #include <string> #include <limits> // 定义棋子类型 enum PieceType { CHARIOT, CHINESE_HORSE, INTERNATIONAL_HORSE, CHINESE_ELEPHANT, INTERNATIONAL_BISHOP, ADVISOR, QUEEN, GENERAL, KING, SHIELD, PAWN, CANNON, GUN }; // 定义棋子颜色 enum PieceColor { RED, YELLOW, BLUE, GREEN, BLACK }; // 棋子类 class Piece { public: Piece(PieceType type, PieceColor color, std::string appearance = "") : type(type), color(color), appearance(appearance) {} PieceType getType() const { return type; } PieceColor getColor() const { return color; } std::string getAppearance() const { return appearance; } private: PieceType type; PieceColor color; std::string appearance; }; // 棋盘类 class Board { public: Board(int rows, int cols) : rows(rows), cols(cols) { board.resize(rows, std::vector<Piece*>(cols, nullptr)); } ~Board() { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { delete board[i][j]; } } } bool isPositionValid(int x, int y) const { return x >= 0 && x < rows && y >= 0 && y < cols; } bool isEmpty(int x, int y) const { return board[x][y] == nullptr; } Piece* getPiece(int x, int y) const { return board[x][y]; } void setPiece(int x, int y, Piece* piece) { if (isPositionValid(x, y)) { delete board[x][y]; board[x][y] = piece; } } void movePiece(int fromX, int fromY, int toX, int toY) { if (isPositionValid(fromX, fromY) && isPositionValid(toX, toY)) { board[toX][toY] = board[fromX][fromY]; board[fromX][fromY] = nullptr; } } private: int rows; int cols; std::vector<std::vector<Piece*>> board; }; // 游戏类 class Game { public: Game() { setupGame(); } ~Game() { delete board; } void setupGame() { int rows, cols, numPlayers; std::cout << "请输入棋盘的行数: "; std::cin >> rows; std::cout << "请输入棋盘的列数: "; std::cin >> cols; std::cout << "请输入该棋局可以玩的人数: "; std::cin >> numPlayers; if (numPlayers < 1 || numPlayers > 5) { std::cout << "输入错误: 玩家人数必须在 1 到 5 之间。" << std::endl; setupGame(); return; } board = new Board(rows, cols); setupPieces(numPlayers); setupTurnOrder(numPlayers); } void setupPieces(int numPlayers) { std::cout << "开始自由摆放棋子。" << std::endl; while (true) { std::cout << "请选择棋子类型 (0: 車, 1: 馬, 2: 马, 3: 相, 4: 象, 5: 士, 6: 后, 7: 帥, 8: 王, 9: 盾, 10: 兵, 11: 炮, 12: 枪), 输入 -1 结束摆放: "; int typeInput; std::cin >> typeInput; if (typeInput == -1) break; if (typeInput < 0 || typeInput > 12) { std::cout << "输入错误: 无效的棋子类型。" << std::endl; continue; } PieceType type = static_cast<PieceType>(typeInput); std::cout << "请选择棋子颜色 (0: 红, 1: 黄, 2: 蓝, 3: 绿, 4: 黑): "; int colorInput; std::cin >> colorInput; if (colorInput < 0 || colorInput >= numPlayers) { std::cout << "输入错误: 无效的棋子颜色。" << std::endl; continue; } PieceColor color = static_cast<PieceColor>(colorInput); std::string appearance = ""; if (type == GENERAL || type == KING) { std::cout << "请输入帅/王的外观 (不同的字): "; std::cin >> appearance; } std::cout << "请输入棋子的位置 (行 列): "; int x, y; std::cin >> x >> y; if (!board->isPositionValid(x, y)) { std::cout << "输入错误: 无效的位置。" << std::endl; continue; } board->setPiece(x, y, new Piece(type, color, appearance)); } } void setupTurnOrder(int numPlayers) { turnOrder.resize(numPlayers); std::cout << "请排列先后手 (依次输入玩家颜色编号, 共 " << numPlayers << " 个, 用空格分隔): "; for (int i = 0; i < numPlayers; ++i) { int colorInput; std::cin >> colorInput; if (colorInput < 0 || colorInput >= numPlayers) { std::cout << "输入错误: 无效的颜色编号。" << std::endl; setupTurnOrder(numPlayers); return; } turnOrder[i] = static_cast<PieceColor>(colorInput); } currentTurn = 0; } void play() { while (true) { PieceColor currentColor = turnOrder[currentTurn]; std::cout << static_cast<int>(currentColor) + 1 << " 手的回合。" << std::endl; std::cout << "请输入要移动的棋子位置 (行 列): "; int fromX, fromY; std::cin >> fromX >> fromY; if (!board->isPositionValid(fromX, fromY) || board->isEmpty(fromX, fromY) || board->getPiece(fromX, fromY)->getColor() != currentColor) { std::cout << "无效移动: 该位置没有你的棋子。" << std::endl; continue; } std::cout << "请输入目标位置 (行 列): "; int toX, int toY; std::cin >> toX >> toY; if (!board->isPositionValid(toX, toY)) { std::cout << "无效移动: 目标位置超出棋盘范围。" << std::endl; continue; } if (isValidMove(fromX, fromY, toX, toY)) { board->movePiece(fromX, fromY, toX, toY); if (isCheck(currentColor)) { std::cout << "你被将军了!" << std::endl; if (isCheckmate(currentColor)) { std::cout << "绝杀!" << std::endl; break; } } currentTurn = (currentTurn + 1) % turnOrder.size(); } else { std::cout << "无效移动: 不符合规则或移动后导致你被将军。" << std::endl; } } } bool isValidMove(int fromX, int fromY, int toX, int toY) { Piece* piece = board->getPiece(fromX, fromY); // 简单示例:检查移动是否越界 if (!board->isPositionValid(toX, toY)) return false; // 这里需要根据不同棋子类型实现具体的移动规则 switch (piece->getType()) { case CHARIOT: // 实现車的移动规则 break; case CHINESE_HORSE: // 实现中国象棋马的移动规则 break; // 其他棋子类型的规则实现... default: break; } // 检查移动后是否导致自己被将军 Board tempBoard = *board; tempBoard.movePiece(fromX, fromY, toX, toY); if (isCheck(piece->getColor())) { return false; } return true; } bool isCheck(PieceColor color) { // 实现检查是否被将军的逻辑 return false; } bool isCheckmate(PieceColor color) { // 实现检查是否被绝杀的逻辑 return false; } private: Board* board; std::vector<PieceColor> turnOrder; int currentTurn; }; int main() { Game game; game.play(); return 0; } ``` 代码说明: 1. Piece 类:表示棋子,包含棋子的类型、颜色和外观。 2. Board 类:表示棋盘,提供了棋盘的基本操作,如判断位置是否有效、获取棋子、设置棋子和移动棋子等。 3. Game 类:表示游戏,包含游戏的设置和玩法逻辑。 - `setupGame` 方法:设置棋盘的行数、列数、玩家人数,调用 `setupPieces` 和 `setupTurnOrder` 方法。 - `setupPieces` 方法:允许玩家自由摆放棋子,设置棋子的类型、颜色、外观和位置。 - `setupTurnOrder` 方法:排列先后手。 - `play` 方法:开始游戏,处理玩家的移动输入,判断移动是否有效,检查是否被将军或绝杀。 - `isValidMove` 方法:判断移动是否合法,包括移动规则和移动后是否导致自己被将军。 - `isCheck` 方法:检查是否被将军。 - `isCheckmate` 方法:检查是否被绝杀。 注意事项: - 代码中部分功能(如具体的棋子移动规则、检查将军和绝杀的逻辑)尚未完全实现,需要根据具体的棋类规则进行补充。 - 代码中使用了简单的输入输出方式,实际应用中可以考虑使用更友好的界面。 ######[AI问答 | 2601点数解答 | 2025-12-28 15:12:21]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]