var map, player = {
x: 1,
y: 8
},
i, maze = document.getElementById('maze'),
win, playerDirection = 'north';
function mapToHTML(map) {
var html = '',
x, y, dictionary = {
'H': '<span class="tile wall">H</span>',
'.': '<span class="tile floor">.</span>',
'@': '<span class="tile player ' + playerDirection + '">@</span>',
'X': '<span class="tile goal">X</span>'
};
for (y = 0; y < map.length; y += 1) {
for (x = 0; x < map[y].length; x += 1) {
html += dictionary[map[y][x]];
}
html += '<br>';
}
return html;
}
function updateMap() {
var displayMap = [],
x, y;
for (y = 0; y < map.length; y += 1) {
displayMap[y] = displayMap[y] || [];
for (x = 0; x < map[y].length; x += 1) {
displayMap[y][x] = map[y][x];
}
}
displayMap[player.y][player.x] = '@';
for (y = 0; y < displayMap.length; y += 1) {
displayMap[y] = displayMap[y].join('');
}
maze.innerHTML = mapToHTML(displayMap);
}
function playerMove(x, y) {
var toX = player.x + x,
toY = player.y + y;
if (map[toY][toX] === '.' || map[toY][toX] === 'X') {
player.x = toX;
player.y = toY;
updateMap();
if (document.body.className !== 'ready') {
// this is a bit kludgey, but if we set the className every time it breaks the animation (at least in safari)
document.body.className = 'ready';
}
}
if (map[toY][toX] === 'X') {
maze.innerHTML = 'YOU WIN';
document.getElementById('buttons').innerHTML = '';
}
}
function updateDisplay(type) {
maze.className = 'display-' + type;
}
map = [
'HHHHHHHHHH',
'HH......XH',
'HH.H.H.H.H',
'HH.HHHHH.H',
'H........H',
'HHHHH.H.HH',
'H...H.H.HH',
'H.H...H.HH',
'H.H.HHH.HH',
'HHHHHHHHHH'
];
for (i = 0; i < map.length; i += 1) {
map[i] =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.