-
Notifications
You must be signed in to change notification settings - Fork 5
/
generator.go
141 lines (112 loc) · 2.7 KB
/
generator.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package camp
import (
"math"
"github.com/cheatsnake/shadify/internal/helpers"
)
func generateField(w, h, t int) [][]int {
field := make([][]int, h)
for i := range field {
field[i] = make([]int, w)
}
for t > 0 {
treeY := helpers.GetRandomInteger(0, h-1)
treeX := helpers.GetRandomInteger(0, w-1)
isTreeFit := checkIsAreaFit(treeX, treeY, field, false, true)
if !isTreeFit {
continue
}
field[treeY][treeX] = treeValue
tentX, tentY, isTentFit := placeTent(treeX, treeY, field)
if !isTentFit {
field[treeY][treeX] = emptyValue
continue
}
field[tentY][tentX] = tentValue
t--
}
return field
}
func calcTreesCount(w, h int) int {
return int(math.Floor(float64(w*h) * percentageOfTrees))
}
func checkIsCoordsFit(x, y int, field [][]int) bool {
return x >= 0 && y >= 0 && x < len(field[0]) && y < len(field)
}
func placeTent(x, y int, field [][]int) (int, int, bool) {
possibleCoords := [][]int{{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}}
isCoordsFit := false
var randCoords []int
var tentX, tentY int
for len(possibleCoords) > 0 {
randIndex := helpers.GetRandomInteger(0, len(possibleCoords)-1)
randCoords = possibleCoords[randIndex]
tentX, tentY = randCoords[0], randCoords[1]
isCoordsFit = checkIsCoordsFit(tentX, tentY, field)
if isCoordsFit {
break
}
possibleCoords = helpers.RemoveElement(possibleCoords, randIndex)
}
if !isCoordsFit {
return 0, 0, isCoordsFit
}
isTentFit := checkIsAreaFit(tentX, tentY, field, false, false)
return tentX, tentY, isTentFit
}
func checkIsAreaFit(srcX, srcY int, field [][]int, isVerify, isTree bool) bool {
treesAround := 0
tentsAround := 0
coordsCount := 0
if field[srcY][srcX] != emptyValue {
return false
}
checkArea := func(x, y int) {
if checkIsCoordsFit(x, y, field) {
coordsCount++
if field[y][x] == treeValue {
treesAround++
}
if field[y][x] == tentValue {
tentsAround++
}
}
}
checkArea(srcX-1, srcY)
checkArea(srcX+1, srcY)
checkArea(srcX, srcY-1)
checkArea(srcX, srcY+1)
if isVerify {
return tentsAround == 0
}
if isTree {
return treesAround+tentsAround < coordsCount
}
return treesAround < coordsCount && tentsAround == 0
}
func countTents(field [][]int) ([]int, []int) {
row := make([]int, len(field))
column := make([]int, len(field[0]))
for i := range field {
for j := range field[0] {
if field[i][j] == tentValue {
column[j]++
row[i]++
}
}
}
return row, column
}
func hideTents(field [][]int) [][]int {
task := make([][]int, len(field))
for i := range task {
task[i] = make([]int, len(field[0]))
}
for i := range field {
for j := range field[0] {
if field[i][j] == treeValue {
task[i][j] = treeValue
}
}
}
return task
}