帮我写一段c语言程序实现军棋小游戏
时间: 2024-01-24 14:20:05 浏览: 35
好的,下面是一个简单的军棋小游戏的C语言程序:
```c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// 定义棋盘大小
#define ROW 10
#define COL 9
// 定义棋子的种类
#define NONE 0 // 空
#define JIANG 1 // 将
#define SHI 2 // 士
#define XIANG 3 // 相
#define CHE 4 // 车
#define MA 5 // 马
#define PAO 6 // 炮
#define BING 7 // 兵
// 定义棋子颜色
#define RED 0
#define BLACK 1
// 定义棋子结构体
typedef struct chess_piece {
int type; // 棋子种类
int color; // 棋子颜色
} ChessPiece;
// 定义棋盘数组
ChessPiece board[ROW][COL];
// 初始化棋盘
void init_board() {
// 将棋盘清空
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
board[i][j].type = NONE;
board[i][j].color = NONE;
}
}
// 将红方棋子放置在棋盘上
board[0][0].type = CHE;
board[0][0].color = RED;
board[0][1].type = MA;
board[0][1].color = RED;
board[0][2].type = XIANG;
board[0][2].color = RED;
board[0][3].type = SHI;
board[0][3].color = RED;
board[0][4].type = JIANG;
board[0][4].color = RED;
board[0][5].type = SHI;
board[0][5].color = RED;
board[0][6].type = XIANG;
board[0][6].color = RED;
board[0][7].type = MA;
board[0][7].color = RED;
board[0][8].type = CHE;
board[0][8].color = RED;
board[2][1].type = PAO;
board[2][1].color = RED;
board[2][7].type = PAO;
board[2][7].color = RED;
board[3][0].type = BING;
board[3][0].color = RED;
board[3][2].type = BING;
board[3][2].color = RED;
board[3][4].type = BING;
board[3][4].color = RED;
board[3][6].type = BING;
board[3][6].color = RED;
board[3][8].type = BING;
board[3][8].color = RED;
// 将黑方棋子放置在棋盘上
board[9][0].type = CHE;
board[9][0].color = BLACK;
board[9][1].type = MA;
board[9][1].color = BLACK;
board[9][2].type = XIANG;
board[9][2].color = BLACK;
board[9][3].type = SHI;
board[9][3].color = BLACK;
board[9][4].type = JIANG;
board[9][4].color = BLACK;
board[9][5].type = SHI;
board[9][5].color = BLACK;
board[9][6].type = XIANG;
board[9][6].color = BLACK;
board[9][7].type = MA;
board[9][7].color = BLACK;
board[9][8].type = CHE;
board[9][8].color = BLACK;
board[7][1].type = PAO;
board[7][1].color = BLACK;
board[7][7].type = PAO;
board[7][7].color = BLACK;
board[6][0].type = BING;
board[6][0].color = BLACK;
board[6][2].type = BING;
board[6][2].color = BLACK;
board[6][4].type = BING;
board[6][4].color = BLACK;
board[6][6].type = BING;
board[6][6].color = BLACK;
board[6][8].type = BING;
board[6][8].color = BLACK;
}
// 打印棋盘
void print_board() {
printf(" 0 1 2 3 4 5 6 7 8\n");
for (int i = 0; i < ROW; i++) {
printf("%d ", i);
for (int j = 0; j < COL; j++) {
switch (board[i][j].type) {
case NONE:
printf(". ");
break;
case JIANG:
printf("J ");
break;
case SHI:
printf("S ");
break;
case XIANG:
printf("X ");
break;
case CHE:
printf("C ");
break;
case MA:
printf("M ");
break;
case PAO:
printf("P ");
break;
case BING:
printf("B ");
break;
}
}
printf("\n");
}
}
// 判断某个位置是否在棋盘范围内
int is_valid_pos(int row, int col) {
return row >= 0 && row < ROW && col >= 0 && col < COL;
}
// 判断两个位置是否相同
int is_same_pos(int row1, int col1, int row2, int col2) {
return row1 == row2 && col1 == col2;
}
// 判断两个位置之间是否有障碍物
int has_obstacle(int row1, int col1, int row2, int col2) {
int d_row = row2 - row1;
int d_col = col2 - col1;
if (d_row == 0) { // 水平方向
int step = d_col > 0 ? 1 : -1;
for (int i = col1 + step; i != col2; i += step) {
if (board[row1][i].type != NONE) {
return 1;
}
}
} else { // 竖直方向
int step = d_row > 0 ? 1 : -1;
for (int i = row1 + step; i != row2; i += step) {
if (board[i][col1].type != NONE) {
return 1;
}
}
}
return 0;
}
// 判断将帅之间是否有障碍物
int has_obstacle_between_jiang(int row1, int col1, int row2, int col2) {
if (col1 != col2) {
return 1;
}
int step = row1 < row2 ? 1 : -1;
for (int i = row1 + step; i != row2; i += step) {
if (board[i][col1].type != NONE) {
return 1;
}
}
return 0;
}
// 判断某个位置是否被对方控制
int is_under_attack(int row, int col, int color) {
// 判断是否被对方将军
int jiang_row, jiang_col;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j].type == JIANG && board[i][j].color != color) {
jiang_row = i;
jiang_col = j;
break;
}
}
}
if (jiang_col == col && has_obstacle_between_jiang(jiang_row, jiang_col, row, col) == 0) {
return 1;
}
// 判断是否被对方车、炮控制
int d_row[4] = {-1, 0, 1, 0};
int d_col[4] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int r = row + d_row[i];
int c = col + d_col[i];
while (is_valid_pos(r, c)) {
if (board[r][c].type != NONE) {
if (board[r][c].color != color && (board[r][c].type == CHE || board[r][c].type == PAO)) {
return 1;
} else {
break;
}
}
r += d_row[i];
c += d_col[i];
}
}
// 判断是否被对方马控制
int horse_d_row[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int horse_d_col[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
for (int i = 0; i < 8; i++) {
int r = row + horse_d_row[i];
int c = col + horse_d_col[i];
if (is_valid_pos(r, c) && board[r][c].type == MA && board[r][c].color != color) {
int r2 = row + horse_d_row[i] / 2;
int c2 = col + horse_d_col[i] / 2;
if (board[r2][c2].type == NONE) {
return 1;
}
}
}
// 判断是否被对方兵、卒控制
if (color == RED) {
if (is_valid_pos(row - 1, col - 1) && board[row - 1][col - 1].type == BING && board[row - 1][col - 1].color != color) {
return 1;
}
if (is_valid_pos(row - 1, col + 1) && board[row - 1][col + 1].type == BING && board[row - 1][col + 1].color != color) {
return 1;
}
} else {
if (is_valid_pos(row + 1, col - 1) && board[row + 1][col - 1].type == BING && board[row + 1][col - 1].color != color) {
return 1;
}
if (is_valid_pos(row + 1, col + 1) && board[row + 1][col + 1].type == BING && board[row + 1][col + 1].color != color) {
return 1;
}
}
return 0;
}
// 判断某个位置是否合法
int is_valid_move(int src_row, int src_col, int dst_row, int dst_col) {
ChessPiece src_piece = board[src_row][src_col];
ChessPiece dst_piece = board[dst_row][dst_col];
// 判断是否移动到了同一位置
if (is_same_pos(src_row, src_col, dst_row, dst_col)) {
return 0;
}
// 判断是否越界
if (!is_valid_pos(src_row, src_col) || !is_valid_pos(dst_row, dst_col)) {
return 0;
}
// 判断是否移动到了自己的棋子上
if (dst_piece.color == src_piece.color) {
return 0;
}
// 判断是否走出了棋子的走法范围
switch (src_piece.type) {
case JIANG:
if (dst_row < 7 || dst_row > 9 || dst_col < 3 || dst_col > 5) {
return 0;
}
if (has_obstacle_between_jiang(src_row, src_col, dst_row, dst_col)) {
return 0;
}
break;
case SHI:
if (dst_row < 7 || dst_row > 9 || dst_col < 3 || dst_col > 5) {
return 0;
}
if (abs(dst_row - src_row) != 1 || abs(dst_col - src_col) != 1) {
return 0;
}
break;
case XIANG:
if (dst_row < 5 || dst_row > 9 || dst_col < 0 || dst_col > 8) {
return 0;
}
if (abs(dst_row - src_row) != 2 || abs(dst_col - src_col) != 2) {
return 0;
}
if (board[(src_row + dst_row) / 2][(src_col + dst_col) / 2].type != NONE) {
return 0;
}
break;
case CHE:
if (src_row == dst_row) {
if (has_obstacle(src_row, src_col, dst_row, dst_col)) {
return 0;
}
} else if (src_col == dst_col) {
if (has_obstacle(src_row, src_col, dst_row, dst_col)) {
return 0;
}
} else {
return 0;
}
break;
case MA:
if (abs(dst_row - src_row) == 2 && abs(dst_col - src_col) == 1) {
if (board[src_row + (dst_row - src_row) / 2][src_col].type != NONE) {
return 0;
}
} else if (abs(dst_row - src_row) == 1 && abs(dst_col - src_col) == 2) {
if (board[src_row][src_col + (dst_col - src_col) / 2].type != NONE) {
return 0;
}
} else {
return 0;
}
break;
case PAO:
if (src_row == dst_row) {
if (has_obstacle(src_row, src_col, dst_row, dst_col) != 1) {
return 0;
}
} else if (src_col == dst_col) {
if (has_obstacle(src_row, src_col, dst_row, dst_col) != 1) {
return 0;
}
} else {
return 0;
}
if (dst_piece.type == NONE && has_obstacle(src_row, src_col, dst_row, dst_col) != 1) {
return 0;
}
if (dst_piece.type != NONE && has_obstacle(src_row, src_col, dst_row, dst_col) != 2) {
return 0;
}
break;
case BING:
if (src_piece.color == RED) {
if (dst_row < src_row) {
return 0;
}
if (src_row < 5 && dst_row == src_row) {
return 0;
}
if (dst_row - src_row + abs(dst_col - src_col) != 1) {
return 0;
}
} else {
if (dst_row > src_row) {
return 0;
}
if (src_row > 4 && dst_row == src_row) {
return 0;
}
if (src_row - dst_row + abs(dst_col - src_col) != 1) {
return 0;
}
}
break;
default:
return 0;
}
// 判断是否被对方控制
if (is_under_attack(src_row, src_col, 1 - src_piece.color)) {
return 0;
}
return 1;
}
// 将棋子从一处位置移动到另一处位置
void move_piece(int src_row, int src_col, int dst_row, int dst_col) {
board[dst_row][dst_col] = board[src_row][src_col];
board[src_row][src_col].type = NONE;
board[src_row][src_col].color = NONE;
}
// 判断游戏是否结束
int game_over() {
int red_jiang_count = 0;
int black_jiang_count = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j].type == JIANG) {
if (board[i][j].color == RED) {
red_jiang_count++;
} else {
black_jiang_count++;
}
}
}
}
if (red_jiang_count == 0) {
printf("黑方胜利!\n");
return 1;
} else if (black_jiang_count == 0) {
printf("红方胜利!\n");
return 1;
}
return 0;
}
int main() {
init_board();
print_board();
while (1) {
// 红方走棋
printf("红方走棋:");
int src_row, src_col, dst_row, dst_col;
while (1) {
scanf("%d %d %d %d", &src_row, &src_col, &dst_row, &dst_col);
if (is_valid_move(src_row, src_col, dst_row, dst_col)) {
break;