TicTacToe game help [Still need help]

Sep 17, 2011 at 6:39pm
Hi, I'm making a tictactoe game, and i created a player class with a function "PlayersTurn", the problem is I need to call some functions in the tictactoe class
I tried friendship, then putting in my class member function TicTacToe.showTTTboard();
and i get the following error
expected unqualified-d before '.' token

edit i just realized friendship is used to access private members, meh so friendship is useless, can anyone lead me in the right direction?
Last edited on Sep 19, 2011 at 6:23pm
Sep 17, 2011 at 7:23pm
closed account (10oTURfi)
Post your code?
Sep 19, 2011 at 5:23pm
TicTacToe.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef TICTACTOE_H
#define TICTACTOE_H


class TicTacToe
{
    int TTTboard[3][3];
    public:
        friend class Player; //probably going to take this out
        TicTacToe();
        virtual ~TicTacToe() {}
        void showTTTBoard();
        void setTTTBoard(int,int,char);
        void clearTTTBoard();
        char returnTTTBoard(int,int);
};

#endif // TICTACTOE_H

TicTacToe::TicTacToe() //constructor that adds a space in all of the array boxes 
//so that showboard() actually looks like a tic tac toe board
{
    for (int n=0; n<3; n++)
    {
        for (int i=0; i<3; i++)
        {
            TTTboard[n][i] = ' ';
        }
    }
}

/* Shows
  | | 
--------
  | | 
--------
  | | 
assuming the board doesn't have X and Os added into the array
*/
void TicTacToe::showTTTBoard()
{
    for (int n=0; n<3; n++)
    {
        for (int i=0; i<3; i++)
        {
            std::cout<<" ";
            std::cout<<TTTboard[n][i];
            if (i == 0 || i == 1)
            {
            std::cout<<"|";
            }
        }
        if (n == 0 || n == 1)
        {
            std::cout<<"\n---------\n";
        }
    }
    std::cout<<"\n\n";
}

void TicTacToe::setTTTBoard(int x, int y, char XO)
{
    TTTboard[x][y] = XO;
}

void TicTacToe::clearTTTBoard()
{
    for (int n=0; n<3; n++)
    {
        for (int i=0; i<3; i++)
        {
            TTTboard[n][i] = ' ';
        }
    }
}

char TicTacToe::returnTTTBoard(int x,int y)
{
    return TTTboard[x][y];
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PLAYER_H
#define PLAYER_H

class Player
{
    int score;
    char letter;
    public:
        friend class TicTacToe; //probably going to take this out
        Player() {score = 0;}
        virtual ~Player() {}
        void assignLetter(char letterChoice) {letter = letterChoice;}
        char getLetter() {return letter;}
        void addPoint() {score++;}
        int getScore() {return score;} 
        void PlayersTurn();

};

#endif // PLAYER_H 


.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include "Player.h"
#include "TicTacToe.h"



void Player::PlayersTurn()
{
/* i want to be able to call TicTacToe.h's function member to set a letter in the board array and to show the current pieces on the board */
}

void startTTT(Player*,Player*,TicTacToe*,int *);
int winCheckTTT(TicTacToe*);

int main()
{
    char again;
    char letterChoice;
    int *winner = new int;
    *winner = 0;
    Player *player1 = new Player;
    Player *player2 = new Player;
    TicTacToe *TTTgame = new TicTacToe;
    do
    {
    std::cout<<"\t\tHiimnew Games"<<std::endl;
    std::cout<<"\tDefault will be TicTacToe for now."<<std::endl;
    do
    {
        std::cout<<"Player 1 Please choose your letter (X/O) ";
        std::cin>>letterChoice;
        if (letterChoice == 'x' || letterChoice == 'X')
        {
            letterChoice = 'X';
            player1->assignLetter(letterChoice);
            player2->assignLetter('O');
        }
        else if ((letterChoice == 'o') ^ (letterChoice == 'O'))
        {
            letterChoice = 'O';
            player1->assignLetter(letterChoice);
            player2->assignLetter('X');
        }
        else
        std::cout<<"\nBad letter choice, please try again!\n\n";
    } while ((letterChoice != 'X') && (letterChoice != 'O'));
    std::cout<<"Player 1 has been assigned "<<player1->getLetter()<<" and Player 2 has been assigned "<<player2->getLetter()<<"!"<<std::endl;
    startTTT(player1,player2,TTTgame,winner);
    if (*winner == 45) //player 1 wins
    {
        std::cout<<"Player 1 Wins!"<<std::endl;
        player1->addPoint();
    }
    else if (*winner == 67) //player 2 wins
    {
        std::cout<<"Player 2 Wins!"<<std::endl;
        player2->addPoint();
    }
    else if (*winner == 90) //tie
    {
        std::cout<<"Tie!"<<std::endl;
    }
    else
    {
        std::cout<<"Error!"<<std::endl;
    }
    std::cout<<"Play again? (y/n) ";
    std::cin>>again;
    } while ((again == 'y') ^ (again == 'Y'));
    return 0;
}

void startTTT(Player *player1,Player *player2, TicTacToe *TTTgame, int *winner)
{
    //Determines who goes first based on who has more points default is player 1 goes first
    if (player1->getScore() == 0 && player2->getScore() == 0)
    {
       //playersturn member function calls for player 1 and 2 here
    }
    else if (player1->getScore() < player2->getScore())
    {
       //playersturn member function calls for player 1 and 2 here
    }
    else if (player1->getScore() > player2->getScore())
    {
        //playersturn member function calls for player 1 and 2 here
    }
    else
    {
        std::cout<<"ERROR @ line 57"<<std::endl;
    }
    do
    {
        //playersturn member function calls for player 1 here
        *winner = winCheckTTT(TTTgame);
        //playersturn member function calls for player 2 here
        *winner = winCheckTTT(TTTgame);
    } while (*winner == 0);
}

int winCheckTTT(TicTacToe *TTTgame)
{
    return 0;
    //under construction, uses and algorithm to check to see if anyone wins or if its a tie,
    //then returns 1 of 4 values Player1 won, player 2 won, tie, & default to continue the game
}

/*compare 3 letters 123
                    456
                    789

  123 456 789 159 357 147 258 369*/
Last edited on Sep 19, 2011 at 6:46pm
Topic archived. No new replies allowed.