C++做象棋小游戏代码
时间: 2023-07-31 11:13:36 浏览: 97
以下是一个简单的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;
}
```
该代码实现了基本的象棋规则,包括棋子的移动、棋子的类型和颜色、棋盘的初始化和打印等功能。您可以根据自己的需求进行修改和扩展。