summaryrefslogtreecommitdiff
path: root/city.h
blob: de8e17f617dccfccbf304c5e26816a1325829688 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef CITY_H
#define CITY_H
// Programmer: Brett Weiland
// Date: 4/30/23
// File: city.h
// Assignment: SP-B-finalProject
// Purpose: Contains the city object, 
//          which acts as a game engine for gameObject based objects to interact with eachother.
//          Also containts game over conditions.


#include <iostream>
#include <cstddef>
#include <climits>

#include "gameObject.h"
#include "coordinate.h"
#include "npc.h"
#include "cop.h"
#include "robber.h"
#include "jewl.h"


class city {
  private:
    //inventory for NPCs are relient on this being 47 unless modified.
    //Please see npc.h for explaination.
    static const unsigned int JEWL_COUNT = 47; 
    static const unsigned int ROBBERS_COUNT = 4;  
    static const unsigned int COPS_COUNT = 1;
    const unsigned int ROBBERS_GREEDY = 2; //must be lower then JEWL_COUNT
    
    //map dimentions
    static const int MAP_WIDTH = 10;
    static const int MAP_HEIGHT = 10;
    
    //used for keeping track of which jewls are free outside of map_ptr;
    //useful for finding random free jewls
    unsigned int jewls_available_qty = JEWL_COUNT;
    jewl *jewls_available[JEWL_COUNT];

    //this array of gameObject pointers is useful for looking up objects on location,
    //as well as keep track of where objects are in a sane way.
    //If objects are stacked, they are linked in a doubly linked list for that cell.
    //If there are no objects, the cell is a null pointer.
    gameObject *map_ptr[MAP_HEIGHT][MAP_WIDTH] = {NULL};

    char rendered_map[MAP_HEIGHT][MAP_WIDTH]; //map as seen by humans. use for display only

    static unsigned int rounds_taken;
    
    //win conditions for robbers.
    //robbers win if they collectively get more then ROBBER_WIN_VALUE or uncaught in MAX_ROUNDS
    const unsigned int ROBBER_WIN_VALUE; //if robber gets more money then this, they win
    const unsigned int MAX_ROUNDS;      //if robbers aren't caught b

    
    //gameObjects
    cop<jewl> cops[COPS_COUNT];
    robber<jewl> robbers[ROBBERS_COUNT];
    jewl jewls[JEWL_COUNT];


    //Desc: gets a random UNOCUPIED location in the map.
    //Pre:  map_ptr is updated with objects to avoid stacking.
    //Post: random unocupied coordinate within bounds returned
    coordinate randomLocation() const;

    //Desc: uses map_ptr to generate a 2d array of characters, rendered_map.
    //      when gameObjects are stacked, the first one on the linked list is printed.
    //      checks the gameObject's get_icon() function.
    //Pre:  map_ptr updated
    //Post: rendered_map updated
    void renderMap();

    //Desc: tests if npc derived class is movable, and calls its move() function.
    //      updates map_ptr accordingly.
    //Pre:  valid npc passed
    //Post: character is moved, map_ptr updated
    void moveNPC(npc<jewl> &character);

    //Desc: tests to see if all robbers arrested for game over condition.
    //Pre:  none
    //Post: returns true if all robbers arrested
    bool allRobbersArrested() const;


  public:
    //used to communicate if/why the game has stopped running.
    enum class CITY_STATUS { CONTINUE, COPS_WIN, ROBBERS_MAX_VAL, ROUNDS_DEPLETED };

    //Desc: city constructor
    //Pre:  win conditions passed (see docs in private section of this object)
    //Post: city is constructed along with all of its gameObjects
    city(const unsigned int robber_win_value, const unsigned int rounds);


    //Desc: operate city for a single turn.
    //Pre:  none
    //Post: CITY_STATUS describing win condition or nothing returned
    CITY_STATUS runTurn();

    //Desc: Adds gameObject to map_ptr location.
    //      order in doubly linked list based on gameObject.get_drawPriority().
    //Pre:  gameObject with updated coordinates passed
    //Post: gameObject added to map_ptr
    void addMapMarker(gameObject &item);

    //Desc: removes gameObject from map_ptr location.
    //Pre:  coordinates of gameObjects reflect where it is on the map_ptr
    //Post: gameObject removed from that location on map_ptr
    void delMapMarker(gameObject &item);

    //Desc: prints rendered_map.
    //Pre:  rendered_map is updated using renderMap()
    //Post: map is printed
    void printMap() const;

    //Desc: finds top gameObject in location. 
    //      Other gameObjects at that location can be found using the top's linked list.
    //Pre:  coordinate for location to check is passed
    //Post: pointer to top gameoOject returned or NULL if nothing resides that space
    gameObject *findObjsByLocation(const coordinate loc);

    //Desc: gets a random directoin while avoiding borders.
    //Pre:  coordinates to get direction from passed
    //Post: random direction is returned
    direction::TYPE randomDirection(const coordinate &scanner) const;

    //Desc: gets direction to nearest cop. 
    //Pre:  one cop must exist.
    //Post: direction to nearest cop returned.
    direction::TYPE nearestCopDirection(const coordinate &scanner) const;

    //Desc: gets the direction to any jewl in jewls_available.
    //Pre:  one unpicked jewl must exist.
    //Post: direction to any random unpicked jewl returned.
    //      returns random direction if no jewl found.
    direction::TYPE directionToAnyJewl(const coordinate &scanner) const;

    //Desc: unarrests every robber.
    //Pre:  none
    //Post: all robbers freed
    void freeAllRobbers();

    //Desc: adds jewl to list of available/unpicked jewls.
    //Pre:  jewl passed. this function not responsible for adding jewl to map
    //Post: jewl is added to list
    void markJewlAvailable(jewl &item);

    //Desc: removes jewl from list of unpicked jewls.
    //Pre:  jewl passed. this function not responsible for removing jewl from map
    //Post: jewl is removed from list
    void markJewlTaken(jewl &item);

    //Desc: prints summary of NPCs.
    //Pre:  none
    //Post: prints NPC info
    void summarize() const;

};


#endif