Skip to content

Multiple interesting variations of the classic tic-tac-toe game.

Notifications You must be signed in to change notification settings

SiddharthMago/tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Online tictactoe (using networking concepts)

Execution:

Run 'make' on any terminal, the object files for server and client will be created.

Run './server' on server terminal and './client' on client terminal.

Run 'make clean' once done testing.

Implementation:

1) In play again situation, starting players do not have to alternate.

2) No disconnect messages have to be handled.

Working:

Server Working:

1) We start by creating a (SOCK_STREAM for TCP, SOCK_DGRAM for UDP) socket, which we make resuable so we dont have to keep changing port.

    int server_fd, client1_sock, client2_sock;
    struct sockaddr_in address;
    int addrlen = sizeof(address);

    // Create socket
    if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        printe("ERROR: Socket failed");
        exit(EXIT_FAILURE);
    }

    // Set socket options -> make address reusable incase of server crash
    int opt = 1;
    if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
        printe("ERROR: socket options failed");
        exit(EXIT_FAILURE);
    }

2) We bind the socket to the port and listen for 2 incoming connections - 1 for each player.

    if(bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        printe("ERROR: Bind failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    // Listen for incoming connections
    if(listen(server_fd, 2) < 0) {
        printe("ERROR: Listen failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

We check for both the clients acceptance before starting the game.

TCP -
    // Accept connection from Client 1
    if((client1_sock = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
        printe("Accept Client 1 failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }
    printf("Client 1 connected\n");

    // Accept connection from Client 2
    if((client2_sock = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
        printe("Accept Client 2 failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }
    printf("Client 2 connected\n");
UDP -
    bool client1_connected = 0;
    while(!client1_connected) {
        recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&client1_addr, &addr_len);
        buffer[strlen(buffer)] = '\0'; // Null-terminate the string

        if(strcmp(buffer, "connected") == 0) {
            printf("\nClient 1 connected\n");
            client1_connected = 1;

            // Send acknowledgment to client 1
            char* connected_message = "Hello Player1!\n";
            sendto(sock, connected_message, strlen(connected_message), 0, (struct sockaddr*)&client1_addr, addr_len);
        }
    }

    // Check for client 2 to send "connected"
    bool client2_connected = 0;
    while(!client2_connected) {
        recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&client2_addr, &addr_len);
        buffer[strlen(buffer)] = '\0'; // Null-terminate the string

        if (strcmp(buffer, "connected") == 0) {
            printf("\nClient 2 connected\n");
            client2_connected = 1;
            char* connected_message = "Hello Player2!\n";
            sendto(sock, connected_message, strlen(connected_message), 0, (struct sockaddr*)&client2_addr, addr_len);
        }
    }

Server Functions -

sendBoard - sends the board to specified client.

handleClientMove - Recieves the move from client and updates the board if move is valid and sends the updated board to the client. If move is invalid, it sends an error message to client.

handleEnd - Sends the win message and final board to both the clients according to the winner.

After handleEnd, we recieve play_again messages from both clients and accordingly either exit (if both havent agreed to rematch) or continue in loop to start game again. Depending on client response the appropriate message is also sent and displayed.

About

Multiple interesting variations of the classic tic-tac-toe game.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published