15
SOURCE CODE /*This is a program of Tic-Tac-toe Game Group Members : 1. Ahmad Faris Bin Harun Iskandar ( Group Leader ) - SN093049 2. Muhamad Akhmal Haziq Bin Abd Halim -SN093931 3. Nur Ain Nadhihah Binti Mohd Yusoff -SN093155 4. Nur Aimi Najihah Binti Kamarrudin –SN093052 */ #include <stdio.h> #include <winsock.h> #include <iostream> #include <cstdlib> //random #include <ctime> //random void initialise_array( char arr[3][3]); void print_board( char [3][3]); int check_victory( char [3][3]); int playersini_move ( char , char [3][3]); int playersana_move ( int , char , char [3][3]); int mark_move( char , int , char [3][3]); using namespace std; #define BUFFER_SIZE 100 void InitWSA( void ) { WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD (1, 1); int nRc = WSAStartup(wVersionRequested, &wsaData); if (nRc != 0) { fprintf( stderr , "Error when calling WSAStartup()\n" ); exit(-1); } if ( LOBYTE ( wsaData.wVersion ) != 1 || HIBYTE ( wsaData.wVersion ) != 1 ) { fprintf( stderr , "Error: Version is not available\n" ); exit(-1); } } void main( void ) { //Declaration

Source Code

Embed Size (px)

DESCRIPTION

tictactoe

Citation preview

Page 1: Source Code

SOURCE CODE

/*This is a program of Tic-Tac-toe Game

Group Members : 1. Ahmad Faris Bin Harun Iskandar ( Group Leader ) - SN0930492. Muhamad Akhmal Haziq Bin Abd Halim -SN0939313. Nur Ain Nadhihah Binti Mohd Yusoff -SN0931554. Nur Aimi Najihah Binti Kamarrudin –SN093052 */

#include <stdio.h>#include <winsock.h>#include <iostream>#include <cstdlib> //random#include <ctime> //random

void initialise_array(char arr[3][3]);void print_board(char [3][3]);int check_victory(char [3][3]);int playersini_move (char, char[3][3]);int playersana_move (int, char, char[3][3]);int mark_move(char, int, char [3][3]);

using namespace std;#define BUFFER_SIZE 100

void InitWSA(void){

WORD wVersionRequested;WSADATA wsaData;wVersionRequested = MAKEWORD(1, 1);

int nRc = WSAStartup(wVersionRequested, &wsaData);

if (nRc != 0) {

fprintf(stderr, "Error when calling WSAStartup()\n");exit(-1);

}

if (LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { fprintf(stderr, "Error: Version is not available\n");exit(-1);

}}

void main(void){

//Declarationint sock, newsock, addrlen, status, first;struct sockaddr_in server, sender;char buffer[BUFFER_SIZE];

InitWSA();

//---------------------------------------------------------------------

Page 2: Source Code

//Create a socketsock = socket(AF_INET, SOCK_STREAM, 0);if (sock == -1) {

printf("Error in socket(). Error code: %d\n", WSAGetLastError());exit(-1);

}

//--------------------------------------------------------------------

//Defineserver.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);

//Bindingstatus = bind(sock, (struct sockaddr *)&server, sizeof(struct

sockaddr));if (status == -1) {

printf("Error in bind(). Error code: %d\n", WSAGetLastError());

exit(-1);}

//----------------------------------------------------------------------

// Listening for connectionprintf("Listening...\n");status = listen(sock, 1); //only 1 can connectif (status == -1) {

printf("Error in listen(). Error code: %d\n", WSAGetLastError());

exit(-1);}

addrlen = sizeof(sender);

// Accepting incoming connection

Page 3: Source Code

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));

//----------------------------------------------------------------------

//Server will randomly choose who are going to move first [Question 4 and bonus mark no 4]

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 portserver.sin_port = htons(7000);

//Bindingstatus = bind(sock, (struct sockaddr *)&server, sizeof(struct

sockaddr));if (status == -1) {

printf("Error in bind(). Error code: %d\n", WSAGetLastError());

exit(-1);}

//---------------------------------------------------------------------

Page 4: Source Code

//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 connectedsender.sin_family = AF_INET;sender.sin_port = htons(9999);sender.sin_addr.s_addr = inet_addr(ip);

//Connecting....status = connect(sock, (struct sockaddr *)&sender, sizeof(struct

sockaddr));newsock = sock;printf("Connected\n");if (status == -1) {

printf("Error in connect(). Error code: %d\n", WSAGetLastError());

exit(-1);}

//Waiting for the server to choose who are going to move first.(Randomly)[Continue bonus mark 4]

status = recv(sock, buffer, BUFFER_SIZE, 0);buffer[status] = '\0';

if (strcmp("m1", buffer) == 0)first = 1;

elsefirst = 0;

//----------------------------------------------------------------------}

//--------------------------------------------------------------------------//The game begin here....

//--------------------------------------------------------------------------

cout << "-------------------------------------------\n";cout << "----------------TicTacToe------------------\n";cout << "-------------------------------------------\n";

int i, stat = 0, reply = 1;char board[3][3];

do {

Page 5: Source Code

//Prepare the gameinitialise_array(board);print_board(board);stat = 0;

//----------------------------------------------------

//Exchange players name [Bonus mark 3]char name[10];

cout << "Enter your name:" ;cin >> name;

strcpy(buffer, name);send(newsock, buffer, strlen(buffer), 0);

status = recv(newsock, buffer, BUFFER_SIZE, 0);buffer[status] = '\0';strcpy(name, buffer);

cout << "Your opponent:" << name;

//----------------------------------------------------

cout << "\nServer randomly choose who will move first.";

//The game begin!for (i = first; i < 9; i++) {

if (i % 2 == 0) //This side move{

cout << "\nIt is your turn.\n";

int x = playersini_move('x', board);

if (x == 1)strcpy(buffer, "a1");

else if (x == 2)strcpy(buffer, "a2");

else if (x == 3)strcpy(buffer, "a3");

else if (x == 4)strcpy(buffer, "a4");

else if (x == 5)strcpy(buffer, "a5");

else if (x == 6)strcpy(buffer, "a6");

else if (x == 7)strcpy(buffer, "a7");

else if (x == 8)strcpy(buffer, "a8");

else if (x == 9)strcpy(buffer, "a9");

else if (x == -1) //if the user want to quit [Bonus mark 1]

{strcpy(buffer, "qq");send(newsock, buffer, strlen(buffer), 0);

Page 6: Source Code

break;}

send(newsock, buffer, strlen(buffer), 0);

print_board(board);stat = check_victory(board);if (stat == 1) {

cout << "Congratulation!! You win!\n";break;

}}

//Other side moveelse{

cout << endl << name << " turn.\n";

//receive data from other sidestatus = recv(newsock, buffer, BUFFER_SIZE, 0);buffer[status] = '\0';

//compare and convertif (strcmp("a1", buffer) == 0)

playersana_move(1, 'o', board);else if (strcmp("a2", buffer) == 0)

playersana_move(2, 'o', board);else if (strcmp("a3", buffer) == 0)

playersana_move(3, 'o', board);else if (strcmp("a4", buffer) == 0)

playersana_move(4, 'o', board);else if (strcmp("a5", buffer) == 0)

playersana_move(5, 'o', board);else if (strcmp("a6", buffer) == 0)

playersana_move(6, 'o', board);else if (strcmp("a7", buffer) == 0)

playersana_move(7, 'o', board);else if (strcmp("a8", buffer) == 0)

playersana_move(8, 'o', board);else if (strcmp("a9", buffer) == 0)

playersana_move(9, 'o', board);else if (strcmp("qq", buffer) == 0)//if the user want to

quit [Continue bonus mark 1]{

cout << name << " quit.";break;

}

print_board(board);stat = check_victory(board);if (stat == 1) {

cout << "\nYou Lose!\n";break;

}

}

Page 7: Source Code

}//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;

} while (reply == 1);closesocket(newsock); //if no, the socket will close [continue question 6]

}

//----------------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;

for ( i = 0; i < 3; i++) {for (j = 0; j < 3; j++) {

arr[i][j] = k;k++;

}}

}

// 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) {

Page 8: Source Code

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;}

// This function will ask the user to enter his/her move.// The function will loop until the user enters a valid move.// This function returns the move entered by the user.int playersini_move (char y, char array[3][3])

{int reply, flag = 0;

Page 9: Source Code

do{cout << "\nPlease enter the number of the space( 1 to 9) you wish to

mark.(-1 to quit)\n";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 boardvoid print_board(char array[3][3]){

int i,j;

cout << endl;for(i = 0; i < 3;i++) {

for(j = 0; j < 3; j++) {

cout << array[i][j] << " ";}cout << endl;

}}

// 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])

Page 10: Source Code

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

Page 11: Source Code
Page 12: Source Code

ScreenshotS of Server

Page 13: Source Code
Page 14: Source Code