-
Global Variables?
I know a lot of people ay that global variables are bad practice, but it looks like I might need them. The problem is with my TTT Game. I have two functions that seem to be causing this...
ShowBoard() and MakeMove().
Because MakeMove() requires char board; I need to define it in the function. But ShowBoard (obviously) also requires char board; so I have to re-declare char board; in both. Which means that ShowBoard() keeps the old version, therefore the board is never really updated, so would I be right in saying that global variables would be a good idea in this situation? I Could post some code if you wish, but I don't think it's neccessary.
Edit: How would I go about declaring a global variable?
-
Neither do I. By all means carry on using global variables. Chances are your current project is either small enough not to have to worry about it, or it'll become an unmanagable mess (in which case you won't have to post stating you've heard that global variables are bad practice).
If you really want to program using "good practice" then you could look up pass by reference (or since we're in a C++ forum, OOP).
>Edit: How would I go about declaring a global variable?
Declare it outside any function.
-
detail?
How exactly would I do that? Could you show me some code?
-
More specifically you could create a class called TicTacToe ( for example) and define char board within that class along with ShowBoard() and MakeMove(). That way they both would have access to char board. However to keep with "good practice" you should create daughter classes, one for one player and the other for the computer with more specific member declarations. This would probably be the more structured way of creating Tic Tac Toe in C++.
Edit:
class CTicTacToe //create the class
{
protected: //allows derived classes to access
char Board[3][3];
void Showboard(void);
void MakeMove();
};
void CTicTacToe::Showboard(void)
{ //whatever you want
}
void CTicTacToe::MakeMove()
{//...your code here
}
int main()
{
CTicTacToe ttt; //create an instance
ttt.Showboard(); //call the function
return 0;
}
just a quick example but I hope it helps.Oh yeah the daughter classes. Same way, well almost that you created the TicTacToe class:
class CPlayer : CticTacToe // Now Cplayer can access any
// members of CTicTacToe
{
//whatever you want
};
-
Thanks Heaps, I was Just thinking about classes, and making my functions a member of it. I'll start on that right away, thx again!
-
Ok, For now I've decided not to use functions, although my codemay get messy, I'll always be able to seperate it into functions, at the moment, without the functions it's working great.
-
Global variables are frowned upon in C++ because it's not good Object Oriented Programming practice, like Sorenson mentioned.
Most of the time there are ways around it, but in game programming I think you see quite a bit more use of them. Using classes is a good start. If you take a look at most of the DirectX samples you'll see that the main objects of the programs are declared global. This relieves a great deal of headache and is typically OK due to the nature of the application.
Game programmers, in general, tend to push the limits of good program practice in order to get the job done.
Try and avoid them if you can (by using other methods of data passing) but it's OK if you end up using a few to make life easier.