-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (74 loc) · 2.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
document.addEventListener('DOMContentLoaded', function () {
var gameArea = document.getElementById('gameArea');
var rowsInput = document.getElementById('rowsInput');
var columnsInput = document.getElementById('columnsInput');
var rowsSpan = document.getElementById('rowsSpan');
var columnsSpan = document.getElementById('columnsSpan');
var tileSize = 50;
var store = Redux.createStore(slidingGame);
function moveTile() {
var tileState = JSON.parse(this.dataset.state);
store.dispatch({
type: 'MOVE',
i: tileState.i,
j: tileState.j
});
}
function render() {
var state = store.getState(),
rows = state.length,
columns = state[0].length,
i = 0,
j = 0,
tile,
value
;
gameArea.innerHTML = '';
gameArea.style.width = tileSize * columns + 'px';
gameArea.style.height = tileSize * rows + 'px';
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
value = state[i][j];
tile = document.createElement('div');
tile.classList.add('tile');
if (value === null) {
tile.classList.add('empty');
} else {
tile.textContent = value;
}
gameArea.appendChild(tile);
tile.dataset.state = JSON.stringify({ i: i, j: j, value: value });
tile.onclick = moveTile;
}
}
}
render();
store.subscribe(render);
document.getElementById('resetButton').onclick = function () {
store.dispatch({
type: 'RESET',
rows: +rowsInput.value,
columns: +columnsInput.value
});
gameArea.focus();
};
document.getElementById('shuffleButton').onclick = function () {
store.dispatch({
type: 'SHUFFLE'
});
gameArea.focus();
};
rowsInput.oninput = function () {
rowsSpan.textContent = this.value;
};
columnsInput.oninput = function () {
columnsSpan.textContent = this.value;
};
gameArea.addEventListener('keydown', function (e) {
store.dispatch({
type: 'KEY_MOVE',
key: e.key
});
});
gameArea.focus();
});