CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: conecct four

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Exclamation conecct four

    ive just started programming, and am struggling rather a lot, i am to make a connect four game which i have started but cannot get past this point,

    could anyone help me with how am i suppose to get the computer to recognise that the cell is occupied and rather than say choose another cell i need it to automatically re place '_' with the '@' above it/

    [code]
    #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() &#37; 7;
    //cout << (rand() % 6) << endl;
    //cout << (rand() % 7) <<endl;

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

    return 0;

    }
    Last edited by newuser2c++; December 17th, 2008 at 09:49 AM. Reason: figured out origianal problem

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: conecct four

    Please add the closing code tag. The code is impossible to read, but I try anyways:

    Start with level=5, then while level is still positive and the cell at this level (and the user-chosen column) is occupied decrease level.

    After that, if level is negative (ensure it is declared as int, not unsigned int), the whole column is used. Otherwise the field at level is free.
    Last edited by treuss; December 17th, 2008 at 12:37 PM.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured