Need help with solving the problem with I need for the next step.

Ok, so the 'gameboard' class part of my program works. It draws the board, shows the board, allows you to move on the board, and spawns the player and enemys at random locations.

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

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

int main()
{

    gameboard board(5,5);

    board.draw();
    board.show();
    board.spawn();
    board.move();

    return 0;
}


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 = 10;
        static const int COL = 10;
        int xCo;
        int yCo;
        char board[ROW][COL];
    public:
        gameboard();
        gameboard(int x, int y);
        void draw();
        void show();
        void move();
        void spawn();
        void areaOfAttack();
};

#endif // 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

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

gameboard::gameboard() : xCo(0), yCo(0){}
gameboard::gameboard(int x, int y) : xCo(x), yCo(y){}
void gameboard::draw(){
    for (int i=0; i < ROW; i++){
        for (int j=0; j < COL; j++){
                this->board[i][j] =  176;

            }
        }
    spawn();
}

void gameboard::show(){
    cout << endl;
    for (int i=0; i < ROW; i++){
        for (int j=0; j < COL; j++){
                cout << this->board[i][j];
            }
        cout << endl;
    }
}

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

    cout << "Type Enter to quit\n";

    while (dir != '\r'){
        cout << "\nYour location is x" << xCo <<",y" << yCo;
        cout << "\nPress direction key(n,s,e,w):";

        dir = getche();
        int tempx = xCo;
        int tempy = yCo;
        bool error = 0;

        switch(dir){
            case 'e':
                if (yCo >= 9){
                    yCo = 9;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }

                else
                    yCo++;
                break;
            case 'w':
                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;
            case 's':
                if (xCo >= 9){
                    xCo = 9;
                    cout << "\n\n********************" << endl;
                    cout << "\nYou\'re running into a wall.\n\n";
                    cout << "********************\n" << endl;
                    error = 1;
                }
                else
                    xCo++;
                break;
            case 'n':
                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 '\r':
                cout << "\n\n********************" << endl;
                cout << "\nHope you enjoy\'d playing, come again!\n\n";
                cout << "********************\n" << endl;
                break;
            default:
                cout << "\n\n********************" << endl;
                cout << "\nYou didn't follow instructions.\n\n";
                cout << "********************\n" << endl;
                error = 1;
        }
        if (!error){
            this->board[xCo][yCo] = 233;
            this->board[tempx][tempy] = 176;
            show();
        }

    }
}

void gameboard::spawn(){
    srand(time(NULL));

    for (int j=0; j < 3; j++){
        int rX = rand() % 9;
        int rY = rand() % 9;
        while (rX == xCo && rY == yCo)
        {
            rX = rand() % 9;
            rY = rand() % 9;
        }
        this->board[xCo][yCo] = 233;
        this->board[rX][rY] = 157;
    }

}

void gameboard::areaOfAttack(){

}


but now in my player class, I am wondering how I am going to connect the movement in the gameboard to the player, and when they get to an enemy cords, the enemy and player class will fight. I am just not sure how to go about connecting theses dots.

The player and enemy class will basically be derived from the same base player class since they both will have hp and attack power, just need help connecting the dots.

This is all I have for the player class
1
2
3
4
5
6
7
8
9
10
11
12
13

#ifndef PLAYER_H
#define PLAYER_H


class person
{
    private:
        int hp, attackPwr, xCo, yCo;
    public:
};

#endif // PLAYER_H 


Thanks for any help!
Last edited on
I think your issue is that the gameboard has too much control.

Perhaps you could change it so the person/player class has the function to get the input, and that will query the gameboard as to whether or not the player can move there.
Thanks, that's what I was starting to think as well.. I did give more control with person with the move command, but I was getting a big buy when trying to make the gameboard and player classes play nice with each other.

I guess its back to the drawing board.
Topic archived. No new replies allowed.