-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe_client.c
204 lines (185 loc) · 5.6 KB
/
tictactoe_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
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
#define MAX 100
#define PORT 8080
#define SA struct sockaddr
char buff[MAX];
char sym;
struct timeval tv = { 15 , 0};
fd_set rd;
//Function to get a valid move from the user, return -1 on timeout
int get_valid_move(char* board) {
int r,c;
input :
printf("Enter row , col : ");
fflush(stdout);
FD_ZERO(&rd);
FD_SET(STDIN_FILENO, &rd);
tv.tv_sec = 15;
tv.tv_usec = 0;
if(select(1+STDIN_FILENO, &rd, 0, 0, &tv) > 0)
scanf("%d %d",&r,&c);
else
return -1;
if(r<1 || r>3 || c<1 || c>3) {
printf("\nInvalid move! 1 <= row,col <= 3, try again\n");
goto input;
}
r--;
c--;
int m = r*3 + c;
if(board[m]=='X' || board[m]=='O') {
printf("\nInvalid move! Move must be on an empty cell. Try again\n");
goto input;
}
board[m] = sym;
return m;
}
//Function to render the current board state
void render(char* board) {
for(int i=0;i<9;i++) {
if(board[i]=='*')
board[i]=' ';
}
printf(" %c | %c | %c \n",board[0],board[1],board[2]);
printf("-----------\n");
printf(" %c | %c | %c \n",board[3],board[4],board[5]);
printf("-----------\n");
printf(" %c | %c | %c \n",board[6],board[7],board[8]);
}
void start_game(int sockfd) {
int player_id;
read(sockfd,&player_id,4);
printf("Your player ID is %d\n",player_id);
while(1) {
int check = 0;
//Read messages from server
read(sockfd,buff,sizeof(buff));
if(strncmp(buff,"W",1) == 0) { //Wait for partner request
printf("Waiting for a partner to join . . .\n\n");
}
else if(strncmp(buff,"X",1)==0) { //Symbol X allocation
int opponent_id;
char ch;
sscanf(buff,"%c %d",&ch,&opponent_id);
printf("Your partner’s ID is %d\n",opponent_id);
printf("Your symbol is X\n");
sym = 'X';
}
else if(strncmp(buff,"O",1)==0) { //Symbol O allocation
int opponent_id;
char ch;
sscanf(buff,"%c %d",&ch,&opponent_id);
printf("Your partner’s ID is %d\n",opponent_id);
printf("Your symbol is O\n");
sym = 'O';
}
else if(strncmp(buff,"S",1)==0) { //Start game
printf("Starting game...\n\n");
}
else if(strncmp(buff,"M",1)==0) { //Request to make a move
char board[10];
char ch;
sscanf(buff,"%c %s",&ch,board);
printf("\nYour opponent played : \n");
render(board);
int move = get_valid_move(board);
printf("\nYou played : \n");
render(board);
write(sockfd,&move,4);
}
else if(strncmp(buff,"F",1)==0) { //Request to make the first move
char board[10];
for(int i=0;i<9;i++) board[i]='*';
char ch;
printf("\nYour turn to play : \n");
render(board);
int move = get_valid_move(board);
printf("\nYou played : \n");
render(board);
write(sockfd,&move,4);
}
else if(strncmp(buff,"V",1)==0) { //Victory
printf("\nGame Over. You Won!\n\n");
check=1;
}
else if(strncmp(buff,"D",1)==0) { //Defeat
char ch;
char board[10];
sscanf(buff,"%c %s",&ch,board);
printf("\nYour opponent played : \n");
render(board);
printf("\nGame Over. Defeat :\(\n\n");
check=1;
}
else if(strncmp(buff,"T",1)==0) { //Tie
char ch;
char board[10];
sscanf(buff,"%c %s",&ch,board);
printf("\nFinal state : \n");
render(board);
printf("\nGame Over. Its a tie.\n\n");
check=1;
}
else if(strncmp(buff,"E",1)==0) { //Exit
printf("Session ended. Disconnected from server\n");
close(sockfd);
return;
}
else if(strncmp(buff,"I",1)==0) { //Timeout by user
printf("You took too long to respond.\n");
check = 1;
}
else if(strncmp(buff,"J",1)==0) { //Timeout by partner
printf("Opponent took too long to respond.\n");
check = 1;
}
if(check) { //Checking for continuing game
char cont;
printf("\nDo you wish to play again ? [1 for yes, no otherwise]\n");
FD_ZERO(&rd);
FD_SET(STDIN_FILENO, &rd);
tv.tv_sec = 15;
tv.tv_usec = 0;
int ok = 0;
if(select(1+STDIN_FILENO, &rd, 0, 0, &tv) > 0) {
scanf("%d",&ok);
}
write(sockfd,&ok,4);
}
}
}
int main(int argc, char** argv)
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
char* ip = argv[1];
if(argc != 2) {
printf("Too few or too many arguments\nUsage : ./<program name> <ip>\n");
return 0;
}
//Socket creation
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("Socket creation failed\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(ip);
servaddr.sin_port = htons(PORT);
//Connecting to game server
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("Connection with the server failed\n");
exit(0);
}
else
printf("Connected to the game server\n");
start_game(sockfd);
return 0;
}