#include #include #include #include "MapT.h" #include "WarehouseT.h" #include "GameConstants.h" using namespace std; void PrintSectorInfo(MapT & map, PointT p); void Explore(MapT & map, PointT p); bool GameOver(MapT & map, const WarehouseT & warehouse); void PrintHeader(int year, int season); void IncrementTime(int & year, int & season); void PrintWarehouse(const WarehouseT & warehouse); int PopulationCount(MapT & map); void UpdateWorld(MapT & map, WarehouseT & warehouse); void FakeTurn(MapT & map, WarehouseT & warehouse, int year, int season); int main() { MapT map("Dandyland.map"); WarehouseT warehouse; int year = 1; int season = 0; while(not GameOver(map, warehouse)) { PrintHeader(year, season); map.PrintMap(); PrintWarehouse(warehouse); cout <<"\tThe pulation is "<< PopulationCount(map) << endl; cout << endl; FakeTurn(map, warehouse, year, season); UpdateWorld(map, warehouse); IncrementTime(year, season); } return 0; } void ExploreSomewhere(MapT & map, vector & points) { PointT base, target; size_t i = 0; bool done = false; while (i < points.size() and not done ) { base = points[i]; target = Up(base); if (map.Sector(target)->IsHidden()) { cout << endl; cout << "Exploring to " << target << endl; cout << endl; map.Sector(target)->Discover(); done = true; points.push_back(target); } target = Right(base); if (not done and map.Sector(target)->IsHidden()) { cout << endl; cout << "Exploring to " << target << endl; cout << endl; map.Sector(target)->Discover(); done = true; points.push_back(target); } i++; } return; } void Migrate(MapT & map, vector points) { PointT lowSector, highSector; int lowCount, highCount; lowSector = points[0]; lowCount = map.Sector(lowSector)->Population(); highSector = lowSector; highCount = lowCount; for(auto x: points) { int tmp = map.Sector(x)->Population(); if (tmp > highCount) { highSector = x; highCount = tmp; } if (tmp < lowCount) { lowCount = tmp; lowSector = x; } } if (highCount > 100 and lowCount < 900) { int move = (highCount - lowCount)/2; map.Sector(highSector)->Population(-move); map.Sector(lowSector)->Population(move); cout << "\t" << move << " people migrated from " << highSector << " to " << lowSector << "." << endl; cout << endl; PrintSectorInfo(map, lowSector); PrintSectorInfo(map, highSector); } } void FakeTurn(MapT & map, WarehouseT & warehouse, int year, int season){ PointT center('C',3); static vector points{center}; if (year % 2 == 0 and season == 0) { ExploreSomewhere(map, points); } for (auto x: points) { map.Sector(x)->Build(warehouse); } Migrate(map, points); } void UpdateWorld(MapT & map, WarehouseT & warehouse){ int x; int y; SectorPtr sector; for(y =0; y < map.Height() ; y++) { for(x = 0 ; x < map.Width(); x++) { char tmp = static_cast('A'+x); PointT p(tmp, y); sector = map.Sector(p); if (not sector->IsHidden()) { cout << "Producing in Sector " << p << endl; sector->PopulationPhase(warehouse); sector->GoodsPhase(warehouse); sector->ResourcesPhase(warehouse); cout << endl; } } } } int PopulationCount(MapT & map) { int x; int y; int totalPop{0}; for(x = 0 ; x < map.Width(); x++) { for(y =0; y < map.Height() ; y++) { char tmp = static_cast('A'+x); totalPop += map.Sector(PointT(tmp, y))->Population(); } } return totalPop; } bool GameOver(MapT & map, const WarehouseT & warehouse){ if (warehouse.Victory() == POINTS_FOR_VICTORY) { return true; } if (PopulationCount(map) == 0) { return true; } return false; } void PrintHeader(int year, int season){ string seasonName; switch(season) { case 0: seasonName = "Spring"; break; case 1: seasonName = "Summer"; break; case 2: seasonName = "Fall"; break; case 3: seasonName = "Winter"; break; } cout << endl; cout << "************************************************************" << endl; cout << seasonName << " Year " << year << endl; } void IncrementTime(int & year, int & season){ season++; if (season > 3) { season=0; year++; } } void PrintWarehouse(const WarehouseT & warehouse) { cout << "The warehouse contains : " << endl; cout << "\tFood " << warehouse.Food() << endl; cout << "\tWood " << warehouse.Wood() << endl; cout << "\tGold " << warehouse.Gold() << endl; cout << "\tConstruction " << warehouse.Construction() << endl; cout << "\tCoins " << warehouse.Coins() << endl; cout << "\tVictory " << warehouse.Victory() << endl; } bool ExploreFrom(MapT & map, PointT p ) { p = map.NormalizePoint(p); //cout << "Checking from " << p; if (map.Sector(p)->IsHidden()) { //cout << " no, you have not yet descoverd " << p << endl; return false; } else { //cout << " yes, you can explore from" << p << endl; return true; } } void Explore(MapT & map, PointT p) { SectorPtr s; PointT orig = p; cout << "Attempting to explore " << p << endl; s = map.Sector(p); if (s->IsHidden()) { bool adjacent = false; adjacent = adjacent or ExploreFrom(map, Up(p)); adjacent = adjacent or ExploreFrom(map, Down(p)); adjacent = adjacent or ExploreFrom(map, Left(p)); adjacent = adjacent or ExploreFrom(map, Right(p)); if (not adjacent) { cout << "Can not explore " << orig << " nothing is adjacent" << endl; cout << endl; } else { cout << "Exploring " << orig << endl; s->Discover(); s->Population(1); PrintSectorInfo(map, orig); } } else { cout << "You have already explored " << p << endl; } return; } void PrintSectorInfo(MapT & map, PointT p){ SectorPtr s; p = map.NormalizePoint(p); s = map.Sector(p); cout << "The Sector at " << p ; if (s->IsHidden()) { cout << " is Hidden" << endl; } else { cout << " has been explored" << endl; cout << "\tThe type is " << s->Type() << endl; cout << "\tThe population is " << s->Population() << endl; cout << "\tBuilding count " << s->Buildings() << endl; } cout << endl; }