简体   繁体   中英

C++ Need advice on (why it isn't working properly) my basic text console game (I'm an beginner). Not working as I want it to

So I'm new to programming and I've encountered a problem when trying to make a basic text game.

BTW I do know I could've made some more functions to decrease the code (like instead of copy and paste the algorithm of the percentage I could've made a function that returned the value. But I just wanted to get it done so I could advance slowely

When compiling and running it expand the console, otherwise it'll not look good. (for menu and settings). Thanks in advance!

It isn't properly working. eHealth and hHealth (private class variables) isn't properly getting subtracted. I've the best I could but I have no idea what I'm doing wrong so please help me! Here's the source code:

2D game V1.cpp

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include "Header.h"
#include <windows.h>


using namespace std;


int main()
{
    combat CombatEngine;
    CombatEngine.renderMenu(0);

    return 0;
}

Header.h

#ifndef Header_H_
#define Header_H_

class combat {

private:
    int hHealth;
    int eHealth;

public:
    combat() : hHealth(100), eHealth(100) { };
    combat(int H, int E) : hHealth(H), eHealth(E) {};
    float fGen();
    void renderMenu(int choise);
    void renderSettings();
    void renderHuman(int D);
    void renderHumanAttack();
    void refreshScreen();
    void renderSkeleton(int D);
    void renderSkeletonAttack();
    void renderHumanVSkeleton(int D);
    void renderHumanVSkeletonAttack(int D);
    void inGame();
    int attacks(int attack);
    void dealDMG(int dmg, int d);
};


#endif

Combat Engine.cpp

    #include "stdafx.h"
#include <random>
#include <iostream>
#include <Windows.h>
#include "Header.h"
#include "string"
#include <ctime>


using namespace std;

float combat::fGen() {

    mt19937 fGenerator(time(NULL));
    uniform_real_distribution<float> randF(0.0f, 1.0f);
    float fNum(randF(fGenerator));

    return fNum;

}

void combat::renderMenu(int rendNum) {
    if (rendNum == 0)
    {
        for (int i = 0; i != 12; i++)
        {
            cout << "####################################################################################################\n";
        }
        cout << "############################################|-Play (1)|#############################################\n";
        cout << "###########################################|Settings (2)|###########################################\n";
        for (int i = 0; i != 13; i++)
        {
            cout << "####################################################################################################\n";

        }

    }

    int dec = 0;
    while (dec != 1 && dec != 2) {
        cin >> dec;
        if (dec != 1 && dec != 2)
        {
            cout << "Error: Invalid input!\n";
        }
    }
    if (dec == 1) {
        refreshScreen();
        inGame();
    }
    if (dec == 2) {
        renderSettings();
}

}

void combat::renderSettings() {
    refreshScreen();
    for (int i = 0; i != 12; i++)
    {
        cout << "####################################################################################################\n";
    }
    cout << "#####################################| change human health (1)|#####################################\n";
    cout << "######################################| change mob health (2)|######################################\n";
        cout << "#############################################|Back (3)|#############################################\n";
    for (int i = 0; i != 12; i++)
    {
        cout << "####################################################################################################\n";
    }
    int dec = 0;
    while (dec != 1 && dec != 2 && dec != 3)
    {
        cin >> dec;
        if (dec != 1 && dec != 2 && dec != 3)
        {
            cout << "Error: Invalid input!\n";
        }
    }
    for (;;)
    {
        if (dec == 1)
        {
            refreshScreen();
            for (int i = 0; i != 13; i++)
            {
                cout << "####################################################################################################\n";
            }

                cout << "#############################| << Type the new value (previously: " << hHealth <<  ".)|############################\n";

            for (int i = 0; i != 13; i++)
            {
                cout << "####################################################################################################\n";
            }
            int inc;
            cin >> inc;
            hHealth = inc;
            refreshScreen();
            renderMenu(0);

        }
        if (dec == 2)
        {
            refreshScreen();
            for (int i = 0; i != 13; i++)
            {
                cout << "####################################################################################################\n";
            }

            cout << "#############################| << Type the new value (previously: " << eHealth << ".)|############################\n";

            for (int i = 0; i != 13; i++)
            {
                cout << "####################################################################################################\n";
            }
            int inc;
            cin >> inc;
            eHealth = inc;
            refreshScreen();
            renderMenu(0);
        }
        if (dec == 3)
        {
            refreshScreen();
            renderMenu(0);

        }
    }
}


void combat::renderHuman(int D) {
    if (D == 0) {
        cout << "_____\n";
        cout << "|   |\n";
        cout << "O _ O\n";
        cout << "/|_|\\ \n";
        cout << "_| |_\n";
        cout << "#############################################" << endl;
    }
    if (D == 1) {
        cout << "_____\n";
        cout << "|   |\n";
        cout << "O _ O/\n";
        cout << "/|_|/ \n";
        cout << "_| |_\n";
        cout << "#############################################" << endl;
    }



}

void combat::renderHumanAttack()
{

    Sleep(500);
    refreshScreen();
    renderHuman(1);
    Sleep(250);
    refreshScreen();
    renderHuman(0);
    Sleep(500);
    refreshScreen();
    renderHuman(1);
    Sleep(250);
    refreshScreen();
    renderHuman(0);
}

void combat::refreshScreen()
{
    system("cls");
}

void combat::renderSkeleton(int D)
{
    if (D == 0)
    {
            cout << " ___" << endl;
            cout << "{o_o}" << endl;
            cout << "/( )\\ " << endl;
            cout << " / \\" << endl;
            cout << "#############################################" << endl;
    }
    if (D == 1)
    {
        cout << "  ___" << endl;
        cout << "\\{o_o}" << endl;
        cout << " \\( )\\ " << endl;
        cout << "  / \\" << endl;
        cout << "#############################################" << endl;
    }
}

void combat::renderSkeletonAttack()
{
    Sleep(500);
    refreshScreen();
    renderSkeleton(1);
    Sleep(250);
    refreshScreen();
    renderSkeleton(0);
    Sleep(500);
    refreshScreen();
    renderSkeleton(1);
    Sleep(250);
    refreshScreen();
    renderSkeleton(0);
}

void combat::renderHumanVSkeleton(int D)
{
    if (D == 0)
    {
        //refreshScreen();
        cout << "_____               \n";
        cout << "|   |               ";
        cout << " ___" << endl;
        cout << "O _ O               ";
        cout << "{o_o}" << endl;
        cout << "/|_|\\               ";
        cout << "/( )\\ " << endl;
        cout << "_| |_               ";
        cout << " / \\" << endl;
        cout << "#############################################" << endl;






    }

    if (D == 1) 
    {
        //human attacking
        //refreshScreen();
        cout << "_____               \n";
        cout << "|   |               ";
        cout << " ___" << endl;
        cout << "O _ O/              ";
        cout << "{o_o}" << endl;
        cout << "/|_|/               ";
        cout << "/( )\\ " << endl;
        cout << "_| |_               ";
        cout << " / \\" << endl;
        cout << "#############################################" << endl;





    }

    if (D == 2)
    {
        //skeleton attacking
        //refreshScreen();

        cout << "_____               \n";
        cout << "|   |               ";
        cout << "  ___" << endl;
        cout << "O _ O               ";
        cout << "\\{o_o}" << endl;
        cout << "/|_|\\               ";
        cout << " \\( )\\ " << endl;
        cout << "_| |_               ";
        cout << "  / \\" << endl;
        cout << "#############################################" << endl;




    }
}



void combat::renderHumanVSkeletonAttack(int D)
{
    if (D == 0)
    {
        //human attacking
        Sleep(500);
        refreshScreen();
        renderHumanVSkeleton(1);
        Sleep(250);
        refreshScreen();
        renderHumanVSkeleton(0);
        Sleep(500);
        refreshScreen();
        renderHumanVSkeleton(1);
        Sleep(250);
        refreshScreen();
        renderHumanVSkeleton(0);
    }

    if (D == 1)
    {
        //skelleton attacking
        Sleep(500);
        refreshScreen();
        renderHumanVSkeleton(2);
        Sleep(250);
        refreshScreen();
        renderHumanVSkeleton(0);
        Sleep(500);
        refreshScreen();
        renderHumanVSkeleton(2);
        Sleep(250);
        refreshScreen();
        renderHumanVSkeleton(0);
    }
}

int combat::attacks(int attack)
{

    if (attack == 1) {
        //25% chance for 15 DMG if hit 40% for hit

        if (fGen() <= 0.25f)
        {

            if (fGen() <= 0.25f)
            {

                return  15 * 1.5;
            }
            else
                return 15;


        }
        if (attack == 2) {
            //60% chance for 7 DMG if hit 40% for hit
            if (fGen() <= 0.60f)
            {
                if (fGen() <= 0.25f)
                {
                    return 7 * 2;
                }
                else
                    return 7;

            }
        }

        if (attack == 3) {
            //75% chance for 3 DMG if hit 40% for hit
            if (fGen() <= 0.75f)
            {

                if (fGen() <= 0.40f)
                {
                    return 3 * 2;
                }
                else
                    return 3;

            }
        }

        if (attack == 4) {
            //10% chance for 20 DMG if hit 25% for crit
            if (fGen() <= 0.10f)
            {
                if (fGen() <= 0.25f)
                {
                    return (int)20 * (int)1.5;
                }
                else
                    return 20;

            }
        }


    }
    return 0;
}

void combat::dealDMG(int dmg, int d)
{
    if (d == 0)
    {
        eHealth - dmg;
    }
    else
    {
        hHealth - dmg;
    }
}

void combat::inGame()
{
    float t = fGen();
    cout << t;
    bool who;
    if (t >= 0.5f)
    {
        cout << "You start attacking first!" << endl;
        who = true;
    }
    else
    {
        cout << "The skeleton starts attacking first!" << endl;
        who = false;
    }

    while (eHealth > 0 || hHealth > 0)
    {
        if (!who)
        {
            refreshScreen();
            cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
            renderHumanVSkeleton(0);
            Sleep(1500);
            int decision;
            float randF = fGen();

            if (randF <= 0.25f)
                decision = 1;
            else if (randF > 0.25f && randF < 0.50f)
                decision = 2;
            else if (randF > 0.50f && randF < 0.75f)
                decision = 3;
            else
                decision = 4;



            int dmg = attacks(decision);



            dealDMG(dmg, 1);
            refreshScreen();
            cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
            renderHumanVSkeletonAttack(1);
            who = true;
        }


        if (who)
        {
            refreshScreen();
            cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
            renderHumanVSkeleton(0);
            cout << "It's your turn to attack!\n1) 25% chance to hit, DMG = 15. 40% chance of crit (22 DMG)\n2) 60% chance for 7 dmg. 40% chance for crit (14 DMG)\n3) 75 chance to hit, DMG = 3 dmg. 40% to crit (6 DMG)\n4)10% chance to hit, DMG = 20. 25% chance to crit (30 DMG)\n";
            int decision2;
            cin >> decision2;
            while (decision2 != 1 && decision2 != 2 && decision2 != 3 && decision2 != 4) {
                cin >> decision2;
            }
            int dmg = attacks(decision2);

            dealDMG(dmg, 0);
            refreshScreen();
            cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
            renderHumanVSkeletonAttack(0);
            who = false;


        }

    }
    if (eHealth == 0) {
        refreshScreen();
        renderHuman(0);
        cout << "You won!" << endl;
        renderMenu(0);
    }
    else
    {
        refreshScreen();
        renderSkeleton(0);
        cout << "You loose!" << endl;
        renderMenu(0);
    }

}
eHealth - dmg;

shouldn't that be

eHealth -= dmg;

and

#include "string"

try

#include <string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM