Demo entry 3819034

beefyRobot

   

Submitted by anonymous on Feb 29, 2016 at 08:32
Language: JavaScript. Code size: 3.4 kB.

'use strict';
const config = {
    placed: false,
    direction: ['NORTH', 'EAST', 'SOUTH', 'WEST'],
    position: {x: 0, y: 0, direction: null},
    tableSize: {width: 5, height: 5},
    validCommands: ['PLACE', 'MOVE', 'LEFT', 'RIGHT', 'REPORT'],
};
const form = document.querySelector('.instructions');
const input = form.querySelector('.instruct');
const report = document.querySelector('.report-position');

function handleSubmit(e) {
    let inputValue;
    e.preventDefault();
    if ( config.validCommands.indexOf(input.value) !== -1 ) {
        inputValue = input.value;
    } else if ( input.value.indexOf('PLACE') !== -1 ) {
        inputValue = input.value.split(' ');
    } else {
        console.log('Incorrect command.');
        return;
    }
    input.value = '';
    handleInstruction(inputValue);
}

function handleInstruction(command) {
    let position = {};
    if ( typeof command === 'object' ) {
        let cmd = command[0];
        let posTmp = command[1].split(',');
        position.x = posTmp[0];
        position.y = posTmp[1];
        position.direction = posTmp[2];
        placeBeefy(position.x, position.y, position.direction);
    } else if ( command === 'MOVE' && config.placed ) {
        moveBeefy();
    } else if ( ( command === 'LEFT' || command === 'RIGHT') && config.placed ) {
        turnBeefy(command);
    } else if ( command === 'REPORT' && config.placed ) {
        reportPosition();
    }
}

function placeBeefy(x, y, direction) {
    if ( x <= config.tableSize.width && y <= config.tableSize.height ) {
        config.position.x = parseInt(x);
        config.position.y = parseInt(y);
        config.position.direction = direction;
        config.placed = true;
    }
}

function moveBeefy() {
    if ( config.position.direction === 'NORTH' && config.position.y < config.tableSize.height ) {
        config.position.y += 1;
    }
    if ( config.position.direction === 'EAST' && config.position.x < config.tableSize.width ) {
        config.position.x += 1;
    }
    if ( config.position.direction === 'SOUTH' && config.position.y <= config.tableSize.height ) {
        config.position.y = config.position.y > 0 ? config.position.y - 1 : 0;
    }
    if ( config.position.direction === 'WEST' && config.position.x <= config.tableSize.width ) {
        config.position.x = config.position.x > 0 ? config.position.x - 1 : 0;
    }
}

function handleDirection(turn_direction, current_direction) {
    if ( turn_direction === 'LEFT' ) {
        if ( current_direction !== 'NORTH' ) {
            return config.direction.indexOf(current_direction) - 1;
        } else {
            return config.direction.length - 1;
        }
    }
    if ( turn_direction === 'RIGHT' ) {
        if ( current_direction !== 'WEST' ) {
            return config.direction.indexOf(current_direction) + 1;
        } else {
            return 0;
        }
    }
}

function turnBeefy(direction) {
    config.position.direction = config.direction[handleDirection(direction, config.position.direction)];
}

function getPositionString() {
    let position = Object.keys(config.position).map(function(key) { return config.position[key] });
    return position.join(',');
}

function reportPosition() {
    report.textContent = getPositionString();
}

form.addEventListener("submit", handleSubmit, false);

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).