用C语言写一个联机跳棋的代码
时间: 2023-12-29 22:50:59 浏览: 40
这是一个使用C语言实现的简单联机跳棋游戏的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BOARD_SIZE 8
#define MOVE_BUF_SIZE 256
typedef struct {
int x, y;
} Position;
Position create_position(int x, int y) {
Position pos;
pos.x = x;
pos.y = y;
return pos;
}
typedef struct {
Position start, end;
} Move;
Move create_move(Position start, Position end) {
Move move;
move.start = start;
move.end = end;
return move;
}
typedef enum {
EMPTY = 0,
BLACK = 1,
WHITE = 2
} Piece;
typedef struct {
Piece pieces[BOARD_SIZE][BOARD_SIZE];
} Board;
Board create_board() {
Board board;
memset(&board, 0, sizeof(Board));
// 初始化棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if ((i + j) % 2 == 1) {
if (i < 3) {
board.pieces[i][j] = BLACK;
} else if (i > 4) {
board.pieces[i][j] = WHITE;
}
}
}
}
return board;
}
void print_board(Board board) {
printf(" ");
for (int j = 0; j < BOARD_SIZE; j++) {
printf("%c ", 'a' + j);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i + 1);
for (int j = 0; j < BOARD_SIZE; j++) {
switch (board.pieces[i][j]) {
case EMPTY:
printf(". ");
break;
case BLACK:
printf("B ");
break;
case WHITE:
printf("W ");
break;
}
}
printf("%d\n", i + 1);
}
printf(" ");
for (int j = 0; j < BOARD_SIZE; j++) {
printf("%c ", 'a' + j);
}
printf("\n");
}
int is_valid_position(Position pos) {
return pos.x >= 0 && pos.x < BOARD_SIZE && pos.y >= 0 && pos.y < BOARD_SIZE;
}
int is_empty_position(Board board, Position pos) {
return is_valid_position(pos) && board.pieces[pos.x][pos.y] == EMPTY;
}
int is_valid_move(Board board, Move move, Piece player) {
Position start = move.start, end = move.end;
if (!is_valid_position(start) || !is_valid_position(end)) {
return 0;
}
if (board.pieces[start.x][start.y] != player) {
return 0;
}
if (!is_empty_position(board, end)) {
return 0;
}
int dx = end.x - start.x;
int dy = end.y - start.y;
if (player == BLACK) {
if (dy != 1 && dy != 2) {
return 0;
}
if (dy == 2) {
int cx = (start.x + end.x) / 2;
int cy = (start.y + end.y) / 2;
if (board.pieces[cx][cy] != WHITE) {
return 0;
}
}
} else {
if (dy != -1 && dy != -2) {
return 0;
}
if (dy == -2) {
int cx = (start.x + end.x) / 2;
int cy = (start.y + end.y) / 2;
if (board.pieces[cx][cy] != BLACK) {
return 0;
}
}
}
if (dx != -1 && dx != 1) {
return 0;
}
return 1;
}
void apply_move(Board *board, Move move) {
board->pieces[move.start.x][move.start.y] = EMPTY;
board->pieces[move.end.x][move.end.y] = board->pieces[move.start.x][move.start.y];
if (abs(move.start.x - move.end.x) == 2) {
int cx = (move.start.x + move.end.x) / 2;
int cy = (move.start.y + move.end.y) / 2;
board->pieces[cx][cy] = EMPTY;
}
}
int is_game_over(Board board) {
int black_count = 0, white_count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board.pieces[i][j] == BLACK || board.pieces[i][j] == WHITE) {
if (board.pieces[i][j] == BLACK) {
black_count++;
} else {
white_count++;
}
if (i + 1 < BOARD_SIZE && j + 1 < BOARD_SIZE) {
if (board.pieces[i][j] == BLACK && board.pieces[i + 1][j + 1] == EMPTY) {
return 0;
}
if (board.pieces[i][j] == WHITE && board.pieces[i - 1][j + 1] == EMPTY) {
return 0;
}
}
if (i + 1 < BOARD_SIZE && j - 1 >= 0) {
if (board.pieces[i][j] == BLACK && board.pieces[i + 1][j - 1] == EMPTY) {
return 0;
}
if (board.pieces[i][j] == WHITE && board.pieces[i - 1][j - 1] == EMPTY) {
return 0;
}
}
}
}
}
if (black_count == 0 || white_count == 0) {
return 1;
}
return 0;
}
void play_game(int sockfd) {
Board board = create_board();
Piece player = BLACK;
char move_buf[MOVE_BUF_SIZE];
while (!is_game_over(board)) {
print_board(board);
printf("Player %c's turn\n", player == BLACK ? 'B' : 'W');
Move move;
while (1) {
printf("Enter move (e.g. a3 b4): ");
fgets(move_buf, MOVE_BUF_SIZE, stdin);
char *start_str = strtok(move_buf, " \n");
char *end_str = strtok(NULL, " \n");
if (!start_str || !end_str) {
printf("Invalid move format\n");
continue;
}
Position start = create_position(start_str[1] - '1', start_str[0] - 'a');
Position end = create_position(end_str[1] - '1', end_str[0] - 'a');
move = create_move(start, end);
if (!is_valid_move(board, move, player)) {
printf("Invalid move\n");
continue;
}
break;
}
apply_move(&board, move);
write(sockfd, &move, sizeof(Move));
read(sockfd, &move, sizeof(Move));
apply_move(&board, move);
player = player == BLACK ? WHITE : BLACK;
}
print_board(board);
printf("Game over\n");
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s server_ip server_port\n", argv[0]);
return 1;
}
const char *server_ip = argv[1];
int server_port = atoi(argv[2]);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return 1;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(server_ip);
server_addr.sin_port = htons(server_port);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
return 1;
}
play_game(sockfd);
close(sockfd);
return 0;
}
```
注意这个代码是一个简单的示例,只实现了跳棋的基本功能,没有考虑优化、安全性等问题。使用时需要在两个端口上分别启动两个程序,才能进行联机对战。