简单的C++代码实现象棋游戏

//以下只是基础代码,不是全部

cpp#include <iostream>

#include <string>

 

using namespace std;

 

// 定义棋子类型

enum PieceType {

    EMPTY, // 空位

    PAWN, // 兵

    KNIGHT, // 马

    ROOK, // 车

    BISHOP, // 象

    QUEEN, // 后

    KING // 王

};

 

// 定义棋子结构体

struct Piece {

    PieceType type; // 棋子类型

    char symbol; // 棋子符号(用于输出)

};

 

// 定义棋盘结构体

struct Board {

    Piece** board; // 棋盘数组,二维数组,每个元素是一个Piece结构体指针,指向对应位置的棋子

    int size; // 棋盘大小,8x8

};

 

// 初始化棋盘

Board initBoard() {

    Board board;

    board.size = 8;

    board.board = new Piece*[board.size];

    for (int i = 0; i < board.size; i++) {

        board.board[i] = new Piece[board.size];

        for (int j = 0; j < board.size; j++) {

            board.board[i][j].type = EMPTY;

            board.board[i][j].symbol = '-';

        }

    }

    return board;

}

 

// 输出棋盘状态

void printBoard(Board board) {

    for (int i = 0; i < board.size; i++) {

        for (int j = 0; j < board.size; j++) {

            cout << board.board[i][j].symbol << " "; // 输出棋子符号(用于输出)

        }

        cout << endl; // 换行

    }

}

 

// 在棋盘上移动棋子(假设只支持向前移动)

void move(Board& board, int fromRow, int fromCol, int toRow, int toCol) {

    // 检查是否越界、是否为有效位置、是否为同一类型的棋子等,此处省略,只简单移动棋子位置

    board.board[toRow][toCol].type = board.board[fromRow][fromCol].type; // 将原位置的棋子移动到目标位置,并更新目标位置的棋子类型和符号(用于输出)

    board.board[fromRow][fromCol].type = EMPTY; // 将原位置的棋子清空,变成空位(用于输出)

}

 

int main() {

    // 初始化棋盘并输出初始状态

    Board board = initBoard();

    printBoard(board);

    // 在第2行第2列的兵向前移动3个格子(假设只能向前移动)

    move(board, 1, 1, 4, 1); // 注意:从第2行第2列移动到第4行第1列,需要先修改目标位置的行和列号,再调用move函数移动棋子,这里先修改目标位置的行号和列号是为了方便演示,实际使用时需要根据具体情况修改参数顺序和值。

    printBoard(board); // 输出移动后的状态,此处省略输出结果,请自行运行代码查看

    return 0;

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++象棋游戏代码示例: ``` #include <iostream> #include <string> #include <vector> using namespace std; // 定义棋子类型 enum PieceType { EMPTY, KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN }; // 定义棋子颜色 enum PieceColor { BLACK, WHITE }; // 定义棋子结构体 struct Piece { PieceType type; PieceColor color; }; // 定义棋盘类 class Board { public: Board() { // 初始化棋盘 pieces.resize(8, vector<Piece>(8)); for (int i = 0; i < 8; i++) { pieces[1][i] = { PAWN, BLACK }; pieces[6][i] = { PAWN, WHITE }; } pieces[0][0] = pieces[0][7] = { ROOK, BLACK }; pieces[7][0] = pieces[7][7] = { ROOK, WHITE }; pieces[0][1] = pieces[0][6] = { KNIGHT, BLACK }; pieces[7][1] = pieces[7][6] = { KNIGHT, WHITE }; pieces[0][2] = pieces[0][5] = { BISHOP, BLACK }; pieces[7][2] = pieces[7][5] = { BISHOP, WHITE }; pieces[0][3] = { QUEEN, BLACK }; pieces[7][3] = { QUEEN, WHITE }; pieces[0][4] = { KING, BLACK }; pieces[7][4] = { KING, WHITE }; } // 打印棋盘 void print() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { Piece p = pieces[i][j]; if (p.type == EMPTY) { cout << " "; } else { string color = (p.color == BLACK ? "B" : "W"); string type; switch (p.type) { case KING: type = "K"; break; case QUEEN: type = "Q"; break; case ROOK: type = "R"; break; case BISHOP: type = "B"; break; case KNIGHT: type = "N"; break; case PAWN: type = "P"; break; } cout << color << type; } cout << " "; } cout << endl; } } // 移动棋子 bool move(int from_row, int from_col, int to_row, int to_col) { Piece from = pieces[from_row][from_col]; Piece to = pieces[to_row][to_col]; if (from.type == EMPTY) { return false; } if (from.color == to.color) { return false; } switch (from.type) { case KING: if (abs(from_row - to_row) <= 1 && abs(from_col - to_col) <= 1) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } break; case QUEEN: if ((from_row == to_row || from_col == to_col || abs(from_row - to_row) == abs(from_col - to_col)) && isPathClear(from_row, from_col, to_row, to_col)) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } break; case ROOK: if ((from_row == to_row || from_col == to_col) && isPathClear(from_row, from_col, to_row, to_col)) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } break; case BISHOP: if (abs(from_row - to_row) == abs(from_col - to_col) && isPathClear(from_row, from_col, to_row, to_col)) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } break; case KNIGHT: if ((abs(from_row - to_row) == 2 && abs(from_col - to_col) == 1) || (abs(from_row - to_row) == 1 && abs(from_col - to_col) == 2)) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } break; case PAWN: if (from.color == BLACK) { if (from_col == to_col && to_row == from_row + 1 && to.type == EMPTY) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } else if (from_col == to_col && to_row == from_row + 2 && from_row == 1 && to.type == EMPTY && pieces[from_row + 1][from_col].type == EMPTY) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } else if (abs(from_col - to_col) == 1 && to_row == from_row + 1 && to.type != EMPTY && to.color == WHITE) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, BLACK }; return true; } } else { if (from_col == to_col && to_row == from_row - 1 && to.type == EMPTY) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, WHITE }; return true; } else if (from_col == to_col && to_row == from_row - 2 && from_row == 6 && to.type == EMPTY && pieces[from_row - 1][from_col].type == EMPTY) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, WHITE }; return true; } else if (abs(from_col - to_col) == 1 && to_row == from_row - 1 && to.type != EMPTY && to.color == BLACK) { pieces[to_row][to_col] = from; pieces[from_row][from_col] = { EMPTY, WHITE }; return true; } } break; } return false; } private: vector<vector<Piece>> pieces; // 判断路径是否通畅 bool isPathClear(int from_row, int from_col, int to_row, int to_col) { if (from_row == to_row) { int start_col = min(from_col, to_col); int end_col = max(from_col, to_col); for (int i = start_col + 1; i < end_col; i++) { if (pieces[from_row][i].type != EMPTY) { return false; } } } else if (from_col == to_col) { int start_row = min(from_row, to_row); int end_row = max(from_row, to_row); for (int i = start_row + 1; i < end_row; i++) { if (pieces[i][from_col].type != EMPTY) { return false; } } } else if (abs(from_row - to_row) == abs(from_col - to_col)) { int start_row = min(from_row, to_row); int end_row = max(from_row, to_row); int start_col = min(from_col, to_col); int end_col = max(from_col, to_col); for (int i = 1; i < end_row - start_row; i++) { if (pieces[start_row + i][start_col + i].type != EMPTY) { return false; } } } else { return false; } return true; } }; int main() { Board board; board.print(); while (true) { int from_row, from_col, to_row, to_col; cout << "Enter move (from_row from_col to_row to_col): "; cin >> from_row >> from_col >> to_row >> to_col; if (board.move(from_row, from_col, to_row, to_col)) { board.print(); } else { cout << "Invalid move!" << endl; } } return 0; } ``` 该代码实现了基本的象棋规则,包括棋子的移动、棋子的类型和颜色、棋盘的初始化和打印等功能。您可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值