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
|
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
void readSize(std::ifstream &in, int &width, int &height)
{
in >> width >> height;
}
std::vector<int> createArray(int width, int height)
{
std::vector<int> vec;
vec.reserve(width * height); // To hold all of the rows and columns
return vec;
}
int getFromMaze(std::vector<int> &maze, int width, int x, int y)
{
return maze.at(width*y + x);
}
int main(int argc, char **argv)
{
// Open maze file
ifstream infile;
infile.open("maze10.txt");
// Get width and height
int width, height;
readSize(infile, width, height);
// Load maze
std::vector<int> maze = createArray(width, height);
int index = 0;
while (!infile.eof())
{
int nextObject;
infile >> nextObject;
maze.at(index) = nextObject;
++index;
}
// Index a position in the maze
int value = getFromMaze(maze, width, height, 3, 2);
std::cout << "3rd column, 2nd row: " << value << '\n';
}
|