Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
黛琳ghz
2048
提交
dca5207f
2048
项目概览
黛琳ghz
/
2048
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
2048
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
dca5207f
编写于
3月 08, 2014
作者:
G
Gabriele Cirulli
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
extract the grid to its own class
上级
cf31e146
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
69 addition
and
52 deletion
+69
-52
index.html
index.html
+1
-0
js/game_manager.js
js/game_manager.js
+9
-50
js/grid.js
js/grid.js
+57
-0
js/html_actuator.js
js/html_actuator.js
+2
-2
未找到文件。
index.html
浏览文件 @
dca5207f
...
...
@@ -7,6 +7,7 @@
<link
href=
"style/main.css"
rel=
"stylesheet"
type=
"text/css"
>
<script
src=
"js/html_actuator.js"
></script>
<script
src=
"js/grid.js"
></script>
<script
src=
"js/tile.js"
></script>
<script
src=
"js/game_manager.js"
></script>
<script
src=
"js/application.js"
></script>
...
...
js/game_manager.js
浏览文件 @
dca5207f
...
...
@@ -3,81 +3,40 @@ function GameManager(size, actuator) {
this
.
actuator
=
actuator
;
this
.
startTiles
=
2
;
this
.
grid
=
[]
;
this
.
grid
=
new
Grid
(
this
.
size
)
;
this
.
setup
();
}
// Set up the game
GameManager
.
prototype
.
setup
=
function
()
{
this
.
buildGrid
();
this
.
addStartTiles
();
// Update the actuator
this
.
update
();
};
// Build a grid of the specified size
GameManager
.
prototype
.
buildGrid
=
function
()
{
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
this
.
grid
[
y
]
=
[];
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
this
.
grid
[
y
].
push
(
null
);
}
}
this
.
actuate
();
};
// Set up the initial tiles to start the game with
GameManager
.
prototype
.
addStartTiles
=
function
()
{
for
(
var
i
=
0
;
i
<
this
.
startTiles
;
i
++
)
{
this
.
addTile
();
this
.
add
Random
Tile
();
}
};
// Adds a tile in a random position
GameManager
.
prototype
.
addTile
=
function
()
{
this
.
insertTile
(
new
Tile
(
this
.
randomCell
()));
};
// Find the first available random position
GameManager
.
prototype
.
randomCell
=
function
()
{
// TODO: build a map of available positions and choose from it
var
self
=
this
;
var
position
;
function
randomPosition
()
{
return
Math
.
floor
(
Math
.
random
()
*
self
.
size
);
}
do
{
position
=
{
x
:
randomPosition
(),
y
:
randomPosition
()
};
}
while
(
this
.
cellOccupied
(
position
));
return
position
;
};
// Check if the specified cell is taken
GameManager
.
prototype
.
cellOccupied
=
function
(
cell
)
{
return
!!
this
.
grid
[
cell
.
x
][
cell
.
y
];
};
// Insert a tile at the specified position
GameManager
.
prototype
.
insertTile
=
function
(
tile
)
{
this
.
grid
[
tile
.
x
][
tile
.
y
]
=
tile
;
GameManager
.
prototype
.
addRandomTile
=
function
()
{
var
tile
=
new
Tile
(
this
.
grid
.
randomAvailableCell
());
this
.
grid
.
insertTile
(
tile
);
};
// Sends the updated grid to the actuator
GameManager
.
prototype
.
upd
ate
=
function
()
{
this
.
actuator
.
upd
ate
(
this
.
grid
);
GameManager
.
prototype
.
actu
ate
=
function
()
{
this
.
actuator
.
actu
ate
(
this
.
grid
);
};
// Move the grid in the specified direction
GameManager
.
prototype
.
move
=
function
(
direction
)
{
// 0: up, 1: right, 2:down, 3: left
this
.
upd
ate
();
this
.
actu
ate
();
};
js/grid.js
0 → 100644
浏览文件 @
dca5207f
function
Grid
(
size
)
{
this
.
size
=
size
;
this
.
cells
=
[];
this
.
build
();
}
// Build a grid of the specified size
Grid
.
prototype
.
build
=
function
()
{
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
var
row
=
this
.
cells
[
x
]
=
[];
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
row
.
push
(
null
);
}
}
};
// Find the first available random position
Grid
.
prototype
.
randomAvailableCell
=
function
()
{
var
cells
=
this
.
availableCells
();
if
(
cells
.
length
)
{
return
cells
[
Math
.
floor
(
Math
.
random
()
*
cells
.
length
)];
}
};
Grid
.
prototype
.
availableCells
=
function
()
{
var
cells
=
[];
for
(
var
x
=
0
;
x
<
this
.
size
;
x
++
)
{
for
(
var
y
=
0
;
y
<
this
.
size
;
y
++
)
{
var
cell
=
{
x
:
x
,
y
:
y
};
if
(
this
.
cellAvailable
(
cell
))
{
cells
.
push
(
cell
);
}
}
}
return
cells
;
};
// Check if the specified cell is taken
Grid
.
prototype
.
cellAvailable
=
function
(
cell
)
{
return
!
this
.
cellOccupied
(
cell
);
};
Grid
.
prototype
.
cellOccupied
=
function
(
cell
)
{
return
!!
this
.
cells
[
cell
.
x
][
cell
.
y
];
};
// Inserts a tile at its position
Grid
.
prototype
.
insertTile
=
function
(
tile
)
{
this
.
cells
[
tile
.
x
][
tile
.
y
]
=
tile
;
};
js/html_actuator.js
浏览文件 @
dca5207f
...
...
@@ -2,9 +2,9 @@ function HTMLActuator() {
}
HTMLActuator
.
prototype
.
upd
ate
=
function
(
grid
)
{
HTMLActuator
.
prototype
.
actu
ate
=
function
(
grid
)
{
// Temporary debug visualizer
grid
.
forEach
(
function
(
row
)
{
grid
.
cells
.
forEach
(
function
(
row
)
{
var
mapped
=
row
.
map
(
function
(
tile
)
{
return
tile
?
tile
.
value
:
"
"
;
}).
join
(
"
|
"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录