質問編集履歴

2

具体的な状況の記載

2019/01/27 03:41

投稿

ijuhs
ijuhs

スコア13

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
  別の場面でも同じバグが起こりましたが、その時はelseとその後ろの{の間にスペースを入れたら解決しました。今回はそれでは解決せず、困っています。回答よろしくお願いします。
12
12
 
13
+ ##状況
14
+
15
+ セルを選択しただけでブロックの消去がされ、矢印は一瞬しか表示されない
16
+
13
17
  ##試したこと
14
18
 
15
19
  elseをelse if(selectedX >=0 )にする

1

プログラム全体を記載しました。

2019/01/27 03:41

投稿

ijuhs
ijuhs

スコア13

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,207 @@
18
18
 
19
19
  ```C++
20
20
 
21
+ #include<stdio.h>
22
+
23
+ #include<stdlib.h>
24
+
25
+ #include<time.h>
26
+
27
+ #include <string.h>
28
+
29
+ #include<conio.h>
30
+
31
+
32
+
33
+ //マップ縦横幅
34
+
35
+ #define FIELD_WIDTH 8
36
+
37
+ #define FIELD_HEIGHT 8
38
+
39
+ //ブロックの種類
40
+
41
+ #define BLOCK_TYPE_MAX 7
42
+
43
+
44
+
45
+ //セルの状態
46
+
47
+ enum{
48
+
49
+ CELL_TYPE_NONE,//0
50
+
51
+ CELL_TYPE_BLOCK_0,//1
52
+
53
+ CELL_TYPE_BLOCK_1,//2
54
+
55
+ CELL_TYPE_BLOCK_2,//3
56
+
57
+ CELL_TYPE_BLOCK_3,//4
58
+
59
+ CELL_TYPE_BLOCK_4,//5
60
+
61
+ CELL_TYPE_BLOCK_5,//6
62
+
63
+ CELL_TYPE_BLOCK_6,//7
64
+
65
+ CELL_TYPE_BLOCK_MAX//8
66
+
67
+ };
68
+
69
+
70
+
71
+ //セルAA情報
72
+
73
+ char cellAA[][3+1] = {
74
+
75
+ "・",//CELL_TYPE_NONE
76
+
77
+ "〇",//CELL_TYPE_BLOCK_0
78
+
79
+ "△",//CELL_TYPE_BLOCK_1
80
+
81
+ "□",//CELL_TYPE_BLOCK_2
82
+
83
+ "●",//CELL_TYPE_BLOCK_3
84
+
85
+ "▲",//CELL_TYPE_BLOCK_4
86
+
87
+ "■",//CELL_TYPE_BLOCK_5
88
+
89
+ "☆",//CELL_TYPE_BLOCK_6
90
+
91
+ };
92
+
93
+
94
+
95
+ //セル(0,0)~(7,7)
96
+
97
+ int cells[FIELD_HEIGHT][FIELD_WIDTH];
98
+
99
+
100
+
101
+ //連結チェックされたか
102
+
103
+ int ischecked[FIELD_HEIGHT][FIELD_WIDTH];
104
+
105
+
106
+
107
+ //カーソル位置
108
+
109
+ int cursorX, cursorY;
110
+
111
+
112
+
113
+ //選択されたセル
114
+
115
+ int selectedX = -1, selectedY = -1;
116
+
117
+
118
+
119
+ /*
120
+
121
+ 連結判定関数
122
+
123
+ x:対象のx座標
124
+
125
+ y:対象のy座標
126
+
127
+ cellType:ブロックの種類
128
+
129
+ count:つながっている数
130
+
131
+ */
132
+
133
+ int getConnectedBlockCount(int _x, int _y, int _cellType, int _count){
134
+
135
+ //判定対象か否か
136
+
137
+ if ((_x < 0) || (_x >= FIELD_WIDTH) || (_y < 0 ) || (_y >= FIELD_HEIGHT) || ischecked[_y][_x] || (cells[_y][_x] == CELL_TYPE_NONE) || (cells[_y][_x] != _cellType))
138
+
139
+ return _count;
140
+
141
+ _count++;
142
+
143
+ ischecked[_y][_x] = true;
144
+
145
+
146
+
147
+ //再帰処理
148
+
149
+ //上
150
+
151
+ _count = getConnectedBlockCount(_x, _y-1, _cellType, _count);
152
+
153
+ //下
154
+
155
+ _count = getConnectedBlockCount(_x, _y+1, _cellType, _count);
156
+
157
+ //左
158
+
159
+ _count = getConnectedBlockCount(_x-1, _y, _cellType, _count);
160
+
161
+ //右
162
+
163
+ _count = getConnectedBlockCount(_x+1, _y, _cellType, _count);
164
+
165
+
166
+
167
+ return _count;
168
+
169
+ }
170
+
171
+
172
+
173
+ //連結したブロックを消す関数
174
+
175
+ void eraseConnectedBlocks(int _x, int _y, int _cellType){
176
+
177
+ //対象か否か
178
+
179
+ if ((_x < 0) || (_x >= FIELD_WIDTH) || (_y < 0 ) || (_y >= FIELD_HEIGHT) || (cells[_y][_x] == CELL_TYPE_NONE) || (cells[_y][_x] != _cellType))
180
+
181
+ return;
182
+
183
+ //自身を消す
184
+
185
+ cells[_y][_x] = CELL_TYPE_NONE;
186
+
187
+ //上下左右も判定し消す
188
+
189
+ eraseConnectedBlocks(_x, _y-1, _cellType);
190
+
191
+ eraseConnectedBlocks(_x, _y+1, _cellType);
192
+
193
+ eraseConnectedBlocks(_x-1, _y, _cellType);
194
+
195
+ eraseConnectedBlocks(_x+1, _y, _cellType);
196
+
197
+ }
198
+
199
+ //main
200
+
201
+ int main() {
202
+
203
+ //ブロックのランダム化
204
+
205
+ srand((unsigned int)time(NULL));
206
+
207
+ for (int y = 0; y < FIELD_HEIGHT; y++){
208
+
209
+ for(int x = 0; x < FIELD_WIDTH; x++){
210
+
211
+ //1 + 0...6 =1...7
212
+
213
+ cells[y][x] = CELL_TYPE_BLOCK_0 + rand() % BLOCK_TYPE_MAX;
214
+
215
+ }
216
+
217
+ }
218
+
219
+ //描画とか
220
+
21
- while (1) {
221
+ while (1) {
22
222
 
23
223
  system("cls");
24
224
 
@@ -150,4 +350,8 @@
150
350
 
151
351
  }
152
352
 
353
+ }
354
+
355
+
356
+
153
357
  ```