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

Problem 7 #209

Open
uginuss opened this issue Feb 27, 2023 · 1 comment
Open

Problem 7 #209

uginuss opened this issue Feb 27, 2023 · 1 comment

Comments

@uginuss
Copy link

uginuss commented Feb 27, 2023

Uncaught TypeError: Cannot read properties of undefined (reading '7')
at r.getNodeAt (pathfinding-browser.min.js:1:4349)
at r.findPath (pathfinding-browser.min.js:1:7595)
at js.js:37:19
have such problem
How can I decide it?
Leave a code:
const lab = [
["1", "1", "1", "1", "1", "1", "1", "1", "1"],
["1", "1", "1", "1", "0", "1", "1", "1", "1"],
["1", "1", "0", "0", "0", "1", "1", "1", "1"],
["1", "1", "0", "1", "0", "1", "1", "1", "1"],
["1", "1", "1", "0", "0", "0", "0", "0", "1"],
["1", "1", "1", "1", "0", "1", "1", "1", "1"],
["1", "1", "1", "1", "0", "1", "1", "0", "1"],
["1", "1", "0", "0", "0", "0", "0", "0", "1"],
["1", "1", "1", "1", "1", "1", "0", "1", "1"]
];

// Определяем координаты входа и выхода из лабиринта
const startx = 5;
const starty = 2;
const exitx = 7
const exity = 9;
var width = 9;
var height = 9;

var grid = new PF.Grid(width, height);

for (var i = 0; i < lab.length; i++) {
for (var j = 0; j < lab[i].length; j++) {
if (lab[i][j]=="1") {
grid.setWalkableAt(j, i, false);
}
else{
grid.setWalkableAt(j, i, true);
};
}
}
console.log(grid);

var finder = new PF.AStarFinder();

37 line) var path = finder.findPath(startx, starty, exitx, exity, grid);

document.getElementById("decider").innerHTML = path;

@brean
Copy link

brean commented Feb 27, 2023

  1. you forgot a semicolon after extix
  2. the positions are indices, so you need to subtract 1
    With a small simplification of your loop it should look like this:
const lab = [
["1", "1", "1", "1", "1", "1", "1", "1", "1"],
["1", "1", "1", "1", "0", "1", "1", "1", "1"],
["1", "1", "0", "0", "0", "1", "1", "1", "1"],
["1", "1", "0", "1", "0", "1", "1", "1", "1"],
["1", "1", "1", "0", "0", "0", "0", "0", "1"],
["1", "1", "1", "1", "0", "1", "1", "1", "1"],
["1", "1", "1", "1", "0", "1", "1", "0", "1"],
["1", "1", "0", "0", "0", "0", "0", "0", "1"],
["1", "1", "1", "1", "1", "1", "0", "1", "1"]
];
const startx = 4;
const starty = 1;
const exitx = 6;
const exity = 8;
const width = 9;
const height = 9;
const grid = new PF.Grid(width, height);
for (let i = 0; i < lab.length; i++) {
    for (let j = 0; j < lab[i].length; j++) {
        grid.setWalkableAt(j, i, lab[i][j]!=="1");
    }
}
console.log(grid);
const finder = new PF.AStarFinder();
const path = finder.findPath(startx, starty, exitx, exity, grid);

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

2 participants