-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
228 lines (225 loc) · 7.31 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>パズルゲーム</title>
<style>
.p{
position:absolute;
background:aqua;
width:30px;
height:30px;
text-align: center;
cursor:default;
}
.field{
position: absolute;
background-color:bisque;
left:0px;
top:0px;
width:145px;
height:145px;
}
.text{
position: absolute;
left:150px;
top:0px;
line-height: 2px;
}
.space{
position:absolute;
left:0;
top:145px;
width:100%;
}
</style>
<script>
let pieces;
let field;
let ctrl;
let air;
let cnt;
let time;
class piece{
constructor(num){
this.x = 0;
this.y = 0;
this.num = num;
if(!num){
return;
}
let p = document.createElement("div");
p.setAttribute("class","p");
let id = "b" + num;
p.setAttribute("id",id);
p.innerText = num;
this.touchair = false;
field.appendChild(p);
this.e = document.getElementById(id);
this.e.onclick=e=>{
detecttouch();
if(this.touchair){
let ox = this.x;
let oy = this.y;
this.setpos(air.x,air.y);
air.setpos(ox,oy);
}
}
}
setpos(x,y){
this.x = x;
this.y = y;
if(this.e){
this.e.style.left = 5+x*35+"px";
this.e.style.top = 5+y*35+"px";
}
}
}
function detecttouch(){
let d = [45,45,45,45];
let x = air.x;
let y = air.y;
for(let i = 0;i < 15;i++){
pieces[i].touchair = false;
let px = pieces[i].x;
let py = pieces[i].y;
if(x+1 == px || x-1 == px){
if(y == py){
pieces[i].touchair = true;
if(x+1 == px) d[3] = i;
if(x-1 == px) d[1] = i;
}
}
if(y+1 == py || y-1 == py){
if(x == px){
pieces[i].touchair = true;
if(y+1 == py) d[2] = i;
if(y-1 == py) d[0] = i;
}
}
}
return d;
}
function downkey(e){
if(cnt == 0){
memhand();
}
switch(e.key){
case 'w':
case 'ArrowUp':
ctrl="↑";
if(detecttouch()[0]!=45){
pieces[detecttouch()[0]].e.click();
cnt++;
}
break;
case 'a':
case 'ArrowLeft':
ctrl="←";
if(detecttouch()[1]!=45){
pieces[detecttouch()[1]].e.click();
cnt++;
}
break;
case 's':
case 'ArrowDown':
ctrl="↓";
if(detecttouch()[2]!=45){
pieces[detecttouch()[2]].e.click();
cnt++;
}
break;
case 'd':
case 'ArrowRight':
ctrl="→";
if(detecttouch()[3]!=45){
pieces[detecttouch()[3]].e.click();
cnt++;
}
break;
}
document.querySelector("#cnt").innerText = cnt;
document.querySelector("#ctrl").innerText = ctrl;
}
function load(){
document.onselectstart=()=>false;
field = document.querySelector(".field");
document.addEventListener('keydown',downkey);
document.addEventListener('keyup',e=>{
ctrl="";
document.querySelector("#ctrl").innerText = ctrl;
});
solve();
}
function scramble(){
let k = ['w','a','s','d'];
for(let i = 0;i < 1000;++i) {
let c = k[Math.floor(Math.random() * 1000)%4];
let o = {
key: c
}
downkey(o);
}
ctrl = 0;
cnt=0;
time = 0;
document.querySelector("#cnt").innerText = 0;
document.querySelector("#time").innerText = 0;
}
function memhand(){
document.querySelector("#time").innerText = time / 100;
time++;
if(pieces[0].x == 0 && pieces[0].y == 0){
if(pieces[1].x == 1 && pieces[1].y == 0){
if(pieces[2].x == 2 && pieces[2].y == 0){
if(pieces[3].x == 3 && pieces[3].y == 0){
if(pieces[4].x == 0 && pieces[4].y == 1){
if(pieces[5].x == 1 && pieces[5].y == 1){
if(pieces[6].x == 2 && pieces[6].y == 1){
if(pieces[7].x == 3 && pieces[7].y == 1){
if(pieces[8].x == 0 && pieces[8].y == 2){
if(pieces[9].x == 1 && pieces[9].y == 2){
if(pieces[10].x == 2 && pieces[10].y == 2){
if(pieces[11].x == 3 && pieces[11].y == 2){
if(pieces[12].x == 0 && pieces[12].y == 3){
if(pieces[13].x == 1 && pieces[13].y == 3){
if(pieces[14].x == 2 && pieces[14].y == 3){
alert("Clear!");
return;
}}}}}}}}}}}}}}}
setTimeout(memhand,10);
}
function solve(){
document.querySelectorAll(".p").forEach((o)=>{o.remove()});
pieces = Array(15);
air = new piece(0);
air.setpos(3,3);
for(let i = 0;i < 15;++i)pieces[i] = new piece(i+1);
let x = 0;
for(let i = 0;i < 4;++i) for(let j = 0;j < 4;++j) {
if(x >= 15) break;
pieces[x].setpos(j,i);
x++;
}
}
</script>
</head>
<body onload="load()">
<div class="field"></div>
<div class="text">
<p>パズルゲーム!</p><br>
<p>操作:矢印キーとWASDキーか</p>
<p>動かせるパズルをタッチ</p>
<br>
<br>
<button onclick="scramble()">パズルを混ぜる</button><br>
<button onclick="solve()">パズルを揃える</button>
</div>
<div class="space">
<p>操作:<span id="ctrl"></span></p>
<p>回数:<span id="cnt"></span></p>
<p>タイム:<span id="time"></span></p>
</div>
</body>
</html>