用C语言实现具有TCP和多线程的21点游戏
时间: 2024-02-23 10:58:56 浏览: 91
好的,我可以给您提供一个简单的实现。首先,您需要了解TCP和多线程的基本概念以及21点游戏的规则。接下来,我们来看一下具体的实现步骤:
1. 创建一个TCP服务器,监听客户端的连接请求;
2. 当有客户端连接时,创建一个新的线程来处理客户端的请求;
3. 在线程中,先发送欢迎消息和游戏规则给客户端;
4. 接下来,循环进行游戏,每轮分为以下几步:
a. 询问客户端是否要继续要牌;
b. 如果客户端要牌,则发一张牌给客户端,并计算当前手牌点数;
c. 如果客户端停止要牌,则计算庄家的手牌点数;
d. 根据点数判断胜负,并将结果发送给客户端;
5. 游戏结束后,关闭连接并退出线程。
下面是一个简单的代码示例,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 9999
#define MAX_CLIENTS 10
int deck[52] = {1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10,1,2,3,4,5,6,7,8,9,10,10,10,10}; // 扑克牌
int deck_count = 52; // 剩余扑克牌数量
struct client_info {
int sock_fd; // 客户端socket
int score; // 手牌点数
};
void send_msg(int sock_fd, const char* msg) {
write(sock_fd, msg, strlen(msg));
}
void* handle_client(void* arg) {
struct client_info* client = (struct client_info*)arg;
// 发送欢迎消息和游戏规则
send_msg(client->sock_fd, "Welcome to 21 points game!\n");
send_msg(client->sock_fd, "The goal of the game is to get as close to 21 as possible without going over.\n");
send_msg(client->sock_fd, "Do you want to start the game? (y/n)\n");
char buf[100];
int recv_len;
// 等待客户端开始游戏
while ((recv_len = read(client->sock_fd, buf, 100)) > 0) {
buf[recv_len] = '\0';
if (strcmp(buf, "y\n") == 0) {
break;
} else if (strcmp(buf, "n\n") == 0) {
close(client->sock_fd);
free(client);
return NULL;
} else {
send_msg(client->sock_fd, "Invalid input. Do you want to start the game? (y/n)\n");
}
}
// 发牌
int player_cards[10] = {0};
int dealer_cards[10] = {0};
int player_count = 0, dealer_count = 0;
player_cards[player_count++] = deck[--deck_count];
dealer_cards[dealer_count++] = deck[--deck_count];
player_cards[player_count++] = deck[--deck_count];
dealer_cards[dealer_count++] = deck[--deck_count];
// 计算初始点数
client->score = 0;
for (int i = 0; i < player_count; i++) {
client->score += player_cards[i];
}
// 发送初始手牌
char msg[100];
sprintf(msg, "Your cards: %d %d\n", player_cards[0], player_cards[1]);
send_msg(client->sock_fd, msg);
sprintf(msg, "Dealer's card: %d\n", dealer_cards[0]);
send_msg(client->sock_fd, msg);
// 循环进行游戏
while (1) {
// 询问是否要牌
send_msg(client->sock_fd, "Do you want another card? (y/n)\n");
while ((recv_len = read(client->sock_fd, buf, 100)) > 0) {
buf[recv_len] = '\0';
if (strcmp(buf, "y\n") == 0) {
player_cards[player_count++] = deck[--deck_count];
client->score += player_cards[player_count - 1];
sprintf(msg, "Your card: %d\n", player_cards[player_count - 1]);
send_msg(client->sock_fd, msg);
break;
} else if (strcmp(buf, "n\n") == 0) {
break;
} else {
send_msg(client->sock_fd, "Invalid input. Do you want another card? (y/n)\n");
}
}
// 判断是否爆牌
if (client->score > 21) {
send_msg(client->sock_fd, "Bust! You lose.\n");
close(client->sock_fd);
free(client);
return NULL;
}
// 庄家继续要牌
while (dealer_count < 5 && dealer_cards[dealer_count - 1] < 17) {
dealer_cards[dealer_count++] = deck[--deck_count];
}
// 计算庄家点数
int dealer_score = 0;
for (int i = 0; i < dealer_count; i++) {
dealer_score += dealer_cards[i];
}
// 判断胜负
if (dealer_score > 21 || client->score > dealer_score) {
send_msg(client->sock_fd, "You win!\n");
} else if (client->score == dealer_score) {
send_msg(client->sock_fd, "Push!\n");
} else {
send_msg(client->sock_fd, "You lose.\n");
}
// 关闭连接
close(client->sock_fd);
free(client);
return NULL;
}
}
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
pthread_t threads[MAX_CLIENTS];
int thread_count = 0;
// 创建服务器socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(1);
}
// 绑定端口并开始监听连接请求
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(1);
}
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(1);
}
// 接受客户端连接并创建线程处理请求
while ((client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addr_len)) > 0) {
struct client_info* client = (struct client_info*)malloc(sizeof(struct client_info));
client->sock_fd = client_fd;
if (pthread_create(&threads[thread_count++], NULL, handle_client, (void*)client) != 0) {
perror("pthread_create failed");
exit(1);
}
if (thread_count >= MAX_CLIENTS) {
break;
}
}
// 等待所有线程结束
for (int i = 0; i < thread_count; i++) {
pthread_join(threads[i], NULL);
}
// 关闭服务器socket
close(server_fd);
return 0;
}
```
这个实现可能还有很多需要改进的地方,比如错误处理、客户端输入的校验、加入多个玩家等等。但是这个示例可以帮助您理解如何使用C语言实现具有TCP和多线程的21点游戏。
阅读全文