This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper2.html
397 lines (372 loc) · 14.4 KB
/
minesweeper2.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- <link rel="stylesheet" href="style.css"> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Document</title>
<style>
table {
border-bottom: 1px solid #8F8C8F;
border-right: 1px solid #8F8C8F;
}
td.cell {
margin: 0;
padding: 0;
font-size: 5px;
text-align: center;
/* border: 0px; */
}
td.close {
width: 12px;
height: 12px;
background: #c8c8cb;
border-left: 1px solid #FFFFFF;
border-top: 1px solid #FFFFFF;
border-right: 1px solid #8F8C8F;
border-bottom: 1px solid #8F8C8F;
}
td.open {
width: 13px;
height: 13px;
background: #CBC8CB;
border-left: 1px solid #8F8C8F;
border-top: 1px solid #8F8C8F;
border-right: none;
border-bottom: none;
}
</style>
</head>
<body onload="init(), stopTimer()">
<p><span id="counter">0</span>秒</p>
<p>フラッグ<span id="flagNum">0</span></p>
<p>爆弾<span id="bombNum">0</span>個</p>
<button onclick="init(), resetTimer()">リセット</button>
<table id="field" cellspacing="0"></table>
<!-- <button id="btn">爆弾</button> -->
<canvas id="c" width=420 height=30></canvas>
<script>
"use strict";
var field = [];
var bombNum = 20; //爆弾の数
var col = 15; //行数
var row = 15; //列数
var nonBombNum = col * row - bombNum; //爆弾以外の位置を示す
var flagCount = 0;
function init(){
flagCount = 0;
document.getElementById("flagNum").textContent = flagCount;
document.getElementById("bombNum").textContent = bombNum;
// 多次元配列を生成する
for(var j = 0; j < col; j++){ //行
field[j] = [];
for(var k = 0; k < row; k++){ //列
field[j][k] = {
bomb: false,
open: false,
number: null
}
}
}
createBomb(); // 爆弾を設置する
console.log(field);
console.log(field[0][0]);
}
// 爆弾を置く
//
function createBomb(){
var temp = [];
// 重複チェックのために配列0番目をまず入れる
var s = Math.floor(Math.random()*col);
var t = Math.floor(Math.random()*row);
temp[0] = [s,t];
// この2業で爆弾設置、数字描画
field[s][t].bomb = true;
addNumber(s, t);
// 配列1番目以降を入れる
var n = 1;
while(n < bombNum){
var y = Math.floor(Math.random()*col);
var x = Math.floor(Math.random()*row);
// console.log([y,x]);
if(isArrayExist(temp, [y,x])){
var koyoso = [y,x];
// この2行で爆弾設置、数字描画
field[y][x].bomb = true;
addNumber(y, x);
temp.push(koyoso);
n++;
}
}
checkNumber(); // 爆弾のセルのNumberを"●"にする
draw();
}
// 重複チェック
function isArrayExist(array, value) {
var i = 0;
while(i <= array.length){
var a = JSON.stringify(array[i]);
var b = JSON.stringify(value);
if(a !== b){
++i;
} else if(a == b){
return false;
break;
}
}
return true;
}
// 爆弾の周りに1を配置する
function addNumber(y, x){
// 爆弾の位置を取得する
if(y-1 >= 0){
field[y-1][x].number += 1;
if(x-1 >= 0){
field[y-1][x-1].number += 1;
}
if(x+1 < row){
field[y-1][x+1].number += 1;
}
}
if(y+1 < col){
field[y+1][x].number += 1;
if(x-1 >= 0){
field[y+1][x-1].number += 1;
}
if(x+1 < row){
field[y+1][x+1].number += 1;
}
}
if(x-1 >= 0){
field[y][x-1].number += 1;
}
if(x+1 < row){
field[y][x+1].number += 1;
}
}
// もし爆弾のセルにnumberが入っていたら"●"にする
// 爆弾かそうでないかの判定
function checkNumber(){
for(var l = 0; l < col; l++){ //行
for(var m = 0; m < row; m++){ //列
if(field[l][m].bomb == true){
field[l][m].number = "●";
}
}
}
}
// DOMを変更する
function draw(){
var field = document.getElementById("field");
field.innerHTML = "";
for (var i = 0; i < col; i++) {
var tr = document.createElement("tr");
for (var j = 0; j < row; j++) {
var td = document.createElement("td");
var index = i * col + j;
td.className = "cell close";
td.id = index;
td.textContent = field.number;
// td.innerHTML = "<canvas id='c' width=30 height=30></canvas>";
td.onclick = click;
tr.appendChild(td);
}
field.appendChild(tr);
}
// $('body').append(putIcon());
// putIcon();
}
// 画像を挿入する
function putIcon(){
var canvas = document.getElementById("c");
var ctx = canvas.getContext('2d');
var img = new Image();
img.src = "img.png";
var number = document.getElementsByClassName("cell");
console.log(number);
img.onload = function(){
var width = 30;
var x = number * width;
ctx.drawImage(img, x, 0, 30, 30, 0, 0, 30, 30);
}
}
// クリック後動作
// id取得する
// idを配列番号に変換する
// 配列と連携させる
// セルのopenをtrueに変更する
// →openがtrueだったらtextをそのnumberに変更する
// セルのbombを取得する
// →bombがtrueだったらその周りに1を足す
function click(e){
if(!localStorage.getItem('key')){
startTimer();
}
var cell = e.srcElement;
// フラッグ立てる
if (e.ctrlKey && cell.className != "cell open" || e.metaKey && cell.className != "cell open"){ // Windowsの場合はコントロール。動作未確認。
cell.className = "cell close";
cell.textContent = "F";
flagCount++;
document.getElementById("flagNum").textContent = flagCount;
// console.log(cell);
} else if (cell.textContent == "F"){
cell.textContent = "";
flagCount--;
document.getElementById("flagNum").textContent = flagCount;
} else {
let clickId = e.srcElement.id; //クリックしたセルのidを取得
let rowId = Math.floor(clickId / col); // 列番号
let colId = Math.floor(clickId % row); // 行番号
let clickedCell = field[rowId][colId]; // データと連動
clickedCell.open = true; // クリックしたらopenをtrueに変更
openNull(rowId, colId); // numberがnullだったら、上下左右にnullを検索してopenをtrueにする
checkOpen();
// Number: nullをクリックしたら連鎖して消える仕様
// クリックしたら上下左右を調べる。
// もしNumberがnullだったらopenをtrueにする
}
}
// タイマー関係
var timerId = NaN, count = 0;
function startTimer(){
console.count("0");
clearInterval(timerId);
timerId = setInterval(tick, 1000);
localStorage.setItem('key','timer');
}
function stopTimer(){
clearInterval(timerId);
localStorage.removeItem('key');
}
function resetTimer(){
clearInterval(timerId);
localStorage.removeItem('key');
document.getElementById("counter").textContent = "0";
count = 0;
}
function tick(){
count++;
document.getElementById("counter").textContent = count;
}
// openがtrueだったらnumberを表示する
function checkOpen(){
for(var i = 0; i < col; i++){ // openがtrueのセルはnumberを表示する
for(var j = 0; j < row; j++){
var idNumber = i * col + j; // 多次元配列の番号からidへ変換
var cellId = document.getElementById(idNumber); // 変換取得したIDと同じセルを取得
if(field[i][j].open == true){
cellId.className = "cell open"; // セルのクラスをopenに変更
cellId.textContent = field[i][j].number; // セルのテキストを数字に変更
if(field[i][j].number == "●"){ // 爆弾をふんで負け判定へ
// openBomb(i,j);
stopTimer(); // ストップウォッチが止まる
clearClick();// クリックできなくなる
gameFail();
}
}
}
}
gameJudge();
}
// 勝ち判定
function gameJudge(){
var openNum = 0;
for(var i = 0; i < col; i++){
for(var j = 0; j < row; j++){
if(field[i][j].bomb == false && field[i][j].open == true){
openNum++;
}
}
}
if(openNum == nonBombNum){
clearClick();
stopTimer();
alert("勝ち");
}
console.log(openNum);
}
// ゲーム終了
// 全部の爆弾がオープン
// クリックできなくなる
function gameFail(){
for(var i = 0; i < col; i++){
for(var j = 0; j < row; j++){
if(field[i][j].number == "●"){ //負け
openBomb(i,j);
}
}
}
}
// 全部の爆弾がオープン
function openBomb(i,j){
var a = 0;
while(a < bombNum){
var idNumber = i * col + j; // 多次元配列の番号からidへ変換
var cellId = document.getElementById(idNumber); // 変換取得したIDと同じセルを取得
cellId.className = "cell open"; // セルのクラスをopenに変更
cellId.textContent = field[i][j].number; // セルのテキストを数字に変更
a++
}
}
// function gameGoal(){
// var b = 0;
// while(b < nonBombPlace.length){
// var idNumber = i * col + j; // 多次元配列の番号からidへ変換
// var cellId = document.getElementById(idNumber); // 変換取得したIDと同じセルを取得
// cellId.className = "cell open"; // セルのクラスをopenに変更
// cellId.textContent = field[i][j].number; // セルのテキストを数字に変更
// b++
// }
// }
// クリックできなくなる
function clearClick(){
for(var i = 0; i < col; i++){
for(var j = 0; j < row; j++){
var idNumber = i * col + j; // 多次元配列の番号からidへ変換
var cellId = document.getElementById(idNumber); // 変換取得したIDと同じセルを取得
console.log(cellId);
cellId.setAttribute("onclick", false);
}
}
}
// Number: nullをクリックしたら連鎖して消える仕様
// セルのnumberを取得する
// →numberがnullだったら、上下左右のセルを検索する
// →→上を検索して、nullだったらnumberをnullに変更する
function openNull(rowId, colId){
var r = rowId;
var l = colId;
var round = [
[-1, 0], // 1: 上
[0, 1], // 3: 右
[1, 0], // 5: 下
[0, -1] // 7: 左
]
var count = 0;
function roundCheck(y, x){
count++;
for(var i = 0; i < 4; i++){
var yy = y + round[i][0];
var xx = x + round[i][1];
if(field[yy] !== undefined){
if(field[yy][xx] !== undefined
&& field[yy][xx].open === false
&& field[yy][xx].bomb === false
&& field[y][x].number === null
){
field[yy][xx].open = true;
if(field[yy][xx].number === null){
roundCheck(yy, xx);
}
}
}
}
}
roundCheck(r, l);
}
</script>
</body>
</html>