-
Notifications
You must be signed in to change notification settings - Fork 0
/
griddraw.js
116 lines (70 loc) · 2.99 KB
/
griddraw.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var grid_1 = document.getElementById("grid-1");
var grid_2 = document.getElementById("grid-2");
var message = document.getElementById("message");
grid2_row = 0;
grid2_cels = 0;
response_index = 0;
var response = [];
document.addEventListener('keypress', logKey);
function logKey(e) {
console.log(e.code)
if (e.code === 'KeyN' || e.code === 'KeyB' || e.code === 'Space' || e.code === 'Enter') {
message.innerHTML = "";
if (e.code === 'KeyN') {
grid_2.rows[grid2_row].cells[grid2_cels].style.backgroundColor = "#252222";
if (grid_2.rows[grid2_row].cells.length - 1 > grid2_cels) {
grid2_cels++;
} else if (grid_2.rows.length - 1 > grid2_row) {
grid2_row++;
grid2_cels = 0;
}
}
if (e.code === 'KeyB') {
grid_2.rows[grid2_row].cells[grid2_cels].style.backgroundColor = "#e2e2e2";
if (grid_2.rows[grid2_row].cells.length - 1 > grid2_cels) {
grid2_cels++;
} else if (grid_2.rows.length - 1 > grid2_row) {
grid2_row++;
grid2_cels = 0;
}
}
if (e.code === 'Space') {
grid_2.rows[grid2_row].cells[grid2_cels].style.backgroundColor = "white";
if (grid_2.rows[grid2_row].cells.length - 1 >= grid2_cels && grid2_cels != 0) {
grid2_cels--;
} else if (grid2_cels == 0 && grid2_row != 0) {
grid2_row--;
grid2_cels = grid_2.rows[grid2_row].cells.length - 1;
}
response = [];
response_index = 0;
}
if (e.code === 'Enter') {
console.log("enter")
if (response.length == 0){
for (let i = 0; i < grid_1.rows.length; i++) {
for (let j = 0; j < grid_1.rows[i].cells.length; j++) {
grid_1.rows[i].cells[j].style.backgroundColor = "white";
}
}
}
for (let i = 0; i < grid_2.rows.length; i++) {
for (let j = 0; j < grid_2.rows[i].cells.length; j++) {
if (grid_2.rows[i].cells[j].style.backgroundColor == "rgb(226, 226, 226)"){
response.push("rgb(226, 226, 226)");
}else if (grid_2.rows[i].cells[j].style.backgroundColor == "rgb(37, 34, 34)"){
response.push("rgb(37, 34, 34)");
}
}
}
for (let i = 0; i < grid_1.rows.length; i++) {
for (let j = 0; j < grid_1.rows[i].cells.length; j++) {
grid_1.rows[i].cells[j].style.backgroundColor = response[response_index];
response_index ++;
}
}
}
} else {
message.innerHTML = "Solo se aceptan las letras B y N.";
}
}