Go Back   Flash Flash Revolution > General Discussion > Critical Thinking > Homework & Help

Reply
 
Thread Tools Display Modes
Old 08-31-2015, 02:49 AM   #1
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Alright so let me begin with saying that I've been studying C++ for about the past 6 weeks (only just started my course) and the assignment I've been slapped with is comparable to learning Ancient Latin in French (that's how I see it anyway). I literally don't even know where to start.

Since I'm not allowed to just ask for answers, I'll be asking for the next closest thing that won't get this thread locked. Any help or advice will aid me! And if you can give advice/help, please explain it like you we're talking to a dummy (cause I kinda feel that way atm). I'm also willing to talk on Skype or anything other chatting thingy cause I'm getting desperate at this stage. I spent 4 hours the other night trying to figure out the first question (which is 'What is the data type GUI_BUFFER?') and I'm pretty sure my answer is wrong.

Alright so here's the code that I've been supplied with. I will ask questions at the bottom of this post. Most of them will probably be simple to anyone with reasonable knowledge of C++. I guess I'll just post a few questions now and if people can help, I'll ask more questions that I get stuck on.

Also, sorry about the poor quality of the code layout, it's not as pretty as it looks in Visual Studio.

Code:
/******************************************************************************
* Program Description
* File: tilemap.cpp
* Application demonstrating the use of 2D arrays to represent tile-based game
* environments. Also demonstrates limited use of the Windows Console API.
******************************************************************************
*/


#include "tilemap.h"

// Declare map height and width parameters
const unsigned int MAP_HEIGHT = 20;
const unsigned int MAP_WIDTH = 30;

// Declare map elements using an enumeration
enum entity_labels	{
	EMPTY = 0,
	WALL
};
typedef entity_labels	ENTITY;

// Define an array of ASCII codes to use for visualising the map
const int TOKEN[2] = {
	32,		// EMPTY
	178		// WALL
};

// create type aliases for console and map array buffers
using GUI_BUFFER = CHAR_INFO[MAP_HEIGHT][MAP_WIDTH];
using MAP_BUFFER = ENTITY[MAP_HEIGHT][MAP_WIDTH];


//Declare application subroutines
void InitConsole(unsigned int, unsigned int);
void ClearConsole(HANDLE hStdOut);
WORD GetKey();
void DrawMap(MAP_BUFFER & rMap);



/** Function: main
  * Description:
  *		 Application entry point
  */
int main()
{

	/**************************************************************************
	  * Initialise the standard output console
	  */		
	HANDLE	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut != INVALID_HANDLE_VALUE)
	{
		ClearConsole(hStdOut);

		// Set window title
		SetConsoleTitle(TEXT("Tile Map Demo"));

		// Set window size
		SMALL_RECT srWindowRect;
		srWindowRect.Left = 0;
		srWindowRect.Top = 0;
		srWindowRect.Bottom = srWindowRect.Top + MAP_HEIGHT;
		srWindowRect.Right = srWindowRect.Left + MAP_WIDTH;

		SetConsoleWindowInfo(hStdOut, true, &srWindowRect);

		// Set screen buffer size
		COORD cWindowSize = { MAP_WIDTH, MAP_HEIGHT };
		SetConsoleScreenBufferSize(hStdOut, cWindowSize);
	}
	/*************************************************************************/


	/*************************************************************************
	* Initialise the tile map with appropriate ENTITY values
	*/
	MAP_BUFFER		tileMap;
	
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			tileMap[row][col] = EMPTY;
		}
	}
	/*************************************************************************/



	/***************************************************************************
	* Execute the main application loop
	*/
	bool bExit = false;

	do
	{
		// Get user input (non-blocking) if it exists
		WORD wKeyCode = GetKey();

		// Process input to update application state
		switch (wKeyCode)
		{
			case VK_ESCAPE:
				bExit = true;
		};

		// Render the map state to the console
		DrawMap(tileMap);

	} while (!bExit);


	return 0;
}


/******************************************************************************
  *                        Define application subroutines                     *
  *****************************************************************************/

/** Function: ClearConsole
  * Inputs:
  *		hConsole : HANDLE	- handle to console to be cleared 
  * Description:
  *     Fill console internal buffer with blank characters
  */
void ClearConsole(HANDLE hConsole)
{
	CONSOLE_SCREEN_BUFFER_INFO csbiConsoleInfo;
	GetConsoleScreenBufferInfo(hConsole, &csbiConsoleInfo);
	SMALL_RECT srcWindow = csbiConsoleInfo.srWindow;
	DWORD dNumChars = (srcWindow.Right - srcWindow.Left + 1)*(srcWindow.Bottom - srcWindow.Top + 1);
	COORD cStart;
	cStart.X = srcWindow.Left;
	cStart.Y = srcWindow.Top;
	FillConsoleOutputCharacter(hConsole, ' ', dNumChars, cStart, &dNumChars);
}


/** Function: DrawMap
  * Inputs:
  *		rMap : MAP_BUFFER	- array of ENTITY map elements
  * Description:
  *		Use the ENTITY values in rMap to determine ASCII characters to be drawn to standard output console
  */
void	DrawMap(MAP_BUFFER & rMap)
{
	static GUI_BUFFER	rScreenBuffer = {};

	// Set the ScreenBuffer characters and attributes given the ENTITY values in rMap
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			ENTITY & rTile = rMap[row][col];
			rScreenBuffer[row][col].Char.AsciiChar = (char)TOKEN[rTile];
			switch (rTile)
			{
			case EMPTY:
				rScreenBuffer[row][col].Attributes = 0;
				break;

			case WALL:
				rScreenBuffer[row][col].Attributes = FOREGROUND_WHITE;
				break;
			};
		}
	}

	// Copy the ScreenBuffer content to the standard output console 
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut != INVALID_HANDLE_VALUE)
	{
		COORD pos = { 0, 0 };
		COORD size = { MAP_WIDTH, MAP_HEIGHT };

		SMALL_RECT srDrawRegion;
		srDrawRegion.Left = 0;
		srDrawRegion.Top = 0;
		srDrawRegion.Bottom = srDrawRegion.Top + MAP_HEIGHT;
		srDrawRegion.Right = srDrawRegion.Left + MAP_WIDTH;

		WriteConsoleOutput(hStdOut, (CHAR_INFO *)rScreenBuffer, size, pos, &srDrawRegion);
	}
}


/** Function: GetKey
  * Description:
  *		Check console event buffer for keydown events and return keycode of first event found
  *		Does not block - i.e., will return if no events in console event buffer
  */
WORD GetKey()
{
	bool bExit = false;
	WORD wKeyCode = 0;
	HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
	const unsigned int MAX_INPUTS = 128;
	if (hStdIn != INVALID_HANDLE_VALUE)
	{
		INPUT_RECORD irInBuf[MAX_INPUTS];
		DWORD dNumRead = 0;

		ReadConsoleInput(hStdIn, irInBuf, (DWORD)MAX_INPUTS, &dNumRead);
		for (DWORD i = 0; i < dNumRead; i++)
		{
			// Only consider events that are key-down events
			if (irInBuf[i].EventType == KEY_EVENT && irInBuf[i].Event.KeyEvent.bKeyDown)
			{
				// return the keycode of the first key-down event
				wKeyCode = irInBuf[i].Event.KeyEvent.wVirtualKeyCode;
				bExit = true;
				break;
			}
		}
	}
	return wKeyCode;
}
Questions
I'll try and break down to the best of my knowledge each question with what I think the answer is. If I'm close, please tell me and same if I'm completely wrong. You don't have to point out the answer, just point me in the right direction. Any help is appreciated. Willing to give credits to people who give decent help. Many thanks in advance.

Consider the first statement within the definition of the function DrawMap:
static GUI_BUFFER rScreenBuffer = {};

a) What is the data type GUI_BUFFER? Identify where this type is declared and determine the underlying data type. Explain the meaning of the declaration of this type in the context of the using keyword. How is this different to the use of the typedef keyword, as in the declaration of the ENTITY data type?

MY ANSWER - COMPLETED THIS QUESTION. The first 6 words are already confusing me. I thought the data type of GUI_BUFFER was static but I'm really second guessing myself now. I think the answer is void. As for where the type is declared, I'm guessing it's mentioned in the declaration near the top? No idea what the underlying data type is. The rest of the question is just a shamble to me.

b) What is the effect of the keyword static upon the scope and lifetime of the variable rScreenBuffer? Within which memory region would you expect to find this variable allocated? What performance benefit is obtained by making this buffer a static variable?

MY ANSWER - COMPLETED THIS QUESTION. I'm pretty sure static means that variable stays stored in the same location for the entire during of execution, but it's value can change. As for the rest of the question, memory region and performance benefits, I'm completely stumped.

c) What is the data type underlying the MAP_BUFFER array? What are the possible values that this data type can take (and hence the possible values stored in the map)?

MY ANSWER - I've written down so far that it's ENTITY[MAP_HEIGHT][MAP_WIDTH], only because it seems like a repeat of question one. Although I'm not sure of the possible values that this data type can take. I'm assuming that the data type is ints and because of this, they can only hold numerical values. Can someone clarify if I'm heading in the right direction?

d) Within the function DrawMap the MAP_BUFFER parameter is used to determine the values written into the GUI_BUFFER local variable. Discuss briefly the relationship between these data structures by explaining how the contents of an element of the MAP_BUFFER array determines the contents of the GUI_BUFFER array.

MY ANSWER - Don't have one yet.
__________________

Last edited by sickufully; 09-2-2015 at 02:42 AM..
sickufully is offline   Reply With Quote
Old 08-31-2015, 05:16 AM   #2
Scruffman
FFR Player
 
Scruffman's Avatar
 
Join Date: Feb 2013
Posts: 1
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

You're sort of on the right track.

a) The answer is not void. GUI_BUFFER has been defined as a type alias which means it refers to a previously defined type in the code, acting like a legend for complex types so your code is more understandable. If you find where GUI_BUFFER has been assigned with the using keyword, the value is the data type assigned. typedef is also very similar to using, you may want to review this Stack Overflow post for a better breakdown between the two:
Code:
http://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11
b) You are along the right track, you might want to review this link which gives a broad overview:
Code:
http://www.learncpp.com/cpp-tutorial/43-static-duration-variables/
Regarding performance, static variables are initialized only once which can have a major benefit over say a function which needs to keep initializing a variable (such as an integer array) each time it is run. You should also review where static values are stored (Heap or Stack) and that can help you get a better idea regarding performance.
Scruffman is offline   Reply With Quote
Old 08-31-2015, 07:42 AM   #3
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Cheers for the reply. Gotta hit the hay for now but I'll have a look at these tomorrow and leave some feedback to see if I'm any closer with the answer. Thanks.

p.s would be great if you could drop by the thread again. You seem to have a decent understanding of this stuff.
__________________

Last edited by sickufully; 08-31-2015 at 07:43 AM..
sickufully is offline   Reply With Quote
Old 08-31-2015, 04:41 PM   #4
Reincarnate
x'); DROP TABLE FFR;--
Retired StaffFFR Veteran
 
Reincarnate's Avatar
 
Join Date: Nov 2010
Posts: 6,332
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Personally, I think using aliases like that is bad practice, but that's another story.
Reincarnate is offline   Reply With Quote
Old 08-31-2015, 06:01 PM   #5
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Alright so since I'm up at this ungodly hour, I'm gonna try and smash as much of this out as I can.

a) So for the GUI_BUFFER data type, the answer would be const unsigned int? The only way I got this was the fact that [MAP_HEIGHT] & [MAP_WIDTH] are integers and they're the attributes of CHAR_INFO. Or is the answer CHAR_INFO? Or am I still completely wrong? Gahhh this is so confusing. As for where it's declared, I'm going to go with in the initialization list. I still don't understand what is meant by underlying data type? This is what I have for the second half of the question: The keyword using helps improve readability and structure of the code and is also useful when creating templates as typedef cannot be used in templates. Is there anything else worth mentioning about the difference between the keywords using and typedef cause I can't seem to find much.

b) working on it.

Quote:
Originally Posted by Reincarnate View Post
Personally, I think using aliases like that is bad practice, but that's another story.
You should take it up with my teacher
__________________

Last edited by sickufully; 08-31-2015 at 06:01 PM..
sickufully is offline   Reply With Quote
Old 08-31-2015, 07:59 PM   #6
Reincarnate
x'); DROP TABLE FFR;--
Retired StaffFFR Veteran
 
Reincarnate's Avatar
 
Join Date: Nov 2010
Posts: 6,332
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Speaking broadly, a data type is the thing that describes the data.

For example:

int x = 5;

Here, the variable x holds the value 5. The data type is int -- or more generally, an integer.

float x = 5.0;

Same idea. x is a float datatype.

vector<int> x(10);

Now we've declared a vector of 10 ints -- as you can guess, the data type is vector<int>.

The underlying datatype of GUI_BUFFER is just CHAR_INFO (although I guess the answer to this may depend on how "deep" they want you to go with the answer -- for example CHAR_INFO[MAP_HEIGHT][MAP_WIDTH] to show that you're aliasing a multi-dimensional data type, etc)

As for typedef vs. using, they are the same thing -- they're just phrased a little differently.

For instance:

typedef long long ll;

allows the coder to use the shorthand "ll" to refer to the "long long" datatype.

Last edited by Reincarnate; 08-31-2015 at 08:07 PM..
Reincarnate is offline   Reply With Quote
Old 08-31-2015, 08:24 PM   #7
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Hmm yeah that mostly seems to make sense to me. If the underlying multi-dimensional data type is CHAR_INFO[MAP_HEIGHT][MAP_WIDTH], does that mean the data type GUI_BUFFER is int int?

I can only assume the teacher wants a basic description of the underlying data type as this is only the first assignment and we haven't covered much of what's being asked on the assignment, hence why I'm finding this so difficult.
__________________
sickufully is offline   Reply With Quote
Old 08-31-2015, 09:01 PM   #8
Reincarnate
x'); DROP TABLE FFR;--
Retired StaffFFR Veteran
 
Reincarnate's Avatar
 
Join Date: Nov 2010
Posts: 6,332
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Quote:
Originally Posted by sickufully View Post
Hmm yeah that mostly seems to make sense to me. If the underlying multi-dimensional data type is CHAR_INFO[MAP_HEIGHT][MAP_WIDTH], does that mean the data type GUI_BUFFER is int int?

I can only assume the teacher wants a basic description of the underlying data type as this is only the first assignment and we haven't covered much of what's being asked on the assignment, hence why I'm finding this so difficult.
The size parameters are integers (specifically, constant unsigned ints), but that doesn't necessarily mean that the underlying datatype is. For example you could have a two-dimensional array of strings, ints, floats, etc.

If you want more detail on that piece:


In this case, note that "rScreenBuffer" is declared in your code, of type GUI_BUFFER (and therefore of type CHAR_INFO), and we see how it gets used elsewhere:

rScreenBuffer[row][col].Char.AsciiChar = (char)TOKEN[rTile];

rScreenBuffer[row][col].Attributes = 0;

So the CHAR_INFO multidimensional array doesn't contain ints -- it contain some sort of object that has "Char" and "Attributes" members. So it's probably a multidimensional array of structs, but I imagine this is more than what your teacher is asking for so I put this in spoiler tags.


In this case, I would say the data type is "CHAR_INFO[][]" to show that it's a two-dimensional datatype being used.

Last edited by Reincarnate; 08-31-2015 at 09:04 PM..
Reincarnate is offline   Reply With Quote
Old 08-31-2015, 10:12 PM   #9
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Thanks for the breakdown, I'm pretty sure I've got an answer for the first question now. Although I'm a little unsure as to whether my explanation is correct or not. But never mind, we got an answer after 2 days...

For the second question, I'm 99% sure that the memory region that rScreenBufferfer is stored in is Static Memory because it's a global variable and won't be de-allocated for the entirety of the program. Please correct me if I'm wrong (which it probably is :P).

Also on the second question, I've read that there isn't really many or any performance benefits to static variables compared to non-static variables. Has it got something to do with storing memory or faster execution of the program?
__________________

Last edited by sickufully; 08-31-2015 at 10:16 PM..
sickufully is offline   Reply With Quote
Old 08-31-2015, 10:29 PM   #10
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 31
Posts: 921
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

A non-static variable is (ignoring compiler optimizations) always allocated (including calling it's constructor which may be a costy operation for some types) and deallocated from stack memory during the duration of its scope. e.g. everytime the function where it is declared is called.

Compare to a static variable where the variable, simply put, 'stays there' through the lifetime of the process after its first use.
AutotelicBrown is offline   Reply With Quote
Old 09-1-2015, 12:03 AM   #11
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Thanks man, that was simple and straight to the point, enough for a newbie like me to understand (I think so anyway). Completed question 2 now.

Moving on, question 3 asks for the underlying data type of the MAP_BUFFER array. I've written down so far that it's ENTITY[MAP_HEIGHT][MAP_WIDTH], only because it seems like a repeat of question one. Although I'm not sure of the possible values that this data type can take. I'm assuming that the data type is ints and because of this, they can only hold numerical values. Can someone clarify if I'm heading in the right direction?
__________________

Last edited by sickufully; 09-1-2015 at 12:04 AM..
sickufully is offline   Reply With Quote
Old 09-1-2015, 12:35 AM   #12
FoJaR
The Worst
 
FoJaR's Avatar
 
Join Date: Nov 2005
Location: space ghost is dead to me
Posts: 2,816
Send a message via AIM to FoJaR Send a message via MSN to FoJaR
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

using is similar to typedef. MAP_BUFFER is a 2D array(with specific dimensions) of type ENTITY.

ENTITY is a typedef of entity_labels.

so really MAP_BUFFER is a 20x30 array of entity_labels.
__________________
FoJaR is offline   Reply With Quote
Old 09-2-2015, 03:04 AM   #13
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

So what you're saying FoJaR, is that the underlying data type of MAP_BUFFER is a 2D array of 20x30 ints?
I'm confused by what you mean here:
Quote:
Originally Posted by FoJaR View Post
MAP_BUFFER is a 2D array(with specific dimensions) of type ENTITY.
What is ENTITY? Is it the name of the array or the name of the MAP_BUFFER or...? And while on this, what exactly is MAP_BUFFER? Is that just a name for something or does it have some meaning behind it?
Code:
typedef entity_labels	ENTITY;
Basically, the question above can be answered if you can explain to me what each word in this line of code means (except using)
Code:
using MAP_BUFFER = ENTITY[MAP_HEIGHT][MAP_WIDTH];
This is my answer to question c) so far:
a. The underlying data type of MAP_BUFFER is ENTITY[MAP_HEIGHT][MAP_WIDTH] which shows that it is a two-dimensional array of ints.

Is this correct? If not, please guide me. I'm feeling quite dim-witted about all of this.

Also if someone could clear this up too.
Quote:
Originally Posted by sickufully
Although I'm not sure of the possible values that this data type can take. I'm assuming that the data type is ints and because of this, they can only hold numerical values. Can someone clarify if I'm heading in the right direction?
__________________

Last edited by sickufully; 09-2-2015 at 03:05 AM..
sickufully is offline   Reply With Quote
Old 09-2-2015, 06:40 AM   #14
Xayphon
sausage
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Xayphon's Avatar
 
Join Date: Nov 2008
Location: Germany
Posts: 1,630
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

I wish I could help you more with this stuff, but I only know Java and Python, and I can't pinpoint how datatypes work in C++ yet myself without reading into it more. So this'll be an attempt to help you without knowing C++.

Concerning your last question, I think I can safely tell you that, even though "MAP_HEIGHT" and "MAP_WIDTH" are ints, they do not necessarily mean that two integers are used within the Array ENTITY[MAP_HEIGHT][MAP_WIDTH]. They only declare the size of the Array, which is MAP_HEIGHT x MAP_WIDTH, so 20x30. The Array can thus hold 20x30 elements of a certain Datatype.

Now the Array itself is literally empty, as in all 20x30 elements are Null, so now you have to look more into the code and find out where this array is filled with actual elements that replace the Nulls in the array. Those elements that are being put into the array are probably the datatype you are looking for.

This piece of code struck the most for me:

Code:
/*************************************************************************
	* Initialise the tile map with appropriate ENTITY values
	*/
	MAP_BUFFER		tileMap;
	
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			tileMap[row][col] = EMPTY;
		}
	}
	/*************************************************************************/
To me it seems like that what is saved in MAP_BUFFER (which is ENTITY[MAP_HEIGHT][MAP_WIDTH] ?) is now renamed to "tileMap", so the value "tileMap" becomes the ENTITY[][] Array. Thanks to the for-loop, "tileMap" aka ENTITY[][] is now being completely filled with a value called EMPTY, which is, according to this little comment before the for-loop, an "appropriate ENTITY value" (be aware that just because it is called EMPTY it does not mean the Array's elements are Null i.e. literally empty anymore, it actually contains something now). Doing 1+1, I'd then conclude that the Array holds elements of type ENTITY, which would indeed make the Array and as such MAP_BUFFER a datatype of ENTITY.

Take this advice with a grain of salt, though. I don't know if this is what's even asked for or if this is actually correct given that C++ might work completely different than Java. "tileMap" being ENTITY[][] all of sudden just because it says MAP_BUFFER was something I simply concluded without doing some read ups, but I thought this seems the most reasonable to go with. Someone might be able to provide a better or correct explanation, but I'd be glad if this helps.

Last edited by Xayphon; 09-2-2015 at 08:12 AM..
Xayphon is offline   Reply With Quote
Old 09-3-2015, 03:54 AM   #15
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Wow thanks man, that was a huge help. The whole array part of this is slowly starting to click into place in my head now and I think I have a better understanding of how this array works (providing that your non-existent c++ knowledge is correct but since you sound like you know what you're taking about, I'm assuming it is correct :P). Also, sorry for the late reply, been at work for the past 10 hours.

So basically what you're saying is that the Array called ENTITY[MAP_HEIGHT][MAP_WIDTH] is empty, even though map height & width are ints, it doesn't mean that the 20x30 'boxes' in the array are filled with ints, until the value called EMPTY fills each 'box' in the array?

When I say boxes (of an array), this is what I mean. Also, each of these boxes are empty until the value EMPTY is used.
______________________________________
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|

From what I gather, EMPTY is a value that equals 0.
Code:
enum entity_labels	{
	EMPTY = 0,
	WALL
};
So, when initialising the tilemap array with data elements, each time it moves to the next box, consecutive numbers are entered. This is what I mean:
______________________________________________
|__0__|__1__|__2__|__3__|__4__|__5__|__6__|__7__|
|__8__|__9__|__etc__|__etc__|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|

The only way I came to this conclusion is by these lines and the increment signs (row++ & col++). This also leads me to believe that the data type in the array is int because if EMPTY = 0, then increments can only be 1, 2, 3, 4, 5, etc.. Actually, just re-reading over that last sentence, that doesn't even make sense to me. It's surely wrong.
Code:
for (unsigned int row = 0; row < MAP_HEIGHT; row++)

		for (unsigned int col = 0; col < MAP_WIDTH; col++)
I'm not sure how close I am to figuring this out but this seemed like a logical answer.
__________________

Last edited by sickufully; 09-3-2015 at 06:38 AM..
sickufully is offline   Reply With Quote
Old 09-3-2015, 06:45 AM   #16
Xayphon
sausage
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Xayphon's Avatar
 
Join Date: Nov 2008
Location: Germany
Posts: 1,630
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Quote:
Originally Posted by sickufully View Post
Wow thanks man, that was a huge help. The whole array part of this is slowly starting to click into place in my head now and I think I have a better understanding of how this array works (providing that your non-existent c++ knowledge is correct but since you sound like you know what you're taking about, I'm assuming it is correct :P).
I'm just assuming that arrays work the same way in Java as they do in C++, since Java took a lot of influence from C and C++. Looking at the code, it did seem to me that they do sort of, so that's why I think I can help you with that part at least.

Quote:
Originally Posted by sickufully View Post
So basically what you're saying is that the Array called ENTITY[MAP_HEIGHT][MAP_WIDTH] is empty
Yes, the Array is completely useless until you actually fill it up like it's done in the for loop. Both MAP_HEIGHT and MAP_WIDTH, which are integers, only define the size of the Array that you mustn't step over. And obviously you only can define the size of an Array with a number.

Quote:
Originally Posted by sickufully View Post
So, when initialising the tilemap array with data elements, each time it moves to the next box, consecutive numbers are entered. This is what I mean:
______________________________________________
|__0__|__1__|__2__|__3__|__4__|__5__|__6__|__7__|
|__8__|__9__|__etc__|__etc__|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|
|____|____|____|____|____|____|____|____|

The only way I came to this conclusion is by these lines and the increment signs (row++ & col++).
Code:
for (unsigned int row = 0; row < MAP_HEIGHT; row++)

		for (unsigned int col = 0; col < MAP_WIDTH; col++)
I'm not sure how close I am to figuring this out but this seemed like a logical answer.
Not exactly. The values "row" and "col" are used to point to certain boxes (or: Indexes within the Array), as you've visualized them so nicely. "tileMap[row][col] = EMPTY" literally means nothing but "put EMPTY at index [row][col]", meaning that, if EMPTY really is simply 0, the whole array is filled with 0's.

The for-loop iterations are something along the lines of

row: 0
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 1
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 2
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

row: 3
col: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, ...

and so on. After each iteration the Array is thus filled as follows:

tileMap[0][0] = EMPTY tileMap[1][0] = EMPTY ...
tileMap[0][1] = EMPTY tileMap[1][1] = EMPTY ...
tileMap[0][2] = EMPTY tileMap[1][2] = EMPTY ...
tileMap[0][3] = EMPTY tileMap[1][3] = EMPTY ...
tileMap[0][4] = EMPTY tileMap[1][4] = EMPTY ...
...

until the Array is finally completely filled with EMPTY.

Unfortunately, the following part confuses me a little in terms of what datatype the Array is in the end... but since that's what the Array is being filled with, the whole Array is probably, as FoJaR said, an array of entity_labels. The entities being different kinds of Tiles that will be used in your Tile Map. I missed that in my last post, sorry for that.

Quote:
Originally Posted by sickufully View Post
From what I gather, EMPTY is a value that equals 0.
Code:
enum entity_labels	{
	EMPTY = 0,
	WALL
};
If you're a bit irritated by the fact that the whole array is being filled with 0's, try imagining an actual Tile Map like in Terraria. Every 0 in the Array is an empty wall, so, something like sky or w/e. That empty wall needs to exist though in order to work with something like building. You want to be able to place a block anywhere on the screen, so you must be able to access the Array and replace that specific Array index the player wants to place a block on with, say, an Earth Tile (or WALL, as it is in your code, if you wish). That Earth Tile has a value or is labeled as entity "1". You then replace that 0 with a 1 and when you render that array to graphics, you will have a lonely Earth Tile sitting on your screen.

Visualized:

The Array after the for-loop (literally a little empty Tile Map):
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

User clicks on a box on the screen:
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__X__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

Convert clicking position to Array position and change the value at Array index tileMap[1][2] to 1
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__1__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|
|__0__|__0__|__0__|__0__|__0__|__0__|__0__|__0__|

And when you render it next time, you're going to have an Earth Tile where the 1 is (really simply spoken).

I know this goes way beyond your assignment, but I think it's an interesting example that shows how Arrays can be used while also showing the basics of using one. Who knows, maybe you get to program a Terraria clone like me last semester, haha...
Xayphon is offline   Reply With Quote
Old 09-4-2015, 10:16 PM   #17
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

As much as it might not be relevant to my assignment, it was super helpful. The Terraria example you gave made sense and I think I've got an idea of how this array works. Now every time I play Terraria, I'm going to be running around being like, 'some ore here, that tile is a 1' and 'Ooooh there's nothing there, it must be EMPTY in the array cause that's what 0 is'.

Anyway, I've written an answer for that question and that ticks off the code analysis part. Now comes writing and modifying the code. Here is my next question:

Your first coding task is to create a function that performs the initialisation of the console window and to replace the existing code within main that serves this purpose with an invocation of this new function. Your function must adhere to the declaration
Code:
void InitConsole(const unsigned int, const unsigned int);
where the function arguments are the height and width of the window buffer. You will need to provide a definition for this function that implements the functionality currently provided within main.


So far, I think I've located the piece of code that I need to modify, I'm just not sure what to modify. I have declared InitConsole, that was no problem. Here's the section of code I think I need to modify:
Code:
/**************************************************************************
	  * Initialise the standard output console
	  */		
	HANDLE	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut != INVALID_HANDLE_VALUE)
	{
		ClearConsole(hStdOut);

		// Set window title
		SetConsoleTitle(TEXT("Tile Map Demo"));

		// Set window size
		SMALL_RECT srWindowRect;
		srWindowRect.Left = 0;
		srWindowRect.Top = 0;
		srWindowRect.Bottom = srWindowRect.Top + MAP_HEIGHT;
		srWindowRect.Right = srWindowRect.Left + MAP_WIDTH;

		SetConsoleWindowInfo(hStdOut, true, &srWindowRect);

		// Set screen buffer size
		COORD cWindowSize = { MAP_WIDTH, MAP_HEIGHT };
		SetConsoleScreenBufferSize(hStdOut, cWindowSize);
	}
Can someone tell me if this is the right section I need to modify and what exactly I need to change? I've tried changing ClearConsole(hStdOut) to InitConsole but the hStdOut part becomes incorrect. Assuming this because I didn't declare it as a parameter for InitConsole but when I tried to do this, it says 'type name not allowed'. Halp please!
__________________
sickufully is offline   Reply With Quote
Old 09-5-2015, 02:21 AM   #18
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Actually, I'm pretty sure I figured that question out. Some one in my class pointed out that you didn't need to have the const int's in the declaration and just use HANDLE hStdOut in its place.

Next question is:
Identify the code used to initialise the tileMap array. Modify this code so that the outer edges of the array (i.e., first and last row and column) are initialised to contain a WALL entity and all internal cells are initialised to be EMPTY.

I'm looking at this section of the code but again, I'm unsure as to what to change. Any pointers?
Code:
	* Initialise the tile map with appropriate ENTITY values
	*/
	MAP_BUFFER		tileMap;
	
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			tileMap[row][col] = EMPTY;
		}
	}
__________________
sickufully is offline   Reply With Quote
Old 09-5-2015, 08:27 AM   #19
Xayphon
sausage
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
Xayphon's Avatar
 
Join Date: Nov 2008
Location: Germany
Posts: 1,630
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

I assume you need to have something like this as a result? (All last and first col and row values need to become a WALL)



In order to achieve that, you need to think about what conditions need to be met in order for a WALL to be initialised into the Array.

During the for-loop, you need to check whether the row value is at the first or last row OR col is at the first or last value. If one of these is true, put a WALL instead of an EMPTY into the Array. If none of these are true, an EMPTY will be put into the array.

It might sound confusing what I just said, but translate what I just tried to broadly describe into C++ and it should be solved, it's hard to describe conditions when you yourself are going to sound like a confusing computer program..

Ask away if you need anymore tips for this

edit: I'm actually not sure which values your docent wants to see within the array. I went with 0 and 1 because it seemed easy, but WALL does not have a specified value in your code as far as I can see, so you might want ot add WALL = 1 or something

Last edited by Xayphon; 09-5-2015 at 10:34 AM..
Xayphon is offline   Reply With Quote
Old 09-5-2015, 01:22 PM   #20
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 31
Posts: 921
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

By default the enum assigns numeric values sequencially after any explicit definitions, so WALL is going to be 1. In any case the assignment should be in the form of: "tileMap[row][col] = WALL;".
AutotelicBrown is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution