-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife - Figuras.html
154 lines (123 loc) · 3.69 KB
/
gameoflife - Figuras.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en" style="background-color:#8F7291;">
<head>
<meta charset="utf-8"/>
<style>
.cuadrado{
background-color: #ffffff;
position: absolute;
}
</style>
<script>
window.addEventListener("load", inicio, false);
var cuadraditos = new Object();
var pausa = true;
var tamano = 10;
var cPrincipal;
var principal = false;
class Cuadrado{
constructor(posX, posY){
this.posX = posX;
this.posY = posY;
this.id = this.posX+"|"+this.posY;
this.cuadradoDiv = document.createElement("div");
this.cuadradoDiv.style.width= tamano+"px";
this.cuadradoDiv.style.height= tamano+"px";
this.cuadradoDiv.classList.add("cuadrado");
this.cuadradoDiv.style.top=this.posY*tamano +"px";
this.cuadradoDiv.style.left=this.posX*tamano +"px";
document.body.appendChild(this.cuadradoDiv);
if(principal){
this.cuadradoDiv.style.background = "#000000";
cPrincipal = this;
}
}
matar(){
//console.log(this.cuadradoDiv, "muere");
this.cuadradoDiv.remove();
delete cuadraditos[this.id];
}
}
class Compro{
constructor(posX, posY){
this.posX = posX;
this.posY = posY;
this.id = this.posX+"|"+this.posY;
}
}
function click(event){
var x = event.clientX-8;
var y = event.clientY-8;
x = Math.floor(x/10);
y = Math.floor(y/10);
var w = window.innerWidth;
var h = window.innerHeight;
if(cuadraditos[x+"|"+y]){
cuadraditos[x+"|"+y].matar();
}else{
cuadraditos[x+"|"+y] = new Cuadrado(x,y);
}
}
function preguntar(){
console.log(cPrincipal.posX, cPrincipal.posY);
var msgPregunta = "if(";
var msgColor = "";
var msgPintado = "";
var listCompro = new Object();
for(var i in cuadraditos){
var c = cuadraditos[i];
//El mismo
if(!listCompro[c.posX+"|"+c.posY]){
listCompro[c.posX+"|"+c.posY] = new Compro(c.posX, c.posY);
}
var numeritos = [-1, 0, 1];
for (const j in numeritos){
a=numeritos[j];
for (const k in numeritos){
b=numeritos[k];
if(a != 0 || b !=0){
if(!listCompro[(c.posX+a)+"|"+(c.posY+b)]){
listCompro[(c.posX+a)+"|"+(c.posY+b)] = new Compro(c.posX+a, c.posY+b);
}
}
}
}
}
for(var i in listCompro){
var cId = listCompro[i].id;
var cX = listCompro[i].posX;
var cY = listCompro[i].posY;
if(cuadraditos[cId]){
msgPregunta += "cuadraditos[(x+"+(cX-cPrincipal.posX)+")+\"|\"+(y+"+(cY-cPrincipal.posY)+")] && ";
msgPintado += "cuadraditos[(x+"+(cX-cPrincipal.posX)+")+\"|\"+(y+"+(cY-cPrincipal.posY)+")].pintado = true;\n";
msgColor += "cuadraditos[(x+"+(cX-cPrincipal.posX)+")+\"|\"+(y+"+(cY-cPrincipal.posY)+")].cuadradoDiv.style.background = \n";
}else{
msgPregunta += "!cuadraditos[(x+"+(cX-cPrincipal.posX)+")+\"|\"+(y+"+(cY-cPrincipal.posY)+")] && ";
}
}
msgPregunta += "){\n"+msgColor+"\n\n"+msgPintado+"}";;
console.log(msgPregunta);
}
function prin(){
if(principal){
principal = false;
}else{
principal = true;
}
}
function inicio(){
document.addEventListener("click",click);
alert("JUEGO DE LA VIDA: \n pulsa click para crear la celulas y P para iniciar o pausar");
document.addEventListener('keydown', function(event) {
if (event.code == 'KeyP') {
preguntar();
}else if(event.code == 'KeyC'){
prin();
}
});
}
</script>
</head>
<body style="position: relative">
</body>
</html>