跳棋c程序代码是什么

时间:2025-01-30 00:13:27 手机游戏

```c

include

include

include

// 定义棋子的枚举类型

enum Piece {

EMPTY,

BLACK,

WHITE

};

// 定义棋盘的大小

const int BOARD_SIZE = 19;

// 定义棋盘,使用二维数组表示

Piece board[BOARD_SIZE][BOARD_SIZE];

// 初始化棋盘

void initBoard() {

for (int i = 0; i < BOARD_SIZE; i++) {

for (int j = 0; j < BOARD_SIZE; j++) {

board[i][j] = EMPTY;

}

}

}

// 设置棋子的位置

void setPiece(int row, int col, Piece piece) {

board[row][col] = piece;

}

// 获取棋子的位置

Piece getPiece(int row, int col) {

return board[row][col];

}

// 打印棋盘

void printBoard() {

for (int i = 0; i < BOARD_SIZE; i++) {

for (int j = 0; j < BOARD_SIZE; j++) {

if (board[i][j] == EMPTY) {

printf(" ");

} else if (board[i][j] == BLACK) {

printf("B");

} else if (board[i][j] == WHITE) {

printf("W");

}

}

printf("\n");

}

}

// 检查移动是否合法

int isMoveValid(int row, int col, int newRow, int newCol) {

// 检查是否越界

if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {

return 0;

}

// 检查是否已经占领

if (board[row][col] != EMPTY && board[row][col] != piece) {

return 0;

}

// 检查对角线移动

if ((row - newRow) * (col - newCol) != 1) {

return 0;

}

return 1;

}

// 移动棋子

void movePiece(int row, int col, int newRow, int newCol) {

if (isMoveValid(row, col, newRow, newCol)) {

setPiece(newRow, newCol, board[row][col]);

setPiece(row, col, EMPTY);

} else {

printf("Invalid move!\n");

}

}

// 主函数

int main() {

srand((unsigned)time(NULL));

initBoard();

int player = BLACK;

int row, col, newRow, newCol;

while (1) {

printBoard();

printf("Player %d, enter your move (row col): ", player);

scanf("%d %d", &row, &col);

newRow = row;

newCol = col;

// 尝试移动棋子

if (player == BLACK) {

printf("Enter new row (0-%d): ", BOARD_SIZE - 1);

scanf("%d", &newRow);

printf("Enter new column (0-%d): ", BOARD_SIZE - 1);

scanf("%d", &newCol);

} else {

printf("Enter new row (0-%d): ", BOARD_SIZE - 1);

scanf("%d", &newRow);

printf("Enter new column (0-%d): ", BOARD_SIZE - 1);

scanf("%d", &newCol);

}

movePiece(row, col, newRow, newCol);

// 切换玩家

player = (player == BLACK) ? WHITE : BLACK;

// 检查游戏是否结束

// 这里可以添加更多的游戏结束条件,例如检查是否有一方获胜

}

return 0;

}

```

这个程序代码实现了一个简单的跳棋