I am trying to make a single player connect four game in C++. With the specifications being:

  • 7x6
  • In order to win, the player must match four games pieces either horizontally, vertically, or diagonally.
  • One player, playing against a computer that makes random inputs.

I'm confused on how to write the functions to get the users inputs and to declare a winner.
If anyone has an example or snippets i could look at it would be much appreciated.

This is the code i have so far, it only deals with the game board. I am confused on what to do next.

Thanks.

void Initialize(char Slots[ROWS][COLUMNS])
{ 
    // Sets up Game Board
    for (int r = 0; r < ROWS; r++)
        for (int c = 0; c < COLUMNS; c++)
            Slots[r][c] = EMPTY;
}

//-------------------------------------------
void PrintBoard (const char GameBoard[ROWS][COLUMNS])
{
    cout << "  0   1   2   3   4   5 " << endl;
    cout << "|-----------------------|" << endl;
    for (int r = 0; r < ROWS ; r++)
    {
        cout << "| ";
        for (int c = 0; c < COLUMNS; c++)
        {
               cout << GameBoard[r][c];
            cout << " | ";
        }
        cout << endl << "|-----------------------|" << endl;
    }
}

//-------------------------------------------
int main ()
{
// Declare Variables        
char GameBoard[ROWS][COLUMNS]; // Array for tracking the game


    // Initialize the board
    Initialize(GameBoard);

    // Display Game Board
    PrintBoard(GameBoard);

    
    return 0;
}

Recommended Answers

All 5 Replies

i would create a while loop that gets input and loops until there's a valid input, and clear screen/reprint if it's not

then once you have valid input store it into the array then cls/reprint again

for checking for a winner maybe you could do something that counts all of the consecutive slots owned by the player, and if it's > 4 (in whichever direction) then win

Where are your declarations of ROWS and COLUMNS? I don't see them.

I would start by deciding how you're going to identify the Columns (x-coordinate) and Rows (y-coordinate) of player moves. I think you should consider getting from the user the column (x-coord) that they want to place a "chip" in then calculating the row from the pre-existing data.

Hey this is Professor Gauch, The next time you try and cheat your way through an assignment please don't make it so easy to find, I'll be watching this forum for code

Hey this is Professor Gauch, The next time you try and cheat your way through an assignment please don't make it so easy to find, I'll be watching this forum for code

If what you say is true (this is the internet after all), you have nothing to worry about here. While students are welcome on this site, cheating on assignments is not condoned.

A vast majority of the membership is acutely aware of the professional, moral, and educational ramifications of facilitating such behavior. They are cognizant too of schools' ethics in education/plagiarism/cheating policies. We are not rent-a-coders and we are very good at sniffing out cheaters and cheating attempts. We make a conscious effort, and pride ourselves on our ability, to assist students by guiding them to a solution, not by handing them solutions on a silver platter. If there is any hint of an attempt to cheat, they do not get far. Please have a look at this post from Dani herself.

Maybe you should try going to Lab or Office hours. They are there for a reason. I'm an amateur at programming, and even I could tell this was the source code from class. WOW. Just WOW.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.