• Advertisement
Sign in to follow this  

Program Help

This topic is 3954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, this is my source code for a Connect 4 game, however I am having trouble getting things to work correctly Firstly... I need help passing a 2D array into a function..How can that be done? >>> shows where the problem lies Any help is greatly appreciated! #include <iostream> using namespace std; //Function Prototypes void Player_Turn(int&); void The_Board(int, int, int, int, int, bool&); // Draws Board To Screen void Drop(int, char[][7], bool, int, int); int main(char board) { // Intializes board const int MAX_ROWS = 6; const int MAX_COLUMN = 7; int player=1; bool blank = true; //Used to keep track of # of moves for high score int drop1=0; int drop2=0; Player_Turn(player); cout << endl; >>>>>>Drop(player, board[][7], blank, drop1, drop2); The_Board(player, drop1, drop2, MAX_ROWS, MAX_COLUMN, blank); // Keeps screen up to display board int hold = 0; cin >> hold; return 0; } //Function determines which players turn it is to play piece void Player_Turn(int &player) { if(player == 1) { cout << "Player 1's Turn " << endl; player = 2; } else if(player == 2) { cout << "Player 2's Turn " << endl; player = 1; } } //Function displays the board void The_Board(int player, int drop1, int drop2, int MAX_ROWS, int MAX_COLUMN, bool &blank) { char board[6][7];; char bottom = '='; char side = '|'; int tick = 0; //Clears Array of garbage for(int clear_row = 0; clear_row < 7; clear_row++) { for(int clear_col= 0; clear_col < 6; clear_col++) board[clear_row][clear_col]; } for (int row = 0; row < 7; row++) { cout << side; for (int column = 0; column < 6; column++) { cout << board[row][column] << side; } cout << endl; } for(int loop = 0; loop < 13; loop++) cout << bottom; cout << endl; } //Function determines where player chooses to drop piece void Drop(int player, char board[][7], bool blank, int drop1, int drop2) { int choice; cout << "Please choose a column (1-7):"; cin >> choice; choice = (choice - 1) ; if (choice > 6 || choice < 0) cout << "Invalid Column Choice, Choose Another" << endl; do{ if(player == 1) { for (int i=6; i > 0; i--) { if(board[i][choice] != '\0' || board[i][choice] != ' ') { blank = true; drop1++; board[i][choice] = 1; } else { blank = false; i+=1; } }} else if(player == 2) for (int i=6; i > 0; i--) { if(board[i][choice] != '\0' || board[i][choice] != ' ') { blank = true; drop2++; board[i][choice] = 2; } else { blank = false; i+=1; } } }while(!blank); }

Share this post


Link to post
Share on other sites
Advertisement
Try passing the array, board, to a pointer in Drop().


// Function determines where player chooses to drop piece
void Drop(int player, char *board, bool blank, int drop1, int drop2)
{
// And then refer to the board through the pointer..
// board[clear_row][clear_col];
}

// And when passing the array...
Drop(player, board, blank, drop1, drop2);




If you want to directly change the contents of Board, then pass it as
a reference.


Drop(player, &board, blank, drop1, drop2);




Hope this helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by tcrock
    int main(char board)
That's bizarre. Usually it looks like this:
    int main( int argc, char **argv ) 
Ok, if you remove board from main's parameter list, then you need to define it somewhere. Something like this, perhaps:
        // Intializes board
const int MAX_ROWS = 6;
const int MAX_COLUMN = 7;
char board[MAX_ROWS][MAX_COLUMN];
Quote:
Original post by tcrock
        Drop(player, board[][7], blank, drop1, drop2); 
Your parameter to Drop is wrong. Instead it should look like this:
        Drop(player, board, blank, drop1, drop2); 

Share this post


Link to post
Share on other sites
Thanks, for the help....
However, I still am getting errors pertaining to the call to board
says "board is undeclared"


any ideas??

Share this post


Link to post
Share on other sites
depends if you have followed the advice given..

as you have declared board in ... main(char board)

board is only available to main, so your other functions cannot see it.

also as for your call to:
The_Board(player, drop1, drop2, MAX_ROWS, MAX_COLUMN, blank);

this comes before you have even defined the function later on:
//Function displays the board
void The_Board(int player, int drop1, int drop2, int MAX_ROWS, int MAX_COLUMN, bool &blank)
{
// ...
}

did this even compile before?

post your source again with the changes.

Also read this:
http://www.gamedev.net/community/forums/faq.asp#tags

to find out how to post your code in 'tags' like TaylorGJ has done, to make your code readable.

Share this post


Link to post
Share on other sites
Quote:
Original post by tcrock
Thanks, for the help....
However, I still am getting errors pertaining to the call to board
says "board is undeclared"


any ideas??


Don't pass arrays around. Arrays are treated differently when it comes to passing them to functions. Its just as easy to put the array inside a struct or class of some kind, and pass that around:


struct Board {
char board[6][7];
};




There are several things that aren't going to do what you expect in your code. Example:

//Clears Array of garbage
for(int clear_row = 0; clear_row < 7; clear_row++)
{
for(int clear_col= 0; clear_col < 6; clear_col++)
board[clear_row][clear_col]; // <--- This line does nothing!
}



Turn up your compiler error settings to the highest possible to warn you about stuff like this.

If The_Board() is supposed to display the board, why not name it like that? "Display_Board()" is a better description. Display board has board as a "local" variable, which isn't what you want. Later on in your coding you will want to pass the board to this function as a parameter.

Your functions take lots of parameters which, to be honest, don' have very clear descriptions. I think there are 2 parameters you should be passing to each function, the player number and the board's state.

A potential starter for your code is like this:

// includes etc

const int MAX_ROWS = 6;
const int MAX_COLUMNS = 7;

struct Board {
char board[MAX_ROWS][MAX_COLUMNS];
};

void Player_Turn( int & );

// we probably dont need any other parameters
// to this function.
// const means this function promises not to modify
// the "board" parameter
void Display_Board( const Board & board );

// we could maybe call this Make_Turn or something
// but drop is an ok name
// This function could return a bool indicating if the
// game is over if you want.
bool Drop( Board &board , int player );

void Clear_Board( Board &board );

int main( int argc, char *argv[] )
{
Board board;
Clear_Board( board );
int player = 1;
bool playing = true;
while( playing )
{
// run the game
}
}




Having the board dimensions outside of main helps avoid passing these constants to functions.

[Edited by - rip-off on March 24, 2007 1:04:03 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement