Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Rough text game - need help with mapping (array as a map)

As the title suggests, I'm taking a break to try and put some of what I've seen/learned into practice to get it programmed into my fingers. Right now, all I want to do is have one element of an array temporarily changed to @ or any symbol in particular, to reference the players location. Bonus points, I wanted to use strings as a collision mechanism, but I didn't know how, essentially using '|' for walls and '_' for ceiling.. looked nicer. Anyways, snippets for you.

If you're curious about the layout, input comes from an input field (self clearing, push enter, i'm rather proud of that one lol), output is/will be sent into two div's. One is pertinent to the room, the other is the location description.

Here is the array, it's nothing special. This is all very very starting-uppey and has no finished shape yet. const map = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 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, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; var x = 2; var y = 1; var playerStart = map[x][y];

here is the function that runs the map - bad syntax to draw focus and kinda hint at what I'm trying to accomplish.

function disMap () {
    let at = map[x][y] whaaahuhh?
    at[x][y] = '@'; blarrrghgh... 


    for (let i = 0; i < 1; i++)
    {
        for (let j = 0; j < 7; j++)
        {
        console.log(map[j]);
        output.innerHTML += `${at[j]} \n`;

        }
    }
};

And that's my current blocker if anyone would care to help me out. I realize this isn't really entirely what JS was intended for, but it's on my bucket list of things I always wanted to do.

Thanks folks, appreciate you.

1 Answer

Steven Parker
Steven Parker
231,275 Points

It's not entirely clear what your issue is, but just to make the code functional:

    let at = map;
//              ^ no indexing here
    at[x][y] = '@';  //blarrrghgh... 
//                   ^ add slashes before coment

Also, a literal newline in HTML code doesn't cause the output to be on separate lines, but a <br> element will:

        output.innerHTML += `${at[j]}<br>\n`;
//                                   ^ add line break

Does that answer your question? If not, please update and elaborate a bit more about your issue.