雄辩的Javascript中的Terrarium代码不起作用

杜希安特

这是《 Eloquent Javascript》一书中的代码。它创建了一个称为Terrarium的2D对象,昆虫可以在其中移动。使用function:terrarium.step可以移动昆虫。但是当我运行它时,它说“中心未定义”。center是函数的参数:Terrarium.prototype.listSurroundings。该函数调用另一个函数,该函数使用center方法作为参数。有什么事吗 我在这里附上整个代码,包括玻璃容器的初始化等。

function forEach(array,action) {
    for(var i=0;i<array.length;i++)
    {action(array[i]);}
}

function forEachIn(object, action) {
    for (var property in object) {
        if (Object.prototype.hasOwnProperty.call(object, property))
        action(property, object[property]);
    }
}

function Dictionary(startValues) {
    this.values = startValues || {};
}
Dictionary.prototype.store = function(name, value) {
    this.values[name] = value;
};
Dictionary.prototype.lookup = function(name) {
    return this.values[name];
};

Dictionary.prototype.contains = function(name) {
    return Object.prototype.propertyIsEnumerable.call(this.values, name);
};
Dictionary.prototype.each = function(action) {
    forEachIn(this.values, action);
};

function Point(x, y) {
    this.x = x;
    this.y = y;
}
Point.prototype.add = function(other) {
    return new Point(this.x + other.x, this.y + other.y);
};

function Grid(width, height) {
    this.width = width;
    this.height = height;
    this.cells = new Array(width * height);
}
Grid.prototype.valueAt = function(point) {
    return this.cells[point.y * this.width + point.x];
};
Grid.prototype.setValueAt = function(point, value) {
    this.cells[point.y * this.width + point.x] = value;
};
Grid.prototype.isInside = function(point) {
    return point.x >= 0 && point.y >= 0 &&
    point.x < this.width && point.y < this.height;
};
Grid.prototype.moveValue = function(from, to) {
    this.setValueAt(to, this.valueAt(from));
    this.setValueAt(from, undefined);
};

Grid.prototype.each = function(action) {
    for (var y = 0; y < this.height; y++) {
        for (var x = 0; x < this.width; x++) {
            var point = new Point(x, y);
            action(point, this.valueAt(point));
        }
    }
};

var directions = new Dictionary(
{"n": new Point( 0, -1),
 "ne": new Point( 1, -1),
 "e": new Point( 1, 0),
 "se": new Point( 1, 1),
 "s": new Point( 0, 1),
 "sw": new Point(-1, 1),
 "w": new Point(-1, 0),
 "nw": new Point(-1, -1)});

function StupidBug() {};
StupidBug.prototype.act = function(surroundings) {
    return {type: "move", direction: "s"};
};

var wall = {};

function elementFromCharacter(character) {
    if (character == " ")
        return undefined;
    else if (character == "#")
        return wall;
    else if (character == "o")
        return new StupidBug();
}

function Terrarium(plan) {
    var grid = new Grid(plan[0].length, plan.length);
    for (var y = 0; y < plan.length; y++) {
        var line = plan[y];
        for (var x = 0; x < line.length; x++) {
            grid.setValueAt(new Point(x, y), elementFromCharacter(line.charAt(x)));
        }
    }
    this.grid = grid;
}

wall.character = "#";
StupidBug.prototype.character = "o";
function characterFromElement(element) {
    if (element == undefined)
        return " ";
    else
    return element.character;
}

Terrarium.prototype.toString = function() {
    var characters = [];
    var endOfLine = this.grid.width - 1;
    this.grid.each(function(point, value) {
        characters.push(characterFromElement(value));
        if (point.x == endOfLine)
        characters.push("\n");
    });
    return characters.join("");
};

function bind(func, object) {
    return function(){
        return func.apply(object, arguments);
    };
}

function method(object, name) {
    return function() {
        object[name].apply(object, arguments);
    };
}

Terrarium.prototype.listActingCreatures = function() {
    var found = [];
    this.grid.each(function(point, value) {
        if (value != undefined && value.act)
        found.push({object: value, point: point});
    });
    return found;
};

Terrarium.prototype.listSurroundings = function(center) {
    var result = {};
    var grid = this.grid;
    directions.each(function(name, direction) {
        var place = center.add(direction);
        if (grid.isInside(place))
        result[name] = characterFromElement(grid.valueAt(place));
        else result[name] = "#";
    });
    return result;
};

Terrarium.prototype.processCreature = function(creature, point) {
    var action = creature.act(this.listSurroundings(point));
    if (action.type == "move" && directions.contains(action.direction)) {
        var to = point.add(directions.lookup(action.direction));
        if (this.grid.isInside(to) && this.grid.valueAt(to) == undefined)
            this.grid.moveValue(point, to);
    }
    else {
        throw new Error("Unsupported action: " + action.type);
    }
};

Terrarium.prototype.step = function() {
    forEach(this.listActingCreatures(), bind(this.processCreature, this));
};

var thePlan =
["############################",
 "#      #    #      o      ##",
 "#                          #",
 "#          #####           #",
 "##         #   #    ##     #",
 "###           ##     #     #",
 "#           ###      #     #",
 "#  ####                    #",
 "#  ##        o             #",
 "# o #          o       ### #",
 "#   #                      #",
 "############################"]
var terrarium = new Terrarium(thePlan);
terrarium.step();
alert(terrarium);
编码器

我对您的processCreature方法进行了一些更改:

Terrarium.prototype.processCreature = function(creature) {
  var action = creature.object.act(this.listSurroundings(creature.point));
  if (action.type == "move" && directions.contains(action.direction)) {
    var to = creature.point.add(directions.lookup(action.direction));
    if (this.grid.isInside(to) && this.grid.valueAt(to) == undefined)
      this.grid.moveValue(creature.point, to);
  }
  else {
    throw new Error("Unsupported action: " + action.type);
  }
};

我在Firefox中执行了该程序,并且可以正常工作。我不确定它是否按预期运行,但会发出警报。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Javascript代码在Firefox中不起作用

来自分类Dev

php代码在javascript中不起作用

来自分类Dev

Ajax代码在JavaScript中不起作用

来自分类Dev

雄辩的关系在邮件中不起作用

来自分类Dev

雄辩的查询在Laravel中不起作用

来自分类Dev

雄辩的模型在Laravel中不起作用

来自分类Dev

代码不起作用(JavaScript)

来自分类Dev

javaScript代码不起作用

来自分类Dev

雄辩的迁移不起作用

来自分类Dev

简单的Javascript代码在Chrome和Safari中不起作用

来自分类Dev

显示div框在Firefox中不起作用的Javascript代码

来自分类Dev

JavaScript代码在IE8中不起作用

来自分类Dev

javascript代码在IE8中不起作用

来自分类Dev

为什么此C代码在JavaScript中不起作用?

来自分类Dev

Javascript代码在html下拉选项元素中不起作用

来自分类Dev

Javascript 代码在 Ruby on Rails 中不起作用

来自分类Dev

幻灯片代码在 Javascript 中不起作用

来自分类Dev

此 javascript 代码在 Internet Explorer 11 中不起作用

来自分类Dev

Javascript 导入在 Visual Studio 代码中不起作用

来自分类Dev

getPaginationCount()函数在Laravel的雄辩ORM中不起作用

来自分类Dev

以下简单的Javascript代码不起作用

来自分类Dev

TaxCalculator函数Javascript代码不起作用

来自分类Dev

JavaScript代码在foreach内部不起作用

来自分类Dev

简单的JavaScript代码不起作用

来自分类Dev

JAVASCRIPT帮助-代码不起作用

来自分类Dev

JavaScript代码段不起作用

来自分类Dev

JavaScript 画布代码不起作用

来自分类Dev

Javascript 代码不起作用。为什么?

来自分类Dev

代码在jsfiddle中不起作用