質問編集履歴
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,43 +1,495 @@
|
|
1
|
-
現
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
b
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
Expression: vector subscript out of range
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
1
|
+
###前提・実現したいこと
|
2
|
+
|
3
|
+
現在C++とSiv3Dで落ち物パズルゲームを制作しています。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
###発生している問題・エラーメッセージ
|
10
|
+
|
11
|
+
std::vectorを使用して'Block'クラスを格納しようとしていたのですが
|
12
|
+
|
13
|
+
(std::vector<Block> bronzeと宣言)
|
14
|
+
|
15
|
+
bronze[0]などとアクセスしようとするとエラーが起きてしまいました。
|
16
|
+
|
17
|
+
**発生した箇所はboard.cpp内のdropBronze関数です。**
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
Expression: vector subscript out of range
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
###該当のソースコード
|
28
|
+
|
29
|
+
```C+++
|
30
|
+
|
31
|
+
#pragma once
|
32
|
+
|
33
|
+
#include "Block.hpp"
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
///<summary>
|
38
|
+
|
39
|
+
///<para>Boardクラス</para>
|
40
|
+
|
41
|
+
///<para>・Boardのサイズは大きい方</para>
|
42
|
+
|
43
|
+
///<para>・位置=画面上のもの</para>
|
44
|
+
|
45
|
+
///<para>・座標=二次元配列のこと</para>
|
46
|
+
|
47
|
+
///</summary>
|
48
|
+
|
49
|
+
class Board {
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public:
|
54
|
+
|
55
|
+
Board() = default;
|
56
|
+
|
57
|
+
Board(WhichPlayer _whichPlayer);
|
58
|
+
|
59
|
+
~Board();
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
public:
|
64
|
+
|
65
|
+
const Point BOARD_VISUAL_SIZE; //見た目上の二次元配列
|
66
|
+
|
67
|
+
const Point BOARD_ACTUAL_SIZE; //実際の二次元配列
|
68
|
+
|
69
|
+
const Point BOARD_POINT_1P; //1Pの画面上の位置
|
70
|
+
|
71
|
+
const Point BOARD_POINT_2P; //2Pの画面上の位置
|
72
|
+
|
73
|
+
const Point INIT_BRONZE_COORDINATE; //ブロンズの基準の初期座標
|
74
|
+
|
75
|
+
const Point BOARD_RECTANGLE_SIZE; //ボードのサイズ
|
76
|
+
|
77
|
+
const int BLOCK_SIZE;
|
78
|
+
|
79
|
+
const unsigned int BRONZE_MAX_SIZE;
|
80
|
+
|
81
|
+
const int RED_NUM;
|
82
|
+
|
83
|
+
const int BLUE_NUM;
|
84
|
+
|
85
|
+
const int GREEN_NUM;
|
86
|
+
|
87
|
+
const int YELLOW_NUM;
|
88
|
+
|
89
|
+
const int PURPLE_NUM;
|
90
|
+
|
91
|
+
const int SELULIAN_NUM;
|
92
|
+
|
93
|
+
const int WALL_NUM;
|
94
|
+
|
95
|
+
const int NONE_NUM;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
public:
|
100
|
+
|
101
|
+
void init();
|
102
|
+
|
103
|
+
void update();
|
104
|
+
|
105
|
+
void draw() const;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
void boardInit();
|
110
|
+
|
111
|
+
void bronzeInit();
|
112
|
+
|
113
|
+
void boardDraw() const;
|
114
|
+
|
115
|
+
void bronzeDraw() const;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
void addBlockToBronze(Point coordinate,BlockColor blockColor);
|
120
|
+
|
121
|
+
void moveBronzeIndicator(int direction);
|
122
|
+
|
123
|
+
void dropBronze();
|
124
|
+
|
125
|
+
void checkBoardConnection();
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
bool isCanMoveBronze(int direction);
|
130
|
+
|
131
|
+
bool isCanAddBlockToBronze();
|
132
|
+
|
133
|
+
bool isBronzeDontCollideWithBoard(Point _coordinate);
|
134
|
+
|
135
|
+
bool isReachFallingDefaultCount();
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
private:
|
140
|
+
|
141
|
+
std::vector<std::vector<BlockColor>> board;
|
142
|
+
|
143
|
+
std::vector<Block> bronze;
|
144
|
+
|
145
|
+
std::vector<Point> candidates;
|
146
|
+
|
147
|
+
Point bronzeIndicator;
|
148
|
+
|
149
|
+
WhichPlayer whichPlayer;
|
150
|
+
|
151
|
+
Point boardVisualPoint;
|
152
|
+
|
153
|
+
Rect rectangle;
|
154
|
+
|
155
|
+
int fallingCount;
|
156
|
+
|
157
|
+
int fallingDefaultCount;
|
158
|
+
|
159
|
+
};
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
#include "Board.hpp"
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
Board::Board(WhichPlayer _whichPlayer) :
|
174
|
+
|
175
|
+
whichPlayer(_whichPlayer),
|
176
|
+
|
177
|
+
BOARD_VISUAL_SIZE(Point(7, 13)),
|
178
|
+
|
179
|
+
BOARD_ACTUAL_SIZE(Point(9, 15)),
|
180
|
+
|
181
|
+
BOARD_POINT_1P(150, 100),
|
182
|
+
|
183
|
+
BOARD_POINT_2P(850, 100),
|
184
|
+
|
185
|
+
INIT_BRONZE_COORDINATE(3,0),
|
186
|
+
|
187
|
+
BOARD_RECTANGLE_SIZE(280,520),
|
188
|
+
|
189
|
+
BLOCK_SIZE(40),
|
190
|
+
|
191
|
+
BRONZE_MAX_SIZE(5),
|
192
|
+
|
193
|
+
RED_NUM(0),
|
194
|
+
|
195
|
+
BLUE_NUM(1),
|
196
|
+
|
197
|
+
GREEN_NUM(2),
|
198
|
+
|
199
|
+
YELLOW_NUM(3),
|
200
|
+
|
201
|
+
PURPLE_NUM(4),
|
202
|
+
|
203
|
+
SELULIAN_NUM(5),
|
204
|
+
|
205
|
+
WALL_NUM(6),
|
206
|
+
|
207
|
+
NONE_NUM(7)
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
init();
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
Board::~Board() {
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
void Board::init() {
|
228
|
+
|
229
|
+
boardInit();
|
230
|
+
|
231
|
+
bronzeInit();
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
void Board::update() {
|
240
|
+
|
241
|
+
if (Input::Key1.clicked) {
|
242
|
+
|
243
|
+
dropBronze();
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
void Board::draw() const {
|
252
|
+
|
253
|
+
rectangle.draw();
|
254
|
+
|
255
|
+
boardDraw();
|
256
|
+
|
257
|
+
bronzeDraw();
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
void Board::boardInit() {
|
264
|
+
|
265
|
+
board.resize(BOARD_ACTUAL_SIZE.x);
|
266
|
+
|
267
|
+
for (int i = 0; i < BOARD_ACTUAL_SIZE.x; i++) {
|
268
|
+
|
269
|
+
board[i].resize(BOARD_ACTUAL_SIZE.y);
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
for (int i = 0; i < BOARD_ACTUAL_SIZE.x; i++) {
|
276
|
+
|
277
|
+
for (int j = 0; j < BOARD_ACTUAL_SIZE.y; j++) {
|
278
|
+
|
279
|
+
board[i][j] = static_cast<BlockColor>(NONE_NUM);
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
for (int i = 0; i < BOARD_ACTUAL_SIZE.x; i++) {
|
286
|
+
|
287
|
+
board[i][0] = static_cast<BlockColor>(WALL_NUM);
|
288
|
+
|
289
|
+
board[i][BOARD_ACTUAL_SIZE.y - 1] = static_cast<BlockColor>(WALL_NUM);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
for (int i = 0; i < BOARD_ACTUAL_SIZE.y; i++) {
|
294
|
+
|
295
|
+
board[0][i] = static_cast<BlockColor>(WALL_NUM);
|
296
|
+
|
297
|
+
board[BOARD_ACTUAL_SIZE.x - 1][i] = static_cast<BlockColor>(WALL_NUM);
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
if (whichPlayer == WhichPlayer::PLAYER1) {
|
304
|
+
|
305
|
+
boardVisualPoint = BOARD_POINT_1P;
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
else {
|
310
|
+
|
311
|
+
boardVisualPoint = BOARD_POINT_2P;
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
rectangle = Rect(boardVisualPoint, BOARD_RECTANGLE_SIZE);
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
void Board::bronzeInit() {
|
320
|
+
|
321
|
+
bronzeIndicator = INIT_BRONZE_COORDINATE;
|
322
|
+
|
323
|
+
std::vector<Block>().swap(bronze);
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
void Board::bronzeDraw() const {
|
328
|
+
|
329
|
+
for (unsigned int i = 0; i<bronze.size(); i++) {
|
330
|
+
|
331
|
+
TextureAsset(L"Block")(BLOCK_SIZE*static_cast<int>(bronze[i].getBlockColor()), 0, BLOCK_SIZE, BLOCK_SIZE).draw(boardVisualPoint +BLOCK_SIZE*(bronzeIndicator+bronze[i].getPoint()));
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
void Board::boardDraw() const {
|
338
|
+
|
339
|
+
for (int i = 0; i < BOARD_VISUAL_SIZE.x; i++) {
|
340
|
+
|
341
|
+
for (int j = 0; j < BOARD_VISUAL_SIZE.y; j++) {
|
342
|
+
|
343
|
+
if (static_cast<int>(board[i + 1][j + 1]) < 5) {
|
344
|
+
|
345
|
+
TextureAsset(L"Block")(BLOCK_SIZE*static_cast<int>(board[i + 1][j + 1]), 0, BLOCK_SIZE, BLOCK_SIZE).draw(boardVisualPoint + Point(i*BLOCK_SIZE, j*BLOCK_SIZE));
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
void Board::addBlockToBronze(Point coordinate,BlockColor blockColor) {
|
358
|
+
|
359
|
+
for (auto& x : bronze) {
|
360
|
+
|
361
|
+
x.setPoint(x.getPoint() + Point(0, 1));
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
bronze.emplace_back(Block(blockColor,coordinate));
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
void Board::moveBronzeIndicator(int direction) {
|
372
|
+
|
373
|
+
bronzeIndicator += Point(direction, 0);
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
void Board::dropBronze() {
|
380
|
+
|
381
|
+
Point dropBaseCoordinate = Point(0,0);
|
382
|
+
|
383
|
+
for (auto& x : bronze) {
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
// while (board[bronzeIndicator.x+1][(dropBaseCoordinate + bronze.front().getPoint()).y + 1] == BlockColor::NONE) {
|
390
|
+
|
391
|
+
//dropBaseCoordinate += Point(0, 1);
|
392
|
+
|
393
|
+
// }
|
394
|
+
|
395
|
+
for (auto& x : bronze) {
|
396
|
+
|
397
|
+
board[bronzeIndicator.x + 1][x.getPoint().y + dropBaseCoordinate.y + 1] = x.getBlockColor();
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
bronze[0];
|
404
|
+
|
405
|
+
bronzeInit();
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
bool Board::isCanAddBlockToBronze() {
|
412
|
+
|
413
|
+
if (bronze.size() < BRONZE_MAX_SIZE) {
|
414
|
+
|
415
|
+
bool isCanBronzeAdd = true;
|
416
|
+
|
417
|
+
for (auto& x : bronze) {
|
418
|
+
|
419
|
+
if (!isBronzeDontCollideWithBoard(x.getPoint() + bronzeIndicator + Point(0, 1))) {
|
420
|
+
|
421
|
+
isCanBronzeAdd = false;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
return isCanBronzeAdd;
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
return false;
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
bool Board::isCanMoveBronze(int direction) {
|
438
|
+
|
439
|
+
int valid = bronzeIndicator.x + direction;
|
440
|
+
|
441
|
+
if (valid >= 0 && valid < BOARD_VISUAL_SIZE.x) {
|
442
|
+
|
443
|
+
bool isCanBronzeMove = true;
|
444
|
+
|
445
|
+
for (auto& x : bronze) {
|
446
|
+
|
447
|
+
if (!isBronzeDontCollideWithBoard(x.getPoint()+bronzeIndicator+Point(direction,0))) {
|
448
|
+
|
449
|
+
isCanBronzeMove = false;
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
return isCanBronzeMove;
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
return false;
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
bool Board::isBronzeDontCollideWithBoard(Point _coordinate) {
|
466
|
+
|
467
|
+
if (board[_coordinate.x + 1][_coordinate.y + 1] == BlockColor::NONE) {
|
468
|
+
|
469
|
+
return true;
|
470
|
+
|
471
|
+
}
|
472
|
+
|
473
|
+
else {
|
474
|
+
|
475
|
+
return false;
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
```
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
###試したこと
|
486
|
+
|
487
|
+
brozneをfor文やfor auto文で回した場合エラーはありませんでした。
|
488
|
+
|
489
|
+
添字アクセスまたは、front()などでアクセスしてしまうとエラーが発生しました。
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
42
494
|
|
43
495
|
よろしくお願いいたします。
|