Source Code
Source Code
<stdio.h>
<winsock.h>
<iostream>
<cstdlib> //random
<ctime> //random
void main(void)
{
//Declaration
int sock, newsock, addrlen, status, first;
struct sockaddr_in server, sender;
char buffer[BUFFER_SIZE];
InitWSA();
//---------------------------------------------------------------------
//Create a socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Error in socket(). Error code: %d\n", WSAGetLastError());
exit(-1);
}
//-------------------------------------------------------------------//Define
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
//Your're the server(S) or the client(C)? [Question no 1]
char cos;
cout << "Your're the server(S) or the client(C)?";
cin >> cos;
if (cos == 'S' || cos == 's')
{
//----------------------------------------------------------------------//If this is server, this section will activate.
//----------------------------------------------------------------------//Create port 9999 for server [Question no 2]
server.sin_port = htons(9999);
//Binding
status = bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
if (status == -1) {
printf("Error in bind(). Error code: %d\n", WSAGetLastError());
exit(-1);
}
//---------------------------------------------------------------------// Listening for connection
printf("Listening...\n");
status = listen(sock, 1); //only 1 can connect
if (status == -1) {
printf("Error in listen(). Error code: %d\n", WSAGetLastError());
exit(-1);
}
addrlen = sizeof(sender);
// Accepting incoming connection
newsock = accept(sock, (struct sockaddr *) &sender, &addrlen);
if (newsock == -1) {
printf("Error in accept(). Error code: %d\n", WSAGetLastError());
printf("Accepting next connection...\n\n");
}
printf("A connection from %s has been accepted\n", inet_ntoa(sender.sin_addr));
//---------------------------------------------------------------------mark no 4]
//Server will randomly choose who are going to move first [Question 4 and bonus
srand((unsigned)time(0));
first = (rand() % 2) + 1;
if (first == 1){
strcpy(buffer, "m1");
send(newsock, buffer, strlen(buffer), 0);
first = first - 1;
}
else{
strcpy(buffer, "m2");
send(newsock, buffer, strlen(buffer), 0);
first = first - 1;
}
//---------------------------------------------------------------------
else
{
//--------------------------------------------------------------------//If this is the client, then this section will activate
//--------------------------------------------------------------------//Create own port
server.sin_port = htons(7000);
//Binding
status = bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
if (status == -1) {
printf("Error in bind(). Error code: %d\n", WSAGetLastError());
exit(-1);
}
//--------------------------------------------------------------------//Asking server IP [Question no 3]
char ip[15];
cout << "Enter server IP address:";
cin >> ip;
cout << "Connecting to the server...\n";
//Define which Server to be connected
sender.sin_family = AF_INET;
sender.sin_port = htons(9999);
sender.sin_addr.s_addr = inet_addr(ip);
//Connecting....
{
cout << name << " quit.";
break;
}
print_board(board);
stat = check_victory(board);
if (stat == 1) {
cout << "\nYou Lose!\n";
break;
}
}
}//End for loop.
//If not win, then the game is tie [Question no 5]
if (stat != 1)
cout << "\nThe game is tie.\n";
//Play again? [Question 6]
cout << "Play again? (1 to play, 0 to stop): ";
cin >> reply;
//----------------Tictactoe function-----------------//----------------------------------------------------
// This function initializes the tic-tac-toe game board by putting the number 1 to 9 on each
square.
void initialise_array(char arr[3][3])
{
int i, j, k = 49;
// This function takes the move entered by user and put the user's mark on the selected spot.
// If the move is not valid, this function returns a 0. Otherwise, it returns a 1.
int mark_move(char y, int move, char array[3][3])
{
if (move == 1) {
if (array[0][0] == 'x' || array[0][0] == 'o') return 0;
array[0][0] = y;
return 1;
}
else if (move == 2) {
if (array[0][1] == 'x' || array[0][1] == 'o') return 0;
array[0][1] = y;
return 1;
}
else if (move == 3) {
if (array[0][2] == 'x' || array[0][2] == 'o') return 0;
array[0][2] = y;
return 1;
}
else if (move == 4) {
if (array[1][0] == 'x' || array[1][0] == 'o') return 0;
array[1][0] = y;
return 1;
}
else if(move == 5) {
if (array[1][1] == 'x' || array[1][1] == 'o') return 0;
array[1][1] = y;
return 1;
}
else if (move == 6) {
if (array[1][2] == 'x' || array[1][2] == 'o') return 0;
array[1][2] = y;
return 1;
}
else if (move == 7) {
if (array[2][0] == 'x' || array[2][0] == 'o') return 0;
array[2][0] = y;
return 1;
}
else if (move == 8) {
if (array[2][1] == 'x' || array[2][1] == 'o') return 0;
array[2][1] = y;
return 1;
}
else if (move == 9) {
if (array[2][2] == 'x' || array[2][2] == 'o') return 0;
array[2][2] = y;
return 1;
}
else if (move == -1) {
return 1;
}
}
else return 0;
cout << "\nPlease enter the number of the space( 1 to 9) you wish to mark.(-1 to
cout << "Your move ==> ";
cin >> reply;
flag = mark_move(y, reply, array);
if (flag == 0) {
cout << "Invalid move. Please try again.\n";
}
}while(flag == 0);
return reply;
}
int playersana_move (int m, char y, char array[3][3])
{
int reply, flag = 0;
do{
reply = m;
flag = mark_move(y, reply, array);
}while(flag == 0);
return reply;
}
// This function prints the tic-tac-toe board
void print_board(char array[3][3])
{
int i,j;
cout << endl;
for(i = 0; i < 3;i++) {
for(j = 0; j < 3; j++) {
}
// Thus function checks whether there is any 3-in-a-row squares that have the same mark. If there
is, then somebody wins.
// The function returns a 1 if a winner is found. Otherwise it returns 0.
int check_victory(char array[3][3])
{
int status; //status = 1 means win
if (array[0][0] == array[0][1] && array[0][1] == array[0][2])
status = 1;
else if (array[1][0] == array[1][1] && array[1][1] == array[1][2])
status = 1;
else if (array[2][0] == array[2][1] && array[2][1] == array[2][2])
status = 1;
else if (array[0][0] == array[1][0] && array[1][0] == array[2][0])
status = 1;
else if (array[0][1] == array[1][1] && array[1][1] == array[2][1])
status = 1;
else if (array[0][2] == array[1][2] && array[1][2] == array[2][2])
status = 1;
else if (array[0][0] == array[1][1] && array[1][1] == array[2][2])
status = 1;
else if (array[2][0] == array[1][1] && array[1][1] == array[0][2])
status = 1;
else status = 0;
return status;
}
Screenshots of Client
ScreenshotS of Server