-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathMineMapGenerator.kt
More file actions
55 lines (41 loc) · 1.8 KB
/
Copy pathMineMapGenerator.kt
File metadata and controls
55 lines (41 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package domain
import util.RandomLocationGenerator
object MineMapGenerator {
fun createMineMap(height: Int, width: Int, numOfMine: Int): MineMap {
require(numOfMine <= height * width) { "지뢰의 수는 지도의 크기(가로*세로)보다 많을 수 없습니다." }
// mine 위치 정보 받아오기
val mineLocations = mutableSetOf<Location>()
while (mineLocations.size < numOfMine) {
val locationGenerator = RandomLocationGenerator(height, width)
val randomLocation = locationGenerator.generate()
mineLocations.add(randomLocation)
}
// mine 위치 정보를 바탕으로 지도 제작
val elementLocations = mutableListOf<List<MapElement>>()
for (i in 0 until height) {
elementLocations += createRow(i, width, mineLocations)
}
val mineMap = MineMap(elementLocations.toList())
// EmptyElement에 대해 주변 지뢰의 수 기입
mineMap.elements.flatMap { innerList -> innerList.filterIsInstance<EmptyElement>() }
.forEach { emptyElement -> emptyElement.countMine(mineMap) }
return mineMap
}
private fun createRow(
rowNum: Int,
rowLen: Int,
mineLocations: MutableSet<Location>
): List<MapElement> {
val row = mutableListOf<MapElement>()
// 해당 row에서 mine의 index만 가지는 set
val minesOfRow = mineLocations.filter { it.row == rowNum }
.map { it.column }
for (y in 0 until rowLen) {
row += getElement(rowNum, y, minesOfRow)
}
return row.toList()
}
private fun getElement(x: Int, y: Int, minesOfRow: List<Int>): MapElement {
return if (minesOfRow.contains(y)) Mine.create(x, y) else EmptyElement.create(x, y)
}
}