Hey I'm new to programming but I know enough now that I have been trying to program my own game. I works, but I think my code is too complicated. Does someone want to test it and give me some suggestions? Thanks!

Recommended Answers

All 13 Replies

I'll check it out, and see what I can come up with to help you out. I'll try debugging it, as well. Please note that my knowledge in C++ is limited, but I'm starting college on March 3rd for video game programming. I'll let you know if I come up with anything.

Here's something that I already found after reading just a few lines.

line #254

player.health = player.str * 10;

You already have that declared on line 158, so you're not going to have that declared again.

lines #247 - #267

if(playerexp >= 50)
{
	  player.lvl++;
	  player.str++;
	  player.acc = player.acc + 0;
	  player.intell++;
	  player.adrenaline = player.intell * 10;
	  player.health = player.str * 10;
	  cout << "Congratulations, you gained a level! You have gained\n";
	  cout << "1 Strength\n";
	  cout << "0 Accuracy\n";
	  cout << "1 Intelligence\n" << endl;
	  cout << "Your new stats are: " << endl;
	  cout << "Level: " << player.lvl << endl;
	  cout << "Strength: " << player.str << endl;
	  cout << "Accuracy: " << player.acc << endl;
	  cout << "Intelligence: " << player.intell << endl;
	  cout << "Health: " << player.health << endl;
	  cout << "Adrenaline: " << player.adrenaline << endl << endl;
	  system("PAUSE");
}

I'd just have that be in a module that you'd call at the beginning of the file. It could be in something like functions.cpp. Once you have the module created, you'd replace that code with the following

if neededexp = 0
{
     levelup();
}
else
{
     neededexp = neededexp - expgained;
}

The repeated battle animations can be cut from this file, and put into a function that would be in functions.cpp

lines #319 - #795

Cut them out of this file entirely, and make them a part of functions.cpp and include them at the beginning of this file. It'll save you a lot of time that way.

With your comments, just have them be like this:

// whatever

and the at the end of the code that is commented

// end whatever

This will make it so that the computer has to read less code, which will optimize loading times.

That's just what I've found just by skimming over your file quickly. #include <functions.cpp>;
should make it so that your load times will be smaller, and it'll also make it so that you don't have to keep repeating the same code over and over.

I hope that I helped you out.

Good luck, man (^_^)

Ya, I modularized a bunch of the code today, including the leveling part you were explaining. I'll go through the battle function and look for repeated/unnecessary code. I didn't know you could include your own functions written in other files, so I'll expirement with that too.

Thanks for the tips!

Hey, no problem. I'm just glad that I was able to help.

I don't know if you're old enough to go to college (or even in USA), but if you are, you might want to think about going to DeVry for their online game and simulation programming courses. I'm doing that starting in March.

Thanks for letting me help you out.

Ok, I tried putting the battle function in a new file and #including it into the main file, but it tells me that there is no such file or directory when I try to compile. The name of the function file is bfunction.cpp and in the main file I am including it like so: #include <bfunction.cpp>
What am I doing wrong?

You should change the "< >" to quotes ( " " ): #include "bfunction.cpp" This means that the file is in de project-folder


If you want to use multiple files I would recommend that you use .h files. For example:

You have a function void foo(int number) { } You put de declaration in the header file (foo.h): int foo (int); Next you put the implementation in the foo.cpp file:

int foo (int number)
{
     return number*2;
}

Now you put #include "foo.h" in your main.cpp and your done!

I'm assuming that this is going to be on a web page.

I know that in PHP you can do things like this

<?php
$phpbb_root_path = './../';
include($phpbb_root_path . 'includes/functions.php');
?>

Or you can also do it like phpBB does it:

<?php
$phpbb_root_path = './../';
include($phpbb_root_path . 'includes/functions.' .$phpEx);
?>

You will have to declare the root directory before you're able to do any including in PHP.

Since PHP is based off of C++, you might be able to do it something like this:

#include "functions/bfunction.cpp";

Like I said, I have limited knowledge of C++ right now, so I'm not guarenteeing that anything will work exactly how you want it to, or at all for that matter.

@Suetan, since the original poster's code is C++, I sincerly doubt that the means for performing a PHP include will be much help... PHP isn't a subset/superset of C++ : PHP is parsed by and interpretted by a C++ runtime engine; if that's what you mean by 'based off of C++'.

@kidprogrammer, it looks good to me. Perhaps some of the stuff in main could be moved out into other functions; but only if it looks like you'll gain any benefit, and not have many costs ( i.e. having to pass more than about 8 parameters to a 10 line function, or a function that's only called from one place is too many costs ).

Also, a minor; you use the windows.h header only for the 'Sleep' function.. That means your program will only compile on Windows machines, even though most of the code is platform-neutral. If you're only targeting Windows, that's fine. There are other ways to do a sleep( n ) though [ i.e, you should be able to use a call to system( "sleep n" ) and thus cut out the dependancy on windows.h ].

@MattEvans
What I was doing was using my knowledge with PHP to be able to hopefully implement it into C++. You're right about PHP's parsing by and interpretation on a C++ runtime engine.

I was merely trying to show how things are done in PHP and then work that into C++. I showed an example in a language that I know, and tried to work it into C++. That's all I was doing.

I'll keep it in mind not to refrence other languages in the future though.

Hope you'll excuse the thread necromancy, but how did this turn out? kidprogrammer, if you see this, are you interested in posting your updated code for further critique?

Hope you'll excuse the thread necromancy, but how did this turn out? kidprogrammer, if you see this, are you interested in posting your updated code for further critique?

I've made a lot of progress since the last code I uploaded. I completely started over and modularized the battle function, and I have added many new features. Right now I am not able to access the the actual code (on a different computer), so I will post the code tomarrow. Thanks for your interest in the game and wanting to help!

Ok, here are all the files needed to compile and run my game. What I want to do now is be able to pass an entire array or matrix to a function. It would be easy to program an inventory this way. I just can't figure out how to do it. More later, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.