/
Kogeta
/
Pokemon_Console_Battle_Sim
Обзор
Документация
Войти
/
Kogeta
/
Pokemon_Console_Battle_Sim
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
258 строк
14 KB
ip314s20
First version of the project
21 окт 2024, 07:10
21 окт 2024, 07:10
0955c2a
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> enum class PokemonType { Fire, Water, Grass, Ice, Electric, Psychic, Normal, Fighting, Poison, Ground, Flying, Bug, Rock, Ghost, Steel, Dragon, Dark, Fairy }; enum class MoveType { Attack, Status }; // Define move types class Move { public: std::string name; int power; // Power for attack moves int accuracy; MoveType type; // Type of the move (Attack or Status) PokemonType moveType; // Type of the move (e.g., Fire, Water, etc.) Move(std::string n, int p, int a, MoveType t, PokemonType mt) : name(n), power(p), accuracy(a), type(t), moveType(mt) {} }; class Pokemon { public: std::string name; int maxHP; int currentHP; int attack; int defense; int speed; PokemonType type; // Type of the Pokémon (e.g., Fire, Water, etc.) std::vector<Move> moves; Pokemon(std::string n, int hp, int att, int def, int spd, PokemonType t) : name(n), maxHP(hp), currentHP(hp), attack(att), defense(def), speed(spd), type(t) {} void addMove(const Move& move) { moves.push_back(move); } bool isKnockedOut() { return currentHP <= 0; } void heal(int amount) { currentHP += amount; if (currentHP > maxHP) { currentHP = maxHP; // Ensure HP does not exceed max HP } } void decreaseAttack(float percentage) { attack *= (1.0f - percentage); } void decreaseSpeed(float percentage) { speed *= (1.0f - percentage); // Decrease speed by a percentage } void printStatus() { // Print Pokémon status std::cout << name << " | HP: " << currentHP << "/" << maxHP << " | Attack: " << attack << " | Defense: " << defense << " | Speed: " << speed << std::endl; } }; int calculateDamage(int attack, int defense, int movePower, PokemonType attackerType, PokemonType defenderType, PokemonType moveType) { // Simple damage calculation with type effectiveness double typeEffectiveness = 1.0; // Default type effectiveness if (moveType == PokemonType::Fire) { if (defenderType == PokemonType::Grass) typeEffectiveness = 2.0; // Fire is super effective against Grass else if (defenderType == PokemonType::Water) typeEffectiveness = 0.5; // Fire is not very effective against Water } else if (moveType == PokemonType::Water) { if (defenderType == PokemonType::Fire) typeEffectiveness = 2.0; // Water is super effective against Fire else if (defenderType == PokemonType::Grass) typeEffectiveness = 0.5; // Water is not very effective against Grass } else if (moveType == PokemonType::Grass) { if (defenderType == PokemonType::Water) typeEffectiveness = 2.0; // Grass is super effective against Water else if (defenderType == PokemonType::Fire) typeEffectiveness = 0.5; // Grass is not very effective against Fire } else if (moveType == PokemonType::Ice) { if (defenderType == PokemonType::Grass) typeEffectiveness = 2.0; // Ice is super effective against Grass else if (defenderType == PokemonType::Fire) typeEffectiveness = 0.5; // Ice is not very effective against Fire } else if (moveType == PokemonType::Electric) { if (defenderType == PokemonType::Water) typeEffectiveness = 2.0; // Electric is super effective against Water else if (defenderType == PokemonType::Grass) typeEffectiveness = 0.5; // Electric is not very effective against Grass } else if (moveType == PokemonType::Psychic) { if (defenderType == PokemonType::Fighting) typeEffectiveness = 2.0; // Psychic is super effective against Fighting else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Psychic is not very effective against Poison } else if (moveType == PokemonType::Normal) { if (defenderType == PokemonType::Rock) typeEffectiveness = 0.5; // Normal is not very effective against Rock } else if (moveType == PokemonType::Fighting) { if (defenderType == PokemonType::Normal) typeEffectiveness = 2.0; // Fighting is super effective against Normal else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Fighting is not very effective against Poison } else if (moveType == PokemonType::Poison) { if (defenderType == PokemonType::Grass) typeEffectiveness = 2.0; // Poison is super effective against Grass else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Poison is not very effective against Poison } else if (moveType == PokemonType::Ground) { if (defenderType == PokemonType::Electric) typeEffectiveness = 2.0; // Ground is super effective against Electric else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Ground is not very effective against Poison } else if (moveType == PokemonType::Flying) { if (defenderType == PokemonType::Ground) typeEffectiveness = 2.0; // Flying is super effective against Ground else if (defenderType == PokemonType::Electric) typeEffectiveness = 0.5; // Flying is not very effective against Electric } else if (moveType == PokemonType::Bug) { if (defenderType == PokemonType::Grass) typeEffectiveness = 2.0; // Bug is super effective against Grass else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Bug is not very effective against Poison } else if (moveType == PokemonType::Rock) { if (defenderType == PokemonType::Flying) typeEffectiveness = 2.0; // Rock is super effective against Flying else if (defenderType == PokemonType::Ground) typeEffectiveness = 0.5; // Rock is not very effective against Ground } else if (moveType == PokemonType::Ghost) { if (defenderType == PokemonType::Ghost) typeEffectiveness = 2.0; // Ghost is super effective against Ghost else if (defenderType == PokemonType::Poison) typeEffectiveness = 0.5; // Ghost is not very effective against Poison } else if (moveType == PokemonType::Steel) { if (defenderType == PokemonType::Rock) typeEffectiveness = 2.0; // Steel is super effective against Rock else if (defenderType == PokemonType::Electric) typeEffectiveness = 0.5; // Steel is not very effective against Electric } else if (moveType == PokemonType::Dragon) { if (defenderType == PokemonType::Dragon) typeEffectiveness = 2.0; // Dragon is super effective against Dragon else if (defenderType == PokemonType::Electric) typeEffectiveness = 0.5; // Dragon is not very effective against Electric } else if (moveType == PokemonType::Dark) { if (defenderType == PokemonType::Psychic) typeEffectiveness = 2.0; // Dark is super effective against Psychic else if (defenderType == PokemonType::Ghost) typeEffectiveness = 0.5; // Dark is not very effective against Ghost } else if (moveType == PokemonType::Fairy) { if (defenderType == PokemonType::Dragon) typeEffectiveness = 2.0; // Fairy is super effective against Dragon else if (defenderType == PokemonType::Dark) typeEffectiveness = 0.5; // Fairy is not very effective against Dark } return static_cast<int>((attack * movePower) / defense * typeEffectiveness); } void battle(Pokemon& player, Pokemon& opponent) { srand(static_cast<unsigned int>(time(0))); // Seed random number generator while (!player.isKnockedOut() && !opponent.isKnockedOut()) { std::cout << "Player's turn:" << std::endl; player.printStatus(); opponent.printStatus(); std::cout << "Choose a move: "; for (int i = 0; i < player.moves.size(); ++i) { std::cout << i + 1 << ". " << player.moves[i].name << std::endl; } int moveChoice; std::cin >> moveChoice; Move playerMove = player.moves[moveChoice - 1]; if (playerMove.type == MoveType::Attack) { int damage = calculateDamage(player.attack, opponent.defense, playerMove.power, player.type, opponent.type, playerMove.moveType); opponent.currentHP -= damage; std::string effectiveness; if (damage > (player.attack * playerMove.power) / opponent.defense) { effectiveness = "It's super effective!"; } else if (damage < (player.attack * playerMove.power) / opponent.defense) { effectiveness = "It's not very effective"; } else { effectiveness = ""; } std::cout << player.name << " uses " << playerMove.name << " and deals " << damage << " damage!" << effectiveness << std::endl; } else if (playerMove.type == MoveType::Status) { if (playerMove.name == "Heal") { player.heal(20); std::cout << player.name << " uses " << playerMove.name << " and heals 20 HP!" << std::endl; } else if (playerMove.name == "Decrease Attack") { opponent.decreaseAttack(0.2f); std::cout << player.name << " uses " << playerMove.name << " and decreases opponent's attack by 20%!" << std::endl; } else if (playerMove.name == "Decrease Speed") { opponent.decreaseSpeed(0.2f); std::cout << player.name << " uses " << playerMove.name << " and decreases opponent's speed by 20%!" << std::endl; } } if (opponent.isKnockedOut()) { std::cout << opponent.name << " is knocked out! " << player.name << " wins!" << std::endl; return; } std::cout << "Opponent's turn:" << std::endl; int randomMoveIndex = rand() % opponent.moves.size(); Move opponentMove = opponent.moves[randomMoveIndex]; if (opponentMove.type == MoveType::Attack) { int damage = calculateDamage(opponent.attack, player.defense, opponentMove.power, opponent.type, player.type, opponentMove.moveType); player.currentHP -= damage; std::string effectiveness; if (damage > (opponent.attack * opponentMove.power) / player.defense) { effectiveness = " It's super effective!"; } else if (damage < (opponent.attack * opponentMove.power) / player.defense) { effectiveness = " It's not very effective"; } else { effectiveness = ""; } std::cout << opponent.name << " uses " << opponentMove.name << " and deals " << damage << " damage!" << effectiveness << std::endl; } else if (opponentMove.type == MoveType::Status) { if (opponentMove.name == "Heal") { opponent.heal(20); std::cout << opponent.name << " uses " << opponentMove.name << " and heals 20 HP!" << std::endl; } else if (opponentMove.name == "Decrease Attack") { player.decreaseAttack(0.2f); std::cout << opponent.name << " uses " << opponentMove.name << " and decreases player's attack by 20%!" << std::endl; } else if (opponentMove.name == "Decrease Speed") { player.decreaseSpeed(0.2f); std::cout << opponent.name << " uses " << opponentMove.name << " and decreases player's speed by 20%!" << std::endl; } } if (player.isKnockedOut()) { std::cout << player.name << " is knocked out! " << opponent.name << " wins!" << std::endl; return; } } } int main() { Pokemon arcanine("Arcanine", 380, 110, 80, 105, PokemonType::Fire); //name, att, def, speed, type Pokemon delphox("Delphox", 320, 160, 70, 100, PokemonType::Psychic); Pokemon decidueye("Decidueye", 300, 160, 75, 200, PokemonType::Grass); Pokemon sylveon("Sylveon", 350, 90, 65, 80, PokemonType::Fairy); Pokemon umbreon("Umbreon", 330, 70, 120, 75, PokemonType::Dark); arcanine.addMove(Move("Flamethrower", 90, 100, MoveType::Attack, PokemonType::Fire)); arcanine.addMove(Move("Extreme Speed", 80, 100, MoveType::Attack, PokemonType::Normal)); arcanine.addMove(Move("Heal Up", 0, 100, MoveType::Status, PokemonType::Normal)); // Heal Up is a status move arcanine.addMove(Move("Roar", 0, 100, MoveType::Status, PokemonType::Normal)); // Roar is a status move delphox.addMove(Move("Psychic", 90, 100, MoveType::Attack, PokemonType::Psychic)); delphox.addMove(Move("Fire Blast", 110, 85, MoveType::Attack, PokemonType::Fire)); delphox.addMove(Move("Heal Up", 0, 100, MoveType::Status, PokemonType::Normal)); delphox.addMove(Move("Growl", 0, 100, MoveType::Status, PokemonType::Normal)); // Growl is a status move decidueye.addMove(Move("Leaf Blade", 90, 100, MoveType::Attack, PokemonType::Grass)); decidueye.addMove(Move("Spirit Shackle", 80, 100, MoveType::Attack, PokemonType::Ghost)); decidueye.addMove(Move("Heal Up", 0, 100, MoveType::Status, PokemonType::Normal)); decidueye.addMove(Move("Roar", 0, 100, MoveType::Status, PokemonType::Normal)); sylveon.addMove(Move("Moonblast", 95, 100, MoveType::Attack, PokemonType::Fairy)); sylveon.addMove(Move("Hyper Voice", 90, 100, MoveType::Attack, PokemonType::Normal)); sylveon.addMove(Move("Heal Up", 0, 100, MoveType::Status, PokemonType::Normal)); sylveon.addMove(Move("Growl", 0, 100, MoveType::Status, PokemonType::Normal)); umbreon.addMove(Move("Foul Play", 95, 100, MoveType::Attack, PokemonType::Dark)); umbreon.addMove(Move("Moonlight", 0, 100, MoveType::Status, PokemonType::Fairy)); umbreon.addMove(Move("Heal Up", 0, 100, MoveType::Status, PokemonType::Normal)); umbreon.addMove(Move("Roar", 0, 100, MoveType::Status, PokemonType::Normal)); // List of Pokémon for selection std::vector<Pokemon*> pokemonList = { &arcanine, &delphox, &decidueye, &sylveon, &umbreon }; // Display Pokémon for selection std::cout << "Choose your Pokémon (enter the number):" << std::endl; for (size_t i = 0; i < pokemonList.size(); ++i) { std::cout << i + 1 << ": " << pokemonList[i]->name << std::endl; } int playerChoice; int opponentChoice; std::cout << "Enter your choice (1-" << pokemonList.size() << "): "; std::cin >> playerChoice; std::cout << "Enter opponent's choice (1-" << pokemonList.size() << "): "; std::cin >> opponentChoice; // Start battle with chosen Pokémon battle(*pokemonList[playerChoice - 1], *pokemonList[opponentChoice - 1]); return 0; }