Thread: Connect 4

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Connect 4

    At the bottom of the following function where it says STARTING HERE I am trying to code an algorithm that will place a computer move, denoted as -1, into a board. My program has a pointer called children that points to 7 different boards. Therefore each board should have 1 potential move, 1 different move in each column. But when I run my code, it puts the correct move in the correct column in each ->board but it fills the entire column with that move instead of just the spot it should.. Any ideas would be greatly appreciated. Thanks!

    void buildTree(gameTree *root/* in-out */, int move/* in */, int depth/* in */)
    {
    /* purpose: given a gameTree node containing the current board, and whose move
    it is if the depth is greater than zero,
    evaluate the board and copy that in the value field of the node
    else generate all legal children nodes with appropriate boards,
    and moves, call buildTree for each node with depth -1,
    look at all children and choose max (or min) of children to
    determine the value of the passed in node
    pre : board is valid
    move is 1 or -1 to determine who is to move next (redundant)
    depth is an integer >=0
    post : a full game tree is created with the root as the root
    calls : cost
    called by : puterMove
    */
    if (depth>0)
    {
    depth--;
    move = (depth%2==0)?-1:1;


    int makechildren;
    int made = 0;
    for(makechildren = 0; makechildren < SIZE; makechildren ++)
    {

    //Determine if board is valid*************************************
    int boardvalid = 0;
    int valid = 0;
    int toprow = SIZE - 1;
    int t,s;
    int col;
    for (col = 0; col<SIZE; col++)
    {
    if(root->board[toprow][col] == 0)
    {
    root->children[col] = new gameTree; //new MAKING CHILDREN
    root->children[col]->move = move;
    root->children[col]->value = col;
    for(t = 0; t < SIZE; t++)
    for(s = 0; s < SIZE; s++)
    root->children[col]->board[t][s] = root->board[t][s];
    for (t= 0; t<SIZE && root->board[t][col] == 0; t++)
    root->children[col]->board[t][col] = -1*move;
    cout << "TEST: " << root->children[col]->board[t][col] << endl;
    made++;
    valid++;
    }
    }
    cout << "MADE:" << made;
    //Done determining if board is valid*******************************


    //root->value = cost(root->children[7]->board);
    }//end of for(makechildren) loop
    //root->value = soemthing./!!!! max or min of all of your children
    }//end of if(depth > 0)
    //else cost();


    int place = 0;
    int rplace = 0;
    int cplace = 0;
    int d = 0;
    int dcol = 0;
    int dchild = 0;
    //STARTING HERE!!!!
    while(root->children[dchild]->board[d][dcol] != 0)
    {

    d++;

    root->children[dchild]->board[d][dcol] = -1;

    dchild++;

    }

    for(place = 0; place < SIZE; place++)
    for(rplace = 0; rplace < SIZE; rplace++)
    for(cplace = 0; cplace < SIZE; cplace++) //output tests
    cout << root->children[place]->board[rplace][cplace];

    cout << "HEY: " << root->children[0]->board[0][1];


    //else
    //root->value = cost(root->board);
    }//end of function

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    make a whatever by whatever array and when a user inputs into that column, start at the bottom and loop the value up until you find a value that is not filled.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  2. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  3. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  4. MySQL Connect from Outside Computer
    By juschillin in forum Windows Programming
    Replies: 0
    Last Post: 09-27-2002, 08:02 AM
  5. Advanced connect four game
    By Ion Blade in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2002, 07:52 AM