Trying to set up my classes properly, but I can't get them to work together.

ok, so I know there is inheritance and friend that I can use on a class to kind of link two of them together. From what I understand inheritance is an extension of another class. SO if my two classes have nothing to do with each other they shouldn't be derived. As I believe my class fall under.

So I have two classes, Gameboard which creates, displays, and sets the players location on the gameboard.

And then I have the player class, which allows the player to move around the board.

I understand how to display the board and everything and get the player to move in ONE class, but when I try to organize my code and make it proper coding by splitting everything up, I am not sure how I get the Gameboard array to change its value from the player class (the array is what holds the x,y and the current value for that position board[x][y]

Does anyone know how I can make it so that when the player changes their x, y cords that its transferred to the gameboard array, and it changes the value accordingly.

I was thinking something like this

1
2
GameBoard::set(tile, tempX, tempY); //changes old x,y to standard gameboard tile
Gameboard::set(player, xCo, yCo); //changes current x,y to player tile. 


but I just can't get everything linked up properly.

here are the files,

gameboard.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
#ifndef GAMEBOARD_H
#define GAMEBOARD_H


class Gameboard
{
    private:
       static const int ROW = 15;
       static const int COL = 25;
       static const char tile = 176;
       char board[ROW][COL];
       int xCo;
       int yCo;
    public:
        Gameboard() : xCo(0),yCo(0){}
        Gameboard(int r, int c) : xCo(r), yCo(c){}
        void create();
        void display();
        void set(char player);
        void set(char player, int x, int y);
};

#endif // GAMEBOARD_H


gameboard.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
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;

void Gameboard::create(){
    for (int j = 0; j < ROW; j++){
        for (int i = 0; i < COL; i++){
            board[j][i] = tile;
        }
    }
}

void Gameboard::display(){
    for (int j = 0; j < ROW; j++){
        for (int i = 0; i < COL; i++){
            cout << board[j][i];
        }
        cout << endl;
    }
}

void Gameboard::set(char player){
    this->board[xCo][yCo] = player;
}

void Gameboard::set(char player, int x, int y){
    this->board[x][y] = player;
}


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


class Player
{
    private:
        static const char player = 233;
        int hp;
        int attackPower;
        int xCo;
        int yCo;
    public:
        Player() : hp(100), attackPower(5), xCo(5), yCo(5) {}
        Player(int h, int ap, int x, int y) : hp(h), attackPower(ap), xCo(x), yCo(y) {}
        char setPlayer();
        void move();
};

#endif // PLAYER_H


player.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
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "gameboard.h"
#include "player.h"
using namespace std;

char Player::setPlayer(){
    return player;
}

void Player::move(){
    char dir = 'x';

    while (dir != '/r'){
        cout << "Your location is x:" << xCo << " y:" << yCo << endl;
        cout << "User W(up), A(left), S(down), D(right) to move." << endl;

        dir = getche();
        int tempX = xCo;
        int tempY = yCo;
        bool error = 0;

        switch(dir){
            case 'w':
                if (xCo >= 15)
                    xCo = 15;
                else
                    xCo++;
                break;
            case 's':
                if (xCo <= 0)
                    xCo = 0;
                else
                    xCo--;
                break;
            case 'a':
                if (yCo >= 25)
                    yCo = 25;
                else
                    yCo++;
                break;
            case 'd':
                if (yCo <= 0)
                    yCo = 0;
                else
                    yCo--;
                break;
        }
    }


}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;

int main()
{
    Gameboard board(5,5);
    Player p1;

    board.create();
    board.display();
    board.set(p1.setPlayer());
    board.display();
    p1.move();

    return 0;
}


thanks for any help and possible leads in the right direction.

cheers.
Last edited on
You'll become a good coder if you keep on worrying over how best to organise your code (true - not a joke)

There is often no one best way but there is one rule - don't duplicate data!

So the players position should be either in the gameboard or in the player but not both.

This gives you a 100% guarantee that they will always be aligned!

I would incline to having the position stored in the player - if the gameboard wants to know where the player is it will have to ask it.

Alternatively the coordinates could be in the gameboard, but then when the player wants to know where it is or get itself moved then it will have to ask the gameboard, but then the gameboard has to know which player is doing the asking.
Thank you for the reply.

Yeah that's what I was thinking.. The xCo, and yCo where in both gameboard and class, cause I wasn't sure how I was going to connect everything yet. I was also leaning towards storing the xCo and yCo in player.

So my question is, how will I be able to access and changed the board array from player by getting the xCo and yCo of player and change the value accordingly? Should I make the array protected, and then have the player be a 'friend' of gameboard?

Again, thanks for anything help. It's much appreciated.
This is what I tried just now with friending the gameboard with player.

1
2
3
4
gameboard.board[xCo][yCo] = 178;
gameboard.board[tempX][tempY] = 176;
    for (int i = 0; i < 15; i++){
        cout << gameboard.board[i][i] << endl;


this is in the player class, after I made the two friends, and moved the board[x][y] array to protected.

Ok so it work somewhat, but the problem I am having is when I call the array to print it out, or just get access to it, it changes ALL the values to random 'char' characters every time. Is there a way to prevent this?

so my array instead of being one value that I specified, it is for some reason adding a bunch of random values instead, when called.

instead of my table looking like this


#######
#######
#######
#######


it looks like this instead


$_#!*($(@
gj*(@94_!
(^%2*@!!
&%!(*%0!


and I only this problem when I access the array outside of the gameboard class.

edit: HAHA! I think I solved it. I had to "create" the variable with the values I want in my move method. I guess if I don't set it before, its going to just add random values to what ever it wants. Heres my current code, that works for what I am looking for atm.
gameboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef GAMEBOARD_H
#define GAMEBOARD_H


class Gameboard
{
    private:
       static const int ROW = 15;
       static const int COL = 25;
       static const char tile = 176;
       int xCo;
       int yCo;
    protected:
        char board[ROW][COL];
    public:
        Gameboard(){}
        void create();
        void display();
    friend class Player;
};

#endif // GAMEBOARD_H 


gameboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;

void Gameboard::create(){
    for (int j = 0; j < ROW; j++){
        for (int i = 0; i < COL; i++){
            board[j][i] = tile;
        }
    }
}

void Gameboard::display(){
    for (int j = 0; j < ROW; j++){
        for (int i = 0; i < COL; i++){
            cout << board[j][i];
        }
        cout << endl;
    }
}


player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef PLAYER_H
#define PLAYER_H


class Player
{
    private:
        Gameboard gameboard;
        static const char player = 233;
        int hp;
        int attackPower;
        int xCo;
        int yCo;
    public:
        Player() : hp(100), attackPower(5), xCo(5), yCo(5) {}
        Player(int h, int ap, int x, int y) : hp(h), attackPower(ap), xCo(x), yCo(y) {}
        char setPlayer();
        void move();

};

#endif // PLAYER_H 


player.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

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "gameboard.h"
#include "player.h"
using namespace std;

char Player::setPlayer(){
    return player;
}

void Player::move(){
    char dir = 'x';
    gameboard.create();

    while (dir != '/r'){
        cout << "Your location is x:" << xCo << " y:" << yCo << endl;
        cout << "Use W(up), A(left), S(down), D(right) to move." << endl;

        dir = getche(); cout << endl;
        int tempX = xCo;
        int tempY = yCo;
        bool error = 0;

        switch(dir){
            case 's':
                if (xCo >= gameboard.ROW-1){
                    xCo = gameboard.ROW-1;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }
                else
                    xCo++;
                break;
            case 'w':
                if (xCo <= 0){
                    xCo = 0;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }

                else
                    xCo--;
                break;
            case 'd':
                if (yCo >= gameboard.COL-1){
                    yCo = gameboard.COL-1;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }
                else
                    yCo++;
                break;
            case 'a':
                if (yCo <= 0){
                    yCo = 0;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }

                else
                    yCo--;
                break;
        }
    if (!error){
        gameboard.board[xCo][yCo] = player;
        gameboard.board[tempX][tempY] = gameboard.tile;
        gameboard.display();
    }

    }


}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;

int main()
{
    Gameboard board;
    Player p1;

    p1.move();

    return 0;
}


Any tips would be much appreciated.
Last edited on
Topic archived. No new replies allowed.