teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード内容の追加

2021/02/19 09:02

投稿

退会済みユーザー
answer CHANGED
@@ -1,28 +1,29 @@
1
- 適当に書いてみた。
2
- そこら中Containsになってしまった。
3
- オブジェクト貼り付けたら一応動く
1
+ 質問回答はなってないお遊びコードの修正です
4
2
 
5
- ランダムで3色球が生成されて
3
+ 修正前コードより落ちる処理と連鎖も作ってみた。
6
- 4つ以上繋がっヤツは消える
4
+ 軽くコメントも入れといた。
7
- それだけ。
8
5
 
9
- 落ちて連鎖の部分眠くなったのでヤメときます。データを落ちた位置に整列させて再度searchを呼び出すけ、呼び出し回数が連鎖回数になる、でいいはず
6
+ Dictionary失敗だった気も面倒た。
10
7
 
11
- そもそも、繋がってる判定(下記コードのsearch、searchPosition同じ様な内容)が出来てるというならその先は特に難しくなのでは、、、?
8
+ gif入れてる回答か分かりやす入れたかったけど、えばgif作った事なかった。全くわからん。
12
9
 
10
+ コピペで動作します。
13
- Listの使い方ってなって話なら、もっ簡単な所ら学習た方がいい
11
+ 処理遅いとコードが汚いとか、変数名がおかいとか、つっこみどころは山ほどありそうです、その他ろ気にしなければこんな感じで特に難しい事はせずに作れますよって程度にってもらえれば
14
12
 
15
13
  ```
16
14
  using System.Collections;
17
15
  using System.Collections.Generic;
18
16
  using UnityEngine;
17
+ using System.Linq;
19
18
 
19
+ // puyoクラスにしてみた
20
20
  public class Puyo
21
21
  {
22
22
  public GameObject gameObject;
23
23
  public int color;
24
24
  public bool searched = false;
25
- public int connectedValue;
25
+ public int connectedValue = 0;
26
+ public int chainId = 0;
26
27
 
27
28
  public Puyo(int color, GameObject gameObject)
28
29
  {
@@ -30,17 +31,46 @@
30
31
  this.gameObject = gameObject;
31
32
  }
32
33
  }
34
+
33
35
  public class Scripts : MonoBehaviour
34
36
  {
37
+ // お遊び
38
+ int[,] twentythree = new int [,] {
39
+ { 0,0,0,0,1,3 },
40
+ { 0,0,0,0,1,1 },
41
+ { 0,3,0,0,4,1 },
42
+ { 2,4,4,4,3,3 },
43
+ { 3,3,1,5,2,3 },
44
+ { 3,2,2,1,5,1 },
45
+ { 2,1,1,5,2,2 },
46
+ { 4,5,4,5,3,2 },
47
+ { 4,4,3,3,1,1 },
48
+ { 1,5,1,2,3,1 },
49
+ { 2,5,5,1,2,5 },
50
+ { 3,1,1,4,2,2 },
51
+ { 4,4,4,5,5,5 },
52
+ { 3,3,1,2,1,4 },
53
+ { 3,4,3,2,1,4 },
54
+ { 2,2,4,3,2,1 },
55
+ { 2,1,4,3,2,1 },
56
+ { 1,1,4,3,4,4 },
57
+ };
58
+
59
+ // データ管理用、正直この方法は失敗したと思います。面倒だった
35
60
  Dictionary<Vector2Int, Puyo> data = new Dictionary<Vector2Int, Puyo>();
61
+ private bool falled = true;
36
62
 
63
+ // 動きを確認出来るようコルーチンにして描画処理を走らせる(コードを減らす為)
37
64
  IEnumerator Start()
38
65
  {
66
+ // puyoの位置決定
39
67
  for (int y = 0; y < 18; y++)
40
68
  {
41
69
  for (int x = 0; x < 6; x++)
42
70
  {
43
- int color = Random.Range(0, 4);
71
+ // int color = Random.Range(0, 6);
72
+ int color = twentythree[17 - y, x];
73
+
44
74
  if (color == 0) continue;
45
75
  var obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
46
76
  data.Add(new Vector2Int(x, y), new Puyo(color, obj));
@@ -48,52 +78,129 @@
48
78
  if (color == 1) obj.GetComponent<Renderer>().material.color = Color.red;
49
79
  if (color == 2) obj.GetComponent<Renderer>().material.color = Color.blue;
50
80
  if (color == 3) obj.GetComponent<Renderer>().material.color = Color.yellow;
81
+ if (color == 4) obj.GetComponent<Renderer>().material.color = Color.green;
82
+ if (color == 5) obj.GetComponent<Renderer>().material.color = Color.cyan;
51
83
  }
52
84
  }
53
-
54
- Search();
55
85
 
86
+
87
+ // デバッグ表示用の変数
56
- foreach (var puyo in data)
88
+ int chainCount = 0;
89
+ int totalCount = 0;
90
+
91
+ // 連鎖のループ
92
+ while (falled)
57
93
  {
58
- if (puyo.Value.connectedValue > 3) Destroy(puyo.Value.gameObject);
94
+ FallStart();
59
95
  yield return new WaitForFixedUpdate();
96
+ SearchStart();
97
+ int id = data.Max(_ => _.Value.chainId);
98
+ yield return new WaitForSeconds(1.0f);
99
+ if (id > 0) chainCount++;
100
+ int count = 0;
101
+ // 同時消しを別々に処理してみた、id個の同時消しが存在する
102
+ for (int i = 1; i <= id; i++)
103
+ {
104
+ var eraseData = data.Where(_ => _.Value.chainId == i);
105
+ foreach (var puyo in eraseData)
106
+ {
107
+ Destroy(puyo.Value.gameObject);
108
+ count++;
109
+ }
110
+ foreach (var erasePosition in eraseData.ToList())
111
+ {
112
+ data.Remove(erasePosition.Key);
113
+ }
114
+ yield return new WaitForSeconds(0.2f);
115
+ }
116
+ // 処理終了でクラスに持たせてしまった変数の初期化、これは正直無駄すぎる気がする
117
+ foreach (var puyo in data)
118
+ {
119
+ puyo.Value.searched = false;
120
+ puyo.Value.connectedValue = 0;
121
+ }
122
+
123
+ if (id == 0) continue;
124
+ Debug.Log($"{chainCount}連鎖目、{id}箇所で同時消し、{count}個のpuyoが消えました");
125
+ totalCount += count;
60
126
  }
127
+ Debug.Log($"結果、{chainCount} 連鎖、{totalCount}のpuyoが消えました");
61
128
  }
62
129
 
130
+ // 上下左右を調べる、currentPositionと同じ色なら位置を再設定して再起、connectedValue++して繋がっている個数を設定
63
- void Search()
131
+ // 再設定された位置はここで検索済みとなる
132
+ void SearchPosition(Vector2Int currentPosition, Vector2Int searchPosition, List<Vector2Int> positions)
64
133
  {
65
- for (int y = 0;y < 18; y++)
134
+ foreach (var position in new Vector2Int[] { Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right })
66
135
  {
136
+ if (!data.ContainsKey(searchPosition + position)) continue;
137
+ if (data[currentPosition].color == data[searchPosition + position].color && !data[searchPosition + position].searched)
138
+ {
139
+ data[currentPosition].connectedValue++;
140
+ data[searchPosition + position].searched = true;
141
+ positions.Add(searchPosition + position);
142
+ SearchPosition(currentPosition, searchPosition + position, positions);
143
+ }
144
+ }
145
+ }
146
+
147
+ // 繋がりを検索する
148
+ void SearchStart()
149
+ {
150
+ int id = 0;
151
+ for (int y = 0; y < 18; y++)
152
+ {
67
153
  for (int x = 0; x < 6; x++)
68
154
  {
155
+ var key = new Vector2Int(x, y);
69
- List<Vector2Int> positions = new List<Vector2Int>();
156
+ List<Vector2Int> connectedPositions = new List<Vector2Int>();
70
- var position = new Vector2Int(x, y);
71
- if (!data.ContainsKey(position)) continue;
157
+ if (!data.ContainsKey(key)) continue;
72
- if (data[position].searched) continue;
158
+ if (data[key].searched) continue;
73
- SearchPosition(position, position, positions);
159
+ SearchPosition(key, key, connectedPositions);
74
- data[position].searched = true;
160
+ data[key].searched = true;
161
+ if (connectedPositions.Count > 3) id++;
75
- foreach(var pos in positions)
162
+ foreach (var position in connectedPositions)
76
163
  {
77
- data[pos].connectedValue = data[position].connectedValue;
164
+ data[position].connectedValue = data[key].connectedValue;
165
+ if (data[key].connectedValue > 3)
166
+ {
167
+ data[position].chainId = id;
168
+ falled = true;
169
+ }
78
170
  }
79
171
  }
80
172
  }
81
173
  }
174
+
82
-
175
+ // 全ての位置情報を回す
83
- void SearchPosition(Vector2Int currentPosition, Vector2Int position, List<Vector2Int> positions)
176
+ void FallStart()
84
177
  {
178
+ falled = false;
85
- foreach (var pos in new Vector2Int[] { Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right })
179
+ for (int y = 0; y < 18; y++)
86
180
  {
87
- if (!data.ContainsKey(position + pos)) continue;
181
+ for (int x = 0; x < 6; x++)
88
- if (data[currentPosition].color == data[position + pos].color && !data[position + pos].searched)
89
182
  {
90
- data[currentPosition].connectedValue++;
91
- data[position + pos].searched = true;
183
+ if (data.ContainsKey(new Vector2Int(x, y)))
184
+ {
92
- positions.Add(position + pos);
185
+ Fall(new Vector2Int(x, y));
93
- SearchPosition(currentPosition, position + pos, positions);
186
+ }
94
187
  }
95
188
  }
96
189
  }
190
+
191
+ // 下方向、最下段、もしくは下にpuyoが無くなるまで回す
192
+ void Fall(Vector2Int position)
193
+ {
194
+ var falledPosition = new Vector2Int(position.x, position.y - 1);
195
+ if (position.y < 1) return;
196
+ if (!data.ContainsKey(falledPosition))
197
+ {
198
+ data.Add(falledPosition, data[position]);
199
+ data[falledPosition].gameObject.transform.position = new Vector3(falledPosition.x, falledPosition.y, 0);
200
+ data.Remove(position);
201
+ falled = true;
202
+ Fall(falledPosition);
203
+ }
204
+ }
97
205
  }
98
-
99
206
  ```