Connect Four game...need help

This is a discussion on Connect Four game...need help within the C++ Programming forums, part of the General Programming Boards category; I am making a connect four game in console mode. I have just written the function that determines what to ...

  1. #1
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35

    Talking Connect Four game...need help

    I am making a connect four game in console mode.
    I have just written the function that determines what to do when the current player drops their piece in a row.

    Here's what it is:

    Code:
    void DropRow(int row)
    {
       char piece;
    
       if (p1.myturn)
          piece = 'Q';
       else if (p2.myturn)
          piece = 'X';
       //check drop and win criteria
       //not done yet
       //Big stuff--------------
       if (putvert < 0)
       {
          msg = 1;
          putvert = 7;
          StartGame();
       }
    
       if (board[putvert][row-1] != 'O')
       {
          putvert--;
          DropRow(row);
       }
    
       if (board[putvert][row-1] == 'O')
       {
          board[putvert][row-1] = piece;
       }
    
       //End of big stuff-------
       SwitchTurns();
       putvert = 7;
    }
    Putvert is an int that determines where to place the piece vertically. Board[][] is the char that stores all the grid spots. There is a global struct named Player that stores 2 people's score, name, and turn status.

    'O' is an empty grid space, 'Q' is player one's piece, and 'X' is player two's piece. The 'msg' you see being assigned to 1 is an instruction to a function that prints error messages. StartGame() is the main game loop, where it prints all the necessary stuff, the board, and lets they player input their row choice.

    Now here is the bug. If a player drops a piece on top of a piece that exists, they get an extra turn. I have no idea why it is happening.

    Here is the switch turn function:

    Code:
    void SwitchTurns()
    {
       if (p1.myturn == TRUE)
       {
          p1.myturn = FALSE;
          p2.myturn = TRUE;
       }
       else if (p2.myturn == TRUE)
       {
          p1.myturn = TRUE;
          p2.myturn = FALSE;
       }
    }
    Anyone have any ideas?
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Is an 'else if' checked even if the first 'if' is correct? (I have no idea, and no way to test that quickly right now); where for art thou Prelude, Barjor?

    If it is, it would change it to player 2's turn, then back to player 1's I guess.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no need for the second if check. There can only be one of two things: It's playerA's turn, or it's playerB's turn. There is no other possibility. Therefore, there is no need for the second check.

    Code:
    void SwitchTurns()
    {
       if (p1.myturn == TRUE)
       {
          p1.myturn = FALSE;
          p2.myturn = TRUE;
       }
       else
       {
          p1.myturn = TRUE;
          p2.myturn = FALSE;
       }
    }
    However, I doubt this is where your problem lies. The real problem is likely where you check to see who's turn it is.

    Code:
    void DropRow(int row)
    {
       char piece;
    
        /**
        *** No need for second 'if'.
        **/
        piece = p1.myturn ? 'Q' : 'X';
    
       //check drop and win criteria
       //not done yet
       //Big stuff--------------
       if (putvert < 0)
       {
          msg = 1;
          putvert = 7;
          StartGame();
       }
    
       if (board[putvert][row-1] != 'O')
       {
          putvert--;
          DropRow(row);
       }
    
        /**
        *** You never check 'row' for
        *** validity. This could be your
        *** problem.
        **/
       if (board[putvert][row-1] == 'O')
       {
          board[putvert][row-1] = piece;
       }
    Personally, I wouldn't have the drop function handle my side switching. I'd basicly do something like:

    Code:
    while( !game_over )
    {
        ShowBoard( );
        row = PromptRow( p1.myturn ? p1 : p2 );
        Drop( row );
        game_over = CheckWin( );
        SwitchTurns( );
    }
    CongradulateWinner( );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. u got me wrong fellas
    By clover in forum Game Programming
    Replies: 4
    Last Post: 10-18-2003, 05:23 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21