質問編集履歴
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,14 +40,128 @@
|
|
40
40
|
|
41
41
|
```C#
|
42
42
|
|
43
|
+
using UnityEngine;
|
44
|
+
|
45
|
+
using UnityEngine.UI;
|
46
|
+
|
47
|
+
|
48
|
+
|
43
|
-
public
|
49
|
+
public class Cell : MonoBehaviour
|
44
|
-
|
50
|
+
|
45
|
-
|
51
|
+
{
|
52
|
+
|
53
|
+
public Minesweeper minesweeper;
|
54
|
+
|
55
|
+
public Vector2Int positionCell;
|
56
|
+
|
57
|
+
public bool Open;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
[SerializeField]
|
62
|
+
|
63
|
+
public GameObject m_button = null;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
[SerializeField]
|
68
|
+
|
69
|
+
private Text _view = null;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
[SerializeField]
|
74
|
+
|
75
|
+
private CellState _cellState = CellState.None;
|
76
|
+
|
77
|
+
public CellState CellState
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
get => _cellState;
|
82
|
+
|
83
|
+
set
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
_cellState = value;
|
88
|
+
|
89
|
+
OnCellStateChanged();
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
private void OnValidate()
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
OnCellStateChanged();
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
private void OnCellStateChanged()
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
if (_view == null) { return; }
|
112
|
+
|
113
|
+
|
46
114
|
|
47
115
|
if (_cellState == CellState.None)
|
48
116
|
|
49
117
|
{
|
50
118
|
|
119
|
+
_view.text = "";
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
else if (_cellState == CellState.Mine)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
_view.text = "X";
|
128
|
+
|
129
|
+
_view.color = Color.red;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
else
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
_view.text = ((int)_cellState).ToString();
|
138
|
+
|
139
|
+
_view.color = Color.blue;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
public void CellOpen()
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
m_button.SetActive(false);
|
152
|
+
|
153
|
+
Open = true;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
public void OnclickThis()
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
if (_cellState == CellState.None)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
51
165
|
m_button.SetActive(false);
|
52
166
|
|
53
167
|
Open = true;
|
@@ -78,8 +192,432 @@
|
|
78
192
|
|
79
193
|
}
|
80
194
|
|
195
|
+
}
|
196
|
+
|
197
|
+
public enum CellState
|
198
|
+
|
199
|
+
{
|
200
|
+
|
201
|
+
None = 0,
|
202
|
+
|
203
|
+
One = 1,
|
204
|
+
|
205
|
+
Two = 2,
|
206
|
+
|
207
|
+
Three = 3,
|
208
|
+
|
209
|
+
Four = 4,
|
210
|
+
|
211
|
+
Five = 5,
|
212
|
+
|
213
|
+
Six = 6,
|
214
|
+
|
215
|
+
Seven = 7,
|
216
|
+
|
217
|
+
Eight = 8,
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
Mine = -1,
|
222
|
+
|
223
|
+
}
|
224
|
+
|
81
225
|
```
|
82
226
|
|
227
|
+
```C#
|
228
|
+
|
229
|
+
using System.Collections.Generic;
|
230
|
+
|
231
|
+
using UnityEngine;
|
232
|
+
|
233
|
+
using UnityEngine.UI;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public class Minesweeper : MonoBehaviour
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
[SerializeField]
|
242
|
+
|
243
|
+
private int _rows = 1;
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
[SerializeField]
|
248
|
+
|
249
|
+
private int _columns = 1;
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
[SerializeField]
|
254
|
+
|
255
|
+
private int _mineCount = 1;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
[SerializeField]
|
260
|
+
|
261
|
+
private GridLayoutGroup _gridLayoutGroup = null;
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
[SerializeField]
|
266
|
+
|
267
|
+
private Cell _cellPrefab = null;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
[SerializeField]
|
272
|
+
|
273
|
+
private Cell[,] _cells;
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
void Start()
|
278
|
+
|
279
|
+
{
|
280
|
+
|
281
|
+
var parent = _gridLayoutGroup.gameObject.transform;
|
282
|
+
|
283
|
+
if (_columns < _rows)
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
_gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
|
288
|
+
|
289
|
+
_gridLayoutGroup.constraintCount = _columns;
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
else
|
294
|
+
|
295
|
+
{
|
296
|
+
|
297
|
+
_gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
|
298
|
+
|
299
|
+
_gridLayoutGroup.constraintCount = _rows;
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
_cells = new Cell[_rows, _columns];
|
306
|
+
|
307
|
+
for (var r = 0; r < _rows; r++)
|
308
|
+
|
309
|
+
{
|
310
|
+
|
311
|
+
for (var c = 0; c < _columns; c++)
|
312
|
+
|
313
|
+
{
|
314
|
+
|
315
|
+
var cell = Instantiate(_cellPrefab);
|
316
|
+
|
317
|
+
cell.transform.SetParent(parent);
|
318
|
+
|
319
|
+
_cells[r, c] = cell;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
for (var i = 0; i < _mineCount;)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
var r = Random.Range(0, _rows);
|
332
|
+
|
333
|
+
var c = Random.Range(0, _columns);
|
334
|
+
|
335
|
+
var cell = _cells[r, c];
|
336
|
+
|
337
|
+
if (cell.CellState != CellState.Mine)
|
338
|
+
|
339
|
+
{
|
340
|
+
|
341
|
+
cell.CellState = CellState.Mine;
|
342
|
+
|
343
|
+
i++;
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
for (var r = 0; r < _rows; r++)
|
350
|
+
|
351
|
+
{
|
352
|
+
|
353
|
+
for (var c = 0; c < _columns; c++)
|
354
|
+
|
355
|
+
{
|
356
|
+
|
357
|
+
if (_cells[r, c].CellState == CellState.Mine)
|
358
|
+
|
359
|
+
{
|
360
|
+
|
361
|
+
AddMine(r, c);
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
//for (var r = 0; r < _rows; r++)
|
372
|
+
|
373
|
+
//{
|
374
|
+
|
375
|
+
// for (var c = 0; c < _columns; c++)
|
376
|
+
|
377
|
+
// {
|
378
|
+
|
379
|
+
// // 周辺の地雷数を設定したいセル
|
380
|
+
|
381
|
+
// var cell = _cells[r, c];
|
382
|
+
|
383
|
+
// Debug.Log($"Cell[{r}, {c}]={cell.CellState}");
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
// if (cell.CellState == CellState.Mine)
|
388
|
+
|
389
|
+
// {
|
390
|
+
|
391
|
+
// continue; // 地雷なら無視する
|
392
|
+
|
393
|
+
// }
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
// // 以下、泥臭く周辺のセルが地雷かどうか調べるコード
|
398
|
+
|
399
|
+
// // ただしそのままだと不正なインデックスになるので注意
|
400
|
+
|
401
|
+
// var count = 0;
|
402
|
+
|
403
|
+
// if (_cells[r - 1, c - 1].CellState == CellState.Mine)
|
404
|
+
|
405
|
+
// {
|
406
|
+
|
407
|
+
// count++;
|
408
|
+
|
409
|
+
// }
|
410
|
+
|
411
|
+
// if (_cells[r - 1, c].CellState == CellState.Mine)
|
412
|
+
|
413
|
+
// {
|
414
|
+
|
415
|
+
// count++;
|
416
|
+
|
417
|
+
// }
|
418
|
+
|
419
|
+
// // etc...
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
// // 最後に count をセルに設定する
|
424
|
+
|
425
|
+
// }
|
426
|
+
|
427
|
+
//}
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
public void AddMine(int r, int c)
|
434
|
+
|
435
|
+
{
|
436
|
+
|
437
|
+
var left = c - 1;
|
438
|
+
|
439
|
+
var right = c + 1;
|
440
|
+
|
441
|
+
var top = r - 1;
|
442
|
+
|
443
|
+
var bottom = r + 1;
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
if (top >= 0)
|
448
|
+
|
449
|
+
{
|
450
|
+
|
451
|
+
if (left >= 0 && _cells[top, left].CellState != CellState.Mine) { _cells[top, left].CellState++; }
|
452
|
+
|
453
|
+
if (_cells[top, c].CellState != CellState.Mine) { _cells[top, c].CellState++; }
|
454
|
+
|
455
|
+
if (right < _columns && _cells[top, right].CellState != CellState.Mine) { _cells[top, right].CellState++; }
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
if (left >= 0 && _cells[r, left].CellState != CellState.Mine) { _cells[r, left].CellState++; }
|
460
|
+
|
461
|
+
if (right < _columns && _cells[r, right].CellState != CellState.Mine) { _cells[r, right].CellState++; }
|
462
|
+
|
463
|
+
if (bottom < _rows)
|
464
|
+
|
465
|
+
{
|
466
|
+
|
467
|
+
if (left >= 0 && _cells[bottom, left].CellState != CellState.Mine) { _cells[bottom, left].CellState++; }
|
468
|
+
|
469
|
+
if (_cells[bottom, c].CellState != CellState.Mine) { _cells[bottom, c].CellState++; }
|
470
|
+
|
471
|
+
if (right < _columns && _cells[bottom, right].CellState != CellState.Mine) { _cells[bottom, right].CellState++; }
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
public int GetMineCount(int r, int c)
|
478
|
+
|
479
|
+
{
|
480
|
+
|
481
|
+
var left = c - 1;
|
482
|
+
|
483
|
+
var right = c + 1;
|
484
|
+
|
485
|
+
var top = r - 1;
|
486
|
+
|
487
|
+
var bottom = r + 1;
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
var count = 0;
|
492
|
+
|
493
|
+
if (top >= 0)
|
494
|
+
|
495
|
+
{
|
496
|
+
|
497
|
+
if (left >= 0 && _cells[top, left].CellState == CellState.Mine) { count++; }
|
498
|
+
|
499
|
+
if (_cells[top, c].CellState == CellState.Mine) { count++; }
|
500
|
+
|
501
|
+
if (right < _columns && _cells[top, right].CellState == CellState.Mine) { count++; }
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
if (left >= 0 && _cells[r, left].CellState == CellState.Mine) { count++; }
|
506
|
+
|
507
|
+
if (right < _columns && _cells[r, right].CellState == CellState.Mine) { count++; }
|
508
|
+
|
509
|
+
if (bottom < _rows)
|
510
|
+
|
511
|
+
{
|
512
|
+
|
513
|
+
if (left >= 0 && _cells[bottom, left].CellState == CellState.Mine) { count++; }
|
514
|
+
|
515
|
+
if (_cells[bottom, c].CellState == CellState.Mine) { count++; }
|
516
|
+
|
517
|
+
if (right < _columns && _cells[bottom, right].CellState == CellState.Mine) { count++; }
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
return count;
|
522
|
+
|
523
|
+
}
|
524
|
+
|
525
|
+
private Cell[] SearchCell(int r, int c)
|
526
|
+
|
527
|
+
{
|
528
|
+
|
529
|
+
var list = new List<Cell>();
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
var left = c - 1;
|
534
|
+
|
535
|
+
var right = c + 1;
|
536
|
+
|
537
|
+
var top = r - 1;
|
538
|
+
|
539
|
+
var bottom = r + 1;
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
if (top >= 0)
|
544
|
+
|
545
|
+
{
|
546
|
+
|
547
|
+
if (left >= 0) { list.Add(_cells[top, left]); }
|
548
|
+
|
549
|
+
list.Add(_cells[top, c]);
|
550
|
+
|
551
|
+
if (right < _columns) { list.Add(_cells[top, right]); }
|
552
|
+
|
553
|
+
}
|
554
|
+
|
555
|
+
if (left >= 0) { list.Add(_cells[r, left]); }
|
556
|
+
|
557
|
+
if (right < _columns) { list.Add(_cells[r, right]); }
|
558
|
+
|
559
|
+
if (bottom < _rows)
|
560
|
+
|
561
|
+
{
|
562
|
+
|
563
|
+
if (left >= 0) { list.Add(_cells[bottom, left]); }
|
564
|
+
|
565
|
+
list.Add(_cells[bottom, c]);
|
566
|
+
|
567
|
+
if (right < _columns) { list.Add(_cells[bottom, right]); }
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
return list.ToArray();
|
574
|
+
|
575
|
+
}
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
public void OpenCell(Cell target)
|
580
|
+
|
581
|
+
{
|
582
|
+
|
583
|
+
//自分の座標を記憶
|
584
|
+
|
585
|
+
var x = target.positionCell.x;
|
586
|
+
|
587
|
+
var y = target.positionCell.y;
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
foreach (var item in SearchCell(x, y))
|
592
|
+
|
593
|
+
{
|
594
|
+
|
595
|
+
if (item.Open) //既にあいてたら次のセルへ
|
596
|
+
|
597
|
+
{
|
598
|
+
|
599
|
+
continue;
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
item.CellOpen(); //セルをあけてフラグをtrueにする
|
604
|
+
|
605
|
+
if (item.CellState == CellState.None)
|
606
|
+
|
607
|
+
{
|
608
|
+
|
609
|
+
OpenCell(item); //Noneなら再帰でNoneを探してあけていく
|
610
|
+
|
611
|
+
}
|
612
|
+
|
613
|
+
}
|
614
|
+
|
615
|
+
}
|
616
|
+
|
617
|
+
}
|
618
|
+
|
619
|
+
```
|
620
|
+
|
83
621
|
|
84
622
|
|
85
623
|
### 試したこと
|