-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
253 lines (184 loc) · 6.58 KB
/
player.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
#include "player.h"
main () {
// get a file descriptor that identifies the server
int clientFd = establish_client_file_descriptor();
// set up some local variables
enum Signal signal;
char affirm = 'r'; // r = response.
int sentinel = 1;
int length;
int confirm;
char buffer[256];
// show the splash page to new users
display_title();
// the run loop: this will continue until the server sends HANG_UP
while(sentinel) {
// wait for instructions from the server
signal = (enum Signal)wait_for_signal(clientFd);
switch(signal) {
// the server says: hang up!
case HANG_UP :
// we write to let server know we got the message
write(clientFd, &affirm, sizeof(char));
sentinel = 0; // takes us out of the loop
break;
// the server says: output the next string
case DISPLAY_OUTPUT :
// get the length of incoming input and set up a string
read(clientFd, &length, sizeof(int));
char * message = malloc(length);
read(clientFd, message, length);
printf(" %s\n", message); // display the message
// we write to let server know we got the message
write(clientFd, &affirm, sizeof(char));
free(message);
break;
// the server says: send me input from the user
case GET_SYMBOL :
display_choices(); // shwo the menu
selection = get_integer_in_range(1,3);
selection--; // the symbol array is 0 indexed
write(clientFd, &selection, sizeof(int));
break;
case GET_STRING :
// get a string from the user and malloc enough memory to keep it
get_string(buffer, 256, stdin);
message = malloc(strlen(buffer));
strcpy(message, buffer);
// first send the length, then the message
length = strlen(message) + 1;
write(clientFd, &length, sizeof(int));
write(clientFd, message, length);
free(message);
break;
case CONFIRM :
// get confirmation of something from the user
confirm = user_confirms();
write(clientFd, &confirm, sizeof(int));
break;
default :
break;
}
}
// we are finished now
return 0;
}
int get_text_of_given_length (char * buffer, int max_length, FILE * stream) {
int i = 0;
// read one character at a time until they are ALL GONE
while (true) {
// stop prematurely if we get less
int ch = fgetc(stream);
if (ch == '\n' || ch == EOF) {
break;
}
// if there are more character on the stream we ignore them
if (i < max_length - 1) {
buffer[i++] = ch;
}
}
// get that NULL in there
buffer[i] = '\0';
return 1; // return true for boolean
}
int user_confirms() {
char choice;
// just keep getting char until the user says y or n
while (choice != 'y' && choice != 'n') {
int ch = fgetc(stdin);
choice = (char)ch;
}
putc(choice, stdin);
return (choice == 'y'); // returns boolean
}
int get_integer_in_range(int min, int max) {
int number = -1; // maybe this should be int min
// keep trying to get an int in range until we have it
while( true ) {
if ( get_an_integer(&number) ) {
if (integer_is_in_range(number, min, max) ) {
break;
}
// chide the user on fail
printf("%2s... %d\n", "Input was not in range", number);
printf("%2s ", "Enter your selection:");
}
}
return number;
}
int get_an_integer(int * number) {
char buff[13];
// if we succesfully read from stdin
if ( fgets(buff, sizeof buff, stdin) ) {
// return true if sscanf read one integer
return sscanf(buff, "%d", number) == 1;
}
return 0;
}
// positive integers please
int integer_is_in_range(int number, int min, int max) {
return (min <= number && number <= max);
}
void get_string(char * buffer, int length, FILE * stream) {
get_text_of_given_length(buffer, length, stream);
}
void display_choices() {
printf("\n");
int i;
for(i = 0; i < CARDINALITY; i++) {
printf(" %d. %s\n", i + 1, SYMBOLS[i]);
}
printf("\n");
fflush(stdout);
}
void display_title() {
// OH BABY! (^0^)//
system("clear");
printf(" ***** *** ***** ** ******* \n");
printf(" ****** * ** ****** **** * *** \n");
printf(" ** * * ** ** * * *** * ** \n");
printf("* * * ** * * * *** ** * \n");
printf(" * * * * * ** *** \n");
printf(" ** ** * ** ** ** ** *** \n");
printf(" ** ** * ** ** ** *** *** \n");
printf(" ** **** **** ** * *** *** \n");
printf(" ** ** *** * *** ** * *** *** \n");
printf(" ** ** ** ** ******* ** *** \n");
printf(" * ** ** ** ****** ** ** \n");
printf(" * ** ** ** * * \n");
printf(" **** *** ** ** *** * \n");
printf(" * **** ** ** ** * ********* \n");
printf("* ** * ** ** ** * ***** \n");
printf("* *** * * * \n");
printf(" ** *** * ** \n");
printf(" ****** .--. . \n");
printf(" *** : : | o \n");
printf(" | |.--. | . .--. .-. \n");
printf(" : ;| | | | | |(.-' \n");
printf(" `--' ' `-`--' `-' `-`--'\n");
printf(" Welcome to the Paper, Scissors and Rock game.\n");
fflush(stdout);
}
int establish_client_file_descriptor() {
int clientFd, serverLen, result;
struct sockaddr_un serverUNIXAddress;
struct sockaddr* serverSockAddrPtr;
serverSockAddrPtr = (struct sockaddr*) &serverUNIXAddress;
serverLen = sizeof (serverUNIXAddress);
/* Create a UNIX socket, bidirectional, default protocol */
clientFd = socket (AF_UNIX, SOCK_STREAM, 0);
serverUNIXAddress.sun_family = AF_UNIX; /*Server domain */
strcpy (serverUNIXAddress.sun_path,"recipe");/*Server name*/
do /* Loop until a connection is made with the server */
{
result = connect (clientFd, serverSockAddrPtr, serverLen);
if (result == -1) sleep (1); /* Wait and then try again */
}
while (result == -1);
return clientFd;
}
int wait_for_signal(int Fd) {
int signal;
read(Fd, &signal, sizeof(int)); // client will wait until something comes
return signal;
}