Archived

This topic is now archived and is closed to further replies.

MTT

Problem with very simple battle engine

Recommended Posts

MTT    265
Ok, this isnt the most beautifull code but i am having a problem with it. It still has alot of work to do on it but i dont want to code anymore untill i get this sorted out. I am pretty sure that the problem is in the battle function, but it might be in the drawscreen funtion because i call that from the battle function. Now at the botoom of the screen i get 0 errors, 0 warnings. Run the program, it sais dungeon battle has performed an illegal opetration. I am hopeing you can tell me what that illegal operation is. Enuf talking, here is the code.
#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;

#define GRID_WIDTH1 77
#define GRID_HIGHT1 23

void LoadMap(CHAR_INFO ScreenBuffer[], COORD BuffSize, int PicNum);
void DrawScreen(CHAR_INFO ScreenBuffer[], int PlayerX, int PlayerY);
void Battle(CHAR_INFO ScreenBuffer[]);

int PVit = 5;
int PStr = 5;
int PLif = PVit * 5;
int PDmg = PStr * 2;

int MVit = 5;
int MStr = 2;
int MLif = MVit * 5;
int MDmg = MStr * 2;


//the main function

void main()
{
	int PicNum = 2;
	
	CHAR_INFO ScreenBuffer[GRID_WIDTH1 * GRID_HIGHT1];
	COORD BuffSize = {GRID_WIDTH1, GRID_HIGHT1};

	LoadMap(ScreenBuffer, BuffSize, PicNum);
	
	Battle(ScreenBuffer);

	return;
}

void LoadMap(CHAR_INFO ScreenBuffer[], COORD BuffSize, int PicNum)
{	
	ifstream fin;			//creates a file input thing

	
	//checks which map to load, error checks, then loads the map

	switch(PicNum)
	{
		case 1:
		fin.open("demon.txt");
		if (fin.fail())
		{	return;	}
		break;

		case 2:
		fin.open("skeleton.txt");
		if (fin.fail())
		{	return;	}
		break;
	}
	
	//Reads in file into buffer charecter by charecter, it first goes

	//along the first horizontal row, once it finishes it increments the

	//verticle row by 1 and reads it horizontaly again until the map is

	//completely read into the buffer

	char temp;
	for (int y = 0; y < BuffSize.Y; y++)
	{	
		for (int x = 0; x < BuffSize.X; x++)
		{
			fin >> temp;
			
			switch(temp)
			{
				case ''0'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = '' '';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''1'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''X'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
				break;

				case ''2'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''|'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''3'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''/'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''4'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''_'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''5'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = 92;
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''6'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''<'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''7'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''>'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''A'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''A'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''C'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''C'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''E'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''E'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;
				
				case ''G'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''G'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''I'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''I'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''K'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''K'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''M'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''M'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''N'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''N'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''R'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''R'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''T'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''T'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''U'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''U'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				//this is incase none of those charecters are found

				//it creates an error and automaticly exits

				default:
				fin.close();
				cout << "error2"; return;
				break;
			
			}//end switch

		}//end x for loop

	}//end y for loop


	fin.close();			//close the file because we are done with it

	return;					//exits this function


}//end LoadMap



void DrawScreen(CHAR_INFO ScreenBuffer[], int PlayerX, int PlayerY)
{
	
	//tells the computer the 4 corners of the area we a drawing to,

	//a coord storing the size of the buffer, the corner we are drawing

	//from (top left, and a handle for acess to the screen

	SMALL_RECT DrawRect = {1, 1, 1 + (GRID_WIDTH1 - 1), 1 + (GRID_HIGHT1 - 1)};
	COORD BuffSize = {GRID_WIDTH1, GRID_HIGHT1};
	COORD ZeroZero = {0, 0};
	HANDLE hOutput;			

	//This function gets a standard hanlde for hOutput and allows

	//acess to our screen buffer

	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	ScreenBuffer[PlayerX + PlayerY * GRID_WIDTH1].Char.AsciiChar = ''>'';
	ScreenBuffer[PlayerX + PlayerY * GRID_WIDTH1].Attributes = FOREGROUND_BLUE;

	//draws out the contents of the screen

	WriteConsoleOutput(hOutput, ScreenBuffer, BuffSize, ZeroZero, &DrawRect);

	return;					//exits function


}//End drawscreen




void Battle(CHAR_INFO ScreenBuffer[])
{
	//initializes a structure that stores what has been input, the position

	//of  our charecter (this is used later in colision detection),

	//somthing that thells the computer the number of events that were

	//input, and a handle for acess to the input

	COORD CursorPos = {19,55};
	INPUT_RECORD InputRecord;
	DWORD Events = 0;
	HANDLE hInput;
	
	while(PLif > 0 && MLif > 0)
	{
		DrawScreen(ScreenBuffer, CursorPos.X, CursorPos.Y);
		
		bool bPlayerChose = false;
		
		//lets the computer know if the player was moved and if a key

		//was pressed

		bool bPlayerMoved = false;
		int bKeyDown = 0;

		//gets a standard input handle then processes any commands

		hInput = GetStdHandle(STD_INPUT_HANDLE);
		ReadConsoleInput(hInput, &InputRecord, 1, &Events);
	
		//This is for NT based users, on windows 95, 98, ect. it only reads

		//when the key is pressed it only reads it when the button goes from

		//off to on but in NT it reads it when it goes off to on also so this

		//is so they too only move 1 space per press

		bKeyDown = InputRecord.Event.KeyEvent.bKeyDown;	

	
		//this entire thing processes what key is pressed, makes sure the

		//player hasnt gone over the edge of the grid, moves the player,

		//then lets the computer know the player has moved.

		if(InputRecord.EventType == KEY_EVENT && bKeyDown)
		{
		
			if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
			{
				if(CursorPos.X != 66)
				{
					CursorPos.X = 66;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
			{
				if(CursorPos.X != 55)
				{
					CursorPos.X = 55;
					bPlayerMoved = true;
				}
			}
	
			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
			{
				if(CursorPos.Y != 21)
				{
					CursorPos.Y = 21;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
			{
				if(CursorPos.Y != 19)
				{
					CursorPos.Y = 19;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RETURN)
			{
				bPlayerChose = true;
			}
			
			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
			{
				exit(0);
			}
		
		}	
		
		if(bPlayerMoved = true)
		{
			DrawScreen(ScreenBuffer, CursorPos.X, CursorPos.Y);
		}

		if(bPlayerChose = true)
		{
			MLif -= PDmg;
			PLif -= MDmg;
		}
	}
	
	return;
}


and here is a map file just incase you are wondering

   
11111111111111111111111111111111111111111111111111111111111111111111111111111
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
11111111111111111111111111111111111111111111111111111111111111111111111111111
10000000000000000000000000000000000000000000000000001000000000000000000000001
1000000000000000000000000000000000000000000000000000100ATTACK00000MAGIC000001
10000000000000000000000000000000000000000000000000001000000000000000000000001
1000000000000000000000000000000000000000000000000000100ITEM0000000RUN00000001
10000000000000000000000000000000000000000000000000001000000000000000000000001
11111111111111111111111111111111111111111111111111111111111111111111111111111
__________________________________________________________________________________________ "yuo have it all wrong. admiralbiunary is me not the other way reorubnd poppet. just becuase hea was here first doesnt meean hes any mopre valid than me yuo rassit pigf." YodaTheCoda

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
How about using a step through debugger and telling us WHERE the error actually occurs so we don''t have to sort through your whole program.

Share this post


Link to post
Share on other sites