Skip to content
Permalink
Browse files

Cleaned up code

  • Loading branch information...
richelbilderbeek committed Aug 9, 2019
1 parent c3ed513 commit 76aa764fac69ced78bf5fd9c3cb1ca8e5c6262fb
Showing with 37 additions and 47 deletions.
  1. +9 −19 Classes/ai.cpp
  2. +1 −1 Classes/chapter.cpp
  3. +2 −2 Classes/character.cpp
  4. +10 −10 Classes/condition.cpp
  5. +5 −5 Classes/consequence.cpp
  6. +10 −10 Classes/walkthrough.cpp
@@ -65,6 +65,7 @@ void Ai::CreateGraph() const noexcept
case 279: //Tattoo
case 163: //Ring of the golden eye
shape = "doublecircle";
break;
default: break;
}
f << i
@@ -94,7 +95,7 @@ void Ai::CreateGraph() const noexcept
}
else if (!chapter.GetOptions().GetOptions().empty())
{
for (const auto option: chapter.GetOptions().GetOptions())
for (const auto& option: chapter.GetOptions().GetOptions())
{
//No payoff
//f << i << "->" << option.GetNextChapter() << " [ label = \"Choice\"];\n";
@@ -116,11 +117,11 @@ void Ai::CreateGraph() const noexcept
f << i << "-> GameWon;\n";
}
}
catch (std::logic_error& e)
catch (const std::logic_error&)
{
//f << i << ": FAIL" << std::endl;
}
catch (std::runtime_error& e)
catch (const std::runtime_error&)
{
//f << i << ": not present" << std::endl;
}
@@ -161,17 +162,6 @@ double Ai::CalcFinalPayoff(const Character& character) const noexcept
values[Item::lotus_flower] = average / static_cast<double>(m_tally[Item::lotus_flower]);
values[Item::tattoo] = average / static_cast<double>(m_tally[Item::tattoo]);

if (1==2)
{
std::clog
<< character.HasItem(Item::black_pearls) << ":" << m_tally[Item::black_pearls] << " " << values[Item::black_pearls] << " "
<< character.HasItem(Item::silver_arrow) << ":" << m_tally[Item::silver_arrow] << " " << values[Item::silver_arrow] << " "
<< character.HasItem(Item::hags_hair) << ":" << m_tally[Item::hags_hair] << " " << values[Item::hags_hair] << " "
<< character.HasItem(Item::lotus_flower) << ":" << m_tally[Item::lotus_flower] << " " << values[Item::lotus_flower] << " "
<< character.HasItem(Item::tattoo) << ":" << m_tally[Item::tattoo] << " " << values[Item::tattoo] << " "
<< std::endl;
}

const auto visited = character.GetChapters();
const bool got_in_city{std::count(std::begin(visited),std::end(visited),74) == 1 ? true : false};
const bool got_in_forest{std::count(std::begin(visited),std::end(visited),201) == 1 ? true : false};
@@ -317,16 +307,16 @@ void Ai::Start()
}

SetFinalPayoff(CalcFinalPayoff(game.GetCharacter()));
//if (game.HasWon()) { break; }
if (game.HasWon()) { break; }
}
//std::cout << "FINISHED THE GAME, CREATING GRAPH" << std::endl;
//this->CreateGraph();
std::cout << "FINISHED THE GAME, CREATING GRAPH" << '\n';
this->CreateGraph();
}

std::ostream& operator<<(std::ostream& os, const Ai& ai)
{
const auto payoffs = ai.GetPayoffs();
for (const auto payoff: payoffs)
for (const auto& payoff: payoffs)
{
os << "(" << payoff.first << "," << payoff.second << ")\n";
}
@@ -335,7 +325,7 @@ std::ostream& operator<<(std::ostream& os, const Ai& ai)

Option Ai::RequestOption(const std::vector<Option>& options)
{
for (const auto option: options)
for (const auto& option: options)
{
ShowText(" * " + option.GetText() + "\n");
}
@@ -512,7 +512,7 @@ std::ostream& operator<<(std::ostream& os, const Chapter& chapter)
<< "text: " << chapter.GetText() << '\n'
<< "# monsters: " << chapter.GetFighting().GetMonsters().size() << '\n'
;
for (const auto monster: chapter.GetFighting().GetMonsters())
for (const auto& monster: chapter.GetFighting().GetMonsters())
{
os << monster << '\n';
}
@@ -308,7 +308,7 @@ void Character::RemoveItem(Item item)
Item::silver_scorpion_brooch,
Item::silver_spoon
};
for (const auto silver_item: silver_items)
for (const auto& silver_item: silver_items)
{
if (HasItem(silver_item)) RemoveItem(silver_item);
}
@@ -459,7 +459,7 @@ std::string Character::ShowInventory()
}

s << "monsters fought: \n";
for (const auto monster_name: m_fought)
for (const auto& monster_name: m_fought)
{
s << " * " << Helper().ToPretty(monster_name) << '\n';
}
@@ -57,17 +57,17 @@ bool Condition::IsSatisfied(const Character &character) const
if (GetGoldNeeded() > character.GetGold()) return false;
if (GetProvisionsNeeded() > character.GetProvisions()) return false;

for (const auto monster_name: m_fought)
for (const auto& monster_name: m_fought)
{
if (!character.HasFought(monster_name)) return false;
}

for (const auto monster_name: m_not_fought)
for (const auto& monster_name: m_not_fought)
{
if (character.HasFought(monster_name)) return false;
}

for (const auto item_needed: GetItemsNeeded())
for (const auto& item_needed: GetItemsNeeded())
{
if (item_needed == Item::all_needed_to_slay_zanbar_bone)
{
@@ -93,7 +93,7 @@ bool Condition::IsSatisfied(const Character &character) const
if (!character.HasItem(item_needed)) return false;
}
}
for (const auto item_not_needed: GetItemsNotNeeded())
for (const auto& item_not_needed: GetItemsNotNeeded())
{
if (item_not_needed == Item::any_scorpion_brooch)
{
@@ -142,7 +142,7 @@ Condition ParseCondition(std::stringstream& s)

if (what.empty())
{
assert(!"Should not get here");
assert(false);
}
else if (what == "fought" || what == "has_fought")
{
@@ -160,7 +160,7 @@ Condition ParseCondition(std::stringstream& s)
if (!IsItem(item))
{
std::cerr << "Unknown item " << item << std::endl;
assert(!"Should not get here");
assert(false);
}
const Item item_needed{ToItem(item)};
condition.AddItemNeeded(item_needed);
@@ -171,7 +171,7 @@ Condition ParseCondition(std::stringstream& s)
if (!IsItem(item))
{
std::cerr << "Unknown item " << item << std::endl;
assert(!"Should not get here");
assert(false);
}
const Item item_not_needed{ToItem(item)};
condition.AddItemNotNeeded(item_not_needed);
@@ -194,7 +194,7 @@ Condition ParseCondition(std::stringstream& s)
else
{
std::cerr << "Unknown option after if: " << what << std::endl;
assert(!"Should not get here");
assert(false);
}
return condition;
}
@@ -205,12 +205,12 @@ std::ostream& operator<<(std::ostream& os, const Condition& condition)
<< "gold needed: " << condition.GetGoldNeeded()
<< ", items needed: {"
;
for (const auto item: condition.GetItemsNeeded())
for (const auto& item: condition.GetItemsNeeded())
{
os << ToStr(item) << ", ";
}
os << "}, items not_needed: {";
for (const auto item: condition.GetItemsNotNeeded())
for (const auto& item: condition.GetItemsNotNeeded())
{
os << ToStr(item) << ", ";
}
@@ -33,11 +33,11 @@ void Consequence::Add(const Consequence& other)
this->m_change_provisions += other.m_change_provisions;

this->m_change_sta += other.m_change_sta;
for (const auto item_to_add: other.GetItemsToAdd())
for (const auto& item_to_add: other.GetItemsToAdd())
{
this->m_add_items.push_back(item_to_add);
}
for (const auto item_to_remove: other.GetItemsToRemove())
for (const auto& item_to_remove: other.GetItemsToRemove())
{
this->m_remove_items.push_back(item_to_remove);
}
@@ -132,12 +132,12 @@ void Consequence::Apply(Character& character) const
character.ChangeCondition(change_sta);
}
}
for (const auto item: this->GetItemsToAdd())
for (const auto& item: this->GetItemsToAdd())
{
if (verbose) { std::clog << "Obtained item " << ToPrettyStr(item) << std::endl; }
character.AddItem(item);
}
for (const auto item: this->GetItemsToRemove())
for (const auto& item: this->GetItemsToRemove())
{
if (item == Item::all_gold)
{
@@ -196,7 +196,7 @@ void Consequence::Apply(Character& character) const
else if (item == Item::all_silver_items)
{
const auto items = character.GetItems();
for (const auto this_item: items)
for (const auto& this_item: items)
{
if (IsSilver(this_item)) character.RemoveItem(this_item);
}
@@ -35,7 +35,7 @@ void Walkthrough::Start()

Option Walkthrough::RequestOption(const std::vector<Option>& options)
{
for (const auto option: options)
for (const auto& option: options)
{
ShowText(" * " + option.GetText() + "\n");
}
@@ -145,7 +145,7 @@ Option Walkthrough::RequestOption(const std::vector<Option>& options)
if (chapter_number == 52)
{
///Buy all needed
for (const auto option: options)
for (const auto& option: options)
{
if (option.GetConsequence().GetItemsToAdd().empty()) continue;
assert(option.GetConsequence().GetItemsToAdd().size() == 1);
@@ -158,7 +158,7 @@ Option Walkthrough::RequestOption(const std::vector<Option>& options)
return option;
}
}
for (const auto option: options)
for (const auto& option: options)
{
if (option.GetConsequence().GetType() == ConsequenceType::leave)
{
@@ -171,7 +171,7 @@ Option Walkthrough::RequestOption(const std::vector<Option>& options)
if (chapter_number == 354)
{
///Buy all needed
for (const auto option: options)
for (const auto& option: options)
{
if (option.GetConsequence().GetItemsToRemove().empty()) continue;
assert(option.GetConsequence().GetItemsToRemove().size() == 1);
@@ -184,7 +184,7 @@ Option Walkthrough::RequestOption(const std::vector<Option>& options)
return option;
}
}
for (const auto option: options)
for (const auto& option: options)
{
if (option.GetConsequence().GetType() == ConsequenceType::leave)
{
@@ -194,20 +194,20 @@ Option Walkthrough::RequestOption(const std::vector<Option>& options)
}


if (solution[chapter_number] == 0)
if (solution[static_cast<size_t>(chapter_number)] == 0)
{
std::cerr << "Do not know where to go from chapter " << chapter_number << std::endl;
}
assert(solution[chapter_number] != 0);
const int target_chapter{solution[chapter_number]};
for (const auto option: options)
assert(solution[static_cast<size_t>(chapter_number)] != 0);
const int target_chapter{solution[static_cast<size_t>(chapter_number)]};
for (const auto& option: options)
{
if (option.GetConsequence().GetNextChapter() == target_chapter)
{
return option;
}
}
assert(!"Should not get here");
assert(false);
return options[1];
}

0 comments on commit 76aa764

Please sign in to comment.
You can’t perform that action at this time.