Large code file

I have a large code file (1151 lines) for my little console game. I've tried to do everything in separate files at first but then it got too confusing.

Now before asking for help, I wanted to ask if I should post the whole code or should I not even bother asking for someone to split my code?
It wouldn't fit into one post, I'd have to post like 10 posts to fit the whole code.
Last edited on
I recommend you try splitting the code, and if you run into problems post the relevant code and error messages. Hopefully your code isn't full of global variables and other bad practices.

Last edited on
Hopefully your code isn't full of global variables and other bad practices
It sure is :D
But ok, I'll try my best to do as much as I can.
closed account (3qX21hU5)
Well first what is your question that you have? Is it about where to split the code at? Or something else?

1151 LOC is a pretty large amount of code to post on the forum (You would have to have a large amount of posts probably since of the cap on characters). So most likely people wouldn't bother reading through all of it.

So my advice if your question is about where to split the code into multiple files, we can give you some advice on where to do it and when to do it. But we won't just take your code and do it for you.

But if its about a specific problem you are having just post a detailed report about the problem and some snippets of code you believe it is happening in and we can try and work it out.

Last edited on
It sure is :D

Then maybe you should spend some time trying to eliminate these global variables before you worry about splitting the program up.

Since my code was so large, I decided to write it from scratch with some improvements (that should take a looong time).

Here's the first problem I ran into:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include "globals.hpp"

class Hero : public Entity
{
public:
    Hero(std::string name) : Entity()
    {
	Health = 20;
	MaxHealth = 20;
    }

};

int main()
{
std::string name;

ClearScreen();
std::cout << "Brave adventurer, what is your name?" << "\n";
getline (std::cin, name);
Hero player(name);

std::cout << "\n\n" << "WELCOME, " << player.name << ", to the world of CliX!" << "\n";
std::cout << "You must pursue your quest to find and kill the legendary evil dragon Alkazar." << "\n";
std::cout << "Good luck!";
std::cin.get();

ClearScreen();
std::cout << "For the first map:" << "\n\n";

player.CurrentY = 24;
std::cout << "Current Y position: " << player.CurrentY << "\n";
player.CurrentX = 4;
std::cout << "Current X position: " << player.CurrentX << "\n";

return 0;
}


globals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "globals.hpp"
#include <vector>
#include <windows.h>

Entity player;
std::vector<Entity> enemies;

void ClearScreen()
{
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
}


globals.hpp
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#ifndef GLOBALS_HPP_INCLUDED
#define GLOBALS_HPP_INCLUDED

#include "attributes.hpp"
#include <vector>

extern Entity player;
extern std::vector<Entity> enemies;
void ClearScreen();

#endif // GLOBALS_HPP_INCLUDED 


and finally attributes.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma once
#ifndef ATTRIBUTES_HPP_INCLUDED
#define ATTRIBUTES_HPP_INCLUDED

class Entity
{
public:
    unsigned int Health;
    unsigned int MaxHealth;
    unsigned int initHealth;
	unsigned int Armour;
	unsigned int Strength;
	bool flagDead;
    unsigned int CurrentX;
    unsigned int CurrentY;
    unsigned int DirectionX;
    unsigned int DirectionY;

    void checkDead()
	{
		if(Health <= 0)
			flagDead = true;
	}
	bool isDead(){ return flagDead; }

};

#endif // ATTRIBUTES_HPP_INCLUDED 


This whole code gives the error
main.cpp:92:46: error: 'class Hero' has no member named 'name'


I don't exactly understand why I get this error, because Hero does have the name which is associated with player.
And since I don't get it, I couldn't think of a good example to post instead of the whole code...
No, Hero does not have a member called name.

1
2
3
4
5
6
7
8
class Hero : public Entity
{
public:
    Hero(std::string name) : Entity()
    {  Health = 20;
        MaxHealth = 20;
    }
};

Your constructor takes a string argument named name, but doesn't do anything with it.

I also suggest you try to eliminate the global variables. Learn to properly pass these variables to and from your functions.

Thank you AbstractionAnon, I see what I missed out there. And jlb, you mean I should pass these parameters by reference?
Basically I should make pointers to all parameters, e.g. Health and _Health or something like that?
What's the difference, when you have global variables and global pointers to those variables?
you mean I should pass these parameters by reference?

Yes, sometimes. Sometimes you will pass parameters by value or by pointer it depends on how you defined the functions.

Basically I should make pointers to all parameters

What? Why would you make pointers to all parameters?

What's the difference, when you have global variables and global pointers to those variables?

I really don't understand what you mean here. What I mean is that you shouldn't have any global variables, period. Create your variables inside your functions and pass these variables to and from your functions using parameters.

closed account (3qX21hU5)
It might be worth checking out how functions work again and also more about how classes and concepts like abstraction work. You seem to have some knowledge on them but don't seem to fully grasp how to use them effectively.

So I suggest maybe looking at some tutorials for the following subjects.

1. Functions, mainly about parameters and passing. Basically go over everything about functions again for a quick refresher.

2. Look into classes a bit more. The reason I say this is because you have all your data in a public slot. So that tells me you aren't familiar with interfaces and abstraction and could use some refreshers on how to make effective classes.
Thanks Zereo and jlb,
I'll try to look more into classes, as I always found them really confusing and I'll read the tutorial on functions again.
Actually I only wrote about 400 lines of the code myself and I copied bits from here and there while slightly modifying them (of course, I first find out what it does). When I run into bugs - then I'm in trouble :D. It's just that I read, that writing a game can help you learn programming very actively (and it does! :))

Now since I posted the shorter code above, can someone, please, rewrite a good example of how the same code should be written properly without global variables? Although I changed it slightly to fix the error, I don't know if my solution is right.
I wrote:
1
2
3
4
5
6
7
8
9
10
class Hero : public Entity
{
public:
    Hero(std::string _name) : Entity()
    { 
        name = _name;
        Health = 20;
        MaxHealth = 20;
    }
};

I promise I'll take note of whatever you do there :D
Last edited on
closed account (3qX21hU5)
I remembered this thread from awhile ago that talks about class design. They cover a lot about class design actually and could be helpful for you. Otherwise there is plenty of other resources on class design.

http://www.cplusplus.com/forum/general/86094/
Last edited on
I also found this awesome link: http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798

Still, I want someone to tell me how to re write the code...
closed account (3qX21hU5)
Use the information from the multiple links that were given. We can't tell you how to do something without knowing the full scope of the program. Since we have only seen a very minor bit of your code it would be very hard to re write your code correctly.

So instead of asking for someone else to do it, I suggest you keep on doing some research (Which is what programming is all about) and figure out the best way to restructure it yourself.

You will have to learn to do it soon or later, so why not start now.
Topic archived. No new replies allowed.