Created
June 9, 2015 13:02
-
-
Save 6desislava6/a6dfb4983cb1a5d2c2e2 to your computer and use it in GitHub Desktop.
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 "Game.h" | |
#include "Player.h" | |
#include "Peasant.h" | |
#include "Footman.h" | |
#include "Archer.h" | |
#include "Griffon.h" | |
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
#include <vector> | |
#include <sstream> | |
#include <iterator> | |
using namespace std; | |
Game::Game() | |
{ | |
this->m_enemy->addPeasant(2); | |
this->m_enemy->addFootman(2); | |
this->m_enemy->addArcher(1); | |
this->m_enemy->addGriffon(1); | |
} | |
Game::~Game() | |
{ | |
delete this->m_player; | |
delete this->m_enemy; | |
delete this->m_shop; | |
delete this->m_battlefield; | |
} | |
Player* Game::getCurrentEnemy() const | |
{ | |
return this->m_enemy; | |
} | |
Player* Game::getCurrentPlayer() const | |
{ | |
return this->m_player; | |
} | |
Shop* Game::getShop() const | |
{ | |
return this->m_shop; | |
} | |
void Game::mainMenu() | |
{ | |
string userInput; | |
cout << "You are in the MAIN MENU." << endl; | |
cout << "Make your choice:" << endl | |
<< "start -> start new game" << endl | |
<< "shop -> go to the shop" << endl | |
<< "exit -> exit the game" << endl; | |
while (cin) | |
{ | |
getline(cin, userInput); | |
if (userInput == "start") | |
{ | |
this->gameMenu(); | |
} | |
else if (userInput == "shop") | |
{ | |
this->shopMenu(); | |
} | |
else if (userInput == "exit") | |
{ | |
cout << "Bye Bye" << endl; | |
break; | |
} | |
else | |
{ | |
cout << "Invalid command!" << endl; | |
} | |
cout << "Please make your choice!" << endl; | |
} | |
} | |
void Game::gameMenu() | |
{ | |
cout << "GAME MENU" << endl; | |
cout << "Make your choice:" << endl | |
<< "move x y m n (x, y: <source coords>; m, n: <destination coords>)" << endl | |
<< "attack x y m n (x, y: <source coords>; m, n: <destination coords>" << endl | |
<< "exit -> go to the MAIN MENU" << endl; | |
string userInput; | |
vector<string> input; | |
this->m_battlefield->printBattlefield(); | |
while (true) | |
{ | |
getline(cin, userInput); | |
input = this->splitUserInput(userInput); | |
if (input.at(0) == "move") | |
{ | |
this->MakeMoveLogic(input); | |
} | |
else if (input.at(0) == "attack") | |
{ | |
int source_x = atoi(input.at(1).c_str()); | |
int source_y = atoi(input.at(2).c_str()); | |
int dest_x = atoi(input.at(3).c_str()); | |
int dest_y = atoi(input.at(4).c_str()); | |
if (source_x < 0 || source_x >= MATRIX_X || source_y < 0 || source_y >= MATRIX_Y || | |
dest_x < 0 || dest_x >= MATRIX_X || dest_y < 0 || dest_y >= MATRIX_Y) | |
{ | |
cout << "Invalid coords!" << endl; | |
} | |
//map от char и int. | |
//и за destinantion-a така | |
else if (this->m_battlefield->getBattlefield(source_x, source_y) == PPeasantChar) | |
{ | |
cout << "Attack Peasant!" << endl; | |
MakeAttackLogic(m_player, 0); | |
} | |
else if (this->m_battlefield->getBattlefield(source_x, source_y) == PFootmanChar) | |
{ | |
cout << "Attack Footman!" << endl; | |
MakeAttackLogic(m_player, 1); | |
} | |
else if (this->m_battlefield->getBattlefield(source_x, source_y) == PArcherChar) | |
{ | |
cout << "Attack Archer!" << endl; | |
MakeAttackLogic(m_player, 2); | |
} | |
else if (this->m_battlefield->getBattlefield(source_x, source_y) == PGriffonChar) | |
{ | |
cout << "Attack Griffon!" << endl; | |
MakeAttackLogic(m_player, 3); | |
} | |
else | |
{ | |
cout << "Attack forbidden!" << endl; | |
} | |
this->m_battlefield->printBattlefield(); | |
} | |
else if (input.at(0) == "exit") | |
{ | |
break; | |
} | |
else | |
{ | |
cout << "Invalid command!" << endl; | |
} | |
cout << "Please make your choice." << endl; | |
} | |
} | |
void Game::shopMenu() | |
{ | |
cout << "SHOP MENU" << endl; | |
cout << "Make your choice:" << endl | |
<< "items -> items available in the shop" << endl | |
<< "buy <item_name> <amount> -> buy new item(s)" << endl | |
<< "gold -> the amount of gold you possess" << endl | |
<< "exit -> go to the MAIN MENU" << endl; | |
string userInput; | |
vector<string> input; | |
while (true) | |
{ | |
getline(cin, userInput); | |
input = this->splitUserInput(userInput); | |
if (input.at(0) == "items") | |
{ | |
this->PrintItems(); | |
} | |
else if (input.at(0) == "buy") | |
{ | |
string userInput = input.at(1); | |
int numberOfCreaturesWanted = atoi(input.at(2).c_str()); | |
this->MakeBuyLogic(userInput, numberOfCreaturesWanted); | |
} | |
else if (input.at(0) == "gold") | |
{ | |
cout << this->getCurrentPlayer()->getCurrentGold() << endl; | |
} | |
else if (input.at(0) == "exit") | |
{ | |
break; | |
} | |
else | |
{ | |
cout << "Invalid command!" << endl; | |
} | |
cout << "Please make your choice." << endl; | |
} | |
} | |
vector<string> Game::splitUserInput(const string& s) | |
{ | |
stringstream ss(s); | |
istream_iterator<string> it(ss); | |
istream_iterator<string> end; | |
vector<string> tokens(it, end); | |
return tokens; | |
} | |
void Game::Run() | |
{ | |
this->mainMenu(); | |
} | |
void Game::MakeBuyLogic(string userInput, int numberOfCreaturesWanted) | |
{ | |
int creatureValue = (this->getShop()->getItems())[userInput]; | |
int cost = creatureValue * numberOfCreaturesWanted; | |
if (cost > this->getCurrentPlayer()->getCurrentGold()) | |
{ | |
cout << "Not enough gold!" << endl; | |
} | |
else | |
{ | |
this->getCurrentPlayer()->addPeasant(numberOfCreaturesWanted); | |
this->getCurrentPlayer()->setCurrentGold(-cost); | |
cout << "Bought " << userInput << " for " << cost<< endl; | |
} | |
} | |
void Game::PrintItems() | |
{ | |
cout << "Peasant: " << this->getShop()->getItems()["Peasant"] << " gold" << endl | |
<< "Footman: " << this->getShop()->getItems()["Footman"] << " gold" << endl | |
<< "Archer: " << this->getShop()->getItems()["Archer"] << " gold" << endl | |
<< "Griffon: " << this->getShop()->getItems()["Griffon"] << " gold" << endl; | |
} | |
void Game::MakeMoveLogic(vector<string> input) | |
{ | |
int source_x = atoi(input.at(1).c_str()); | |
int source_y = atoi(input.at(2).c_str()); | |
int dest_x = atoi(input.at(3).c_str()); | |
int dest_y = atoi(input.at(4).c_str()); | |
map<const char, Creature*> creatures; | |
creatures.insert(std::make_pair(PPeasantChar, new Peasant())); | |
creatures.insert(std::make_pair(PFootmanChar, new Footman())); | |
creatures.insert(std::make_pair(PArcherChar, new Archer())); | |
creatures.insert(std::make_pair(PGriffonChar, new Griffon())); | |
creatures.insert(std::make_pair(EPeasantChar, new Peasant())); | |
creatures.insert(std::make_pair(EFootmanChar, new Footman())); | |
creatures.insert(std::make_pair(EArcherChar, new Archer())); | |
creatures.insert(std::make_pair(EGriffonChar, new Griffon())); | |
if (source_x < 0 || source_x >= MATRIX_X || source_y < 0 || source_y >= MATRIX_Y || | |
dest_x < 0 || dest_x >= MATRIX_X || dest_y < 0 || dest_y >= MATRIX_Y) | |
{ | |
cout << "Invalid coords!" << endl; | |
} | |
else | |
{ | |
if (this->m_battlefield->getBattlefield(dest_x, dest_y) == BATTLEFIELD_SPACE) | |
{ | |
char creatureChar = this->m_battlefield->getBattlefield(source_x, source_y); | |
Creature* creature = creatures[creatureChar]; | |
int m = creature->Move(source_x, source_y, dest_x, dest_y)[0]; | |
int n = creature->Move(source_x, source_y, dest_x, dest_y)[1]; | |
this->m_battlefield->setCreatureCoords(m, n, creatureChar); | |
} | |
else | |
{ | |
cout << "Move forbidden!" << endl; | |
} | |
} | |
this->m_battlefield->printBattlefield(); | |
} | |
void Game::MakeAttackLogic(Player* attackingPlayer, short typeCreatureAttacking, Player* attackedPlayer, short typeCreatureAttacked) | |
{ | |
// Сумарно атака на съществата | |
int summ_attack = 0; | |
vector<Creature*> attackingCreatures = (attackingPlayer->getCreaturesPossesed())[typeCreatureAttacking]; | |
vector<Creature*> attackedCreatures = (attackedPlayer->getCreaturesPossesed())[typeCreatureAttacked]; | |
for (size_t i = 0; i < attackingCreatures.size(); i++) | |
{ | |
//Всяко от съществата във вектора атакува. | |
//Различните могат да притежават различна мана -> различна атака. | |
summ_attack += attackingCreatures[i]->Attack(); | |
} | |
// колко ще бъдат убити. | |
int killed = summ_attack / attackingCreatures[0]->getDefense(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment