【发布时间】:2018-05-09 16:40:07
【问题描述】:
例如,当输入为“y”时,它将打印: the output looks like this. 我使用前端语言来展示示例。我刚刚将嵌套数组生成为 9*9 网格来模拟输出。然后使用 for 循环将它们更改为 HTML 语法。在这种情况下,无论我使用什么语言,关键是算法。我认为我的算法效率不高,因为我必须为 26 个字母生成 26 个嵌套数组。除了创建 26 个嵌套数组之外,有没有什么有效的方法来解决这个问题?
$(document).ready(function(){
var letter_y=[
[0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,1,0],
[0,1,0,0,0,0,0,1,0],
[0,1,1,1,1,1,1,1,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
]
for(var x=0;x<letter_y.length;x++){
for(var j=0;j<letter_y[x].length;j++){
if(letter_y[x][j]===0)
$('.row').append('<div class="grid"></div>');
if(letter_y[x][j]===1)
$('.row').append('<div class="grid2"></div>');
}
$('.row').append('<br>');
}
});
.grid{
background-color: black;
width: 20px;
height: 20px;
display: inline-block;
margin:0px;
}
.row{
line-height: 0px;
}
.grid2{
background-color: blue;
width: 20px;
height: 20px;
display: inline-block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fill_me_in</title>
<meta name="description" content="fill_me_in" >
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css " rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav id="nav_top" class="navbar navbar-inverse navbar-static-top">
<div class="container" id="main_container">
<a class="navbar-brand" href="http://api.jquery.com">Pac_man</a>
</div>
</nav>
<div class="container">
<div class="row">
</div>
</div>
<div class="own"></div>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script src="application.js"></script>
</body>
</html>
【问题讨论】:
-
为什么找不到合适的字体?然后,您可以使用字体大小。您还可以使用 this one 之类的库
-
感谢您的帮助。我的目标是创建一个 PAC-MAN 游戏,如下所示:google.com/… 我想显示我的名字而不是 google 徽标。
标签: javascript arrays algorithm for-loop pacman