Question

In: Computer Science

I'm having trouble programming connect four board game using linked lists, sets and maps in c++....

I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.

Solutions

Expert Solution

You can try the following code to implement the connect four game in C++.

Using maps would make the work more tedious and increase the space complexity as you only have a 6*7 board.

#include <iostream>
#include <cstdlib>
#include <unistd.h>

int tab[7][6];
int choice, player;
bool end = false;
int a=0;

void check(int x)
{
    if(tab[x-1][a]!=0 && a<6)
    {
        a++;
        check(x);
    }   else if (player==1 && a<6)
            {
            tab[x-1][a]=1;
            a=0;
            }
        else if (player==2 && a<6)
            {
            tab[x-1][a]=2;
            a=0;
            }
        else
        {
        std::cout << "WRONG!" << std::endl;
        a=0;
        player++;
        }
}

int draw()
{
    system("clear");
    for(int i = 0; i<9; i++)
    {
        if(i<2)
        {
        std::cout<<"-";
        } else if(i>7)
        {
        std::cout<<i-1<<"--"<<std::endl;
        } else {
        std::cout<<i-1<<"----";
        }
    }
    for(int i = 0; i<6; i++)
    {
        for(int j = 0; j<7; j++)
        {
            if(tab[j][i]!=0)
            {
                if(tab[j][i]==1)
                {
                std::cout<<"| X |";
                }else std::cout<<"| O |";
            }
            else std::cout<<"|   |";
        } std::cout<<std::endl;
    }
    for(int i = 0; i<35; i++)
    {
        std::cout<<"=";
    } std::cout<<std::endl;
}

int win_check()
{
    for(int i = 0; i<6; i++)
    {
        for(int j = 0; j<7; j++)
        {
        if(tab[j][i]==1 && tab[j+1][i+1]==1 && tab[j+2][i+2]==1 && tab[j+3][i+3]==1)
            {
            end = true;
            std::cout << "\nPLAYER 1 WIN!" << std::endl;
            }
        if(tab[j][i]==1 && tab[j+1][i-1]==1 && tab[j+2][i-2]==1 && tab[j+3][i-3]==1)
            {
            std::cout << "\nPLAYER 1 WIN!" << std::endl;
            end = true;
            }
        if(tab[j][i]==2 && tab[j+1][i-1]==2 && tab[j+2][i-2]==2 && tab[j+3][i-3]==2)
            {
            std::cout << "\nPLAYER 2 WIN!" << std::endl;
            end=true;
            }
        else if(tab[j][i]==2 && tab[j-1][i-1]==2 && tab[j-2][i-2]==2 && tab[j-3][i-3]==2)
            {
            end = true;
            std::cout << "\nPLAYER 2 WIN!" << std::endl;
            }
        else if(tab[j][i]==1 && tab[j][i-1]==1 && tab[j][i-2]==1 && tab[j][i-3]==1)
            {
            std::cout << "\nPLAYER 1 WIN!" << std::endl;
            end = true;
            }
        else if(tab[j][i]==1 && tab[j-1][i]==1 && tab[j-2][i]==1 && tab[j-3][i]==1)
            {
            std::cout << "\nPLAYER 1 WIN!" << std::endl;
            end = true;
            }
        else if(tab[j][i]==2 && tab[j][i-1]==2 && tab[j][i-2]==2 && tab[j][i-3]==2)
            {
            std::cout << "\nPLAYER 2 WIN!" << std::endl;
            end = true;
            }
        else if(tab[j][i]==2 && tab[j-1][i]==2 && tab[j-2][i]==2 && tab[j-3][i]==2)
            {
            std::cout << "\nPLAYER 2 WIN!" << std::endl;
            end = true;
            }
        }
    }
}
int p_choice()
{
    player = 1;
    while(end!=true)
    {
    std::cout << "PLAYER " << player << ": ";
    std::cin >> choice;
        if (choice>0 && choice<8)
        {
            check(choice);
            draw();
            if (player == 1)
            {
                player++;
            }
            else
            {
                player--;
            }
        }
    else
        {
    std::cout << "WRONG CHOICE!" << std::endl;
        }
    win_check();
    }
}


int main()
{
    system("clear");
    std::cout<<"WELCOME IN CONNECT 4"<<std::endl;
    sleep(1);
    draw();
    p_choice();
    return 0;
}

Related Solutions

C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists,...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and Algorithms Show especially Especially Algorithm/Iterators/Containers in the STL Minimum of 900 lines Include Full documentation
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
TCP client and server using C programming I am having trouble on how to read in...
TCP client and server using C programming I am having trouble on how to read in the IP adress and port number from the terminal Example: Enter IP address: 127.0.0.1 Enter Port Number: 8000 in both client and server code. How do can I make I can assign the Ip address and port number using the example above. the error I get is that the client couldn't connect with the server whenever i get the port number from the user...
I'm having trouble creating a histogram using openCV (color segmentation)
I'm having trouble creating a histogram using openCV (color segmentation)
I'm having trouble thinking of a way that I can delete duplicates from a c string...
I'm having trouble thinking of a way that I can delete duplicates from a c string of alphabets. So what I'm supposed to do is I'm supposed to delete the repeated letters in the c string using pointers and also dynamically allocating space. I'm guessing that I will have to dynamically allocate space for an array to put in the letters that only appear once. To do that, can I create 2 pointers in a function and have 1 pointer...
Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy...
Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy City opened bidding for an airport construction project, by the usual process of advertising a request for bids. Crafty Construction submitted the lowest responsive bid. When the Happy City Council met to review the bid the Council members discovered the bid exceeded the budget for the project and discussed the possibility of negotiating a bid reduction. The city manager told the Council that Chris...
Create a game of Connect Four using a two dimensional array (that has 6 rows and...
Create a game of Connect Four using a two dimensional array (that has 6 rows and 7 columns) as a class variable. It should be user friendly and be a two person game. It should have clear directions and be visually appealing. The code MUST have a minimum of 3 methods (other than the main method). The player should be able to choose to play again. **Code must be written in Java**
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT