C++ 二维数组迷宫导航

标签 c++ arrays multidimensional-array

我是 C++ 的新手,想知道我这样做是否是最好的方式。我需要从文本文件中读取一行并从中构建一个数组,然后对其进行导航。如果我做错了什么,请告诉我。我可以像现在这样访问矩阵吗?

标题:

#ifndef MAZE_HPP_
#define MAZE_HPP_

#include <fstream>
#include <vector>
#include <string>

class Maze
{
public:
    Maze(int size);
    ~Maze() {}

    enum Direction { DOWN, RIGHT, UP, LEFT };

    // Implement the following functions:

    // read maze from file, find starting location
    void readFromFile(std::ifstream &f);

    // make a single step advancing toward the exit
    void step();

    // return true if the maze exit has been reached, false otherwise
    bool atExit();

    // set row and col to current position of 'x'
    void getCurrentPosition(int &row, int &col);

    // You can add more functions if you like
private:
    // Private data and methods
    int size;
    static string matrix[30][30];
};


#endif /* MAZE_HPP_ */

C++文件:

#include <iostream>
#include "maze.hpp"
#include "utils.hpp"
#include <vector>
#include <string>

Maze::Maze(int size) {
    size = size;
}

Maze::void readFromeFile(std::ifstream &f) {
    std::string line;
    int i, j;
    while(std::getline(f, line)) {
        for(i = 0; i < line.length(); i++) {
            for(j = 0; j < line.length(); j++) {
                matrix[i][j] = line.at(j);
            }
        }
    }
}

Maze::void step() {

}

Maze::bool atExit() {

}

Maze::void getCurrentPosition(int &row, int &col) {

}

最佳答案

Maze::void readFromeFile {}
Maze::void step() {}
Maze::bool atExit() {}
Maze::void getCurrentPosition(int &row, int &col) {}

这些应该是

void Maze::readFromeFile{}
void Maze::step() {}
bool Maze::atExit(){}
void Maze::getCurrentPosition(int &row, int &col){}

关于C++ 二维数组迷宫导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540086/

相关文章:

c++ - 使用之前在 if 语句中定义的变量

c++ - 将字符串作为输入并将其拆分为单词的函数。标点符号应该被忽略

c++ - 针对自定义构建的 tcl85.lib 的链接器错误,适用于 ActiveState 分发的 tcl85.lib

c - 错误:Array subscript is not an integer

arrays - 如何在 matlab 中创建由多个 3d 图像数据数组组成的数组

c++ - 运算符 [] 重载

c - 如何在 C 中将 char* 添加到 char**

javascript - 如何从javascript中的两个json数组中获取不匹配的对象

javascript - 如何在 JavaScript 中匹配二维数组中的对?

python - 比较python中的多维数组