Variables between classes and main.

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.

part of main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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, const char * 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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


class getNames
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
string playerOneName;
string playerTwoName;


class getNames{
    
public:
    
    void onePlayerGet(){
        
        cout << "Player, enter your name: ";
        cin >> playerOneName;
        
    }
    
    void twoPlayerGet(){
        
        cout << "Player One, enter your name: ";
        cin >> playerOneName;
        
        cout << "Player Two, enter your name: ";
        cin >> playerTwoName;
        
    }
    
    void onePlayerPrint(){
        
        cout << playerOneName;
        
    }
    
    void twoPlayerPrintOne(){
        
        cout << playerOneName;
        
    }
    
    void twoPlayerPrintTwo(){
        
        cout << playerTwoName;
        
    }
    
};

#endif
Last edited on
What in Stallmans name is this.

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.
ok, this is my first time using classes without even being taught how to use them.

For the class I'm taking we made tic tac toe in C first and he said to make it in C++. And he said to make the name getting and using in a class.

So are you saying to have one class that gets the name of the player. And have the turns in functions?

Also would it make sense to call a player name an object?

Your reply actually really helped so thank you.
Last edited on
Topic archived. No new replies allowed.