Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CPP-CivLike-Game/main-new.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
210 lines (177 sloc)
7.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "include/termcolor.hpp" | |
#include "src/map.cpp" | |
#include "include/print.h" | |
//#include "src/cities.cpp" | |
//#include "include/units.h" | |
#include "src/Units.cpp" | |
//#include "src/civs.cpp" | |
#include "include/game.h" | |
//#include "include/random.h" | |
#include <string> | |
#include <vector> | |
#include <tuple> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <map> | |
std::vector<int> getCentre(int x, int y) { | |
std::vector<int> centre = {(x / 2 + 1),(y / 2 + 1)}; | |
return centre; | |
} | |
bool nameExists(std::vector<string> list, string searchTerm) { //linear search algorithm | |
for (int i = 0; i < list.size(); i++) { | |
if (list[i] == searchTerm) { | |
return true; | |
} | |
} | |
return false; | |
} | |
std::vector<Civ> createCivs(int playerCount) { | |
std::vector<string> civNames; | |
std::vector<Civ> allCivs; | |
string civName; | |
for (int i = 0; i < playerCount; i++) { | |
while (true) { | |
std::cout << "Player " << i+1 << ", please enter a name for your civ: "; | |
std::cin >> civName; | |
if (nameExists(civNames, civName)) { | |
std::cout << "Civ name already in use." << std::endl; | |
} | |
else { | |
break; | |
} | |
} | |
civNames.push_back(civName); | |
Civ newCiv; | |
newCiv.setCivName(civName); | |
allCivs.push_back(newCiv); | |
} | |
return allCivs; | |
} | |
std::vector<Settler> createInitialSettlers(int playerCount) { | |
std::vector<Settler> settlers; | |
for (int i = 0; i < playerCount; i++) { | |
Settler newSettler(i); | |
settlers.push_back(newSettler); | |
} | |
return settlers; | |
} | |
demoMap placeInitialSettlers(demoMap mapOne, std::vector<Settler> settlers, int playerCount, std::vector<int> mapCentre) { | |
std::vector<int> directionX = {-1,-1,1,1}; | |
std::vector<int> directionY = {-1,1,-1,1}; | |
int multiplier = 10; | |
for (int i = 0; i < playerCount; i++) { | |
mapOne.mapSquares[mapCentre[1] + directionX[i]*10][mapCentre[0] + directionY[i]*10].settler = &settlers[i]; | |
} | |
return mapOne; | |
} | |
/*std::vector<City> createInitialCities(int playerCount, std::vector<Settler> settlers) { | |
std::vector<City> cities; | |
for (int i = 0; i < playerCount; i++) { | |
cities.push_back(City(settlers[i])); | |
} | |
return cities; | |
}*/ | |
std::vector<Warrior> createWarriors(int playerCount) { | |
std::vector<Warrior> warriors; | |
for (int i = 0; i < playerCount; i++) { | |
Warrior newWarrior(i); | |
warriors.push_back(newWarrior); | |
} | |
return warriors; | |
} | |
int main() { | |
srand(time(NULL)); | |
std::cout << "random: " << randomNumber(10) << std::endl; // generates random number between 0 and 9 | |
int playerCount = 4; | |
int mapSizex = 69; | |
int mapSizey = 35; | |
while (true) { | |
std::cout << "How many players are there (max 4)? "; | |
std::cin >> playerCount; | |
if (playerCount <= 4 && playerCount > 1) { | |
break; | |
} | |
std::cout << "Some of the data you inputted was invalid." << std::endl; | |
} | |
while (true) { | |
std::cout << "Enter width: "; | |
std::cin >> mapSizex; | |
std::cout << "Enter length: "; | |
std::cin >> mapSizey; | |
if (mapSizex >= 15 && mapSizey >= 15) { | |
break; | |
} | |
std::cout << "Some of the data you inputted was invalid." << std::endl; | |
} | |
std::vector<int> mapSize = {mapSizex, mapSizey}; | |
std::cout << "Player count: " << playerCount << std::endl; | |
//std::vector<int> mapSize = {35,59}; | |
std::vector<int> mapCentre = getCentre(mapSize[0], mapSize[1]); | |
std::cout << "Map size: " << mapSize[0] << " x "<< mapSize[1] << std::endl; | |
std::cout << "Map centre: ("<< mapCentre[0] << ", " << mapCentre[1] << ")" << std::endl; | |
std::vector<Warrior> warriors = createWarriors(playerCount); | |
demoMap mapOne; | |
mapOne.emptyMapGenerate(mapSize[0] + 1, mapSize[1] + 1); //Using rectangles since it displays nicer in the window screen which is a rectangle | |
mapOne.boundsGeneration(); | |
for (int i = 0; i < 4; i++) { | |
mapOne.initalTerrainGeneration({ "grey", 'O', "MOUNTAIN" }); | |
} | |
for (int i = 0; i < 10; i++) { | |
mapOne.initalTerrainGeneration({ "green", 'G', "GRASS" }); | |
} | |
mapOne.initalTerrainGeneration({ "red", 'H', "HILL" }); | |
mapOne.secondLayerTerrainGen(); | |
std::vector<Civ> allCivs = createCivs(playerCount); | |
std::vector<Settler> settlers = createInitialSettlers(playerCount); | |
mapOne = placeInitialSettlers(mapOne, settlers, playerCount, mapCentre); | |
print printer(mapOne); | |
//printer.printAll(); | |
bool won = false; | |
while (not won) { | |
std::string userInput; | |
for (int i = 0; i < playerCount; i++) { | |
while (userInput != "Y" || userInput != "y") { | |
printer.printAll(); | |
std::cout << "Player " << i << " it's your turn." << std::endl; | |
for (int j = 0; j < settlers.size(); j++) { | |
if (settlers[j].owner == i) { | |
std::cout << "settler matches current player" << std::endl; | |
if (settlers[j].movementRemaining > 0) { | |
std::cout << "Movement remaining: " << settlers[j].movementRemaining << std::endl; | |
std::string settlerAction; | |
std::cout << "Move (M), settle (S), or neither (N)? "; | |
std::cin >> settlerAction; | |
if (settlerAction == "M") { | |
mapOne.moveSettlers(i); // move settler function | |
} | |
else if (settlerAction == "S") { | |
City(settlers[j]); //create city | |
} | |
} | |
} | |
} | |
for (int j = 0; j < warriors.size(); j++) { | |
if (warriors[j].owner == i) { | |
std::cout << "warrior matches current player" << std::endl; | |
if (warriors[j].movementRemaining > 0) { | |
std::cout << "Movement remaining: " << settlers[j].movementRemaining << std::endl; | |
std::string warriorAction; | |
std::cout << "Move unit (Y/N)? "; | |
std::cin >> warriorAction; | |
if (warriorAction == "Y") { | |
mapOne.moveWarriors(i); // move warrior function | |
} | |
} | |
} | |
} | |
// city stuff | |
// production | |
std::cout << std::endl; | |
std::cout << "It looks like you've done all you can.\nWould you like to end your turn? (Y/N): "; | |
std::cin >> userInput; | |
} | |
} | |
} | |
return 0; | |
} |