首页 > 解决方案 > 在 C++ 中使用 2D 数组作为游戏地图

问题描述

软件:Visual Studio 2017 社区

大家好你们好,

我正在用 C++ 制作一个简单的 2d 控制台游戏(如果你知道的话,可能是非常简化的 Dwarf Fortress)。

我希望用 ASCII 在控制台中显示地图。

像这样的东西:

在此处输入图像描述

我在头文件(简化版)中声明了一个 WorldMap 类。我在里面声明了一个二维数组。

#pragma once
#include <iostream>

class WorldMap
{
public:
    WorldMap();
    virtual ~WorldMap();

private:
    int worldWidth;
    int worldHeight;
    char worldMap;     // Declare a variable that will hold all the characters for the map
};

然后在 .cpp 文件中定义它:

#include "WorldMap.h"
#include <algorithm>

WorldMap::WorldMap()
{
    worldWidth = 50;
    worldHeight = 50;
    worldMap[50][50];       // Define the map array
    // And then here I will also somehow need to be able to fill the whole map with '.' symbols, and so on
}

这就是我想要实现的基本理念。我不能立即定义数组大小的原因是因为我希望能够在创建地图时选择地图的大小。

我已经尝试过:

  1. 上面的代码。

错误:

error C2109: subscript requires array or pointer type
  1. 将二维数组声明为char worldMap[][];,然后将其定义为worldMap[50][50]; .

错误:

error C2087: 'worldMap': missing subscript
warning C4200: nonstandard extension used: zero-sized array in struct/union
message : This member will be ignored by a defaulted constructor or copy/move assignment operator
  1. 将二维数组声明为char worldMap[worldWidth][worldHeight];,期望在创建对象时,先定义宽度和高度变量,然后再定义数组。

错误:

error C2327: 'WorldMap::worldWidth': is not a type name, static, or enumerator
error C2065: 'worldWidth': undeclared identifier
error C2327: 'WorldMap::worldHeight': is not a type name, static, or enumerator
error C2065: 'worldHeight': undeclared identifier
  1. 使用char* worldMap;and char** worldMap,但到目前为止我什至无法理解双指针是如何工作的,但char* worldMap实际上可以使用一维数组而没有错误,直到我开始访问数组中元素的值。

我想一种解决方法是使用字符串或一维字符数组,并且在显示它时只需使用 mapWidth 来结束每 50 个字符的行,例如,这将给出相同的结果。但我觉得这不是实现这一目标的好方法,因为我需要访问这张地图的 x 和 y 坐标等等。

我想我要问的是:

  1. 为类声明二维数组然后在对象中定义它的最佳方法是什么?
  2. 为此类主机游戏存储地图的最佳方式是什么?(不一定使用数组)

感谢您的阅读。我将非常感谢任何帮助,即使只是想法和提示也可能将我推向正确的方向:)

标签: c++arraysoopvisual-c++

解决方案


  1. 为类声明二维数组然后在对象中定义它的最佳方法是什么?
  2. 为此类主机游戏存储地图的最佳方式是什么?(不一定使用数组)

这不是“最好的方式”,但它是一种方式。

  • 创建一个class包装 1D std::vector<char>
  • 添加operator()s 以访问各个元素。
  • 添加杂项。其他支持功能class,如save()restore()

我以您class为基础并尝试记录它在代码中所做的事情:如果我使用的某些功能不熟悉,我建议您在https://en.cppreference.com/上查找它们,这是一个优秀的 wiki,通常有关于如何使用您所读到的特定功能的示例。

#include <algorithm>   // std::copy, std::copy_n
#include <filesystem>  // std::filesystem::path
#include <fstream>     // std::ifstream, std::ofstream
#include <iostream>    // std::cin, std::cout
#include <iterator>    // std::ostreambuf_iterator, std::istreambuf_iterator
#include <vector>      // std::vector

class WorldMap {
public:
    WorldMap(unsigned h = 5, unsigned w = 5) : // colon starts the initializer list
        worldHeight(h),      // initialize worldHeight with the value in h
        worldWidth(w),       // initialize worldWidth with the value in w
        worldMap(h * w, '.') // initialize the vector, size h*w and filled with dots.
    {}

    // Don't make the destructor virtual unless you use polymorphism
    // In fact, you should probably not create a user-defined destructor at all for this.
    //virtual ~WorldMap(); // removed

    unsigned getHeight() const { return worldHeight; }
    unsigned getWidth() const { return worldWidth; }

    // Define operators to give both const and non-const access to the
    // positions in the map.
    char operator()(unsigned y, unsigned x) const { return worldMap[y*worldWidth + x]; }
    char& operator()(unsigned y, unsigned x) { return worldMap[y*worldWidth + x]; }

    // A function to print the map on screen - or to some other ostream if that's needed
    void print(std::ostream& os = std::cout) const {
        for(unsigned y = 0; y < getHeight(); ++y) {
            for(unsigned x = 0; x < getWidth(); ++x)
                os << (*this)(y, x); // dereference "this" to call the const operator()
            os << '\n';
        }
        os << '\n';
    }

    // functions to save and restore the map
    std::ostream& save(std::ostream& os) const {
        os << worldHeight << '\n' << worldWidth << '\n'; // save the dimensions

        // copy the map out to the stream
        std::copy(worldMap.begin(), worldMap.end(), 
                  std::ostreambuf_iterator<char>(os));
        return os;
    }

    std::istream& restore(std::istream& is) {
        is >> worldHeight >> worldWidth;            // read the dimensions
        is.ignore(2, '\n');                         // ignore the newline
        worldMap.clear();                           // empty the map
        worldMap.reserve(worldHeight * worldWidth); // reserve space for the new map

        // copy the map from the stream
        std::copy_n(std::istreambuf_iterator<char>(is),
                    worldHeight * worldWidth, std::back_inserter(worldMap));
        return is;
    }

    // functions to save/restore using a filename
    bool save(const std::filesystem::path& filename) const {
        if(std::ofstream ofs(filename); ofs) {
            return static_cast<bool>(save(ofs)); // true if it suceeded
        }
        return false;
    }

    bool restore(const std::filesystem::path& filename) {
        if(std::ifstream ifs(filename); ifs) {
            return static_cast<bool>(restore(ifs)); // true if it succeeded
        }
        return false;
    }

private:
    unsigned worldHeight;
    unsigned worldWidth;

    // Declare a variable that will hold all the characters for the map
    std::vector<char> worldMap;
};

演示


推荐阅读