Вообщем сделал сетевой морской бой, может кому пригодится. Критика приветствуется
Клиент:
| C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
| #include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
const int N = 10;
int def_port = 55378;
struct cell{
int index;
int fire;
};
struct ship{
int deck;
int alive_deck;
int x, y;
};
struct player{
struct cell map[10][10];
struct ship shp[10];
int hit[10][10];
};
int add_ship(struct player* player, int x, int y){
int index;
if(y && player->map[x][y - 1].index != -1)
{
index = player->map[x][y - 1].index;
player->map[x][y].index = index;
}
else
if(x && player->map[x - 1][y].index != -1)
{
index = player->map[x - 1][y].index;
player->map[x][y].index = index;
}
else
{
for(index = 0; index < N; index++)
if(!player->shp[index].deck)
break;
if(index == 10)
return -1;
player->shp[index].x = x;
player->shp[index].y = y;
player->map[x][y].index = index;
}
player->shp[index].deck++;
player->shp[index].alive_deck++;
}
int g_test(struct player* player,int x, int y){
if(x != 9 && y != 9)
if(player->map[x + 1][y + 1].index != -1)
return -1;
if(x != 9 && y != 0)
if(player->map[x + 1][y - 1].index != -1)
return -1;
if(x != 0 && y != 9)
if(player->map[x - 1][y + 1].index != -1)
return -1;
if(x != 0 && y != 0)
if(player->map[x - 1][y - 1].index != -1)
return -1;
//----------------------------------------------
if(y != 9 && player->map[x][y + 1].index != -1)
return g_test(player, x, y + 1);
if(x != 9 && player->map[x + 1][y].index != -1)
return g_test(player, x + 1, y);
return 0;
}
int init_player(struct player* player){
int i, j, tmp, res;
for(i = 0 ; i < N; i++){
for(j = 0; j < N; j ++){
player->map[i][j].index = -1;
player->map[i][j].fire = 0;
player->hit[i][j] = 0;
}
player->shp[i].deck = 0;
player->shp[i].alive_deck = 0;
}
FILE* file = fopen("map", "r");
if(file == NULL){
printf("Файл map не найден\n");
return 0;
}
for(i = 0 ; i < N; i++) //чтение из файла
for(j = 0; j < N; j ++){
res = fscanf(file,"%d",&tmp);
if(res == EOF || res == 0){
printf("Ошибка чтения из файла\n");
return 0;
}
if(tmp){
res = add_ship(player, i, j);
if(res == -1){
printf("Ошибка расстановки кораблей\n");
return 0;
}
}
}
int ships[4] = {0};
for(i = 0; i < N; i++)
ships[player->shp[i].deck - 1]++;
for(i = 0; i < 4; i++)
if(ships[i] != 4 - i){
printf("Неправильный размер или количество кораблей\n");
return 0;
}
for(i = 0; i < N; i++){
if(g_test(player, player->shp[i].x, player->shp[i].y) == -1){
printf("Нельзя использовать Г-образные корабли\n");
return 0;
}
}
return 1;
}
void draw(struct player player)
{
int i, j;
char c = 'A';
printf(" ");
for(i = 0; i < N; i++)
printf(" %c", c++);
c = 'A';
printf(" ");
for(i = 0; i < N; i++)
printf(" %c", c++);
printf("\n");
for(i = 0; i < N; i++)
{
printf("%d ",i);
for(j = 0; j < N; j ++)
{
if(player.map[i][j].index == -1)
printf("~ ");
else if(player.map[i][j].index == -2)
printf(". ");
else if(player.map[i][j].fire == 0)
printf("# ");
else
printf("x ");
}
printf("%d ",i);
for(j = 0; j < N; j++)
{
if(player.hit[i][j] == 1)
printf(". ");
else if(player.hit[i][j] == 2)
printf("x ");
else
printf("~ ");
}
printf("\n");
}
}
int correct(struct player* player, int* point){
if(point[0] >= 10 || point[1] >= 10 || point[0] < 0 || point[1] < 0){
printf("Неправильные координаты, попробуйте еще раз\n");
return 0;
}
else if(player->hit[point[0]][point[1]] == 2 || player->hit[point[0]][point[1]] == 1){
printf("Клетка уже стреляна, попробуйте еще раз\n");
return 0;
}
return 1;
}
int client_sock_create(int port)
{
struct sockaddr_in addr;
char ip[30];
printf("Введите ip адресс: ");
scanf("%s", ip);
int sock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if(inet_aton(ip, &addr.sin_addr) <= 0){
printf("Неправильный ip адресс\n");
return -1;
}
if(connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0){
printf("Не удалось подключиться\n");
return -1;
}
return sock;
}
void client(struct player* client)
{
int sock, n, turn, res;
int hp, enemy_hp, point[2];
hp = enemy_hp = 10;
sock = client_sock_create(def_port);
if(sock < 0)
return;
system("clear");
printf("Соеденение установленно, поиск оппонента\n");
write(sock, client, sizeof(struct player));
read(sock, &turn, sizeof(int));
system("clear");
printf("Оппонент найден, игра начинается\n");
turn = ntohl(turn);
if(turn)
printf("Вы ходите первым!\n");
else
printf("Противник ходит первым!\n");
draw(*client);
while(hp && enemy_hp)
{
if(turn)
{
printf("Ваш ход!\n");
printf("Введите координаты\n");
while(1){
char str[10];
bzero(str, 10);
scanf("%s", str);
point[1] = str[0] - 65;
point[0] = atoi(str+1);
if(correct(client, point))
break;
}
system("clear");
point[0] = htonl(point[0]);
point[1] = htonl(point[1]);
n = write(sock, point, sizeof(point));
n = read(sock, &res, sizeof(int));
res = ntohl(res);
n = read(sock, client->hit, sizeof(client->hit));
if(n <= 0){
printf("Соеденение разорванно!\n");
return;
}
draw(*client);
if(res == 1)
printf("Попал!\n");
if(res == 2){
printf("Убил!\n");
enemy_hp--;
}
if(res == 0){
printf("Мимо!\nХод противника\n");
turn = 0;
}
}
else{
n = read(sock, point, sizeof(point));
point[0] = ntohl(point[0]);
point[1] = ntohl(point[1]);
n = read(sock, &res, sizeof(int));
res = ntohl(res);
n = read(sock, client->map, sizeof(client->map));
if(n <= 0){
printf("Соеденение разорванно!\n");
return;
}
system("clear");
printf("Враг стреляет по координатам %d %d\n", point[0], point[1]);
draw(*client);
if(res == 2)
hp--;
if(res == 0)
turn = 1;
}
}
if(hp == 0)
printf("Противник победил\n");
else
printf("Вы победили\n");
}
int main(int argc, char* argv[])
{
struct player player;
if(!init_player(&player))
return -1;
client(&player);
return 0;
} |
|
Сервер
| C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
| #include <time.h>
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
const int N = 10;
int def_port = 55378;
int sessions = 2;
struct cell{
int index;
int fire;
};
struct ship{
int deck;
int alive_deck;
int x, y;
};
struct player{
struct cell map[10][10];
struct ship shp[10];
int hit[10][10];
};
void oreol(struct player* player,int x, int y)
{
if(x != 9 && y != 9)
player->hit[x + 1][y + 1] = 1;
if(x != 9 && y != 0)
player->hit[x + 1][y - 1] = 1;
if(x != 0 && y != 9)
player->hit[x - 1][y + 1] = 1;
if(x != 0 && y != 0)
player->hit[x - 1][y - 1] = 1;
//-----------------------------------------
if(x != 0 && player->hit[x - 1][y] == 0)
player->hit[x - 1][y] = 1;
if(x != 9 && player->hit[x + 1][y] == 0)
player->hit[x + 1][y] = 1;
if(y != 0 && player->hit[x][y - 1] == 0)
player->hit[x][y - 1] = 1;
if(y != 9 && player->hit[x][y + 1] == 0)
player->hit[x][y + 1] = 1;
//-----------------------------------------
if(y != 9 && player->hit[x][y + 1] == 2)
oreol(player, x, y + 1);
if(x != 9 && player->hit[x + 1][y] == 2)
oreol(player, x + 1, y);
}
int shot(struct player* player1, struct player* player2, int x, int y)
{
//printf("Выстрел по координатам %d %d\n", x, y);
int index = player2->map[x][y].index;
if(index != -1)
{
player2->map[x][y].fire = 1;
player2->shp[index].alive_deck--;
player1->hit[x][y] = 2;
if(!player2->shp[index].alive_deck){
oreol(player1, player2->shp[index].x, player2->shp[index].y);
return 2;
}
return 1;
}
player1->hit[x][y] = 1;
player2->map[x][y].index = -2;
return 0;
}
void* thread(void* arg)
{
struct player player[2];
int pl[2];
pl[0] = pl[1] = 10;
int turn, not_turn, point[2];
int* fd = (int*) arg;
read(fd[0], &player[0], sizeof(struct player));
read(fd[1], &player[1], sizeof(struct player));
srand(time(NULL));
turn = rand() % 2; //0 - player[0], 1 - player[1]
not_turn = !turn;
int nturn = htonl(turn);
int nnot_turn = htonl(not_turn);
write(fd[0], &nnot_turn, sizeof(int));
write(fd[1], &nturn, sizeof(int));
while(pl[0] && pl[1])
{
if(read(fd[turn], point, sizeof(point)) <= 0){
printf("Соеденение с игроком %d разорванно!\n", turn + 1);
close(fd[0]);
close(fd[1]);
return NULL;
}
point[0] = ntohl(point[0]);
point[1] = ntohl(point[1]);
printf("Игрок %d стреляет по координатам %d %d ",turn + 1, point[0], point[1]);
int res = shot(&player[turn], &player[not_turn], point[0], point[1]);
printf("результат: %d\n", res);
int nres = htonl(res);
write(fd[turn], &nres, sizeof(int));
write(fd[turn], player[turn].hit, sizeof(player[turn].hit));
point[0] = htonl(point[0]);
point[1] = htonl(point[1]);
write(fd[not_turn], point, sizeof(point));
write(fd[not_turn], &nres, sizeof(int));
write(fd[not_turn], player[not_turn].map, sizeof(player[not_turn].map));
if(res == 2)
pl[not_turn]--;
if(res == 0)
turn = !turn;
not_turn = !turn;
}
if(!pl[0])
printf("Победил игрок 2\n");
else
printf("Победил игрок 1\n");
}
int server_sock_create(int port)
{
struct sockaddr_in addr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
return -1;
if(listen(sock, sessions * 2) < 0)
return -1;
printf("Сервер включен\n");
return sock;
}
void server()
{
int sock, clients[2];
sock = server_sock_create(def_port);
if(sock == -1)
return;
pthread_t thr;
while(1){
clients[0] = accept(sock, (struct sockaddr *) NULL, NULL);
clients[1] = accept(sock, (struct sockaddr *) NULL, NULL);
pthread_create(&thr, NULL, thread, clients);
}
}
int main(int argc, char* argv[])
{
server();
return 0;
} |
|
0
|