[University - C++ Programming] Help With Assignment (Very Basic Stuff)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xayphon
    sausage
    FFR Simfile Author
    • Nov 2008
    • 1630

    #16
    Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

    Originally posted by sickufully
    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.

    Originally posted by sickufully
    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.

    Originally posted by sickufully
    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.

    Originally posted by sickufully
    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...

    Comment

    • sickufully
      Sir Krisk
      • Dec 2007
      • 930

      #17
      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:
      [I]void InitConsole(const unsigned int, const unsigned int);[/I]
      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!

      Comment

      • sickufully
        Sir Krisk
        • Dec 2007
        • 930

        #18
        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;
        		}
        	}

        Comment

        • Xayphon
          sausage
          FFR Simfile Author
          • Nov 2008
          • 1630

          #19
          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, 09:34 AM.

          Comment

          • AutotelicBrown
            Under the scarlet moon
            FFR Simfile Author
            • Jan 2014
            • 923

            #20
            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;".
            Play my files

            Comment

            • Xayphon
              sausage
              FFR Simfile Author
              • Nov 2008
              • 1630

              #21
              Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

              Oh, yeah, totally missed that

              Comment

              • sickufully
                Sir Krisk
                • Dec 2007
                • 930

                #22
                Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                I'm pretty sure I know what I'm supposed to do, I just don't know how to code it. I'm going to assume I need to add in an if statement for both [row] and [col] to determine if the row or col value is at the first or last box of the array? Do I need to change the for loops to if loops?

                This is what I have so far:
                Code:
                	if (unsigned int row = 0 && 20)
                	{
                		tileMap[MAP_WIDTH][row] = WALL;
                	}
                	else
                	{
                		tileMap[MAP_WIDTH][row] = EMPTY;
                	}
                	if (unsigned int col = 0 && 30)
                	{
                		tileMap[MAP_HEIGHT][col] = WALL;
                	}
                	else
                	{
                		tileMap[MAP_HEIGHT][col] = EMPTY;
                	}
                It doesn't seem to contain any errors but I'm pretty sure all I've done is assign a wall to array locations row[0] and [20] and col[0] and [30] (all the corners and that's it). It doesn't actually put a wall all the way around. But there's something else wrong with my code now and it breaks at (in the function: DrawMap):
                Code:
                rScreenBuffer[row][col].Char.AsciiChar = (char)TOKEN[rTile];
                I think this has something to do with my previous question but I honestly don't know why it stuffs up. If someone could help me out with rectifying this?
                Last edited by sickufully; 09-5-2015, 09:10 PM.

                Comment

                • Xayphon
                  sausage
                  FFR Simfile Author
                  • Nov 2008
                  • 1630

                  #23
                  Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                  Can you show the complete code of the whole tileMap initialization function you have so far? Including the for-loops

                  Comment

                  • AutotelicBrown
                    Under the scarlet moon
                    FFR Simfile Author
                    • Jan 2014
                    • 923

                    #24
                    Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                    Just a reminder that the equality operator is '==' and not '='. You should have something like:
                    Code:
                    if (row == 0 || row == 20)
                    Last edited by AutotelicBrown; 09-5-2015, 10:24 PM.
                    Play my files

                    Comment

                    • sickufully
                      Sir Krisk
                      • Dec 2007
                      • 930

                      #25
                      Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                      This is all that's in the initialisation of Tile Map. I took the for-loops out for some reason (clearly I know what I'm doing...) but I still have them saved in a notepad in case I still needed them (which I'm assuming I do since you mentioned it :P). Oh also, I may have been testing a few things with the if statements so that's why they probably contain a tonne of errors in them.

                      Code:
                      	/*************************************************************************
                      	* Initialise the tile map with appropriate ENTITY values
                      	*/
                      	MAP_BUFFER		tileMap;
                      	
                      	if (unsigned int row = 0 && 20) //values in row 0 and 20 are set to WALL
                      	{
                      		tileMap[MAP_WIDTH][row] = WALL; //not sure whether I'm supposed to assign an Ascii Code or just set it to WALL
                      	}
                      	else
                      	{
                      		tileMap[MAP_WIDTH][row] = EMPTY; //any other row set to EMPTY
                      	}
                      	if (unsigned int col = 0 && 30) //values in col 0 and 30 are set to WALL
                      	{
                      		tileMap[MAP_HEIGHT][col] = WALL;
                      	}
                      	else
                      	{
                      		tileMap[MAP_HEIGHT][col] = EMPTY; //any other col set to EMPTY
                      	}
                      Code:
                      for (unsigned int row = 0; row < MAP_HEIGHT; row++)
                      	{
                      		for (unsigned int col = 0; col < MAP_WIDTH; col++)
                      		{
                      			tileMap[row][col] = WALL;
                      		}
                      	}

                      Comment

                      • AutotelicBrown
                        Under the scarlet moon
                        FFR Simfile Author
                        • Jan 2014
                        • 923

                        #26
                        Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                        Also, if is not a loop, when you have something like:

                        Code:
                        if (unsigned int row = 0 && 20) //values in row 0 and 20 are set to WALL
                        	{
                        		tileMap[MAP_WIDTH][row] = WALL; //not sure whether I'm supposed to assign an Ascii Code or just set it to WALL
                        	}
                        else
                        	{
                        		tileMap[MAP_WIDTH][row] = EMPTY; //any other row set to EMPTY
                        	}
                        You are:
                        -Declaring a new int named row with value being '0 && 20', && being logical and (should evaluate to 0)
                        -Assigning to tileMap[MAP_WIDTH][row], aka tileMap[MAP_WIDTH][0] the value of EMPTY (because 0 evaluates to false).
                        Last edited by AutotelicBrown; 09-5-2015, 10:32 PM.
                        Play my files

                        Comment

                        • sickufully
                          Sir Krisk
                          • Dec 2007
                          • 930

                          #27
                          Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                          Is this any closer to the answer? Also, am I supposed to keep the for-loops in the code and the if statements lie somewhere within the loop?
                          In (row == 0 || row == 20), the first 'row' is underlined as being undefined and same with col in it's statement.
                          Code:
                          	if (row == 0 || row == 20) //values in row 0 and 20 are set to WALL
                          	{
                          		tileMap[MAP_WIDTH][1] = WALL; //not sure whether I'm supposed to assign an Ascii Code or just set it to WALL
                          	}
                          	else
                          	{
                          		tileMap[MAP_HEIGHT][0] = EMPTY; //any other row set to EMPTY
                          	}
                          	if (col == 0 || col == 30) //values in col 0 and 30 are set to WALL
                          	{
                          		tileMap[MAP_HEIGHT][1] = WALL;
                          	}
                          	else
                          	{
                          		tileMap[MAP_HEIGHT][0] = EMPTY; //any other col set to EMPTY
                          	}
                          Last edited by sickufully; 09-5-2015, 10:50 PM.

                          Comment

                          • AutotelicBrown
                            Under the scarlet moon
                            FFR Simfile Author
                            • Jan 2014
                            • 923

                            #28
                            Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                            Yeah, those should be inside the for loop, and note that you should have in all of them something of the form:
                            Code:
                            tileMap[row][col] = WALL;
                            or:

                            Code:
                            tileMap[row][col] = EMPTY;
                            Actually, you should assign EMPTY at first, and only check for the 'true' block of the ifs to change to WALL if necessary. Logically, you are checking all positions of the matrix and, if the position corresponds to a border you assigning a WALL instead of EMPTY.
                            Last edited by AutotelicBrown; 09-5-2015, 11:01 PM.
                            Play my files

                            Comment

                            • sickufully
                              Sir Krisk
                              • Dec 2007
                              • 930

                              #29
                              Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                              Well, I've done it backwards. I think. I'm assuming I'm pretty close now but I'm not sure what I need to change around.


                              Here's my code:
                              Code:
                              	* Initialise the tile map with appropriate ENTITY values
                              	*/
                              	MAP_BUFFER		tileMap;
                              	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
                              	{
                              		if (row == 0 || row == 20) 
                              		{
                              			tileMap[row][0] = EMPTY; 
                              		}
                              		else
                              		{
                              			tileMap[row][1] = WALL;
                              		}
                              
                              		for (unsigned int col = 0; col < MAP_WIDTH; col++)
                              		{
                              			if (col == 0 || col == 30) 
                              			{
                              				tileMap[row][col] = EMPTY;
                              			}
                              			else
                              			{
                              				tileMap[row][col] = WALL; 
                              			}
                              		}
                              	}

                              Comment

                              • sickufully
                                Sir Krisk
                                • Dec 2007
                                • 930

                                #30
                                Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

                                Alright guys, I cannot thank each of you who helped me out on this enough. Without the help I received here, I would've surely achieved no higher than 10-20%. I've handed the assignment in (it was due last Friday but I chose to sacrifice 10% to try and attain another 20-30% worth of marks) but I'm still not expecting to pass. There was still another entire section to complete that I didn't even touch.

                                I can say that I have a slightly better understanding of certain terms and what they mean and do. C++ is A LOT HARDER than I originally thought (cannot emphasis enough on 'A LOT HARDER'). Maybe it's not that hard, maybe I just overthink things or my brain just doesn't seem to grasp the understanding of the language, but one thing is for certain: I need to study a shit tonne more of this if I want to pass the exam in a month or two.

                                If anyone who helped me out wants some credits, just post in here and I'll give you a few. Once again, thank you.

                                p.s I'll probably make another thread for the next assignment (which I've been told is on another level) so keep your eyes peeled. Mostly likely in the next fortnight.
                                Last edited by sickufully; 09-6-2015, 12:51 AM.

                                Comment

                                Working...