-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
executable file
·230 lines (189 loc) · 6.45 KB
/
client.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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
//#include "board_library.h"
#include "list.h"
#include "UI_library.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 3000
int done=0, wait=0;
void recvcolour(int *r, int *g, int *b, int sfd){
read(sfd, r, sizeof(int));
read(sfd, g, sizeof(int));
read(sfd, b, sizeof(int));
}
int fillboard(int sfd){
int x, y, r, g, b;
int i=0;
char vaux[3], colour[14];
card rcvcard;
read(sfd, &i, sizeof(i));
if(i>1){
//read(sfd, colour, sizeof(colour));
//sscanf(colour, "%d %d %d\n", &r, &g, &b);
recvcolour(&r, &g, &b, sfd);
printf("Colour received: %d %d %d\n", r, g, b);
}
while(1){
read(sfd, &rcvcard, sizeof(rcvcard));
printf("x: %d y: %d vaux: %s\n", rcvcard.x, rcvcard.y, rcvcard.v);
x=rcvcard.x;
y=rcvcard.y;
strcpy(vaux, rcvcard.v);
printf("vaux: %s\n", vaux);
/*reaches the end of all cards*/
if(x==-1 && y==-1){
return 1;
}
/*reaches the end of cards belonging to one player
exits to change colour*/
if(x==-2 && y==-2){
/*read(sfd, colour, sizeof(colour));
printf("Colour received: %d %d %d\n", r, g, b);
sscanf(colour, "%d %d %d\n", &r, &g, &b);*/
recvcolour(&r, &g, &b, sfd);
}else{
paint_card(x, y, r, g, b);
write_card(x, y, vaux, 0, 0, 0);
}
}
}
void *listenserver(void *pass){
/*int *sfdpointer=pass;
int sfd=*sfdpointer;*/
int sfd=*(int*) pass;
int r, g, b, x, y, check;
play_response resp;
while(!done){
/*receives play response*/
read(sfd, &resp, sizeof(resp));
/*read(sfd, plays, sizeof(plays));
sscanf(plays, "%d %d %d\n", &r, &g, &b);
printf("colour vector: %s\n", plays);
printf("colour: %d %d %d\n", r, g, b);*/
printf("Resp code: %d\n", resp.code);
if (resp.code!=-4) recvcolour(&r, &g, &b, sfd);
/*switch case response code*/
switch (resp.code) {
case 1:
x=resp.play1[0];
y=resp.play1[1];
paint_card(resp.play1[0], resp.play1[1] , r, g, b);
write_card(resp.play1[0], resp.play1[1], resp.str_play1, 200, 200, 200);
break;
case 3:
printf("Closing the game!\n");
done = 1;
case 2:
paint_card(resp.play1[0], resp.play1[1] , r, g, b);
write_card(resp.play1[0], resp.play1[1], resp.str_play1, 0, 0, 0);
paint_card(resp.play2[0], resp.play2[1] , r, g, b);
write_card(resp.play2[0], resp.play2[1], resp.str_play2, 0, 0, 0);
break;
case -2:
paint_card(resp.play1[0], resp.play1[1] , r, g, b);
write_card(resp.play1[0], resp.play1[1], resp.str_play1, 255, 0, 0);
paint_card(resp.play2[0], resp.play2[1] , r, g, b);
write_card(resp.play2[0], resp.play2[1], resp.str_play2, 255, 0, 0);
sleep(2);
paint_card(resp.play1[0], resp.play1[1], 255, 255, 255);
paint_card(resp.play2[0], resp.play2[1], 255, 255, 255);
break;
case -1:
/*case 2nd card is not pick after 5 seconds*/
//printf("in case -1 x:%d y:%d\n", x, y);
paint_card(resp.play1[0], resp.play1[1], 255, 255, 255);
write_card(resp.play1[0], resp.play1[1], resp.str_play1, 255, 255, 255);
case -4:
wait=1;
read(sfd, &check, sizeof(check));
wait=0;
}
}
//return 0;
pthread_exit(NULL);
}
int main(int argc, char * argv[]){
int dim, end=0, r, score;
/*init events*/
SDL_Event event;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(-1);
}
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(2);
}
/*arguments*/
if(argc<2){
printf("Incorrect arguments!\n");
exit(-1);
}
/*create socket*/
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd==-1){
perror("socket: ");
exit(-1);
}
struct sockaddr_in server_addr;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(PORT);
inet_aton(argv[1], &server_addr.sin_addr);
/*connect*/
if(connect(sfd, (const struct sockaddr *) &server_addr, sizeof(server_addr))==-1){
printf("Error connecting!\n");
exit(-1);
}
printf("Connected!\n");
/*read dimension and create board*/
read(sfd, &dim, sizeof(dim));
printf("dim= %d\n", dim);
create_board_window(300, 300, dim);
r=fillboard(sfd);
pthread_t lst_thread;
/*thread to receive messages from server*/
/*passing arguments to the thread*/
int *pass;
pass=malloc(sizeof(int));
*pass=sfd;
//pthread_create(&lst_thread, NULL, listenserver, pass);
while(!end){
pthread_create(&lst_thread, NULL, listenserver, pass);
while(!done){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT: {
done=1;//SDL_TRUE;
end=1;//SDL_TRUE;
printf("Leaving the game!\n");
break;
}
case SDL_MOUSEBUTTONDOWN:{
/*gets the x and y from button and sends to server*/
int board_x, board_y;
get_board_card(event.button.x, event.button.y, &board_x, &board_y);
if(wait!=1)write(sfd, &board_x, sizeof(board_x));
if(wait!=1)write(sfd, &board_y, sizeof(board_y));
}
}
}
}
if(end!=1){
read(sfd, &score, sizeof(score));
if(score!=0) printf("WINNER SCORE: %d\n", score);
else printf("GAME OVER!\n");
done=0;
close_board_windows();
sleep(10);
create_board_window(300, 300, dim);
}
}
printf("fim\n");
close_board_windows();
close(sfd);
free(pass);
}