hello,

i have just started trying to learn c++ and have been told to create a game of connect four player vs comp. i have managed very little and am stuck on trying to get the computer to recognise that when the cell is occupied rather than not put the coin in ('@') at all it will replace the ('_') above the '@' instead,

any ideas would be greatful thank you

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <limits>

using namespace std;

using std::cout;

using std::cin;

using std::endl;

int main()

{

//char a[] = {'|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'};

        char a[] = {'|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'};

        char b[] = {'|','1','|','2','|','3','|','4','|','5','|','6','|','7','|'};

        char board[6][7];

    int i, j, k =0;

    int row;

    int column;

    int game =1;

    int r = rand() ;

    for (i=0; i<6; i++)

    for (j=0; j<7; j++)

    board[i][j] = ' ';

    srand ( time(NULL) );



        cout<<"";

        for( j = 0; j<15; j++)

        cout <<b[j];

        cout <<endl;

    for ( i=0; i<6; i++)

    { //cout <<i+1;

    for( j = 0; j<15; j++)

    cout <<a[j];

    cout <<endl;

    }

        while(game ==1)

    {

        cout << "your turn please enter which cell you would like" << endl;

        cin >> column;

        if(board[5][column-1] ==' '){

        board[5][column-1] = '@';

        }else if(board[5][column-1] == '@')

        cout << "Cell Not Empty, Choose another cell" <<endl;

        cout<<"";

        for( j = 0; j<15; j++)

        cout <<b[j];

        cout <<endl;

        for(i =0; i<6; i++){

        cout<<'|';

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

    if(i < 5 && board[i][j] == '@' && board[i+1][j] == ' ') {board[i][j] =' '; board[i+1][j] ='@';}

    if(board[i][j] =='@' ) cout<<board[i][j]<<'|';

    else cout <<'_'<<'|';

    }

    cout <<endl;

    }


         k++;

        if( k ==10){



        game =0; // game over

    }

    }

 //cout << "thank you, computer's turn" << endl;

   // int r = rand() % 7;
    //cout << (rand() % 6) << endl;
    //cout << (rand() % 7) <<endl;

        cout << "Game over !" <<endl;

return 0;

}

Dani AI

Generated

The problem in the original post by comes from placing or “sliding” tokens during the print routine and only testing a fixed row. The correct flow is: validate the chosen column, scan that column from the bottom row upward to find the first empty slot, place the piece there, then draw the board. That keeps game state and display separate and avoids out-of-bounds or visual-only fixes. was pointing toward this same idea; raised reasonable concerns about randomness and code structure.

A compact placement helper (new code, fits into the main loop) — it scans bottom-to-top and returns the row placed or -1 if the column is full:

int dropPiece(char board[6][7], int col, char piece) {
    for (int r = 5; r >= 0; --r) {
        if (board[r][col] == ' ') {
            board[r][col] = piece;
            return r;
        }
    }
    return -1; // column full
}

Use a random engine for the computer move and try random columns until one accepts a drop; fall back to a linear scan if needed:

#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 6);

int compMove(char board[6][7], char piece) {
    for (int tries = 0; tries < 10; ++tries) {
        int c = dist(gen);
        if (dropPiece(board, c, piece) != -1) return c;
    }
    for (int c = 0; c < 7; ++c) if (dropPiece(board, c, piece) != -1) return c;
    return -1;
}

Practical notes: validate input (convert 1..7 to 0..6 and check bounds), test board[0][col] to detect a full column before attempting a drop, keep constants for ROWS/COLS, and separate printing from logic so display code never mutates the board. For randomness prefer the C++ <random> facilities over rand()/srand() for better habits. Implement a post-drop win-check (horizontal, vertical, two diagonals) that examines runs of four starting from the last placed cell. Relevant references: C++ random facilities and the game rules at Connect Four.

Recommended Answers

All 4 Replies

The computer doesn't recognize when a cell is occupied because you didn't tell it to.
You're making a check only on board[5][x]. You should use a loop to check which is the the highest place available and just then test it.

Please use code tags. These a thread related to them infact there all over the place!
Chris

commented: Yeah no need to even bother opening the announcement - it's written right on top of the forum :-) +2

hello,i m also a beginner to c++,also i didn't get that why u r taking the @ character only in the last row,but i can give u some ideas i think,first of all i want to suggest that you should mention that which player is pkaying at that time so that after the 4th player the computer plays the game.secondly if you are having a problem with replacement of characters, then u should try without printing of"|" and"_" first(without board).u can add it after the game is complete.
thanks.

You need code tags.... [code] ....all of that here.... [/code] site rules.

If you're using namespace std, why do you need to declare cin, cout, etc.?
When do your loops begin in and end? You need to use brackets to enclose things.
You use rand() before you even seed, and... you just need to learn how to format your code, or get an IDE to do it as you go; because this is near unreadable.

In fact, I suggest just completely rewriting this, since it's small enough that you could rapidly.

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.