Fixed up my code a little bit.

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

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

#1 Xrey274   User is offline

  • D.I.C Head

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

Fixed up my code a little bit.

Posted 26 January 2018 - 03:22 PM

I had a problem with my game, preciselly with "spaghetti code", but I think that I have fixed it up atleast a bit. Tell me if my code has improved and how I can improve it further. Also I there is a little bug, where when you type "-1", when you haven't encountered an enemy in the attack menu, it brings up the movement menu. The playerMovement system is probably the only thing that really needs an overhaul.

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

using namespace std;

void Engine(string player_name);
void menu(int game_over, string player_name, bool enemyEncountered, Enemy& en);
void menu_dank(int game_over, string player_name, bool enemyEncountered, Enemy& en);
void playerMovement(bool& enemyEncountered, Player& pl, Enemy& en);
void playerAttack(bool& enemyEncountered, Player& pl, string player_name, Enemy& en);
void Death();

int main()
{
    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 1.4\n"<<endl;
    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
    cout<<"                                  Update Info"<<endl;

    if(cin.get())
    {
        system("cls");
    }

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

    Engine(player_name);
}

void Engine(string player_name)
{
    bool enemyEncountered = false;

    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, enemyEncountered, en);
        }
        else
        {
            menu(game_over, player_name, enemyEncountered, en);
        }


        string input_string;
        int input_number;

        do
        {
            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");
                playerMovement(enemyEncountered, pl, en);
                input_number = 0;
                break;
            case 2:
                system("cls");
                playerAttack(enemyEncountered, pl, player_name, en);
                input_number = 0;
                break;
        }

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

void menu(int game_over, string player_name, bool enemyEncountered, Enemy& en)
{
    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;
    cout<<" Player Name: "<<player_name<<"                                         Enemy Health: ";

    if(enemyEncountered == true)
    {
        cout<<en.getHealth();
    }
    else
    {
        cout<<"N/A"<<endl;
    }

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

void menu_dank(int game_over, string player_name, bool enemyEncountered, 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 playerMovement(bool& enemyEncountered, Player& pl, Enemy& en)
{
    bool loopOver;
    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(enemyEncountered == false)
        {
            int movementInput;
            cin>>movementInput;

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

                for(int i = x; i <= 5; i++)
                {
                    pl.tileIncrease();
                    loopOver = false;
                    en.enemyEncountering(pl.getTile(), enemyEncountered);
                }
                loopOver = true;
                break;

                cin.get();
            }
            else if(movementInput <= 0)
            {
                loopOver = true;
                break;
            }

        }

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

            cin.ignore();
            cin.get();
        }

        system("cls");
    }while(loopOver == false);

}

void playerAttack(bool& enemyEncountered, Player& pl, string player_name, Enemy& en)
{
    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;

    cout<<" "<<player_name<<"'s Health: "<<pl.getHealth()<<"                                       "<<"Enemy's Health: ";
    if(enemyEncountered == true)
    {
        cout<<en.getHealth()<<endl;
        cout<<" ";
    }
    else
    {
        cout<<"N/A"<<endl;
        cout<<" ";
    }

    string attackString;
    int attackInput;

    if(enemyEncountered == true)
    {
        cin>>attackString;

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

        cin.ignore();
        cin.get();
    }
    switch(attackInput)
    {
        case 1:

            break;
        case 2:
            pl.setHealth(1);
            break;
        default:
            cout<<"Error, try again."<<endl;

    }
}

void Death()
{

}




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

using namespace std;

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

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

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

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

int Player::setHealth(int val)
{
   if(val == 1)
   {
       playerHealth = playerHealth + 25;
   }
}

int Player::take_Damage(int Damage, int& redirecting)
{
    int x = 6 - Damage;

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




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

using namespace std;

Enemy::Enemy()
{
    enemyHealth = 20;
}

void Enemy::enemyEncountering(int tilesMoved, bool& enemyEncountered)
{
    int encounter;

    srand(time(NULL));

    for(int x = 0; x <= 5; x++)
    {
        if(tilesMoved = 1)
        {
            encounter = ( rand()%5 + rand()%4 ) - rand()%3 - tilesMoved;
        }
        if(encounter == 1 && enemyEncountered == false)
        {
            x = 6;

            system("cls");
            cout<<"                              Enemy encountered!"<<endl;
            enemyEncountered = true;

            cin.ignore();
            if(cin.get())
            {
                system("cls");
            }
        }
        else return;
    }
}

int Enemy::enemyAttack(bool& enemyEncountered, int& redirecting)
{
    if(enemyEncountered == true)
    {
        srand(time(NULL));

        int attackChance = 1;

        for(int y; y <= 5; y++)
        {
            attackChance = rand()%3;
        }

        switch(attackChance)
        {
            case 1:
                redirecting = 9;
                return redirecting;
                break;
        }
   }
}

void Enemy::take_Damage(int Damage, int& redirecting)
{
    int x = 6 - Damage;

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

    }
}

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

void Enemy::enemyDeath()
{

}




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

using namespace std;

Animations::Animations()
{

}

void Animations::hadouken_Animation()
{
    system("cls");
    cout<<"         ##############"<<endl;
    cout<<"       ##           ##"<<endl;
    cout<<"      #   /     #     ##"<<endl;
    cout<<"     # ##    #   ##    ######"<<endl;
    cout<<"     #    ##  ###      #"<<endl;
    cout<<"    # /##    #####      #############"<<endl;
    cout<<"    #  #   ######  ## / #########"<<endl;
    cout<<"     # ##  /   ###     ###"<<endl;
    cout<<"     #      ##  ##     #"<<endl;
    cout<<"      #/   #  #    /  ##"<<endl;
    cout<<"       ##      #    #########"<<endl;
    cout<<"         #############"<<endl;
    Sleep(200);
    system("cls");
    cout<<"       ##############"<<endl;
    cout<<"     ##           ##"<<endl;
    cout<<"    #   /     #     ##"<<endl;
    cout<<"   # ##    #   ##    ######"<<endl;
    cout<<"   #    ##  ###      #"<<endl;
    cout<<"  # /##    #####      #############"<<endl;
    cout<<"  #  #   ######  ## / #########"<<endl;
    cout<<"   # ##  /   ###     ###"<<endl;
    cout<<"   #      ##  ##     #"<<endl;
    cout<<"    #/   #  #    /  ##"<<endl;
    cout<<"     ##      #    #########"<<endl;
    cout<<"       #############"<<endl;
    Sleep(200);
    system("cls");
    cout<<"     ##############"<<endl;
    cout<<"   ##           ##"<<endl;
    cout<<"  #   /     #     ##"<<endl;
    cout<<" # ##    #   ##    ######"<<endl;
    cout<<" #    ##  ###      #"<<endl;
    cout<<"# /##    #####      #############"<<endl;
    cout<<"#  #   ######  ## / #########"<<endl;
    cout<<" # ##  /   ###     ###"<<endl;
    cout<<" #      ##  ##     #"<<endl;
    cout<<"  #/   #  #    /  ##"<<endl;
    cout<<"   ##      #    #########"<<endl;
    cout<<"     #############"<<endl;

    cin.ignore();
    cin.get();
}

void Animations::recover_Animation()
{
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   #######"<<endl;
    cout<<" ###################"<<endl;
    cout<<"#####################"<<endl;
    cout<<"#####################"<<endl;
    cout<<" ###################"<<endl;
    cout<<"   ###############"<<endl;
    cout<<"     ###########"<<endl;
    cout<<"       #######"<<endl;
    cout<<"         ###"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ##/"<<endl;
    cout<<"  #######   ###/###"<<endl;
    cout<<" #############/#####"<<endl;
    cout<<"#############/#######"<<endl;
    cout<<"############/########"<<endl;
    cout<<" ##########/########"<<endl;
    cout<<"   #######/#######"<<endl;
    cout<<"     ####/######"<<endl;
    cout<<"       #/#####"<<endl;
    cout<<"         ###"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   ####/##"<<endl;
    cout<<" ##############/####"<<endl;
    cout<<"##############/######"<<endl;
    cout<<"#############/#######"<<endl;
    cout<<" ###########/#######"<<endl;
    cout<<"   ########/######"<<endl;
    cout<<"     #####/#####"<<endl;
    cout<<"       ##/####"<<endl;
    cout<<"         ###"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   #####/#"<<endl;
    cout<<" ###############/###"<<endl;
    cout<<"###############/#####"<<endl;
    cout<<"##############/######"<<endl;
    cout<<" ############/######"<<endl;
    cout<<"   #########/#####"<<endl;
    cout<<"     ######/####"<<endl;
    cout<<"       ###/###"<<endl;
    cout<<"         /##"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   ######/"<<endl;
    cout<<" ################/##"<<endl;
    cout<<"################/####"<<endl;
    cout<<"###############/#####"<<endl;
    cout<<" #############/#####"<<endl;
    cout<<"   ##########/####"<<endl;
    cout<<"     #######/###"<<endl;
    cout<<"       ####/##"<<endl;
    cout<<"         #/#"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   #######"<<endl;
    cout<<" #################/#"<<endl;
    cout<<"#################/###"<<endl;
    cout<<"################/####"<<endl;
    cout<<" ##############/####"<<endl;
    cout<<"   ###########/###"<<endl;
    cout<<"     ########/##"<<endl;
    cout<<"       #####/#"<<endl;
    cout<<"         ##/"<<endl;
    Sleep(125);
    system("cls");
    cout<<"    ###       ###"<<endl;
    cout<<"  #######   #######"<<endl;
    cout<<" ###################"<<endl;
    cout<<"#####################"<<endl;
    cout<<"#####################"<<endl;
    cout<<" ###################"<<endl;
    cout<<"   ###############"<<endl;
    cout<<"     ###########"<<endl;
    cout<<"       #######"<<endl;
    cout<<"         ###"<<endl;

    cin.ignore();
    cin.get();
}




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

using namespace std;

class Player
{
    public:
        Player();
        int getTile();
        void tileIncrease();
        int getHealth();
        int setHealth(int);
        int take_Damage(int, int&);
        void enemyHealthLeft();
    private:
        int movementInput;
        int attackInput;
        string attackString;
        int playerHealth;
        int Tile = 1;
};

#endif // PLAYER_H




Enemy.h
#ifndef ENEMY_H
#define ENEMY_H


class Enemy
{
    public:
        Enemy();
        void enemyEncountering(int, bool&);
        int enemyAttack(bool&, int&);
        void take_Damage(int, int&);
        int getHealth();
        void enemyDeath();
    private:
        int enemyHealth;
};

#endif // ENEMY_H




Animations.h
#ifndef ANIMATIONS_H
#define ANIMATIONS_H


class Animations
{
    public:
        Animations();
        void hadouken_Animation();
        void recover_Animation();
    private:
};

#endif // ANIMATIONS_H




Is This A Good Question/Topic? 0
  • +

Replies To: Fixed up my code a little bit.

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,755
  • Joined: 08-August 08

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 04:51 PM

Spaghetti code starts as repetitive code. Try something like this.
void Animations::recover_Animation()
{
	for(int i = 0; i < 7; i++)
	{
		system("cls");
		heart();
		Sleep(125);
	}
}

void Animations::heart()
{
		cout<<"    ###       ###"<<endl;
		cout<<"  #######   #######"<<endl;
		cout<<" ###################"<<endl;
		cout<<"#####################"<<endl;
		cout<<"#####################"<<endl;
		cout<<" ###################"<<endl;
		cout<<"   ###############"<<endl;
		cout<<"     ###########"<<endl;
		cout<<"       #######"<<endl;
		cout<<"         ###"<<endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 Xrey274   User is offline

  • D.I.C Head

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

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 05:23 PM

You do realise this does not do the same thing as my code? Mine is an animation. Yours with system("cls"), after each loop sycle, would create a still image.

For the animations, atleast, there isn't anything that can be done, or atlest that i can think of, that would remove the need for repetitons with loops.
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 06:19 PM

Hi, the program is coming along.

In enemyAttack :-

The srand function usually only needs to be called once in a program, in main would be fine.

Here you call rand six times and set attackChance to the last value. You only need to call rand once, perhaps it wasn't working properly because srand wasn't being used correctly.

        int attackChance = 1;

        for(int y; y <= 5; y++)
        {
            attackChance = rand()%3;
        }




Here 9 is a magic number and a value is being returned only some of the times. Would a bool be better?

        switch(attackChance)
        {
            case 1:
                redirecting = 9;
                return redirecting;
                break;
        }


Was This Post Helpful? 0
  • +
  • -

#5 Xrey274   User is offline

  • D.I.C Head

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

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 06:35 PM

#define that redirecting stuff is just something I forgot to clean up when I was reorganizing the code. Thanks for pointint it out, as I shall now remove it. Also about the rand thing yes you are totaly right. Probably what I wanted to do back then is simulate is a couple of cycles of the rand() function to make it more "random". Gonna fix that aswell.

Ahh yes now I remember why I made it cycle 5 times before stoping. The idea was to give it a higher chance to land on 1. Since the chance of getting a 1 with 5 cycles is greater, than with 1. So it was supposed to do at least 1 cycle and if it landed on 1 on any of the cycles(1-5) it would stop the loop and continue. Not sure if it actually works this way, probably not.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,755
  • Joined: 08-August 08

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 07:21 PM

View PostXrey274, on 26 January 2018 - 07:23 PM, said:

You do realise this does not do the same thing as my code? Mine is an animation. Yours with system("cls"), after each loop sycle, would create a still image.

Quote

Try something like this.

Like is not the same as try exactly this. You'll need to modify the function to use a parameter to place the slash. There are a number of ways you could do this. An easy way might be to use an array of strings and print the appropriate substrings with dashes placed where desired.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,755
  • Joined: 08-August 08

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 07:47 PM

Here's another example (it still doesn't do exactly what you want!) that shows how you might animate without using repetitive code.
#include <iostream>
#include <string>

using namespace std;

class Animations {
	string aHeart[10] = {
		"    ###       ###",
		"  #######   #######",
		" ###################",
		"#####################",
		"#####################",
		" ###################",
		"   ###############",
		"     ###########",
		"       #######",
		"         ###",
	};

public:
	void recover_Animation();
	void heart(int p);
};

void Animations::recover_Animation()
{
	for(int i = 0; i < 7; i++)
	{
		heart(i+16);
		cout << endl;
	}
}

void Animations::heart(int p)
{
	string temp;
	for(int row = 0; row < 10; row++) {
		temp = aHeart[row];
		if(p >= 0 && p < temp.length() && temp.substr(p,1)=="#") temp[p] = '/';
		cout << temp << endl;
		p--;
	}
}

int main()
{
	Animations test;
	test.recover_Animation();
}


Was This Post Helpful? 1
  • +
  • -

#8 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Fixed up my code a little bit.

Posted 26 January 2018 - 08:22 PM

Xrey274

We try not to give out exact solutions but rather similar solutions,

Its therefore up to you to alter the idea to suit your needs.

The above solution is a good solution, try it out !

And think...

This post has been edited by snoopy11: 26 January 2018 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#9 Xrey274   User is offline

  • D.I.C Head

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

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 06:59 AM

Wow, it worked really well, genuinly suprised. :thumbsup: Also yes, I get why you wouldn't want to give me the complete code right away, sorry for my ignorance. The problem is that I am reading a c++ book and I haven't gotten yet to STL so I didn't know these functions, more specifically substr(). Also my intelligance is no where near yours.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 07:34 AM

Intelligence is a product of education.

Educate yourself.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 08:15 AM

Quote

The problem is that I am reading a c++ book and I haven't gotten yet to STL so I didn't know these functions,

Then perhaps you need to find a book that is actually teaching C++, not C with classes.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 Xrey274   User is offline

  • D.I.C Head

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

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 12:21 PM

jimblumberg the book I am reading was recommended by you guys, on this forum. I am reading Sam's Teach Yourself C++ 1 hour a day, the 8th edition.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 12:31 PM

Quote

I am reading Sam's Teach Yourself C++ 1 hour a day, the 8th edition.

And who recommended that book, I surely didn't.

Okay so I see this as a recommendation in the book list. However you probably didn't read more than the first post, by the third post there were recommendations to avoid that book. And do note the dates of those posts, that "review" is really out of date.


See this link for a more information.


Jim

This post has been edited by jimblumberg: 27 January 2018 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 12:33 PM

me neither must be a really old post... like SAMS is before I joined dreamincode...in 2010...;O
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430   User is online

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

Reputation: 869
  • View blog
  • Posts: 3,493
  • Joined: 13-June 14

Re: Fixed up my code a little bit.

Posted 27 January 2018 - 12:54 PM

FWIW, the 8th edition seems to be recent, since it covers C++14 and C++17.

In any case, Xrey274, I'm glad you finally got a book on the language. I had a quick skim through the code and there are a few things I noticed:

1. Why does Player have an enemyHealthLeft method (line 16 in Player.h)? That class is supposed to represent a player and you have another for the enemy, so its health should live there.

2. Player's setHealth is confusing. A setter usually sets the relevant field to the value passed to it (so that on reading line 242 of main.cpp, one would think that the player's health would be set to 1. That isn't what your method does, though and the name of the parameter to that method. val isn't particularly meaningful since it doesn't illustrate what it's used for.

3. Both the Player and Enemy classes have some behaviour in common (e.g. being able to take damage and being able to ask for their health). It would probably make sense to introduce a common base class for these things.

Some style things:

1. Why are you mixing camel case (e.g. getHealth) and snake case (e.g. take_Damage) for method and variable names? It's better to stick to one convention.

2. In your classes, you don't need to prefix your members and methods with the name of the class (e.g. playerHealth or enemyAttack). The fact that those things are declared in the class tells you they belong to it.
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »