Town.h
/*
Programmer: Jennifer Leopold
Date: November 10, 2016
File: town.h
Purpose: This file contains the definition of a Town class.
*/
#ifndef TOWN_H
#define TOWN_H
#include <iostream>
#include <cstdlib>
#include "point.h"
using namespace std;
// Constants for min and max dimensions of grid
const short MAX_SIZE = 25;
const short MIN_SIZE = 4;
// Constants for char's to display on grid
const char EMPTY_DISPLAY = ' ';
const char WALL_DISPLAY = 'W';
const char EXIT_DISPLAY = 'E';
const char ERROR_DISPLAY = '\0';
class Town
{
private:
// Member variables for grid array and its actual size
char m_grid[MAX_SIZE][MAX_SIZE];
short m_size;
// Clear the grid by setting all entries to EMPTY_DISPLAY.
// Preconditions: None
// Postconditions: All entries in the grid will have been
// set to EMPTY_DISPLAY.
void clearGrid();
// Populate the grid with walls along the boundary of the
// grid and an exit in the middle of each boundary wall.
// Preconditions: None
// Postconditions: The grid will now include walls
// (WALL_DISPLAY chars) along the boundaries of the grid
// and an exit(EXIT_DISPLAY char) in the middle of each
// boundary wall.
void build();
// Determine whether a given point is within the boundaries
// of the grid.
// Preconditions: Point p should have values for its x and
// y coordinates.
// Postconditions: Returns true is p is within the
// boundaries of the grid; otherwise, returns false.
bool inRange(const Point &p) const;
public:
// Parameterized constructor for Town.
// Preconditions: None
// Postconditions: m_size set to size and m_grid
// initialized with walls (WALL_DISPLAY char) and
// exits (EXIT_DISPLAY char).
Town(const short gridSize = MAX_SIZE);
// Accessor for m_size.
// Preconditions: None
// Postconditions: Value of m_size is returned.
short getSize() const { return m_size; }
// Get the 'object' (symbol) that is at the specified
// point in the grid.
// Preconditions: Point p should have values for its x and
// y coordinates, and should be within the boundaries of
// the grid.
// Postconditions: If the point is not within the boundaries
// of the grid, ERROR_DISPLAY is returned. Otherwise, the
// symbol (char) that is at the specified point in the grid
// is returned.
char getLocation(const Point p) const;
// Make the specified location in the grid 'empty.'
// Preconditions: Point p should have values for its x and
// y coordinates, and should be within the boundaries of
// the grid.
// Postconditions: m_grid will have value EMPTY_DISPLAY at
// position p.
void vacateLocation(const Point &p);
// Set the 'object' (symbol) at the specified point in the
// grid.
// Preconditions: Point p should have values for its x and
// y coordinates, and should be within the boundaries of
// the grid. The symbol should have a value.
// Postconditions: If point p was not within the
// boundaries of the grid, false is returned. Otherwise,
// point p in the grid has been changed to the specified
// symbol and true is returned.
bool setLocation(const Point p, const char symbol);
// Make sure that grid contains at least one position
// that is "empty." Otherwise, exit the program.
// Preconditions: m_grid should be initialized with values.
// Postconditions: Exits the program if grid doesn't contain
// at least one position that is EMPTY_DISPLAY; otherwise,
// simply returns;
void checkForOverPopulatedTown() const;
// Randomly choose a point in the grid that is "empty."
// Preconditions: m_grid should be initialized with values.
// Postconditions: Returns a randomly chosen Point that
// equals EMPTY_DISPLAY.
Point getRandomEmpty() const;
// Determine whether a given point in the grid is "empty."
// Preconditions: Point p should have values for its x and
// y coordinates, and should be within the boundaries of
// the grid.
// Postconditions: Returns true if point p in the grid is
// not EMPTY_DISPLAY; otherwise, returns false.
bool positionOccupied(const Point &p) const
{ return(m_grid[p.getX()][p.getY()] != EMPTY_DISPLAY); }
// Overloaded operator for <<.
// Preconditions: None
// Postconditions: Town's grid will be output to outs,
// thereby modifying ostream outs, and outs is returned.
friend ostream& operator <<(ostream& outs,
const Town& town);
};
#endif