C++ Lab: Map Movement Mechanics

Sat Oct 18 2025
Updated: Tue Oct 21 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

Imagine standing in the middle of a map, surrounded by obstacles, deciding your next move — that’s exactly the experience you’ll simulate in this lab. You will build a small program in which the user can navigate a 10 × 15 grid, observe their position, and avoid impassable terrain (trees) that the program loads from a file. It’s a simple game-like setup, but beneath the surface lies a rich exercise in handling spatial data, user input, validation, and file manipulation.

In this lab, you will initialize a two-dimensional array to represent the map, read coordinates from a .csv file to mark obstacles, place the player at a defined starting point, and then enter a loop in which the user chooses to move or quit. On each move the program checks bounds, validates input, determines whether the destination is free or blocked by a tree, and then updates and re-displays the map. You’ll practice using file I/O through <fstream>, parsing comma-separated values, managing 2D data structures, and enforcing robust input checks.

Why does this matter? Because spatial reasoning and efficient data organisation are key skills in many programming domains — from game development and robotics to mapping applications and simulations. Learning to model a grid, load external data, validate user actions, and update state dynamically gives you a foundation for building interactive, stateful programs. By completing this lab, you’ll deepen your understanding of how programs can represent and manage a virtual world — even a small one — and respond meaningfully to user choices.


Task

Create a small program that allows a user to move around a 10x15 2D array map. The map will have the player and trees inside of it, and when the player is moving around they must not move to a spot that has a tree on it. The trees are read from a .csv file similar to the following:

x,y 0,0 1,0 2,0 3,0 4,0 ...

Each x,y pair needs to be read in, spit on the comma, and then the individual x and y values converted to integers to be used as indices into the map to place a ’t’ where the tree belongs. It is important to note that the values come in x,y pairs which x is the column the tree needs to be placed into and y is the row the tree needs to be placed into. So when accessing the array to place a ’t’ each x or y index must be used in the proper column or row index in the map.

Once the trees have been placed in the map the user will then be asked to either move around the map or quit, then the appropriate action is done. When the user moves around the map they will enter a row and column to move to, then if no tree is in the way and the move is to a valid spot the player will be moved to the desired spot. The program will then ask if they want to move again or quit.


Steps to Complete the Lab

All input files needed for this lab can be downloaded here.

You will need to:

  1. Create a 2D array characters of size 10x15 with every item in the array initialized to the null character (0 on the ASCII table).

  2. From trees.csv read each x,y coordinate, split them on the comma, then use them as indices into the map to place a ’t’ in the appropriate position.

  3. Place a p in the map at row 9 column 0 to represent the player, memorize this position into variables of your choosing.

  4. In a loop that loops until the user wants to quit:

    a. Display the map to the terminal by outputting each map item (character) if there is one in its respective position in the map, or outputting an underscore if there is a null character in the position. Each map item or underscore should have a space output after it. The map originally read in should look like the following when output to the terminal initially:

    t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t

    b. Prompt the user to "(m)ove -or- (q)uit? " then read their selection into a character.

    c. If the selection is neither ’m’ or ’q’ output the error "Error: Invalid option\n" and go back to (b).

    d. If the selection is ’q’ output "\nQuitting...\n" and terminate the program.

    e. If the selection is ’m’:

    • Read in the row to move to into an integer using the prompt "\nMove Row: ". If the input fails or is negative or is too large (the map has 10 rows) then output the error "\nError: Move must be between 0 and 9\n\n” and go back to (b).

    • Read in the column to move to into an integer using the prompt "Move Column: ". If the input fails or is negative or is too large (the map has 15 columns) then output the error "\nError: Move must be between 0 and 14\n\n” and go back to (b).

    • If there is a tree in the location that the player wants to move to output "\nThere's a tree in your way\n\n" and go back to (b).

    • If there is move is valid and nothing is in the way, remove the player from their saved (in variables in step (3)) spot on the map by setting it back to the null character. Then place the character in their new location in the map by placing a ’p’ in the location the player entered. The variable from (3) should then be updated to the player's new position.

    • Output the message "\n\nMoved to row PLAYER_ROW column PLAYER_COL\n\n where PLAYER_ROW is the integer value of the row the player was moved to and PLAYER_COL is the integer value of the column the player was moved to.

    • Go back to (b).


Libraries Permitted

You may only use the following libraries in your program:

  1. iostream

  2. fstream


Criteria for Success

See CodeGrade

Example Program Interaction

An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.

lab 7 % ./a.out t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: error Error: Move must be between 0 and 9 t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: -1 Error: Move must be between 0 and 9 t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: 10 Error: Move must be between 0 and 9 t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: 8 Move Column: error Error: Move must be between 0 and 14 t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: 8 Move Column: 12 There's a tree in your way t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t _ p _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? m Move Row: 8 Move Column: 14 Moved to row 8 column 14 t t t t t t t t t t t t t t t t t t t t t t t t t _ _ _ _ _ t t t t _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t t _ _ _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ t t _ _ _ _ _ _ _ _ _ _ _ _ t t t t p _ _ _ _ _ _ _ _ _ t t t t t t (m)ove -or- (q)uit? q Quitting... lab 7 %

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.