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;

}

Edited 3 Years Ago by pyTony: fixed formatting

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

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

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.

This article has been dead for over six months. Start a new discussion instead.