Search code examples
c++maze

c++ : OOP : How do you update an array in a separate header file?


I'm new to c++ and am trying to create a basic maze game. Currently, I'm trying to enable the player to move about the maze, in which the player's movement function updates the characters in the grid array. The grid array is in Grid.h and the Player is in Player.h and I've included the headers in each file but they still can't access each other's member variables. I tried to use 'extern' but that hasn't worked either.

My teacher has suggested to send the m_grid array to the constructor but hadn't specified which constructor or in any case, how to do it. Any help would be appreciated, thank you.

Grid.h

#include <iostream>
#include "Player.h"

class Grid
{
public:
    Grid();
    ~Grid();
    void Print_grid();
    char m_grid[18][32] =
    {
        {"_______________________________"},
        {"|   _______  ____________     |"},
        {" @                            |"},
        {"|_  _____________   __________|"},
        {"|   |                         |"},
        {"| | |__  |  |       |   |     |"},
        {"| |      |  | |___  |   |  |  |"},
        {"| | _____|  | |      ___|  |  |"},
        {"| |           |     |      |  |"},
        {"| |_________         _____    |"},
        {"|                  _______  __|"},
        {"|_______________              |"},
        {"|_____    _______________|  __|"},
        {"|         |        _______    |"},
        {"|   |  ____________   ________|"},
        {"|   |     #                   |"},
        {"|   |  |      __|________     |"},
        {"|______|______________________|"}
    };
};

Player.h

#pragma once
#include "Grid.h"
#include "Enemy.h"

extern char y;
extern char x;
extern char m_grid[18][32];

class Player 
{
public:
    Player();
    ~Player();
    static const char avatar = '@';
    void Set_difficulty();
    void Move(int speed);
    int m_speed{ 0 };
    struct Vector2
    {
        int y{ 0 };
        int x{ 1 };

        int new_y{ 0 };
        int new_x{ 0 };
    };
    Vector2* playerPos = nullptr;

    int m_score{ 0 };
    int m_highscore{ 0 };
    void Print_score();

private:
    char m_difficulty = ' ';
};

Player.cpp

#include <iostream>
#include "Player.h"

Player::Player()
{
    m_grid[playerPos.y][playerPos.x] = avatar;
}

Player::~Player()
{
}

void Player::Set_difficulty()
{

    std::cout << "Pick your difficulty:\nH -> Hard\nN -> Normal\nE -> Easy\n\nInput : ";
    std::cin >> m_difficulty;

    if (m_difficulty == 'e' || m_difficulty == 'E')
    {
        m_speed = 3;
    }
    else if (m_difficulty == 'n' || m_difficulty == 'N')
    {
        m_speed = 2;
    }
    else
    {
        m_speed = 1;
    }

}

void Player::Move(int speed)
{
    if (GetAsyncKeyState(VK_DOWN))
    {
        if (m_grid[playerPos->y+1][playerPos->x] == ' ' || m_grid[playerPos->y + 1][playerPos->x] == '*') //if desired space is empty or has a coin
        {
            playerPos->new_y = playerPos->y + speed; //new player pos = current pos + speed
            m_grid[playerPos->y][playerPos->x] == ' '; //old space is now empty
        }
    }
    if (GetAsyncKeyState(VK_UP))
    {
        if (m_grid[playerPos->y-1][playerPos->x] == ' ' || m_grid[playerPos->y-1][playerPos->x] == '*')
        {
            playerPos->new_y = playerPos->y - speed;
            m_grid[playerPos->y][playerPos->x] == ' ';
        }
    }
    if (GetAsyncKeyState(VK_RIGHT))
    {
        if (m_grid[playerPos->y][playerPos->x+1]== ' ' || m_grid[playerPos->y][playerPos->x+1] == '*')
        {
            playerPos->new_x = playerPos->x + speed;
            m_grid[playerPos->y][playerPos->x] == ' ';
        }
    }
    if (GetAsyncKeyState(VK_LEFT))
    {
        if (m_grid[playerPos->y][playerPos->x-1] == ' ' || m_grid[playerPos->y][playerPos->x-1] == '*')
        {
            playerPos->new_x = playerPos->x - speed;
            m_grid[playerPos->y][playerPos->x] == ' ';
        }
    }

}

void Player::Print_score()
{
}

Solution

  • First: grid.h and player.h include each other. This should be avoided and in this case, grid.h doesn't need player.h.

    Actual issue: there is no Grid object that can be used by your player. Ideally, Grid and Player should be members of a Game class and a reference to the Grid object should be passed to Player. so it can use Grid::m_grid. As it is now, you use another m_grid that is only declared as external, so there is no real instantiation. Without Game class, this can be done in main() for a simple exercise, but you do need to declare a Grid object to access it and player needs a reference to it if it want to move based on the grid.