-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
167 lines (145 loc) · 5.04 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
#include <netdb.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define IP "127.0.0.1"
#define PORT 50555
#define SA struct sockaddr
char* game[3][3];
int counter;
int coords[2];
//Function to check for Winner
bool checkGame(){
if((game[0][0] == "X" && game[0][1] == "X" && game[0][2] == "X") || // hor
(game[1][0] == "X" && game[1][1] == "X" && game[1][2] == "X") ||
(game[2][0] == "X" && game[2][1] == "X" && game[2][2] == "X") ||
(game[0][0] == "X" && game[1][0] == "X" && game[2][0] == "X") || // ver
(game[0][1] == "X" && game[1][1] == "X" && game[2][1] == "X") ||
(game[0][2] == "X" && game[1][2] == "X" && game[2][2] == "X") ||
(game[0][0] == "X" && game[1][1] == "X" && game[2][2] == "X") || // diagonal
(game[0][2] == "X" && game[1][1] == "X" && game[2][0] == "X")){
printf("Winner is Client!\n");
return true;
}
else if((game[0][0] == "O" && game[0][1] == "O" && game[0][2] == "O") || // hor
(game[1][0] == "O" && game[1][1] == "O" && game[1][2] == "O") ||
(game[2][0] == "O" && game[2][1] == "O" && game[2][2] == "O") ||
(game[0][0] == "O" && game[1][0] == "O" && game[2][0] == "O") || // ver
(game[0][1] == "O" && game[1][1] == "O" && game[2][1] == "O") ||
(game[0][2] == "O" && game[1][2] == "O" && game[2][2] == "O") ||
(game[0][0] == "O" && game[1][1] == "O" && game[2][2] == "O") || // diagonal
(game[0][2] == "O" && game[1][1] == "O" && game[2][0] == "O")){
printf("Winner is Server!\n");
return true;
}
else if(counter == 9){
printf("It's a Draw!\n");
return true;
}
return false;
}
// Function to print Game board
void printGame(){
printf("\n\n");
printf("\t\t %s | %s | %s \n", game[0][0], game[0][1], game[0][2]);
printf("\t\t--- --- ---\n");
printf("\t\t %s | %s | %s \n", game[1][0], game[1][1], game[1][2]);
printf("\t\t--- --- ---\n");
printf("\t\t %s | %s | %s \n\n", game[2][0], game[2][1], game[2][2]);
}
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
while(1){
printf("Enter the coordinate value to place X or \"exit\" the game: ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
// String to Int Coordinates
coords[0] = atoi(&buff[0]);
coords[1] = atoi(&buff[2]);
// If Client typed "exit", leave while loop
if((strncmp(buff, "exit", 4)) == 0){
break;
}
else if(game[coords[0]][coords[1]] == " " && // If there is space available
coords[0] < 3 && coords[0] > -1 && coords[1] < 3 && coords[1] > -1){ // If it's not out of range
// Send to Server and write into Game array
write(sockfd, buff, sizeof(buff));
game[coords[0]][coords[1]] = "X";
counter++;
break;
}
else
printf("You can't place there!\n");
}
// If Client typed "exit", leave game
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
// Print Game array
printGame();
// End game Winner Check!
if(checkGame()) break;
// Clear Buffer and get Server response
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
// String to Int Coordinates
coords[0] = atoi(&buff[0]);
coords[1] = atoi(&buff[2]);
game[coords[0]][coords[1]] = "O";
counter++;
// Print Game array
printGame();
// End game Winner Check!
if(checkGame()) break;
}
}
int main()
{
// Nothing else works!
game[0][0] = " ";
game[0][1] = " ";
game[0][2] = " ";
game[1][0] = " ";
game[1][1] = " ";
game[1][2] = " ";
game[2][0] = " ";
game[2][1] = " ";
game[2][2] = " ";
// Count to full array
counter = 0;
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(IP);
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}