Skip to content

Commit

Permalink
Merge branch 'arthur'
Browse files Browse the repository at this point in the history
  • Loading branch information
angeluriot committed Jan 26, 2022
2 parents d0677d0 + a3b0971 commit 939fc47
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.
10 changes: 7 additions & 3 deletions sources/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ void Game::start()
{
std::string name = input(to_str(bold) + to_str(magenta) + "Player " + to_str(i + 1) + to_str(reset) + " name:");
name = remove_spaces(name);
players[i].set_name(name == "" ? "Player " + to_str(i + 1) : name);
players[i].set_name(name == "" ? "Player " + to_str(i + 1) : name); // If the player did not choose a name, they will be named Player 1 or Player 2
}

// If both players choose the same name, player 2 will have a "(2)" next to their name
if (players[0].get_name() == players[1].get_name())
players[1].set_name(players[1].get_name() + " (2)");

Expand All @@ -46,19 +47,22 @@ void Game::create_decks()
std::cout << magenta << bold << player.get_name() << reset << ", which deck do you want to use?" << End(1);
int res = choice(files, additional);

// If player chooses "Refresh"
if (res == (int)files.size())
{
create_decks();
return;
}

// If player chooses "Quit"
else if (res == (int)files.size() + 1)
{
quit_game();
create_decks();
return;
}

// If player chooses a deck
else
{
auto cards = Card::get_cards_from_string(get_cards_from_deck(files[res]));
Expand All @@ -84,15 +88,15 @@ void Game::play()
{
std::cout << magenta << bold << player.get_opponent().get_name() << reset << " has lost!" << End(1);
std::cout << magenta << bold << player.get_name() << reset << " wins this game!" << End(2);
return;
return; // Stops the game if opponent loses
}
}

else
{
std::cout << magenta << bold << player.get_name() << reset << " has lost!" << End(1);
std::cout << magenta << bold << player.get_opponent().get_name() << reset << " wins this game!" << End(2);
return;
return; // Stops the game if active player loses
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions sources/cards/Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,19 @@ PtrList<Card> Card::all_cards = get_all_card();

PtrList<Card> Card::get_cards_from_string(const std::vector<std::string>& cards_string)
{
// Throw error for deck with > 30 cards
if ((int)cards_string.size() > Game::deck_size)
throw_error("A deck can't have more than " + to_str(Game::deck_size) + " cards, please remove " +
(cards_string.size() - Game::deck_size > 1 ? to_str(cards_string.size() - Game::deck_size) + " cards." : "a card."));

// Throw error for deck with < 30 cards
if ((int)cards_string.size() < Game::deck_size)
throw_error("A deck can't have less than " + to_str(Game::deck_size) + " cards, please add " +
(Game::deck_size - cards_string.size() > 1 ? to_str(Game::deck_size - cards_string.size()) + " cards." : "a card."));

PtrList<Card> cards;

// Throw error for deck with duplicates
for (auto& card_string : cards_string)
for (int i = 0; i < (int)all_cards.size(); i++)
{
Expand Down
30 changes: 15 additions & 15 deletions sources/cards/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::string Creature::get_description() const
auto capacities = get_capacities();
std::string description = "";

for (auto& capacity : capacities)
for (auto& capacity : capacities) // Gives a description for each capacity
{
if (capacity == Capacity::Flying)
description += "- " + to_str(underline) + "Flying:" + to_str(no_underline) + " Only creatures whith " + to_str(italic) + "Reach" + to_str(no_italic) + " can attack it.\n";
Expand Down Expand Up @@ -183,19 +183,19 @@ void Creature::apply_attack()
{
bool has_attacked = false;

if (targets.empty())
if (targets.empty()) // If no enemy creature blocked
{
attack(get_power());
has_attacked = true;
}

else
else // If at least one enemy creature blocked
{
int power_left = get_power();

for (Creature* target : targets)
{
if (has(Capacity::FirstStrike) && !target->has(Capacity::FirstStrike))
if (has(Capacity::FirstStrike) && !target->has(Capacity::FirstStrike)) // If attacking creature has FirstStrike but blocking creature doesn't
{
power_left = attack(*target, power_left);
target->modify_power(has(Capacity::Freeze) ? -1 : 0);
Expand All @@ -208,7 +208,7 @@ void Creature::apply_attack()
}
}

else if (target->has(Capacity::FirstStrike) && !has(Capacity::FirstStrike))
else if (target->has(Capacity::FirstStrike) && !has(Capacity::FirstStrike)) // If blocking creature has FirstStrike but attacking creature doesn't
{
target->block(*this);
modify_power(target->has(Capacity::Freeze) ? -1 : 0);
Expand All @@ -221,7 +221,7 @@ void Creature::apply_attack()
}
}

else
else // If both have FirstStrike or neither of them has it
{
modify_power(target->has(Capacity::Freeze) ? -1 : 0);
target->modify_power(has(Capacity::Freeze) ? -1 : 0);
Expand All @@ -234,20 +234,20 @@ void Creature::apply_attack()
break;
}

if (has(Capacity::MultiHit))
if (has(Capacity::MultiHit)) // If attacking creature has MultiHit
{
attack(get_power());
has_attacked = true;
}

else if (m_alive && power_left > 0)
else if (m_alive && power_left > 0) // If attacking creature still has enough strength to attack, attacks the player
{
attack(power_left);
has_attacked = true;
}
}

if (has(Capacity::Kamikaze) && has_attacked)
if (has(Capacity::Kamikaze) && has_attacked) // If attacking creature has Kamikaze
{
m_toughness = 0;
m_alive = false;
Expand All @@ -263,7 +263,7 @@ int Creature::attack(Creature& creature, int power_left)
{
if (power_left > 0)
{
if (creature.has(Capacity::Flying) && !has(Capacity::Reach))
if (creature.has(Capacity::Flying) && !has(Capacity::Reach)) // Attacking creature cannot attack Flying creatures if it doesn't have Reach
return power_left;

if (creature.has_shield())
Expand All @@ -272,10 +272,10 @@ int Creature::attack(Creature& creature, int power_left)
return power_left;
}

int damages = has(Capacity::ZoneDamage) ? get_power() : power_left;
int damages = has(Capacity::ZoneDamage) ? get_power() : power_left; // Attacking creature hits all blocking creatures with the same power
int result = std::max(damages - creature.get_toughness(), 0);

if (damages >= creature.get_toughness())
if (damages >= creature.get_toughness()) // If blocking creature is killed by the attack
on_kill();

creature.modify_toughness(-damages);
Expand All @@ -287,9 +287,9 @@ int Creature::attack(Creature& creature, int power_left)

void Creature::block(Creature& creature)
{
if (!has(Capacity::Unblockable))
if (!has(Capacity::Unblockable)) // Blocking creatures do not deal damage if they have the Unblockable capacity
{
if (creature.m_shield)
if (creature.m_shield) // Destroys the attacking creature's shield if they have one
creature.m_shield = false;

else
Expand All @@ -300,7 +300,7 @@ void Creature::block(Creature& creature)
creature.modify_toughness(-get_power());
}

if (has(Capacity::Kamikaze))
if (has(Capacity::Kamikaze)) // Creature self destructs if it has the Kamikaze ability
{
m_toughness = 0;
m_alive = false;
Expand Down
3 changes: 3 additions & 0 deletions sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ int main()
std::cout << "What do you want to do?" << End(1);
int res = choice({ "- Play -", "- Quit -" }, { green, red });

// If player chooses "Play"
if (res == 0)
{
Game::reset_game();
Game::start();

// Loop for choosing decks
while (true)
{
try
Expand All @@ -46,6 +48,7 @@ int main()
Game::play();
}

// If player chooses "Quit"
else
{
quit_game();
Expand Down
Loading

0 comments on commit 939fc47

Please sign in to comment.