我正在尝试创建一个html Sudoku游戏板,根据php $_GET['level']
和$_GET['puzzle']
值加载不同的拼图。 Php变量$get_level
和$get_puzzle
设置为$_GET
值,因此可以在javascript中使用。
<?php
$default_level = 'medium';
$default_puzzle = 1;
$number_of_puzzles_per_level = 10;
if (isset($_GET['level']) && isset($_GET['puzzle'])) {
if ($_GET['level'] == 'easy' || $_GET['level'] == 'medium' || $_GET['level'] == 'hard') {
$get_level = $_GET['level'];
} else {
$get_level = $default_level;
}
if (is_int($_GET['puzzle'])) {
if ($_GET['puzzle'] > 0 && $_GET['puzzle'] <= $number_of_puzzles_per_level) {
$get_puzzle = $_GET['puzzle'];
} else {
$get_puzzle = $default_puzzle;
}
} else {
$get_puzzle = $default_puzzle;
}
} else {
$get_level = $default_level;
$get_puzzle = $default_puzzle;
}
?>
在javascript中,我有一堆数组:
level_easy_puzzle_1 = [[0, 0, 0, 7, 0, 3, 0, 0, 0], [0, 2, 3, 5, 0, 0, 0, 0, 3], [0, 0, 0, 0, 4, 0, 0, 0, 9], [1, 0, 7, 0, 0, 0, 0, 4, 0], [0, 4, 0, 3, 0, 9, 0, 8, 0], [0, 8, 0, 0, 0, 0, 5, 0, 1], [7, 0, 0, 0, 6, 0, 0, 0, 0], [8, 0, 0, 0, 0, 1, 2, 5, 0], [0, 0, 0, 2, 0, 4, 0, 0, 0]];
//I will fill out the rest of the puzzles later
level_easy_puzzle_2 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_3 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_4 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_5 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_6 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_7 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_8 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_9 = [[], [], [], [], [], [], [], [], []];
level_easy_puzzle_10 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_1 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_2 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_3 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_4 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_5 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_6 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_7 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_8 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_9 = [[], [], [], [], [], [], [], [], []];
level_medium_puzzle_10 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_1 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_2 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_3 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_4 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_5 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_6 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_7 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_8 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_9 = [[], [], [], [], [], [], [], [], []];
level_hard_puzzle_10 = [[], [], [], [], [], [], [], [], []];
然后,我希望将javascript变量puzzle
设置为上面的一个拼图数组,具体取决于以下变量get_level
和get_puzzle
的值。
var get_level = '<?php echo $get_level; ?>';
var get_puzzle = <?php echo $get_puzzle; ?>;
因此,如果get_level
等于'hard'
且get_puzzle
等于3
,我希望变量puzzle
等于数组的内容{{1} }。
level_hard_puzzle_3
希望很清楚我想要什么,并感谢您的帮助。
答案 0 :(得分:2)
您可以使用eval
Javascript功能。
var puzzle = eval('level_' + get_level + '_puzzle_' + get_puzzle);
这将为您提供拼图。
答案 1 :(得分:2)
您可以将其组织为对象。另外,让数组索引处理您的编号,而不是将数字分配给变量名称。 (如果您决定删除拼图,则必须重命名所有拼图。)
var puzzles = {
easy: [
[//Puzzle 0
[0, 0, 0, 7, 0, 3, 0, 0, 0],
[0, 2, 3, 5, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 4, 0, 0, 0, 9],
[1, 0, 7, 0, 0, 0, 0, 4, 0],
[0, 4, 0, 3, 0, 9, 0, 8, 0],
[0, 8, 0, 0, 0, 0, 5, 0, 1],
[7, 0, 0, 0, 6, 0, 0, 0, 0],
[8, 0, 0, 0, 0, 1, 2, 5, 0],
[0, 0, 0, 2, 0, 4, 0, 0, 0]
],
[//Puzzle 1
[0, 0, 0, 7, 0, 3, 0, 0, 0],
[0, 2, 3, 5, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 4, 0, 0, 0, 9],
[1, 0, 7, 0, 0, 0, 0, 4, 0],
[0, 4, 0, 3, 0, 9, 0, 8, 0],
[0, 8, 0, 0, 0, 0, 5, 0, 1],
[7, 0, 0, 0, 6, 0, 0, 0, 0],
[8, 0, 0, 0, 0, 1, 2, 5, 0],
[0, 0, 0, 2, 0, 4, 0, 0, 0]
]
], //end of the easy puzzles
medium: [], //...and so on...
hard: [],
}
我建议制作一个功能来检索谜题,这样你就可以在一个地方整齐地控制你的逻辑:
function getPuzzle(difficulty, level){
if(puzzles[difficulty] && puzzles[difficulty].length >= level -1){
return puzzles[difficulty][level];
}else{
console.warn("That puzzle doesn't exist!");
return;
}
}
我们现在可以执行类似var puzzle = getPuzzle('hard', 2)
之类的操作,但让我们构建它并创建一个可以获得随机拼图的函数。
function getRandomPuzzle(level) {
return getPuzzle(level, Math.floor(Math.random() * (puzzles[level].length)));
}
var puzzle = getRandomPuzzle('hard');