Okay, I have many questions, mainly about subjects that haven't been discussed well in my school class, although I also have a few questions pertaining to a RPG I've been working on.

1. Threads. I've tried to search for a good tutorial on their basics, but I can't seem to find anything, can anyone explain them more fully to me. The main reason I want to use them is for monsters in my RPG, currently I am having to constantly add to a timer while doing other things to make sure the monsters do not pause while the player swings a sword. I just want to be able to assign each monster their own thread, allowing them to move independently of the rest of the game.

2. I'm having trouble making this RPG very flexible, one reason I know is because I used global variables rather than passing by reference, because of that I will most likely have to remake the majority of the program, but my main problem is, when the player swings their weapon, the 3 coordinates on the 2D array of the 3 spaces in front of the character are checked for enemies, this is where I run into some problems, as I have to brute-force each monster individually in order to check them. This makes it impossible for me to have a variable amount of monsters, as adding more requires me to brute-force even more into the program, which I want to avoid.
I've thought about using classes, and giving each monster their own class, but this is a bit of a waste of memory, and I was wondering if there was any other way I could do this.

3. Probably the main reason I want to avoid using classes is because I'm still a little shaky with them, if anyone can provide a good basic tutorial of classes and when to use them, it would be greatly appreciated.

4. Global Variables, I want to know when to use them, vs. when to pass variables around, I assume there are some basic guidelines on when to use either.

Thanks ahead of time for any answers, if you want to see the RPG so far, here's the sourcecode:
RPG

Space to Talk, ASWD to move, space to swing sword.

Attachments
#include <fstream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

double level = 1.0; //Current level, may not nesessarily be loaded when you change level, be sure to run the changelevel function
int levelcolor = 11; //Color of the level(mostly terrain)

double gamespeed; //Changes the speed that the game moves, except the hero.

int mx=5; //Coordinates of the player, the coordinates go xy for the gotoxy function, and 
int my=4; //they go yx when referring to position on the matrix.

int i, j; //Variables used when printing the levels.

char direction = '!';   //Variable that holds the key hit by the player when moving around normally
char facing = 'S';		//Variable that holds which direction the player is currently facing

char levelname[20];
char matrix[20][59];	//The 2D array that holds the level's terrain and position of player and monsters.

char choice = '!';		//Variable used when speaking with people to hold what key they hit.

int counter = 0; //Used to make monsters move while you are using sword

int peoplecolor = 14;

struct playerstats
{
	char playername[11];

	double swordspeed;
	double health;
	double totalhealth;
	double swordpower;
	double mana;
	double maxmana;

	char weaponj[15];
	char weaponu[15];
	char weapono[15];
	char weaponl[15];

	int swordcolor;
	int playercolor;
	int money;
	int maxmoney;

	int mx;
	int my;

	int lookdown;
	int lookup;
	int lookright;
	int lookleft;
};


struct monstats //The stats of a monster, contains all information on the monster
{
	int x; //X position of the monster on the screen, Y position on the Matrix
	int y; //Y position of the monster on the screen, X position on the Matrix
	int radius; //The radius around the monster that you want him to react to the main character(Not all AIs use this)

	double health; //The health remaining for the monster
	double armor; //Armor of the monster
	double maxhealth; //The maximum amount of health the monster can have

	int color; //The color of the monster
	char look; //The symbol used for the monster

	int move; //Direction randomized for the monster to move in
	int lastmove; //The last direction moved by the monster
	double speed;
	double speedcounter;

	int active; //Whether or not the current monster is active(0 or 1, 0 for nonmoving, 1 for moving)
	int ai; //The current AI level of the monster, see AI functions for details on AIs
	int skip; //Is used in AI to stop other loops from running, ignore it.
};

struct monster //The monsters themselves, not completely nessesary, but it creates 6 monsters with their own stats.
{
	monstats one;
	monstats two;
	monstats three;
	monstats four;
	monstats five;
	monstats six;
};

monster mon; //Six monsters that can be activated or deactivated in the game
playerstats stats;

void setcolor(int color) //Allows the changing of color for a single spot
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE );
 SetConsoleTextAttribute(hOut, color);
}

void gotoxy(short x, short y) //Moves cursor to an XY position on the console
{
      HANDLE hConsoleOutput;
      COORD Cursor_an_Pos = {x,y};
      hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
      SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}

void updatemapstats()
{
	int xheart = 59;
	int yheart = 2;
	double health = floor(stats.health); //Returns number of full hearts

	setcolor(15);
	gotoxy(59, 1);
	printf("Health: %.2f/%.2f", stats.health, stats.totalhealth);

	setcolor(12);
	while(health > 0)
	{
		gotoxy(xheart, yheart);   //Prints out full hearts
		putchar(3);
		xheart++;
		health--;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}

	health = fmod((stats.health * 4), 4); //Returns 0-4, depending on how much of heart is left

	gotoxy(59, 15);
	if(health > 0 && health <= 1)
	{   //If 1/4th of heart
		gotoxy(xheart, yheart);
		setcolor(4);
		putchar(3);
		xheart++;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}
	
	if(health > 1 && health <= 2)
	{
		gotoxy(xheart, yheart);
		setcolor(5);
		putchar(3);
		xheart++;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}
	
	if(health > 2 && health <= 3)
	{
		gotoxy(xheart, yheart);
		setcolor(13);
		putchar(3);
		xheart++;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}
	if(health > 3)
	{
		gotoxy(xheart, yheart);
		setcolor(12);
		putchar(3);
		xheart++;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}

	health = floor(stats.totalhealth - stats.health);

	while(health > 0)
	{
		gotoxy(xheart, yheart);
		setcolor(1);
		putchar(3);
		health--;
		xheart++;
		if(xheart == 79 && yheart != 3)
		{
			yheart++;
			xheart = 59;
		}
	}

	double magic = floor(stats.mana); //Returns number of full bars

	yheart += 2;
	xheart = 59;

	setcolor(15);
	gotoxy(xheart, yheart);
	printf("Enerza: %.2f/%.2f", stats.mana, stats.maxmana);
	yheart++;
	xheart = 59;
	setcolor(14);


	while(magic > 0)
	{
		gotoxy(xheart, yheart);   //Prints out full bars
		putchar(219);
		xheart++;
		magic--;
		if(xheart == 79)
		{
			yheart++;
			xheart = 59;
		}
	}

	magic = fmod((stats.mana * 2), 2); //Returns 0-2, depending on how much of bar is left

	gotoxy(59, 15);
	if(magic > 0 && magic <= 1)
	{   //If 1/4th of heart
		gotoxy(xheart, yheart);
		setcolor(30);
		putchar(221);
		xheart++;
		if(xheart == 79)
		{
			yheart++;
			xheart = 59;
		}
	}
	
	if(magic > 1 && magic <= 2)
	{
		gotoxy(xheart, yheart);
		setcolor(14);
		putchar(219);
		xheart++;
		if(xheart == 79)
		{
			yheart++;
			xheart = 59;
		}
	}

	magic = floor(stats.maxmana - stats.mana);

	while(magic >= .5)
	{
		gotoxy(xheart, yheart);
		setcolor(1);
		putchar(219);
		magic--;
		xheart++;
		if(xheart == 79)
		{
			yheart++;
			xheart = 59;
		}
	}
	if(magic > 0)
	{
		gotoxy(xheart, yheart);
		setcolor(1);
		putchar(221);
		magic = 0;
		xheart++;
		if(xheart == 79)
		{
			yheart++;
			xheart = 59;
		}
	}
	setcolor(15);
	gotoxy(59, 7);
	printf("%s", stats.weaponu);
	gotoxy(70, 7);
	printf("%s", stats.weapono);
	gotoxy(59, 9);
	printf("%s", stats.weaponj);
	gotoxy(70, 9);
	printf("%s", stats.weaponl);


	gotoxy(59, 16);
	setcolor(15);
	printf("Dunars: ");
	setcolor(14);
	printf("%.0i", stats.money);
	gotoxy(70, 16);
	setcolor(12);
	putchar(232);

	gotoxy(61, 18);
	setcolor(15);
	printf("%s", levelname);

	gotoxy(58, 0);
}

void updatebattlestats(monstats curmon)
{
}

void loadlevel() //Loads a map into the matrix, the map is not outputted to the screen, it is simply loaded, use printgrid to output to screen.
{     
	if(level == 1.0)
	{
		strcpy(matrix[0]," _________________________________________________________ ");
		strcpy(matrix[1],"|                                                         |");
		strcpy(matrix[2],"|                                                         |");
		strcpy(matrix[3],"|                                                         |");
		strcpy(matrix[4],"|                                                         |");
		strcpy(matrix[5],"|                                                         |");
		strcpy(matrix[6],"|                                                         |");
		strcpy(matrix[7],"|                                                         |");
		strcpy(matrix[8],"|                                                         |");
		strcpy(matrix[9],"|                                                         |");
	   strcpy(matrix[10],"|                                                         |");	//Tutorial level where you begin, here the player is taught
	   strcpy(matrix[11],"|  -----   ----                                           |");	//how to do the basics of the game.
	   strcpy(matrix[12],"| |            |                                          |");
	   strcpy(matrix[13],"| |            |                                          |");
	   strcpy(matrix[14],"| |            |                                          |");
	   strcpy(matrix[15],"| |            |                                          |");
	   strcpy(matrix[16],"|  ------------                                           |");
	   strcpy(matrix[17],"|                                                         |");
	   strcpy(matrix[18],"|                                                         |");
	   strcpy(matrix[19],"|_________________________________________________________|");
	   strcpy(levelname, "Training Quarters");
	}
	if(level == 50)
	{
		strcpy(matrix[0]," _________________________________________________________ ");
		strcpy(matrix[1],"|                                                         |");
		strcpy(matrix[2],"|                                                         |");
		strcpy(matrix[3],"|                                                         |");
		strcpy(matrix[4],"|                                                         |");
		strcpy(matrix[5],"|        __________________________________________       |");
		strcpy(matrix[6],"|       |                                          |      |");
		strcpy(matrix[7],"|       |                                          |      |");
		strcpy(matrix[8],"|       |                                          |      |");
		strcpy(matrix[9],"|       |                                          |      |"); //Battle Arena for fighting monsters
	   strcpy(matrix[10],"|       |                                          |      |");
	   strcpy(matrix[11],"|       |                                          |      |");
	   strcpy(matrix[12],"|       |                                          |      |");
	   strcpy(matrix[13],"|       |                                          |      |");
	   strcpy(matrix[14],"|       |__________________________________________|      |");
	   strcpy(matrix[15],"|                                                         |");
	   strcpy(matrix

You're coding in C++. Forget about global variables, try and make a class that has all those variables. If you can't fit it in a class or function you're quite possibly doing something wrong. Globals are considered dangerous when you tend to use them as much as you do. It also cleans up your code a lot, 2000 lines is a lot, especially when I think you could make it a lot more readable by splitting all the code up in classes. Having the functions that need the member variables of a class in that same class or friends of that class also eliminates the need to pass every variable and allows you to simply make private "safe" data members.

2, 3, 4 should be solved by learning and using classes. 1 is very advanced and will make your eyes bleed and your program deadlock. ;-) Assigning each monster its own thread would do you little good since you'd have to lock every variable/array it'll access and unlock it... It will probably slow down the program quite a lot.

Well, really try to encapsulate most of the functions and variables into classes. That should be your focus. I like the idea and your work on the RPG though! Very cool. :D


The monster hit algorithm could be something like this right:?

For each monster
   for each hit coordinate
       check if the monster.location == hitcoordinate[n]
       if so do damagerollorsomething

You're coding in C++. Forget about global variables, try and make a class that has all those variables. If you can't fit it in a class or function you're quite possibly doing something wrong. Globals are considered dangerous when you tend to use them as much as you do. It also cleans up your code a lot, 2000 lines is a lot, especially when I think you could make it a lot more readable by splitting all the code up in classes. Having the functions that need the member variables of a class in that same class or friends of that class also eliminates the need to pass every variable and allows you to simply make private "safe" data members.

2, 3, 4 should be solved by learning and using classes. 1 is very advanced and will make your eyes bleed and your program deadlock. ;-) Assigning each monster its own thread would do you little good since you'd have to lock every variable/array it'll access and unlock it... It will probably slow down the program quite a lot.

Well, really try to encapsulate most of the functions and variables into classes. That should be your focus. I like the idea and your work on the RPG though! Very cool. :D


The monster hit algorithm could be something like this right:?

For each monster
   for each hit coordinate
       check if the monster.location == hitcoordinate[n]
       if so do damagerollorsomething

Okay, so I guess it all comes down to classes, and I guess I'm fine not using threads, as you can see, the program functions perfectly well right now, I just thought threads might make it little less annoying.

I think that what I could do is assign each monster a class, and that should allow me to be at least slightly more variable. I began to try and get rid of my global variables by passing them all by reference and such, but after about 15 minutes, I realized that my program was way too messy and not flexible enough, so I'll probably have to go and redo a lot of it.

As for hitting the monsters, the biggest problem is that the look of the monsters can vary, so I can't have a fixed look, meaning I have to check to see if the coordinates of the monster match with the coordinates of the sword swing, so I have to brute-force it, although using classes should reduce that at least a bit more, not to mention when you hit the monsters, they are knocked back and damage is done based on which monster you hit, so I need to know exactly which one it was.

You can have a monster class with a function isHit(const unsigned int x, const unsigned int y); that returns whether the monster is hit. This makes the hit function belonging to the player really short, since it only needs to feed 3 coordinates, the monster does the rest. Like this, you can hide big pieces of code, make sure they work and never look at them again until you want to change them.

Best of luck with it, I might also take a look at it myself if you like? I'm kinda out of projects now, and this seems a nice project with a dedicated developer. Besides, I have some OpenGL skills if you'd like it to be more visual...

Let me know,

You can have a monster class with a function isHit(const unsigned int x, const unsigned int y); that returns whether the monster is hit. This makes the hit function belonging to the player really short, since it only needs to feed 3 coordinates, the monster does the rest. Like this, you can hide big pieces of code, make sure they work and never look at them again until you want to change them.

Best of luck with it, I might also take a look at it myself if you like? I'm kinda out of projects now, and this seems a nice project with a dedicated developer. Besides, I have some OpenGL skills if you'd like it to be more visual...

Let me know,

Wait, I still don't think I see what you mean, it sounds to me like you plan on passing in the coordinates of the sword blade, and then using a function in each monsters class to check if their coordinates match, is this what you're talking about, or am I off?

I kinda want to work on this alone, as this is my first large-scale project, I'm pretty much just doing it for practice, thanks for the offer, and the help so far.

Also, I can't seem to remember off the top of my head the command to create a variable type, I want to have a variable with two possible values, 1 or 0, basically, a binary variable, it's not typecast...

It's called a

bool

in C++ ;)

You can create types with typedef.

typedef <newtype> <oldtype>

IIRC

This question has already been answered. Start a new discussion instead.