Counter

Hi, I was wondering how to add the counter bit of the instructions described below into my program. Thanks.


Create the maze
Get a pointer to the start.
Make another pointer for a Cell
Make a stack
Push the start on the stack
Draw the maze
Start the two counters (total steps, steps in path)
While the stack is not empty
Get a pointer to the cell on top of the stack (current cell)
If it’s the end, were done
Update counters
Update symbol to X
Break out of loop
Update the current cell’s symbol to O
Draw the maze
Get a pointer to a neighbouring cell
If the neighbour exists
Update counters
Update current cell’s symbol to o
Push the new cell on the stack
If there are no valid neighbours, we backtrack
Update counters
Set symbol to .
Pop the cell off the stack
Make the program sleep a so it prints nicely (I did 250ms)
Print out the counters
Draw the maze one final tie
Delete things


#include <chrono>
#include <thread>
#include <iostream>
#include "ArrayCellStack.hpp"
#include "Cell.h"
#include "Maze.h"
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
int filenumber;
cout << "enter the maze file number" << endl;
cin >> filenumber;


if (filenumber == 1)
{
Maze* m= new Maze(argv[1]);
Cell* currentcell;
ArrayCellStack* s= new ArrayCellStack();
(*s).push((*m).getStart());
(*m).draw();

while ((*s).is_empty() == 0)
{
currentcell=(*s).peek();

int check2=(*currentcell).isEnd();
if (check2== 0)
{
(*currentcell).setSymbol('X');
}
}
currentcell=(*s).peek();
(*currentcell).setSymbol('O');
(*m).draw();
Cell* neighbour=(*m).getNextNeighbour((*currentcell).getX(),(*currentcell).getY());

if (neighbour != nullptr)
{
(*currentcell).setSymbol('o');
(*s).push(neighbour);
}

if (neighbour == nullptr)
{
(*currentcell).setSymbol('.');
(*s).pop();
}

(*m).draw();
(*s).~ArrayCellStack();
(*m).~Maze();

return 0;
}
Last edited on
that is just about unreadable, so I will leave you with a thought (apart from the ones about using code tags and good variable names).


the keyword static in a function makes that variable exist for the span of the program and retain its value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
using namespace std;  

int funtimes()
{
    static int cool = 0;
    return ++cool;

}


int main()
{
cout << funtimes() << endl;
cout << funtimes() << endl;
cout << funtimes() << endl;
cout << funtimes() << endl;
}


I am not sure what you are counting, but if its number of calls to a function, this will help. If its just in a loop or something else, just count it directly.
Last edited on
Topic archived. No new replies allowed.