This morning I woke up and decided I wanted to code something.  I didn’t know what, but I knew I wanted to make something work. My C++ skills are still fairly limited, as I’ve only been in the class for about 3 weeks now.  My initial thoughts were to make a text based adventure, but  seems as how I had no good ideas for how that would work, I gave that idea up pretty quickly.

My mind turned to the unicode characters we’d covered in class last week.  When I first went through those, the box drawing ones caught my attention. I could make a maze!

Today, I set out to make that maze. And I did, in under 3 hours. I am oh so proud of myself. Not only did I complete my first “sideproject” outside of school, but I did it by myself (no professor assistance), in a short period of time, without freaking out, and it WORKS!

To make my maze work, I started in notepad. I drew out how the maze would look using unicode characters. I assigned each unicode character a specific integer value, 0 being places the player could move, 1 being the location of the player currently, and the rest of the digist standing for different types of walls.

I then created a multidimensional array named maze filled with integers.  To display my maze, I simply looped through each row column by column, and used a series of if statements to output the appropriate wall piece (or blank space or player avatar. And by avatar, I mean the letter X.). The player’s current location is stored in an array of 2 values: the first one for row, the second one for column.

The player is then prompted to pick a number 1-4 for which direction they want to move.  Any number above 4 or any non integer value and the program will ignore it and ask them again. Once the player inputs the direction they want to move, I have a series of if statements. First it checks to see the value stored in the maze array.  (Moving up checks the same column, but a row value of one less, from the players current position.  Moving to the right checks the same row, but one more column value than the current position. You get the picture.) If the value in that location is not equal to 0, it tells the player to pick a different move.  If the value IS 0, then that row and column are updated to be 1 (the current postion), the old position is updated to o, and the array for current position is renumbered.

This loop continues until the array for current position is equal to the end coordinates, which I defined as preprocessor constants.

In order to adjust the maze, all you would have to do is adjust the values in the multidimensional array to create a new map, and change the preprocessor constants for the end point.

What I learned:

As my C++ skills are still very minimal, this was really just good practice for me. I solidified some knowledge on changing specific variables in a multidimensional array, and figured out that you can use arithmetic operators in conjunction with an array value. I’m also pretty proud of myself — I commented the crap out of my code without even thinking about it.  Comments are good, and they seem to be a habit for me already. Woo!

What I would do differently:

My skills with functions aren’t as strong as they should be.  I initially started out with functions like ShowMaze() and GetMovement().  Because I’m not really solid with functions, especially using return values, I ended up scrapping my other functions all together and just putting everything inside main().  This was done in favor of just seeing if I could get the arrays working the way they were supposed to.  If I feel inspired, I might go back and clean up the code by adding some functions.  The next program I write I’ll try and get some more practice with that.

Advertisements