[英]Javascript board game - Finding possible places on board
I am developing HTML5 boardgame with Javascript. 我正在使用Javascript开发HTML5桌面游戏。
How I can find ( recursively ) where I can get with dice number I get? 我如何能够( 递归地 )找到我能得到的骰子数量?
Example: I get 4 from dice and my position is 11 . 例如:我从骰子得到4 ,我的位置是11 。 Possible places are 22 , 15 and 7 . 可能的地方是22,15和7。
I have tried this; 我试过这个; it works great, but returns wrong numbers into console: 它工作得很好,但在控制台中返回错误的数字:
$(function(){
// Initialize
var pos = 11;
var dice = 4;
var diceMax = 4;
var postPlaces = [];
var places = [];
// List of gameboard's numbers: from where to where
numbers = {
1 : [25,21,2],
2 : [1,3],
3 : [2,4],
4 : [3,5],
5 : [4,6],
6 : [5,19,7],
7 : [6,8],
8 : [7,9],
9 : [10,8],
10 : [11,9],
11 : [10,12],
12 : [11,13],
13 : [12,14],
14 : [13,15,22],
15 : [14,16],
16 : [15,17],
17 : [16,20,18],
18 : [17,19],
19 : [18,6],
20 : [17,21],
21 : [1,20],
22 : [14,23],
23 : [22,24],
24 : [23,25],
25 : [24,1]
};
// List all numbers where is possible to go with dice number we got
function findPlaces(number){
dice -= 1;
$.each(numbers[number],function(i,v){
if(postPlaces.indexOf(v) == -1){
postPlaces.push(v);
if(dice>0){
findPlaces(v);
}else{
places.push(v);
}
}
});
dice = diceMax;
}
// When the button get pressed
$("button").click(function(){
postPlaces = [];
places = [];
dice = diceMax;
findPlaces(pos);
console.log(places);
});
});
there are few bugs in the findPlaces() - the following code should work, see comments findPlaces()中的bug很少 - 以下代码应该可以使用,请参阅注释
function findPlaces(number){
dice -= 1;
// moved here so that initial number (11) gets
// stored to postPlaces and we do not step on that number again
postPlaces.push(number);
$.each(numbers[number],function(i,v){
if(postPlaces.indexOf(v) == -1){
if(dice>0){
findPlaces(v);
}else{
places.push(v);
}
}
});
// we are going back one level higher,
// must remove the 'number' from postPlaces
// AND increase the dice (number of steps to go)
postPlaces.pop();
dice += 1;
}
and of course the 13 number neighbours are wrong as @plaix pointed out 当然,正如@plaix指出的那样,13个邻居是错误的
For each adjacent position, look at each adjacent position, except the previous position. 对于每个相邻位置,查看除先前位置之外的每个相邻位置。
var move = (function () {
var adj = {
1: [25, 21, 2], 2: [ 1, 3] , 3: [ 2, 4], 4: [ 3, 5] , 5: [ 4, 6],
6: [ 5, 19, 7], 7: [ 6, 8] , 8: [ 7, 9], 9: [10, 8] , 10: [11, 9],
11: [10, 12] , 12: [11, 13] , 13: [12, 14], 14: [13, 15, 22], 15: [14, 16],
16: [15, 17] , 17: [16, 20, 18], 18: [17, 19], 19: [18, 6] , 20: [17, 21],
21: [ 1, 20] , 22: [14, 23] , 23: [22, 24], 24: [23, 25] , 25: [24, 1]
};
function move(current, steps_remaining, prev) {
var arr = [], i = adj[current].length;
prev || (prev = 0); // if there wasn't a previous position, choose impossible
if (steps_remaining) { // if we can still move
while (i--) // for each adj tile
if (adj[current][i] !== prev) // that isn't where we just came from
arr = arr.concat( // concat together
move(adj[current][i], steps_remaining - 1, current)
); // recursion from adjacent tiles
return arr; // return recursion
}
// else return current tile
return [current];
}
return move;
}());
Then 然后
move(11, 4); // [22, 15, 7]
Added bonus: you didn't need any extra global variables 额外奖励:您不需要任何额外的全局变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.