Fixed up my code a little bit.

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6

86 Replies - 2355 Views - Last Post: 26 February 2018 - 08:26 AM Rate Topic: -----

#76 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 23 February 2018 - 02:38 PM

Again where did you place the breakpoint in that function?

What exactly is Random::get(0,1) actually doing?


What is the value of encounter?

Do you realize that if the value held in encounter is anything other than 1 the program will return to the calling function?

Also that for() loop is not going to do what you probably expect, it will at absolute best run one time.

What is with all of those ridiculous cin.get(), cin.ignore(), system() calls?

Jim
Was This Post Helpful? 0
  • +
  • -

#77 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 23 February 2018 - 02:44 PM

0 errors, 0 warnings
Was This Post Helpful? 0
  • +
  • -

#78 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,458
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 23 February 2018 - 03:27 PM

0 warnings is not indicative of anything without context of what warning levels you have set, and if you have any #pragma warn disable. What warning level do you have the compiler set to? Are you deliberately disabling some warnings?
Was This Post Helpful? 0
  • +
  • -

#79 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 24 February 2018 - 04:18 AM

I haven't disabled any warnings. My complier is set to the default, which I have no idea what level it is. I haven't got any #pragma warn disable.

Ok i just found out that warnings were disabled in the compiler setting for whatever reason. They were on yesterday. Anyways i got 3 warning and one of them was for an uninitialized variable.
Was This Post Helpful? 0
  • +
  • -

#80 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 24 February 2018 - 04:38 AM

And i seem to have fixed it. Thanks. Jimblumberg - Random::get(0,1) gets a random number between 0 and 1. The cin.get() and ci.ignore are for players to press a key before the screen is cleared.
Was This Post Helpful? 0
  • +
  • -

#81 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,458
  • Joined: 05-May 12

Re: Fixed up my code a little bit.

Posted 24 February 2018 - 12:22 PM

View PostXrey274, on 24 February 2018 - 06:38 AM, said:

Random::get(0,1) gets a random number between 0 and 1.

In your other thread, you made a big deal about finding a better random number generator library. Why not use it's full capabilities to give you back a boolean:

Quote

Bool
Generate true with [0; 1] probability
auto val = Random::get<bool>(0.7) // true with 70% probability


auto val = Random::get<bool>() // true with 50% probability by default




View PostXrey274, on 24 February 2018 - 06:38 AM, said:

The cin.get() and ci.ignore are for players to press a key before the screen is cleared.

Instead of copying and pasting this code everywhere, put it into a (well named) helper function or three: Something like:
void PromptForKeyPress(const std::string & text)
{
     // your code here to show the text
     // and then wait for a key press
}

void ClearScreen()
{
    // your code here to clear the screen
}

void PromptForKeyPressAndClearScreen(const std::string & text)
{
    PromptForKeyPress(text);
    ClearScreen();
}


Was This Post Helpful? 2
  • +
  • -

#82 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 25 February 2018 - 05:36 AM

Is it possible to make a function wait for a another function to finish before it runs? Because when an enemy dies the death screen appears just after the attack animation begins and after i press enter the animation continues.
Was This Post Helpful? 0
  • +
  • -

#83 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 825
  • Posts: 3,337
  • Joined: 13-June 14

Re: Fixed up my code a little bit.

Posted 25 February 2018 - 05:38 AM

Well, if you have a function that calls two functions, say, the second one isn't going to be called until the first returns.
Was This Post Helpful? 0
  • +
  • -

#84 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 26 February 2018 - 06:39 AM

I tried to implement a function or two, but I got a error which I can't seem to fix. The error is at line
16 in Player.h and it says - "Enemy" has not been delcared. Also it says this error at the same line twice. The second error is at line 291 in main.cpp - no matching function to call "Player::mightGuy(Enemy&)".

main.cpp
#include "main.h"
#include "resource.h"


//TODO Add stats which change attacks and defenses
//TODO Add an inventory
//TODO Experiment with data saving
//TODO Add music
//TODO Add player death
//TODO Make Death Screen appear after animation is over

using namespace std;



int main()
{
    //bool dataAvalible = false;
    //vector<int> dataStorage;
    string player_name;

    cout<<"                          Welcome to MortalKombat 2.\n\n"<<endl;
    cout<<"                          Press any key to continue.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n Made By: Xrey274\n"<<endl;
    cout<<" Running Version: Alpha 2.9.1\n"<<endl;
    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
    cout<<"                                  Update Info\n"<<endl;
    cout<<" -Now the player has XP, which will be used in the future."<<endl;
    cout<<" -Code was further optimized."<<endl;
    cout<<" -Enemies can now die, but you can encounter new ones."<<endl;
    cout<<" -Better enemy encountering."<<endl;
    cout<<" -Random enemy names."<<endl;
    cout<<" -Bug fixed regarding animations and others"<<endl;

    //loadData(dataAvalible);

    askForePress_clearScreen();

    cout<<"                            Please enter your name:\n\n"<<endl;
    cout<<"                                   ";cin>>player_name;

    Engine(player_name);
}

void Engine(string player_name)
{
    bool game_over = false;
    Player pl;
    Enemy en;
    Animations an;

    while(!game_over)
    {
        system("cls");
        if(pl.getTile() == 69)
        {
            menu_dank(game_over, player_name, en);
        }
        else
        {
            menu(game_over, player_name, pl, en);
        }


        string input_string;
        int input_number = 0;

        do
        {
            input_number = 0;
            cin >> input_string;

            stringstream converter;
            converter << input_string;
            converter >> input_number;

            if(input_number < 1  || input_number > 4)
            {
                cout<<"Error please try input again!"<<endl;
            }
        }while(!(input_number > 0 && input_number < 5));


        switch(input_number)
        {
            case 1:
                system("cls");
                Movement(pl, en);
                break;
            case 2:
                system("cls");
                Attack(player_name, pl, en, an);
                break;
        }

        if(game_over)
        {
            Death();
        }
    }
}

void menu(int game_over, string player_name, Player& pl, Enemy& en)
{
    string str1 = "'s Health: ", str2 = "          ",  healthS, enemyHealthS;
    int totalLen;

    cout<<"                      Please choose what you want to do.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n 1-Move\n"<<endl;
    cout<<" 2-Attack\n"<<endl;
    cout<<" 3-Inventory\n"<<endl;
    cout<<" 4-Stats\n"<<endl;

    stringstream stringToInt;
    stringToInt << pl.getHealth();
    stringToInt >> healthS;

    stringstream stringToInt2;
    stringToInt2 << en.getHealth();
    stringToInt2 >> enemyHealthS;

    cout<<" "<<player_name<<str1<<healthS;

    if(en.getEncounter() == true)
    {
        totalLen = player_name.length() + en.getNameLength() + str1.length() + str2.length() + healthS.length() + enemyHealthS.length() + 1;

        for(int i = 0; i < 66 - totalLen ; i++)
        {
            str2.insert(0, " ");
        }

        cout<<str2<<en.getName()<<str1<<enemyHealthS<<endl;
    }
    else
    {
        cout<<""<<endl;
    }

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void menu_dank(int game_over, string player_name, Enemy& en)
{
    cout<<"                      69696969696969696969696969696969696969.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-6969\n"<<endl;
    cout<<" 2-696969\n"<<endl;
    cout<<" 3-6969696969\n"<<endl;
    cout<<" 4-69696969\n"<<endl;
    cout<<" 69696969:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}


void Movement(Player& pl, Enemy& en)
{
    bool loopOver = false;
    do
    {
        cout<<"                      How many tiles do you want to move?"<<endl;
        cout<<"\n\n                                    Tile:"<<pl.getTile()<<"\n"<<endl;
        cout<<"\n--------------------------------------------------------------------------------"<<endl;
        cout<<"\n\n                    Hint:You can only move 5 tiles at a time."<<endl;

        if(en.getEncounter() == false)
        {
            int movementInput = 0;
            cin>>movementInput;

            if(movementInput > 0 && movementInput < 6)
            {
                int x = 6 - movementInput;

                for(int i = x; i <= 5; i++)
                {
                    pl.tileIncrease();
                    en.Encountering(pl.getTile());
                }

                if(en.getEncounter() == true)
                {
                    loopOver = true;
                    break;
                    askForPress();
                }
            }
            else if(movementInput == -1)
            {
                loopOver = true;
                break;
            }
            else
            {
                cout<<"Error, try again"<<endl;
                continue;
            }

        }

        else if(en.getEncounter() == true)
        {
            cout<<"\n                  Sorry, but you can't move while in a fight!"<<endl;
            loopOver = true;


            askForPress();
        }

        clearScreen();
    }while(loopOver == false);

}

void Attack(string player_name, Player& pl, Enemy& en, Animations& an)
{
    bool loopOver = false;
    string str1 = "'s Health: ", str2 = "          ", healthS, enemyHealthS;
    int totalLen;

    do
    {
        cout<<"                            Please choose an attack!"<<endl;

        cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

        cout<<" 1.Hadouken\n"<<endl;
        cout<<" 2.Recover\n"<<endl;
        cout<<" 3.Might Guy\n"<<endl;
        cout<<" 4.Toxic Spikes"<<endl;


        cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

        stringstream stringToInt;
        stringToInt << pl.getHealth();
        stringToInt >> healthS;

        stringstream stringToInt2;
        stringToInt2 << en.getHealth();
        stringToInt2 >> enemyHealthS;

        totalLen = player_name.length() + en.getNameLength() + str1.length() + str2.length() + healthS.length() + enemyHealthS.length() + 1;

        for(int i = 0; i < 66 - totalLen ; i++)
        {
            str2.insert(0, " ");
        }

        cout<<" "<<player_name<<str1<<healthS<<str2<<en.getName()<<str1<<enemyHealthS<<endl;

        string attackString;
        int attackInput = 0;

        if(en.getEncounter() == true)
        {
            cin>>attackString;

            stringstream converter;
            converter << attackString;
            converter >> attackInput;
        }
        else if(en.getEncounter() == false)
        {
            cout<<"\n\n                 You can't attack without encountering an enemy!"<<endl;
            loopOver = true;

            break;
            askForPress();
        }

        switch(attackInput)
        {
            case -1:
                loopOver = true;
                break;
            case 1:
                en.take_Damage(5, pl);
                an.hadouken_Animation();
                en.Attack(pl);
                break;
            case 2:
                pl.setHealth(1);
                an.recover_Animation();
                break;
            case 3:
                pl.mightGuy(en);
                an.hadouken_Animation();
                break;
            default:
                cout<<"Error, try again."<<endl;
                break;
        }

    }while(loopOver == false);
}

void Death()
{

}

int saveData()
{

}

int loadData(int dataAvalible)
{
    return dataAvalible = true;
}

void askForPress()
{
    cin.get();
    cin.sync();
}

void clearScreen()
{
    system("cls");
}

void askForePress_clearScreen()
{
    cin.get();
    cin.sync();

    system("cls");
}




main.h
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <sstream>
#include <fstream>

void Engine(string player_name);
void menu(int game_over, string player_name, Player& pl, Enemy& en);
void menu_dank(int game_over, string player_name, Enemy& en);
void Movement(Player& pl, Enemy& en);
void Attack(string player_name, Player& pl, Enemy& en, Animations& an);
void Death();
int saveData();
int loadData(int);
void askForPress();
void clearScreen();
void askForePress_clearScreen();

#endif // MAIN_H_INCLUDED




Player.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include "Stats.h"
#include "main.h"
#include "random.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <sstream>

using namespace std;
using Random = effolkronium::random_static;

Player::Player()
{
    playerHealth = 20;
    XP = 0;
}

int Player::getTile()
{
    return Tile;
}

void Player::tileIncrease()
{
    Tile++;
}

int Player::getHealth()
{
    return playerHealth;
}

void Player::setHealth(int healthIncrease)
{
   if(healthIncrease == 25)
   {
       playerHealth = playerHealth + 5;
   }
}

void Player::take_Damage(int Damage)
{
    int x = 6 - Damage;

    for(int y = x; y <= 5; y++)
    {
        playerHealth = playerHealth - 1;
    }
}

void Player::mightGuy(Enemy& en)
{
    playerHealth = playerHealth - 18;
    en.killEnemy(pl);
}

int Player::xpIncrease()
{
    int lastGain = 0;
    int xpGain = Random::get(20+lastGain,50+lastGain);

    lastGain = xpGain;
    XP = XP + lastGain;

    return lastGain;
}

int Player::xpTotal()
{
    return XP;
}




Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player();
        int getTile();
        void tileIncrease();
        int getHealth();
        void setHealth(int);
        void take_Damage(int);
        void mightGuy(Enemy&);
        int xpIncrease();
        int xpTotal();
    private:
        int movementInput;
        int attackInput;
        string attackString;
        int playerHealth;
        int Tile = 1;
        int XP;
};

#endif // PLAYER_H




Enemy.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include "Stats.h"
#include "main.h"
#include "random.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;
using Random = effolkronium::random_static;

Enemy::Enemy()
{
    enemyHealth = 20;
    enemyDeaths = 0;
    enemyKills = 0;
    enemyEncountered = false;

    setName();
}

void Enemy::Encountering(int tilesMoved)
{
    int encounter = Random::get<bool>(0.65);

    cout<<encounter<<endl;

    for(int x = 0; x <= 5; x++)
    {
        if(encounter == 1 && enemyEncountered == false)
        {
            x = 6;

            system("cls");
            cout<<"\n                               Enemy encountered!"<<endl;

            enemyHealth = 20;
            enemyEncountered = true;

            cin.ignore();
            askForePress_clearScreen();
        }
        else continue;
    }
}

void Enemy::Attack(Player& pl)
{
    Animations an;
    if(enemyEncountered == true)
    {
        int attackChance = Random::get(1,4);

        //TODO Add more enemy attacks
        switch(attackChance)
        {
            case 1:
                pl.take_Damage(5);
                an.hadouken_Animation();
                break;
            case 2:
                enemyHealth = enemyHealth +5;
                an.recover_Animation();
                break;
        }
   }
}

string Enemy::getName()
{
    return name;
}

int Enemy::getNameLength()
{
    int len = name.length();

    return len;
}

void Enemy::setName()
{
    int rand = Random::get(1,10);

    switch(rand)
    {
        case 1:
            name = "Bob";
            break;
        case 2:
            name = "Devilman";
            break;
        case 3:
            name = "Stinkhag";
            break;
        case 4:
            name = "Cindertaur";
            break;
        case 5:
            name = "Brineling";
            break;
        case 6:
            name = "Netherseeker";
            break;
        case 7:
            name = "Rustwraith";
            break;
        case 8:
            name = "Vexpaw";
            break;
        case 9:
            name = "Hellsoul";
            break;
        case 10:
            name = "Germbag";
            break;
        default:
            name = "Error";
    }
}

void Enemy::take_Damage(int Damage, Player& pl)
{
    int x = 6 - Damage;
    Stats st(false);

    for(int y = x; y <= 5; y++)
    {
        enemyHealth = enemyHealth - 1;

        if(enemyHealth == 0)
        {
            clearScreen();
            Death(pl);
        }
    }
}

int Enemy::getHealth()
{
    return enemyHealth;
}

void Enemy::setEncounter(int choice)
{
    if(choice == 0)
        enemyEncountered = false;
    else
        enemyEncountered = true;
}

bool Enemy::getEncounter()
{
    return enemyEncountered;
}

void Enemy::killEnemy(Player& pl);
{
    Death(pl);
}

void Enemy::Death(Player& pl)
{
    enemyDeaths++;

    cout<<"                            "<<name<<" has been defeated!\n\n"<<endl;
    cout<<"                                 Player Stats\n"<<endl;
    cout<<"  - Number of kills: "<<enemyDeaths<<endl;
    cout<<"  - XP gained: "<<pl.xpIncrease()<<endl;
    cout<<"  - XP Total: "<<pl.xpTotal()<<endl;

    setEncounter(0);
    setName();

    cin.ignore();
    askForPress();
}




Enemy.h
#ifndef ENEMY_H
#define ENEMY_H


class Enemy
{
    public:
        Enemy();
        void Encountering(int);
        void Attack(Player&);
        string getName();
        int getNameLength();
        void setName();
        void take_Damage(int, Player&);
        int getHealth();
        void setEncounter(int);
        bool getEncounter();
        void killEnemy(Player&);
        void Death(Player&);
    private:
        int enemyHealth;
        int enemyDeaths;
        int enemyKills;
        bool enemyEncountered;
        string name;
};

#endif // ENEMY_H



Was This Post Helpful? 0
  • +
  • -

#85 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Fixed up my code a little bit.

Posted 26 February 2018 - 07:47 AM

Near the top of Player.h, add #include "Enemy.h"
Was This Post Helpful? 0
  • +
  • -

#86 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 26 February 2018 - 07:49 AM

Quote

I tried to implement a function or two, but I got a error which I can't seem to fix. The error is at line
16 in Player.h and it says - "Enemy" has not been delcared.


What exactly don't you understand about this error message, it is quite specific? You need to declare Enemy.

Quote

Also it says this error at the same line twice. The second error is at line 291 in main.cpp - no matching function to call "Player::mightGuy(Enemy&)".


This is being caused by the first error.


Jim
Was This Post Helpful? 0
  • +
  • -

#87 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fixed up my code a little bit.

Posted 26 February 2018 - 08:26 AM

Yes I don't undestand exactly how I have to declare Enemy

I figured it out, thanks anyway.
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6