Ir ao conteúdo

Posts recomendados

Postado

Sou iniciante em C++, fiz esse código a caráter de estudo, o programa pede o nome do usuário (playerName) e um número inteiro (zombieCount), e enquanto playerAlive for verdadeiro o zombieCount define o número de rodadas e em cada rodada dois números aleatórios (createZombie, chance) são gerados definindo texto que será mostrado para o usuário e realizando ou não alterações nos int (playerScore, playerSkill, zombieSkill, zombiesKilled).

 

No fim são exibidos os int (zombiesKilled, playerScore) e a string playerName. E o programa pode reiniciar caso o usuário tecle 'y'.

 

Enfim, gostaria de dividir esse código em algumas classes se possível, porém não sei por onde começar...

Segue em anexo o código

 

 

 


  

  #include <iostream>
    #include <stdlib.h>
    #include <unistd.h>
    #include <time.h>
    #include <windows.h>
    using namespace std;

    int wait = 170;

    int createZombie() {
        if (rand() % 67 > 65) {
            return 21;
        }

        if (rand() % 67 < 10)
            return 11;

        else
            return rand() %10 + 1;
    }

    int chance() {
        return rand() % 100;
    }

    int main() {
        srand(time(NULL));

        char again;

        // game stats
        int playerAlive = true;
        int playerSkill = 9;
        int playerScore = 1;
        string playerName = "";
        int zombieCount = 0;
        int zombiesKilled = 0;
        again = 'y';


        while (again == 'y') {
            // title
            cout << "Welcome to Zombie War 1.3" << endl << "Press [ENTER] to start.";
            cin.get();

            // player name
            cout << "Please enter your name:";
            cin >> playerName;

            //ask how many zombies
            cout << "How many zombies do you wish to fight? ";
            cin >> zombieCount;

            cout << "Get ready to fight for your life, " << playerName << "!" << endl;

            // main game loop
            while (playerAlive && zombiesKilled < zombieCount) {

                // create a random zombie
                int zombieSkill = createZombie();
                int luck = chance();


                // battle sequence
                if (zombieSkill > 20) {
                    cout << endl << "Here comes a warrior deathly zombie!!!" << endl;
                    Sleep (wait);
                }

                else {
                    if (zombieSkill > 10 && zombieSkill < 20) {
                        cout << endl << "Here comes a huge zombie!" << endl;
                        Sleep (wait);
                    }
                    else {
                        cout << endl << "Here comes zombie " << zombiesKilled    + 1    << endl;
                        Sleep (wait);
                    }
                }

                cout << "Fighting..." << endl;
                Sleep(2000);


                //droping itens
                if (luck > 80) {
                    playerSkill = playerSkill + 1;
                    cout << "You ate some cookies! " << endl;
                    Sleep (wait);
                }

                if (luck > 98 && zombieSkill > 10) {
                    playerSkill = playerSkill + 5;
                    cout << "You found a rifle!!" << endl;
                    Sleep (wait);
                }

                if ( luck > 60 && zombieSkill < 5) {
                    playerSkill = playerSkill + 1;
                    cout << "Your hatred for zombies gave you strength." << endl;
                    Sleep (wait);
                }

                if ( luck > 70 && zombieSkill < 11 && playerScore > 3) {
                    playerSkill = playerSkill + 1;
                    cout << "The pain you feel increases your focus!!" << endl;
                    Sleep (wait);
                }

                if ( luck < 3 && zombieSkill < 6) {
                    playerSkill = playerSkill - 2;
                    cout << "The zombie goes into frenzy!!" << endl;
                    Sleep (wait);
                }

                if (luck < 5) {
                    zombieSkill = zombieSkill + 2;
                    cout << "The zombie has a sword!" << endl;
                    Sleep (wait);
                }

                if (luck > 91) {
                    zombieSkill = zombieSkill - 2;
                    playerSkill = playerSkill + 1;
                    cout << endl << "You cut off his leg!! ";
                    Sleep (wait);
                    cout << "You found some bandage!" << endl;
                }

                if (zombieSkill > 10 && zombieSkill < 20 && luck < 35) {
                    cout << endl << "He eats your arm!" << endl;
                    playerSkill = playerSkill -1;
                    Sleep (wait);
                }

                if (zombieSkill > 10 && zombieSkill < 20 && luck > 85) {
                    playerSkill = playerSkill +1;
                    zombieSkill = zombieSkill -1;
                    cout << endl << "The zombie tries to grab you.";
                    Sleep (wait);
                    cout << endl << "You dodge and knock him down"<< endl;
                    Sleep (wait);
                }

                if (zombieSkill > 11 && luck > 85) {
                    zombieSkill = zombieSkill - 4;
                    cout << "You got a critical hit!!" << endl;
                    Sleep (wait);
                    cout << "The zombie became blind!" << endl;
                    Sleep (wait);
                }

                if (zombieSkill > 9 && luck > 80 && playerSkill < 11) {
                    zombieSkill = zombieSkill - 2;
                    cout << "The zombie grabbet you!" << endl;
                    Sleep (wait);
                    cout << "You broke free with a butt in the zombie." << endl;
                    Sleep (wait);
                }

                if (zombieSkill > 20 && luck < 20 ) {
                    zombieSkill = zombieSkill + 2;
                    cout << "The zombie break your leg!!" << endl;
                    Sleep (wait);
                    cout << "He threw you on the ground like it was nothing" << endl;
                    Sleep (wait);
                }
                Sleep (wait);

                //events
                if ( luck < 20 ) {
                    cout << "You lost one of yours tooth!" << endl;
                    Sleep (wait);
                }

                if ( luck < 10 && zombieSkill < 10) {
                    cout << "The zombie bit your ear" << endl;
                    Sleep (wait);
                }
                Sleep (wait);

                //zombie killed the player
                if (playerSkill < zombieSkill) {
                    playerAlive = false;
                    cout << "You have died." << endl;
                }


                // player killed the zombie
                else {
                    if (playerSkill - zombieSkill > 11){
                        cout << "You blew the zombie's head!"<< endl;
                        playerScore = playerScore + 1;
                        Sleep (2 * wait);
                    }
                    if (playerSkill - zombieSkill > 7 && playerSkill - zombieSkill < 12) {
                        cout << "You wasted the zombie!" << endl;
                        playerScore = playerScore + 2;
                        Sleep (2 * wait);
                    }

                    else if (playerSkill - zombieSkill > 5 && playerSkill - zombieSkill <  {
                        cout << "You decapitated the zombie!" << endl;
                        playerScore = playerScore + 3;
                        Sleep (2 * wait);
                    }

                    else if (playerSkill - zombieSkill > 0 && playerSkill - zombieSkill < 6 ) {
                        cout << "You killed the zombie!" << endl;
                        playerScore = playerScore + 4;
                        Sleep (2 * wait);
                    }

                    else if (playerSkill > zombieSkill && zombieSkill > 10 && zombieSkill <20) {
                        playerScore = playerScore + 5;
                        Sleep (2 * wait);
                    }

                    else if (playerSkill > zombieSkill && zombieSkill > 20) {
                        playerScore = playerScore + 20;
                        Sleep (2 * wait);
                    }

                    else {
                        cout << "You killed the zombie, but suffered injuries." << endl;
                        playerSkill = playerSkill - 1;
                        Sleep (2 * wait);
                    }

                    zombiesKilled++;
                }

                cout << endl;
                Sleep (3000);
            }


            // end game
            if (zombiesKilled == zombieCount) {
                // victory
                cout << "You have survived the onslaught! \n" <<
                        "Congratulations " + playerName + "!" << endl;
            }
            else {
                // lost
                cout << "You did not survive the zombie war. \n" <<
                        "Try to come back stronger next time " + playerName + "!" << endl;
            }
            Sleep (wait);
            cout << "Zombies killed: " << zombiesKilled << endl;
            cout<< "Final score: " << playerScore << endl << endl;
            cout << "Do you wanna play again? y/n" << endl;
            cin >> again;

        if ( again != 'y'){
            break;
        }
        else {
            playerAlive = true;
            zombiesKilled = 0;
            zombieCount =0;
        }
    }
        return 0;
}

 

 

 

 

Game.rar

image.png

  • Curtir 1

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!