-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
532 lines (477 loc) · 13.2 KB
/
client.cpp
File metadata and controls
532 lines (477 loc) · 13.2 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <iostream>
#include <signal.h>
#include <errno.h>
#include <arpa/inet.h>
#include <cstring>
#include <csignal>
bool myExit = false;
using namespace std;
int SocketClient;
struct game
{
int matrix[8][8];
int matrix_size = 8;
bool setupMatrix(int size)
{
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
matrix[i][j] = 0;
};
//Urmatoarele 3 functii sunt pentru verificarea si aplicarea deciziei jucatorului
bool columnFull(int columnIndex)
{
if (matrix[0][columnIndex] != 0)
return true;
return false;
};
int firstEmptyRow(int columnIndex)
{
for (int i = matrix_size - 1; i >= 0; i--)
if (matrix[i][columnIndex] == 0)
return i;
};
bool addInColumn(int columnIndex, int value)
{
if (columnFull(columnIndex))
return false;
int row = firstEmptyRow(columnIndex);
matrix[row][columnIndex] = value;
return true;
};
//Urmatoarele functii sunt pentru verificarea jocului
bool latitude(int row, int column)
{
int i = column - 1, j = column + 1;
int color = matrix[row][column];
int sum = 1;
while (i >= 0 && matrix[row][i] == color){
sum++;
i--;
}
while (j < matrix_size && matrix[row][j] == color){
sum++;
j++;
}
if (sum >= 4)
return true;
return false;
};
bool longitude(int row, int column)
{
int i = row - 1, j = row + 1;
int color = matrix[row][column];
int sum = 1;
while (i >= 0 && matrix[i][column] == color){
sum++;
i--;
}
while (j < matrix_size && matrix[j][column] == color){
sum++;
j++;
}
if (sum >= 4)
return true;
return false;
};
bool diagonal(int row, int column){
int color = matrix[row][column];
int sum1 = 1;
int i1 = -1, j1 = 1;
while (row + i1 >= 0 && column + i1 >= 0 && matrix[row + i1][column + i1] == color)
{
i1--;
sum1++;
}
while (row + j1 < matrix_size && column + j1 < matrix_size && matrix[row + j1][column + j1] == color)
{
j1++;
sum1++;
}
if (sum1 >= 4)
return true;
int sum2 = 1;
int i2 = -1, j2 = 1;
while (row + i2 >= 0 && column - i2 < matrix_size && matrix[row + i2][column - i2] == color)
{
i2--;
sum2++;
}
while (row + j2 < matrix_size && column - j2 >= 0 && matrix[row + j2][column - j2] == color)
{
j2++;
sum2++;
}
if (sum2 >= 4)
return true;
return false;
};
bool fourInRow(int row, int column)
{
if (matrix[row][column] == 0) return false;
if (diagonal(row, column) == true) return true;
if (latitude(row, column) == true) return true;
if (longitude(row, column) == true) return true;
return false;
};
int endGame()
{
for (int i = 0; i < matrix_size; i++)
for (int j = 0; j < matrix_size; j++)
if (fourInRow(i, j))
return matrix[i][j];
return 0;
};
void printMatrix()
{
for (int i = 0; i < matrix_size; i++){
for (int j = 0; j < matrix_size; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
printf("\n0 1 2 3 4 5 6 7\n");
};
};
struct utilizator
{
bool loged = false;
char username[20];
int fd;
bool setUser(int link)
{
fd = link;
};
bool setName(char name[20])
{
strcpy(username, name);
return true;
};
bool setUp(int fd, char name[20])
{
this->seLogheaza();
this->setName(name);
this->setUser(fd);
return true;
};
int getLink()
{
return fd;
};
char getName()
{
return *username;
};
bool esteLogat()
{
return loged;
};
bool seLogheaza()
{
loged = true;
return true;
};
};
void readAfisareMatrix(int fd)
{
int value;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
read(fd, &value, sizeof(value));
printf("%d ", value);
}
printf("\n");
}
printf("\n0 1 2 3 4 5 6 7\n");
}
void gamePlayerTurn2(int fd,int PlayerIndex){
int turn;
int decizie;
int raspuns;
int myWins;
int enemyWins;
read(fd, &myWins, sizeof(myWins));
read(fd, &enemyWins, sizeof(enemyWins));
do{
read(fd,&turn,sizeof(turn));
printf("Score is %d You | %d Enemy\n\n",myWins,enemyWins);
readAfisareMatrix(fd);
if(turn==PlayerIndex)
{
do{
printf("Choose a column: ");
cin>>decizie;
write(fd,&decizie,sizeof(decizie));
read(fd,&raspuns,sizeof(raspuns));
}while(raspuns==0);
}
else{
read(fd,&decizie,sizeof(decizie));
write(fd,&decizie,sizeof(decizie));
read(fd,&raspuns,sizeof(raspuns));
}
}while(raspuns!=2&&raspuns!=3);
printf("GameOver\n");
printf("Would you like to play again with your enemy ?\n0 for NO, 1 for YES\n");
int playAgain;
int playAgainEnemy;
do{
cin >> playAgain;
} while (playAgain != 0 && playAgain != 1);
write(fd, &playAgain, sizeof(playAgain));
read(fd, &playAgainEnemy, sizeof(playAgainEnemy));
if (playAgain == 1 && playAgainEnemy == 1)
{
write(fd, &playAgain, sizeof(playAgain));
gamePlayerTurn2(fd, PlayerIndex);
}
else
{
int nogame = 0;
write(fd, &nogame, sizeof(nogame));
}
};
void gamePlayerTurn(int fd, int PlayerIndex)
{
printf("[GameStarted]\n");
//initializam runda cu 0 si variabilele ajutatoare
int turn = 0;
int enemyDecizie;
int decizie;
int flag;
int enemyflag;
int winner = 0;
if (PlayerIndex == 0)
{
flag = 1;
enemyflag = 2;
}
else
{
flag = 2;
enemyflag = 1;
}
game Game;
Game.setupMatrix(8);
int myWins;
int enemyWins;
read(fd, &myWins, sizeof(myWins));
read(fd, &enemyWins, sizeof(enemyWins));
while (winner == 0)
{
decizie = -1;
system("clear");
printf("Score: You:%d | Enemy: %d\n", myWins, enemyWins);
printf("Your flag is : %d \n", flag);
printf("Enemy's flag is : %d\n", enemyflag);
Game.printMatrix();
cin.clear();
cin.sync();
fflush(stdin);
//in cazul in care este runda noastra
if (turn == PlayerIndex)
{
do
{
printf("Is your turn!\n");
//Decidem un mesaj
printf("[Your Message]: ");
cin >> decizie;
} while (decizie > 8 || decizie < 0 || Game.columnFull(decizie));
write(fd, &decizie, sizeof(decizie));
Game.addInColumn(decizie, flag);
}
//in cazul in care este runda inamicului
else
{
printf("Is enemys's turn !\n");
printf("[EnemyMessage]: ");
//citim si afisam mesajul inamicului de la server
read(fd, &enemyDecizie, sizeof(enemyDecizie));
printf("%d\n", enemyDecizie);
Game.addInColumn(enemyDecizie, enemyflag);
//anuntam serverul ca am primit mesajul
write(fd, &turn, sizeof(turn));
}
//Next turn
turn = 1 - turn;
winner = Game.endGame();
}
int win;
int endGameServer = 100;
write(fd, &endGameServer, sizeof(endGameServer));
system("clear");
Game.printMatrix();
if (flag == winner)
{
printf("You Won!\n\n");
win = 1;
}
else
{
printf("You Lost!\n\n");
win = 0;
}
printf("Would you like to play again with your enemy ?\n0 for NO, 1 for YES\n");
write(fd, &win, sizeof(win));
int decizie;
int decizieEnemy;
do
{
cin >> decizie;
} while (decizie != 0 && decizie != 1);
write(fd, &decizie, sizeof(decizie));
read(fd, &decizieEnemy, sizeof(decizieEnemy));
printf("Enemy decision:%d\n", decizieEnemy);
if (decizie == 1 && decizieEnemy == 1)
{
write(fd, &decizie, sizeof(decizie));
gamePlayerTurn(fd, PlayerIndex);
}
else
{
int nogame = 0;
write(fd, &nogame, sizeof(nogame));
}
}
bool joinGame(int fd, utilizator &Utilizator)
{
printf("[In Join Function]\n");
//citim numarul de lobbyuri disponibile
int numberOfGames = 0;
read(fd, &numberOfGames, sizeof(numberOfGames));
printf("Number of open lobbys:%d\n", numberOfGames);
//daca nu exista nici un lobby in lista, iesim si reluam dicizia
if (numberOfGames == 0)
{
printf("[Server]No Lobbys\n");
return false;
}
char name[20];
int len;
int index;
//afisam lista de lobbyuri cu indexi ei
for (int i = 0; i < numberOfGames; i++)
{
read(fd, &index, sizeof(index));
read(fd, &len, sizeof(len));
read(fd, &name, len);
name[len] = 0;
printf("%d %s \n", index, name);
}
//alegem un lobby
printf("Decizia este:");
int decizia_mia;
cin >> decizia_mia;
//anuntam serverul in ce lobby dorim sa intram
write(fd, &decizia_mia, sizeof(decizia_mia));
//incepem jocul cu playerul din lobby
int PlayerIndex;
read(fd, &PlayerIndex, sizeof(PlayerIndex));
gamePlayerTurn2(fd, PlayerIndex);
}
bool createGame(int fd, utilizator &Utilizator)
{
int turn;
printf("[In CreateGame Function] ");
printf("Waiting For Server Response\n");
do
{
printf("You want to be first? Press 0 !\n");
printf("You want to be second? Press 1 !\n");
cin >> turn;
} while (turn != 0 && turn != 1);
write(fd, &turn, sizeof(turn));
//asteptam sa fim anuntati de catre player ca intra in
//lobby, pe urma anuntam serverul ca putem incepe jocul
int response = 1;
read(fd, &response, sizeof(response));
printf("response:%d\n", response);
write(fd, &response, sizeof(response));
//incepem jocul cu playerul ce a intrat in lobby
gamePlayerTurn2(fd, turn);
}
// gcc -pthread server.cpp -lstdc++ -o server.o
// gcc -pthread client.cpp -lstdc++ -o client.o
bool decision(int fd, utilizator &Utilizator)
{
if (!Utilizator.esteLogat())
{
char nume[20];
int usedName = 0;
do
{
if (usedName != 0)
printf("Name allready used, try another one\n");
printf("Enter your name: ");
std::cin.getline(nume, 20, '\n');
int len = strlen(nume);
write(fd, &len, sizeof(len));
write(fd, &nume, len);
read(fd, &usedName, sizeof(usedName));
} while (usedName != 0);
Utilizator.setUp(fd, nume);
printf("Your name is :%s \n", Utilizator.username);
}
else
{
int decision = 2;
do
{
printf("If Exit Enter 2\nIf Join Enter 1\nIf CreateGame Enter 0\n");
printf("Your decision is:");
cin >> decision;
} while (decision != 0 && decision != 1 && decision != 2);
write(fd, &decision, sizeof(decision));
if (decision == 1)
{
printf(" ~~> Join!\n");
joinGame(fd, Utilizator);
}
else if (decision == 0)
{
printf(" ~~> CreateGame!\n");
createGame(fd, Utilizator);
}
else if (decision == 2)
{
myExit = true;
}
}
}
void signalHandler(int signal)
{
exit(signal);
}
int main()
{
signal(SIGINT, signalHandler);
int SocketClient = socket(AF_INET, SOCK_STREAM, 0);
if (SocketClient == -1)
{
std::cerr << "[CLIENT]Eroare la socket\n";
return errno;
}
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(2024);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
int test;
if ((test = connect(SocketClient, (struct sockaddr *)&server, sizeof(server))) == -1)
{
std::cerr << "[CLIENT]Eroare la conectare\n";
return errno;
}
char buffer[512];
utilizator User;
while (!myExit)
{
decision(SocketClient, User);
}
}