有问题。我正在制作填字游戏。我需要实现随机级别生成。级别现在看起来是静态的。我该如何实现呢?请帮帮我... 现在水平看起来像这样 -
var rooms = {
board: [
[ 'U', 'C', 'W', 'G', 'U', 'B', 'T', 'Z', 'Y', 'K', 'C', 'T' ],
[ 'K', 'B', 'N', 'A', 'Z', 'B', 'U', 'J', 'L', 'I', 'D', 'J' ],
[ 'I', 'J', 'S', 'R', 'I', 'A', 'A', 'H', 'A', 'L', 'L', 'D' ],
[ 'T', 'L', 'B', 'A', 'C', 'T', 'P', 'V', 'U', 'H', 'T', 'R' ],
[ 'C', 'O', 'L', 'G', 'O', 'H', 'A', 'W', 'N', 'O', 'V', 'C' ],
[ 'H', 'U', 'A', 'E', 'U', 'R', 'R', 'V', 'D', 'U', 'I', 'N' ],
[ 'E', 'V', 'T', 'T', 'V', 'O', 'T', 'O', 'R', 'S', 'N', 'U' ],
[ 'N', 'E', 'W', 'N', 'W', 'O', 'M', 'F', 'Y', 'E', 'H', 'R' ],
[ 'A', 'B', 'A', 'S', 'E', 'M', 'E', 'N', 'T', 'U', 'U', 'S' ],
[ 'A', 'T', 'T', 'I', 'C', 'Y', 'N', 'Q', 'A', 'U', 'J', 'E' ],
[ 'O', 'C', 'U', 'Y', 'Y', 'E', 'T', 'X', 'C', 'Z', 'O', 'R' ],
[ 'B', 'E', 'D', 'R', 'O', 'O', 'M', 'X', 'M', 'Q', 'X', 'Y' ]
],
words: [
"HOUSE",
"BATHROOM",
"BEDROOM",
"KITCHEN",
"ATTIC",
"BASEMENT",
"APARTMENT",
"GARAGE",
"HALL",
"NURSERY",
"LAUNDRY",
]
};
可以帮助别人吗? 有游戏的构建 - http://rakapudl.github.io/fillword/
答案 0 :(得分:2)
有一个很棒的javascript库https://github.com/bunkat/wordfind,其中包含将来可能需要的许多功能。要解决您的问题,您只需要该库中的两个函数:
1)从给定的单词生成有效表
2)获得解决方案,以便人们可以找到他们没有找到的单词。
为了生成表格,您需要更改打印功能,使其如下所示:
populate: function (puzzle) {
let table = [];
for (var i = 0, height = puzzle.length; i < height; i++) {
var row = puzzle[i];
let rowUpperCase = row.map((letter) => {
return letter.toUpperCase();
});
table.push(rowUpperCase);
}
return table;
}
我们需要将每个字母大写,因为它们默认为小写。
要获得解决方案,我们还需要为您的案例采用求解方法。
solve: function (puzzle, words) {
var options = {
height: puzzle.length,
width: puzzle[0].length,
orientations: allOrientations,
preferOverlap: true
},
found = [];
for(var i = 0, len = words.length; i < len; i++) {
var word = words[i],
locations = findBestLocations(puzzle, options, word);
if (locations.length > 0 && locations[0].overlap === word.length) {
locations[0].word = word;
if (locations[0].orientation == 'vertical') {
found.push(
locations[0].x,
locations[0].y,
locations[0].x,
locations[0].y+locations[0].word.length -1
);
}
if (locations[0].orientation == 'horizontal') {
found.push(
locations[0].x,
locations[0].y,
locations[0].x +locations[0].word.length -1 ,
locations[0].y
);
}
}
}
return found;
}
所以现在你可以让你像这样生成表格
table = populate(words);
解决方案将是
solve = solve(puzzle, words);
就是这样!