回答編集履歴

10

コード改善

2022/09/20 01:58

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -94,18 +94,18 @@
94
94
  const CHUNK_RANGE = 8;
95
95
  const CHUNK_SPLIT_MIN = 4;
96
96
 
97
- let initChunk = (xChunkLoop, yChunkLoop) => {
97
+ const initChunk = (xChunkLoop, yChunkLoop) => {
98
- let chunk = {
98
+ const chunk = {
99
99
  x:0,
100
100
  y:0,
101
101
  w:xChunkLoop,
102
102
  h:yChunkLoop
103
103
  };
104
- let chunks = [chunk];
104
+ const chunks = [chunk];
105
105
  return chunks;
106
106
  };
107
107
 
108
- let splitDangeon = (chunks, limit = 10) => {
108
+ const splitDangeon = (chunks, limit = 10) => {
109
109
  for (let i = 0; i < limit; i++) {
110
110
  const newIndex = chunks.length;
111
111
  const oldIndex = Math.floor(Math.random() * chunks.length);
@@ -113,23 +113,19 @@
113
113
  if(Math.random() > 0.5) {//縦
114
114
  let splitW = CHUNK_SPLIT_MIN;
115
115
  for(let i = 0; Math.random() > i; i += 0.1) splitW++;
116
-
117
- let width = w - splitW;
116
+ const width = w - splitW;
118
- if(width <= CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さなら分割しない
117
+ if(width >= CHUNK_SPLIT_MIN){//最小サイズより大きときに分割
119
- continue;
118
+ chunks[oldIndex] = { x, y, w: splitW, h };
119
+ chunks[newIndex] = { x: x + splitW, y, w: width, h };
120
120
  }
121
- chunks[newIndex] = { x: x + splitW, y, w: width, h };
122
- chunks[oldIndex] = { x, y, w: splitW, h };
123
121
  } else {//横
124
122
  let splitH = CHUNK_SPLIT_MIN;
125
123
  for(let i = 0; Math.random() > i; i += 0.1) splitH++;
126
-
127
- let height = h - splitH;
124
+ const height = h - splitH;
128
- if(height <= CHUNK_SPLIT_MIN){
125
+ if(height >= CHUNK_SPLIT_MIN){
129
- continue;
126
+ chunks[oldIndex] = { x, y, w, h: splitH };
127
+ chunks[newIndex] = { x, y: y + splitH, w, h: height };
130
128
  }
131
- chunks[newIndex] = { x, y: y + splitH, w, h: height };
132
- chunks[oldIndex] = { x, y, w, h: splitH };
133
129
  }
134
130
  }
135
131
  return chunks;
@@ -161,4 +157,5 @@
161
157
 
162
158
  dump(splitDangeon(initChunk(16, 16)));
163
159
  console.log('\n');
160
+
164
161
  ```

9

oldChunkをx,y,w,hに分割代入

2022/09/19 17:37

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -26,48 +26,28 @@
26
26
  };
27
27
 
28
28
  let splitDangeon = (chunks, oldIndex = 0) => {
29
- let newIndex = chunks.length;
29
+ const newIndex = chunks.length;
30
- let oldChunk = chunks[oldIndex];
30
+ const {x, y, w, h} = chunks[oldIndex];
31
31
  if(Math.random() > 0.5) {//縦
32
32
  let splitW = CHUNK_SPLIT_MIN;
33
33
  for(let i = 0; Math.random() > i; i += 0.1) splitW++;
34
34
 
35
- let width = oldChunk.w - splitW;
35
+ let width = w - splitW;
36
36
  if(width < CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さくなったら分割しない
37
37
  return chunks;
38
38
  }
39
- chunks[newIndex] = {
39
+ chunks[newIndex] = { x, y, w:splitW, h };
40
- x:oldChunk.x,
41
- y:oldChunk.y,
42
- w:splitW,
43
- h:oldChunk.h
44
- };
45
- chunks[oldIndex] = {
40
+ chunks[oldIndex] = { x:x+splitW, y, w:width, h };
46
- x:oldChunk.x+splitW,
47
- y:oldChunk.y,
48
- w:width,
49
- h:oldChunk.h
50
- };
51
41
  } else {//横
52
42
  let splitH = CHUNK_SPLIT_MIN;
53
43
  for(let i = 0; Math.random() > i; i += 0.1) splitH++;
54
44
 
55
- let height = oldChunk.h - splitH;
45
+ let height = h - splitH;
56
46
  if(height < CHUNK_SPLIT_MIN){
57
47
  return chunks;
58
48
  }
59
- chunks[newIndex] = {
49
+ chunks[newIndex] = { x, y, w, h:splitH };
60
- x:oldChunk.x,
61
- y:oldChunk.y,
62
- w:oldChunk.w,
63
- h:splitH
64
- };
65
- chunks[oldIndex] = {
50
+ chunks[oldIndex] = { x, y:y+splitH, w, h:height };
66
- x:oldChunk.x,
67
- y:oldChunk.y+splitH,
68
- w:oldChunk.w,
69
- h:height
70
- };
71
51
  }
72
52
  if(chunks.length < 10) {
73
53
  chunks = splitDangeon(chunks, oldIndex);

8

バグ修正

2022/09/19 17:34

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -29,30 +29,30 @@
29
29
  let newIndex = chunks.length;
30
30
  let oldChunk = chunks[oldIndex];
31
31
  if(Math.random() > 0.5) {//縦
32
- let splitX = CHUNK_SPLIT_MIN;
32
+ let splitW = CHUNK_SPLIT_MIN;
33
- for(let i = 0; Math.random() > i; i += 0.1) splitX++;
33
+ for(let i = 0; Math.random() > i; i += 0.1) splitW++;
34
34
 
35
- let width = oldChunk.w - splitX;
35
+ let width = oldChunk.w - splitW;
36
36
  if(width < CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さくなったら分割しない
37
37
  return chunks;
38
38
  }
39
39
  chunks[newIndex] = {
40
40
  x:oldChunk.x,
41
41
  y:oldChunk.y,
42
- w:splitX,
42
+ w:splitW,
43
43
  h:oldChunk.h
44
44
  };
45
45
  chunks[oldIndex] = {
46
- x:splitX,
46
+ x:oldChunk.x+splitW,
47
47
  y:oldChunk.y,
48
48
  w:width,
49
49
  h:oldChunk.h
50
50
  };
51
51
  } else {//横
52
- let splitY = CHUNK_SPLIT_MIN;
52
+ let splitH = CHUNK_SPLIT_MIN;
53
- for(let i = 0; Math.random() > i; i += 0.1) splitY++;
53
+ for(let i = 0; Math.random() > i; i += 0.1) splitH++;
54
54
 
55
- let height = oldChunk.h - splitY;
55
+ let height = oldChunk.h - splitH;
56
56
  if(height < CHUNK_SPLIT_MIN){
57
57
  return chunks;
58
58
  }
@@ -60,11 +60,11 @@
60
60
  x:oldChunk.x,
61
61
  y:oldChunk.y,
62
62
  w:oldChunk.w,
63
- h:splitY
63
+ h:splitH
64
64
  };
65
65
  chunks[oldIndex] = {
66
66
  x:oldChunk.x,
67
- y:splitY,
67
+ y:oldChunk.y+splitH,
68
68
  w:oldChunk.w,
69
69
  h:height
70
70
  };
@@ -78,7 +78,31 @@
78
78
  return chunks;
79
79
  };
80
80
 
81
+ const dump = (chunks, w, h) => {
82
+ console.log(chunks);
83
+ const width = Math.max(...chunks.map(chunk => chunk.x + chunk.w));
84
+ const height = Math.max(...chunks.map(chunk => chunk.y + chunk.h));
85
+ const map = Array(height).fill([]).map(row => Array(width).fill(' '));
86
+ chunks.forEach(chunk => {
87
+ for (let x = chunk.x; x < chunk.x + chunk.w; x++) {
88
+ map[chunk.y][x] = '-';
89
+ map[chunk.y + chunk.h - 1][x] = '-';
90
+ }
91
+ for (let y = chunk.y; y < chunk.y + chunk.h; y++) {
92
+ map[y][chunk.x] = '|';
93
+ map[y][chunk.x + chunk.w - 1] = '|';
94
+ }
95
+ map[chunk.y][chunk.x] = '+';
96
+ map[chunk.y][chunk.x + chunk.w - 1] = '+';
97
+ map[chunk.y + chunk.h - 1][chunk.x] = '+';
98
+ map[chunk.y + chunk.h - 1][chunk.x + chunk.w - 1] = '+';
99
+ });
100
+ for (const row of map) {
101
+ console.log(row.join(' '));
102
+ }
103
+ }
104
+
81
- console.log(splitDangeon(initChunk(16, 16)));
105
+ dump(splitDangeon(initChunk(16, 16)));
82
106
  console.log('\n');
83
107
  ```
84
108
 

7

バグ修正

2022/09/19 17:31

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -82,7 +82,7 @@
82
82
  console.log('\n');
83
83
  ```
84
84
 
85
- 修正案: 偏らないように分割位置をランダムに選択(再帰呼び出ししない)
85
+ 修正案2: 偏らないように分割位置をランダムに選択(再帰呼び出ししない)
86
86
 
87
87
  ```js
88
88
  'use strict';
@@ -105,52 +105,56 @@
105
105
  for (let i = 0; i < limit; i++) {
106
106
  const newIndex = chunks.length;
107
107
  const oldIndex = Math.floor(Math.random() * chunks.length);
108
- const oldChunk = chunks[oldIndex];
108
+ const {x, y, w, h} = chunks[oldIndex];
109
109
  if(Math.random() > 0.5) {//縦
110
- let splitX = CHUNK_SPLIT_MIN;
110
+ let splitW = CHUNK_SPLIT_MIN;
111
- for(let i = 0; Math.random() > i; i += 0.1) splitX++;
111
+ for(let i = 0; Math.random() > i; i += 0.1) splitW++;
112
112
 
113
- let width = oldChunk.w - splitX;
113
+ let width = w - splitW;
114
- if(width < CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さいなら分割しない
114
+ if(width <= CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さいなら分割しない
115
115
  continue;
116
116
  }
117
- chunks[newIndex] = {
117
+ chunks[newIndex] = { x: x + splitW, y, w: width, h };
118
- x:oldChunk.x,
119
- y:oldChunk.y,
120
- w:splitX,
121
- h:oldChunk.h
122
- };
123
- chunks[oldIndex] = {
118
+ chunks[oldIndex] = { x, y, w: splitW, h };
124
- x:splitX,
125
- y:oldChunk.y,
126
- w:width,
127
- h:oldChunk.h
128
- };
129
119
  } else {//横
130
- let splitY = CHUNK_SPLIT_MIN;
120
+ let splitH = CHUNK_SPLIT_MIN;
131
- for(let i = 0; Math.random() > i; i += 0.1) splitY++;
121
+ for(let i = 0; Math.random() > i; i += 0.1) splitH++;
132
122
 
133
- let height = oldChunk.h - splitY;
123
+ let height = h - splitH;
134
- if(height < CHUNK_SPLIT_MIN){
124
+ if(height <= CHUNK_SPLIT_MIN){
135
125
  continue;
136
126
  }
137
- chunks[newIndex] = {
127
+ chunks[newIndex] = { x, y: y + splitH, w, h: height };
138
- x:oldChunk.x,
139
- y:oldChunk.y,
140
- w:oldChunk.w,
141
- h:splitY
142
- };
143
- chunks[oldIndex] = {
128
+ chunks[oldIndex] = { x, y, w, h: splitH };
144
- x:oldChunk.x,
145
- y:splitY,
146
- w:oldChunk.w,
147
- h:height
148
- };
149
129
  }
150
130
  }
151
131
  return chunks;
152
132
  };
153
133
 
134
+ const dump = (chunks, w, h) => {
135
+ console.log(chunks);
136
+ const width = Math.max(...chunks.map(chunk => chunk.x + chunk.w));
137
+ const height = Math.max(...chunks.map(chunk => chunk.y + chunk.h));
138
+ const map = Array(height).fill([]).map(row => Array(width).fill(' '));
139
+ chunks.forEach(chunk => {
140
+ for (let x = chunk.x; x < chunk.x + chunk.w; x++) {
141
+ map[chunk.y][x] = '-';
142
+ map[chunk.y + chunk.h - 1][x] = '-';
143
+ }
144
+ for (let y = chunk.y; y < chunk.y + chunk.h; y++) {
145
+ map[y][chunk.x] = '|';
146
+ map[y][chunk.x + chunk.w - 1] = '|';
147
+ }
148
+ map[chunk.y][chunk.x] = '+';
149
+ map[chunk.y][chunk.x + chunk.w - 1] = '+';
150
+ map[chunk.y + chunk.h - 1][chunk.x] = '+';
151
+ map[chunk.y + chunk.h - 1][chunk.x + chunk.w - 1] = '+';
152
+ });
153
+ for (const row of map) {
154
+ console.log(row.join(' '));
155
+ }
156
+ }
157
+
154
- console.log(splitDangeon(initChunk(16, 16)));
158
+ dump(splitDangeon(initChunk(16, 16)));
155
159
  console.log('\n');
156
160
  ```

6

分割が偏らない案を追記

2022/09/19 16:46

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -6,7 +6,7 @@
6
6
  2 2 undefined
7
7
  ```
8
8
 
9
- 修正案:
9
+ 修正案1 分割箇所が偏ってしまう懸念あり
10
10
 
11
11
  ```js
12
12
  'use strict';
@@ -82,3 +82,75 @@
82
82
  console.log('\n');
83
83
  ```
84
84
 
85
+ 修正案2: 偏らないように分割位置をランダムに選択(再帰呼び出ししない)
86
+
87
+ ```js
88
+ 'use strict';
89
+
90
+ const CHUNK_RANGE = 8;
91
+ const CHUNK_SPLIT_MIN = 4;
92
+
93
+ let initChunk = (xChunkLoop, yChunkLoop) => {
94
+ let chunk = {
95
+ x:0,
96
+ y:0,
97
+ w:xChunkLoop,
98
+ h:yChunkLoop
99
+ };
100
+ let chunks = [chunk];
101
+ return chunks;
102
+ };
103
+
104
+ let splitDangeon = (chunks, limit = 10) => {
105
+ for (let i = 0; i < limit; i++) {
106
+ const newIndex = chunks.length;
107
+ const oldIndex = Math.floor(Math.random() * chunks.length);
108
+ const oldChunk = chunks[oldIndex];
109
+ if(Math.random() > 0.5) {//縦
110
+ let splitX = CHUNK_SPLIT_MIN;
111
+ for(let i = 0; Math.random() > i; i += 0.1) splitX++;
112
+
113
+ let width = oldChunk.w - splitX;
114
+ if(width < CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さいなら分割しない
115
+ continue;
116
+ }
117
+ chunks[newIndex] = {
118
+ x:oldChunk.x,
119
+ y:oldChunk.y,
120
+ w:splitX,
121
+ h:oldChunk.h
122
+ };
123
+ chunks[oldIndex] = {
124
+ x:splitX,
125
+ y:oldChunk.y,
126
+ w:width,
127
+ h:oldChunk.h
128
+ };
129
+ } else {//横
130
+ let splitY = CHUNK_SPLIT_MIN;
131
+ for(let i = 0; Math.random() > i; i += 0.1) splitY++;
132
+
133
+ let height = oldChunk.h - splitY;
134
+ if(height < CHUNK_SPLIT_MIN){
135
+ continue;
136
+ }
137
+ chunks[newIndex] = {
138
+ x:oldChunk.x,
139
+ y:oldChunk.y,
140
+ w:oldChunk.w,
141
+ h:splitY
142
+ };
143
+ chunks[oldIndex] = {
144
+ x:oldChunk.x,
145
+ y:splitY,
146
+ w:oldChunk.w,
147
+ h:height
148
+ };
149
+ }
150
+ }
151
+ return chunks;
152
+ };
153
+
154
+ console.log(splitDangeon(initChunk(16, 16)));
155
+ console.log('\n');
156
+ ```

5

コード修正

2022/09/19 16:23

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -33,7 +33,7 @@
33
33
  for(let i = 0; Math.random() > i; i += 0.1) splitX++;
34
34
 
35
35
  let width = oldChunk.w - splitX;
36
- if(width <= CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さくなったら分割しない
36
+ if(width < CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さくなったら分割しない
37
37
  return chunks;
38
38
  }
39
39
  chunks[newIndex] = {
@@ -53,7 +53,7 @@
53
53
  for(let i = 0; Math.random() > i; i += 0.1) splitY++;
54
54
 
55
55
  let height = oldChunk.h - splitY;
56
- if(height <= CHUNK_SPLIT_MIN){
56
+ if(height < CHUNK_SPLIT_MIN){
57
57
  return chunks;
58
58
  }
59
59
  chunks[newIndex] = {
@@ -70,10 +70,10 @@
70
70
  };
71
71
  }
72
72
  if(chunks.length < 10) {
73
- splitDangeon(chunks, oldIndex);
73
+ chunks = splitDangeon(chunks, oldIndex);
74
74
  }
75
75
  if(chunks.length < 10) {
76
- splitDangeon(chunks, newIndex);
76
+ chunks = splitDangeon(chunks, newIndex);
77
77
  }
78
78
  return chunks;
79
79
  };

4

修正案追記

2022/09/19 16:17

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -6,3 +6,79 @@
6
6
  2 2 undefined
7
7
  ```
8
8
 
9
+ 修正案:
10
+
11
+ ```js
12
+ 'use strict';
13
+
14
+ const CHUNK_RANGE = 8;
15
+ const CHUNK_SPLIT_MIN = 4;
16
+
17
+ let initChunk = (xChunkLoop, yChunkLoop) => {
18
+ let chunk = {
19
+ x:0,
20
+ y:0,
21
+ w:xChunkLoop,
22
+ h:yChunkLoop
23
+ };
24
+ let chunks = [chunk];
25
+ return chunks;
26
+ };
27
+
28
+ let splitDangeon = (chunks, oldIndex = 0) => {
29
+ let newIndex = chunks.length;
30
+ let oldChunk = chunks[oldIndex];
31
+ if(Math.random() > 0.5) {//縦
32
+ let splitX = CHUNK_SPLIT_MIN;
33
+ for(let i = 0; Math.random() > i; i += 0.1) splitX++;
34
+
35
+ let width = oldChunk.w - splitX;
36
+ if(width <= CHUNK_SPLIT_MIN){//チャンクが指定したサイズより小さくなったら分割しない
37
+ return chunks;
38
+ }
39
+ chunks[newIndex] = {
40
+ x:oldChunk.x,
41
+ y:oldChunk.y,
42
+ w:splitX,
43
+ h:oldChunk.h
44
+ };
45
+ chunks[oldIndex] = {
46
+ x:splitX,
47
+ y:oldChunk.y,
48
+ w:width,
49
+ h:oldChunk.h
50
+ };
51
+ } else {//横
52
+ let splitY = CHUNK_SPLIT_MIN;
53
+ for(let i = 0; Math.random() > i; i += 0.1) splitY++;
54
+
55
+ let height = oldChunk.h - splitY;
56
+ if(height <= CHUNK_SPLIT_MIN){
57
+ return chunks;
58
+ }
59
+ chunks[newIndex] = {
60
+ x:oldChunk.x,
61
+ y:oldChunk.y,
62
+ w:oldChunk.w,
63
+ h:splitY
64
+ };
65
+ chunks[oldIndex] = {
66
+ x:oldChunk.x,
67
+ y:splitY,
68
+ w:oldChunk.w,
69
+ h:height
70
+ };
71
+ }
72
+ if(chunks.length < 10) {
73
+ splitDangeon(chunks, oldIndex);
74
+ }
75
+ if(chunks.length < 10) {
76
+ splitDangeon(chunks, newIndex);
77
+ }
78
+ return chunks;
79
+ };
80
+
81
+ console.log(splitDangeon(initChunk(16, 16)));
82
+ console.log('\n');
83
+ ```
84
+

3

説明訂正

2022/09/19 15:47

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,5 +1,4 @@
1
1
  ` console.log(count, chunks.length, chunks[count])` にしてみたところ、未定義のcountでしたよ。
2
- 10個までに限定したいならcountは不要にして `if (chunks.ength < 10) {` にしてはいかがですか?
3
2
 
4
3
  ```js
5
4
  0 1 { x: 0, y: 0, w: 16, h: 16 }

2

追記

2022/09/19 15:44

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,9 +1,9 @@
1
- ` console.log(count, chunks[count])` してみたところ、未定義のcountでしたよ。
1
+ ` console.log(count, chunks.length, chunks[count])` してみたところ、未定義のcountでしたよ。
2
2
  10個までに限定したいならcountは不要にして `if (chunks.ength < 10) {` にしてはいかがですか?
3
3
 
4
4
  ```js
5
- 0 { x: 0, y: 0, w: 16, h: 16 }
5
+ 0 1 { x: 0, y: 0, w: 16, h: 16 }
6
- 1 { x: 0, y: 0, w: 7, h: 16 }
6
+ 1 2 { x: 0, y: 0, w: 6, h: 16 }
7
- 2 undefined
7
+ 2 2 undefined
8
8
  ```
9
9
 

1

追記

2022/09/19 15:43

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,4 +1,5 @@
1
1
  ` console.log(count, chunks[count])` してみたところ、未定義のcountでしたよ。
2
+ 10個までに限定したいならcountは不要にして `if (chunks.ength < 10) {` にしてはいかがですか?
2
3
 
3
4
  ```js
4
5
  0 { x: 0, y: 0, w: 16, h: 16 }