Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line number error [SPOILER] #295

Open
afluriach opened this issue Aug 13, 2014 · 8 comments
Open

Line number error [SPOILER] #295

afluriach opened this issue Aug 13, 2014 · 8 comments

Comments

@afluriach
Copy link

I'm getting syntax errors, but the error doesn't make sense for the given line number, and I'm pretty sure it is not a syntax error.

bug

@afluriach
Copy link
Author

Here is a similar error:

dfs-error

@afluriach afluriach changed the title Line number error Line number error [SPOILER] Aug 13, 2014
@afluriach
Copy link
Author

In fact, I'm pretty sure this solution is valid Javascript. Not sure why it is generating so many parse errors. Also, the parse errors seem inconsistent. Also, I did this solution to run once, but it appears to crash on me.move(). (difficult to debug though)

function Point(x,y)
{
    this.x = x;
    this.y = y;

    this.adj = function(dir)
    {
        switch(dir)
        {
            case 'left': return new Point(this.x-1, this.y);
            case 'right': return new Point(this.x+1, this.y);
            case 'up': return new Point(this.x, this.y-1);
            case 'down': return new Point(this.x, this.y+1);
        }
    };

    //get an integer that will uniquely represent this point
    this.hash = function()
    {
        return this.x + this.y*map.getWidth();
    };
}

function reverseDir(dir)
{
    switch(dir)
    {
        case 'left': return 'right';
        case 'right': return 'left';
        case 'up': return 'down';
        case 'down': return 'up';
    }
}

dirs = ['right', 'down', 'left', 'up'];

//the current best cost to get to this tile, and the direction we moved to get to it
function Tile(x,y,cost,fromDir)
{
    this.x = x;
    this.y = y;
    this.cost = cost;
    this.fromDir = fromDir;

    this.getPoint = function()
    {
        return new Point(this.x, this.y);
    };

    this.isAtPoint = function(point)
    {
        return this.x === point.x && this.y === point.y;
    };

    this.hash = function()
    {
        return this.x + this.y*map.getWidth();    
    };
}

var startTile = new Tile(1,1,0,'none');
var destPoint = new Point(map.getWidth() - 2, 8);
tiles = {};

tiles[startTile.hash()] = startTile;

function dfs(tile, dest)
{
    if(tile.hash() === dest.hash()) return;

    var crntCost = tile.cost;

    //update adjacent tiles as applicable and dfs on them
    for(var dirNum = 0; dirNum < dirs.length; ++dirNum)
    {
        var adjPoint = tile.getPoint().adj(dirs[dirNum]);
        var adjHash = adjPoint.hash();

        //if the tile is visitable and within map range
        //if the tile has not already been visited or the cost is lower
        var occupiable = map.getObjectTypeAt(adjPoint.x, adjPoint.y) !== 'block';
        var withinMapBounds = adjPoint.x >= 0 &&
                              adjPoint.x < map.getWidth() &&
                              adjPoint.y >= 0 &&
                              adjPoint.y < map.getHeight();
        var unvisitedOrBetter = !(adjHash in tiles) || tiles[adjHash].cost > crntCost + 1;
        if( occupiable && withinMapBounds && unvisitedOrBetter)
        {
            tiles[adjHash] = new Tile(adjPoint.x, adjPoint.y, crntCost+1, dirs[dirNum]);
            dfs(tiles[adjHash], dest);
        }
    }
}

//prepare an array of move instructions (move direction string). move backward from destination until we get to start.

//the node adjacent to the start node, cost 1, will represent the first move instruction. thus we know the dest nodes move instruction will be move instruction cost-1 and count down from there

function buildMoveList(destPoint)
{
    var crntTile = tiles[destPoint.hash()];
    var movelist = [];

    for(var inst = crntTile.cost -1; inst >= 0; --inst)
    {
        movelist[inst] = crntTile.fromDir;

        //move backwards
        crntTile = tiles[crntTile.getPoint.adj(reverseDir(crntTile.fromDir)).hash()];
    }

    return movelist;
}

//on first move:
//run DFS
//build move list

if(typeof moveInstList === 'undefined')
{
    dfs(startTile, destPoint);
    moveInstList = buildMoveList(destPoint);
    nextMoveInst = 0;
}

//iterate through move list
//then move down

if(nextMoveInst < moveInstList.length)
{
    me.move(moveInstList[nextMoveInst]);
}
else
{
    me.move('down');
}

@AlexNisnevich
Copy link
Owner

I'm taking a look at this now, but it may take some time to get to the bottom of this.

@Setitch
Copy link

Setitch commented Aug 14, 2014

It might be due to wrongly used curly brackets in new line. in functions, if's, for's and so on.

function name() {

not

function name() 
{

As this can make errors with silent semicolons (or however its called);

@r2vq
Copy link

r2vq commented Aug 14, 2014

From my understanding, Automatic Semicolon Insertion shouldn't occur during function, if, or for statements.

for (true) {

should evaluate the same as

for (true)
{

If he was doing this with do-while, continue, break, return, or throw statements, your case would be more valid.

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Source

@Setitch
Copy link

Setitch commented Aug 15, 2014

I know i had several problems with it, also with different browsers - from that time, i always use proper {} in this language so no errors of this kind would ever happend to me. And i also think, that if you can fix something, you should - then error you had can (at least) point to its proper place not some random ones.

@r2vq
Copy link

r2vq commented Aug 15, 2014

Placing the brace on a new line is called Allman Style and is a valid style of indenting as long as you are consistent and avoid ASI issues like I mentioned above.

Personally, I prefer OTBS but using Allman style should not create a syntax error.

More info on the "holy war" here.

@AlexNisnevich
Copy link
Owner

Your code is valid JavaScript, but it doesn't adhere to Untrusted's 80-character-per-line limit, so some lines get split up when you paste it into the game. This seems to be the cause of most of your problems.

For example, the [Line 62] error is actually caused by invalid syntax on line 67:

The line numbers in the errors are sometimes wrong, which is a problem. However, I'm not sure if there's a good way to fix this, because we're currently getting these line numbers in an extremely fragile way (since JavaScript itself does not provide line numbers for exceptions that occur when you eval a multi-line string).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants