So I have a project(tic tac toe) for a class that involves our first use of c++ and I need some help with classes. Can I use variables/arrays between classes? and can I use variables/arrays between a class and the main.
getNames names;
playerTurns turns;
int checkWin();
int checkTie();
char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};//using this array in a class somewhere
void printBoard();
void fixBoard();
int turn = 1;
int main(int argc, constchar * argv[]){
int players;
cout << "Enter 1 for one player, 2 for two players: ";
cin >> players;
if(players == 1){
//single player version
}else{
int play = 1;
names.twoPlayerGet();
while(play == 1){
while(checkWin() == 0 && checkTie() == 0){
printBoard();
if(turn == 1){
turns.playerOneTurn();//I want this function to change board array
}else{
turns.playerTwoTurn();//"
}
}
}
}
}
//this is what the array does
void printBoard(){
//it prints a tic tac toe board with array parts within it...i would put it here but it's huge
}
class playerTurns{
public:
void playerOneTurn(){
cout << "Enter where you want to put a X, " << playerOneName;//somehow this string works between classes
// this if statement for when single player is in the game
if(players == 1){//this variable doesn't work and I tried making it global in the main.
//turn stuff here
}else{
//turn stuff here
}
}
void playerTwoTurn(){
cout << "Enter where you want to put a X, " << playerTwoName;//and this one
//turn stuff
}
};
#endif
The things you have put into classes could be implemented using functions. A class should be a structure, a defined object. Making a turn or getting a name is not a data structure.
The class would be a player, that has name, can make a turn etc. Try re-organizing your code and coming back.