I don’t need help copying the files. I need to know where and what needs to be p
ID: 3914368 • Letter: I
Question
I don’t need help copying the files. I need to know where and what needs to be placed in order to correctly implement the functions readMaze and solveMaze in the asg8.cpp file. Thank you Introduction To C++ Programming Assignment 8 I am aMAZEd! objectives: Learn how to use two-dimensional azzays. Gain further experience with file I/O and a SFML graphical API Instructions: This assignment is to write a progran that will read in a maze and determine a path from the starting point to the finish point of the maze. You should first prompt the user for a filenane containing the apecifications for a maze. This file should first contain two integer values representing the number of rows and columns in the grid. The remainder of the file will contain one line for each zow of the maze. Below is a sample maze input file: 6 7 wwswww x WW You should read in the number of rows and columns and check that they do not exceed the maximum rows or columns for a grid. For this project the value is 50. If they do, then you should print an appropriate error message and quit. The w character stands for a wall of the maze. The s stands for the starting point and the x stands for the exit. You should read in the characters representing the naze and fi1l in the entries that represent parts of the wall. You should check that theze is exactly one starting and one exit point. If not. then print an appropriate error message and quit. The nainoO in asg8.cpp (a student template project source file) includes the declaration of the following local variables int rows, cols:umber of rows and columcs read fron input file int states [MAX GRID RO?MAX GRID COL]; //naze state int startRow; int startcol: int exitRow int exitCol: 7 zow of starting position 7colum of starting po 77 zow of exit posiio colm of exit positi For this assignment, you are required to implenent the following two functions in asg8.app. void readMaze (ifstream &fin;, int states[1 [MAX GRID COL) int rows, int cols, int &startRow;, int &startCol;, int sexitRow, int SexitCol) void solveMaze (Maze smaze, int statesi1 MAX_GRID COL],int rows, int cols, int startRow. int startCol. int exitRow int exitCol):Explanation / Answer
asg8.cpp
#include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>
#include <random>
#include "maze.h"
using namespace std;
// 2 Function Prototypes - you are only required to
// define (implement) the following 2 functions
//
void readMaze(ifstream &fin, int states[][MAX_GRID_COL],
int rows, int cols, int &startRow, int &startCol,
int &exitRow, int &exitCol);
void solveMaze(Maze &maze, int states[][MAX_GRID_COL],int rows, int cols,
int startRow, int startCol, int exitRow, int exitCol);
// main entry for aMazed application
int main(int argc, char *argv[]) {
int rows, cols;
int states[MAX_GRID_ROW][MAX_GRID_COL]; // maze state
int startRow; // row of starting position
int startCol; // column of starting position
int exitRow; // row of exit position
int exitCol; // column of exit position
startRow = startCol = exitRow = exitCol = -1;
// Get the maze filename and open the file.
cout << "Enter the maze data filename: " << flush;
string Filename;
cin >> Filename;
ifstream fin(Filename.c_str());
// Read the number of rows and cols.
fin >> rows;
fin >> cols;
if (rows >= MAX_GRID_ROW || cols >= MAX_GRID_ROW) {
cerr << "Invalid grid size" << endl;
exit(EXIT_FAILURE);
}
// Read maze configuration from the input file stream object
readMaze(fin, states, rows, cols, startRow, startCol, exitRow, exitCol);
fin.close();
sf::RenderWindow window(sf::VideoMode(600, 800), "I am aMAZEd!!");
float width = (window.getSize().x * 1.0) / cols;
float height = (window.getSize().y * 1.0) / rows;
Maze maze(rows, cols, width, height, states);
maze.createMaze();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
solveMaze(maze, states, rows, cols, startRow,
startCol, exitRow, exitCol);
break;
default:
window.clear();
window.draw(maze);
window.display();
break;
}
}
}
return 0;
}
void readMaze(ifstream &fin, int states[][MAX_GRID_COL],
int rows, int cols, int &startRow, int &startCol,
int &exitRow, int &exitCol)
{
// Initialize the starting and exit points.
startRow = -1;
startCol = -1;
exitRow = -1;
exitCol = -1;
// read in the characters of the maze
char c;
fin.get(c);
cout << "***YOU NEED TO IMPLEMENT readMaze function***" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
}
}
}
void solveMaze(Maze &maze, int states[][MAX_GRID_COL],int rows, int cols,
int startRow, int startCol, int exitRow, int exitCol) {
int steps = 1;
states[startRow][startCol] = steps;
states[exitRow][exitCol] = SPACE;
int row = startRow;
int col = startCol;
cout << "***YOU NEED TO IMPLEMENT solveMaze function**!" << endl;
// restore start and exit value
states[startRow][startCol] = START_M;
states[exitRow][exitCol] = EXIT_M;
maze.updateMazeCellColor(exitRow,exitCol,EXIT_M);
return;
}
Cell.cpp
#include "cell.h"
Cell::Cell() {
// nothing
}
void Cell::addCell(float size_x, float size_y, float pos_x, float pos_y, sf::Color color)
{
curCell.setSize(sf::Vector2f(size_x,size_y));
curCell.setFillColor(color);
curCell.setOutlineColor(sf::Color::White);
curCell.setOutlineThickness(-1);
curCell.setPosition(sf::Vector2f(pos_x,pos_y));
cellContainer.push_back(curCell);
}
void Cell::draw(sf::RenderTarget &target, sf::RenderStates) const
{
for (int i = 0; i < cellContainer.size(); i++)
target.draw(cellContainer[i]);
}
cell.h
#ifndef GUI_MAZE_CELL_H
#define GUI_MAZE_CELL_H
#include <SFML/Graphics.hpp>
class Cell: public sf::Drawable
{
public:
Cell();
sf::FloatRect getCell(sf::RectangleShape rect) const;
void addCell(float size_x, float size_y, float pos_x, float pos_y, sf::Color color);
std::vector<sf::RectangleShape> getContainer() {
return cellContainer;
};
private:
sf::RectangleShape curCell;
std::vector<sf::RectangleShape> cellContainer;
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};
#endif //GUI_MAZE_CELL_H
maze.cpp
#include <SFML/Graphics.hpp>
#include "maze.h"
#include <iostream>
void Maze::createSFMLMaze(std::vector<std::vector<int> > &matrix) {
int r = matrix.size();
int c = matrix[0].size();
std::cout << "row: " << r << std::endl;
std::cout << "col: " << c << std::endl;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
sf::Color color;
switch (gridStates[i][j]) {
case SPACE:
color = sf::Color(100,150,200);
break;
case WALL:
color = sf::Color::Black;
break;
case TRIED:
color = sf::Color(125,125,0);
break;
case START_M:
color = sf::Color(200,200,0);
break;
case EXIT_M:
color = sf::Color(255,255,0);
break;
default:
color = sf::Color(255,0,0); // default RED color
break;
}
grid.addCell(cellSizeX, cellSizeY, cellSizeX * j, cellSizeY * i, color);
}
}
}
void Maze::updateMazeCellColor(int r, int c, int state) {
switch (state) {
case TRIED:
sfmlMaze[r*col+c].setFillColor(sf::Color(125,125,125));
break;
case SPACE:
sfmlMaze[r*col+c].setFillColor(sf::Color(100,150,200));
break;
case START_M:
sfmlMaze[r*col+c].setFillColor(sf::Color(200,200,0));
break;
case EXIT_M:
sfmlMaze[r*col+c].setFillColor(sf::Color(255,255,0));
break;
default:
if (state > SPACE)
sfmlMaze[r*col+c].setFillColor(sf::Color(0,255,0));
break;
}
}
void Maze::genMaze(std::vector<std::vector<int> > &matrix, int r, int c) {
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
// TODO: this needs to be set randomly
matrix[i][j] = 1;
}
}
void Maze::createMaze() {
std::vector<std::vector<int> > matrix(row, std::vector<int>(col));
genMaze(matrix,0,0); // generate maze starting from uppper
createSFMLMaze(matrix);
sfmlMaze = grid.getContainer();
}
void Maze::draw(sf::RenderTarget &target, sf::RenderStates states) const {
for (int i = 0; i < sfmlMaze.size(); i++)
target.draw(sfmlMaze[i]);
}
maze.h
#ifndef GUI_MAZE_GRID_H
#define GUI_MAZE_GRID_H
#include "cell.h"
#include <SFML/Graphics.hpp>
// global constants for maze project
const int MAX_GRID_ROW = 50;
const int MAX_GRID_COL = 50;
const int START_M = -2;
const int EXIT_M = -3;
const int SPACE = 0;
const int WALL = -1;
const int TRIED = -4;
class Maze: public sf::Drawable {
public:
Maze(){};
Maze(int r, int c, float sizeX, float sizeY, int state[][MAX_GRID_COL]):
row(r),col(c),cellSizeX(sizeX), cellSizeY(sizeY) {
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
gridStates[i][j] = state[i][j];
};
~Maze() {};
void createMaze();
std::vector<sf::RectangleShape> sfmlMaze;
void updateMazeCellColor(int r, int c, int state);
private:
int row, col;
float cellSizeX, cellSizeY;
int gridStates[MAX_GRID_ROW][MAX_GRID_COL];
Cell grid; // Rectangle grid
void createSFMLMaze(std::vector<std::vector<int> >& matrix);
void genMaze(std::vector<std::vector<int> >& matrix, int x, int y);
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};
#endif //GUI_MAZE_GRID_H
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.