Skip to content

Commit

Permalink
fix generate maze func and test
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuyoshi30 committed Nov 30, 2019
1 parent b8a5907 commit f43b6e3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 90 deletions.
57 changes: 28 additions & 29 deletions board.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,25 @@ func (m *Maze) Resize() {
}

func (m *Maze) Generate() {
h := m.Height
w := m.Width

wall := make([]*Point, 0) // wall
cand := make([]*Point, 0) // wall candidate

h := m.Height
w := m.Width
m.Points = make([][]*Point, h)
for i := 0; i < h; i++ {
m.Points[i] = make([]*Point, w)
for j := 0; j < w; j++ {
p := &Point{
x: j,
y: i,
}
m.Points = make([][]*Point, m.Height)
for i := 0; i < m.Height; i++ {
m.Points[i] = make([]*Point, m.Width)
for j := 0; j < m.Width; j++ {
p := &Point{x: j, y: i}

if i == 1 && j == 0 {
p.status = START
wall = append(wall, p)
} else if i == h-2 && j == w-1 {
} else if i == m.Height-2 && j == m.Width-1 {
p.status = GOAL
wall = append(wall, p)
} else if i == 0 || i == h-1 || j == 0 || j == w-1 {
} else if i == 0 || i == m.Height-1 || j == 0 || j == m.Width-1 {
p.status = WALL
wall = append(wall, p)
} else {
Expand All @@ -90,13 +88,13 @@ func (m *Maze) Generate() {
m.Points[i][j] = p
}
}
if m.Seed {
rand.NewSource(1) // for test
} else {
rand.Seed(time.Now().UnixNano())
}

for len(cand) > 0 {
if m.Seed {
rand.NewSource(1) // for test
} else {
rand.Seed(time.Now().UnixNano())
}
r := rand.Intn(len(cand))
cp := cand[r]
cand = append(cand[:r], cand[r+1:]...) // remove cp
Expand All @@ -111,8 +109,8 @@ func (m *Maze) Generate() {
dy := [4]int{0, 0, 1, -1}

for {
kw := make([]*Point, 0)
kkw := make([]*Point, 0)
kw := make([]*Point, 0) // 1つ隣
kkw := make([]*Point, 0) // 2つ隣
for i := 0; i < 4; i++ {
_y := cp.y + dy[i]
_x := cp.x + dx[i]
Expand All @@ -123,7 +121,7 @@ func (m *Maze) Generate() {
if 0 <= __y && __y < h && 0 <= __x && __x < w {
nnp := m.Points[__y][__x]

if np.status == PATH && nnp.status != CURRENT {
if np.status == PATH && nnp.status != CURRENT { // 1つ隣が道で、2つ隣が現在地じゃない
kw = append(kw, np)
kkw = append(kkw, nnp)
}
Expand All @@ -133,9 +131,8 @@ func (m *Maze) Generate() {

if len(kw) > 0 {
// 候補から進む方向をランダムに選定
dr := rand.Intn(len(kw))
dp := kw[dr]
ddp := kkw[dr]
dp := kw[rand.Intn(len(kw))]
ddp := kkw[rand.Intn(len(kw))]

if ddp.status == WALL {
for _, c := range current {
Expand All @@ -150,13 +147,15 @@ func (m *Maze) Generate() {
cp = ddp
}
} else {
ddp := current[len(current)-1]
ddp.status = PATH
dp := current[len(current)-2]
dp.status = PATH
current = current[:len(current)-2]
if len(current)-2 > 0 {
ddp := current[len(current)-1]
ddp.status = PATH
dp := current[len(current)-2]
dp.status = PATH
current = current[:len(current)-2]

cp = ddp
cp = ddp
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/default.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gomaze --seed
../build/gomaze --seed
54 changes: 27 additions & 27 deletions test/default.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
S || || || || || ||
|| |||||| |||||||||| || |||||| |||||||||||||||||| ||
|| || || || || || ||
|| || || |||||| || |||||||||||||| |||||||||||||| ||
|| || || || || || || || || ||
|| || || |||||| || |||||||||| |||||| || || || ||
|| || || || || || || || ||
|||||| || |||||||||| || || |||||| || |||||| ||||||
|| || || || || || || || || ||
|| |||||| || |||||||||| |||||| |||||||||| || || ||
|| || || || || || ||
|||||| || |||||| |||||||||| || || || || |||||| ||
|| || || || || || || || ||
|| |||||||||| || |||||| |||||||||| |||||| || ||||||
|| || || || || ||
|| |||||| || |||||| |||||| |||||||||| || ||||||||||
|| || || || || || || || ||
|| || |||||| |||||||||| || || |||||||||| |||||| ||
|| || || || || || || || || || ||
|| |||||| |||||| || || |||||| || || || || ||||||
|| || || || || || || || ||
|||||| |||||| || || || |||||||||| || |||||| || ||
|| || || || || || || || || ||
|| || |||||| |||||||||| |||||||||||||| |||||||||| ||
|| || || || ||
|||||| |||||| |||||||||| |||||||||| || || || || ||
|| || || || || || || || G
S || || || || || || ||
|| || || || |||||| || || |||||| |||||| ||||||||||
|| || || || || || || ||
|| || |||||||||| |||||| || |||||||||||||| || || ||
|| || || || || || ||
|| || || || || |||||||||||||||||||||| |||||| ||||||
|| || || || || || || ||
|| || |||||| |||||| |||||| |||||||||| || ||||||||||
|| || || || || || || || || ||
|| |||||||||| || || || |||||||||| || |||||| ||||||
|| || || || || || || || ||
|| || |||||| || |||||| |||||||||| || || |||||| ||
|| || || || || || || || ||
|| |||||| |||||| |||||||||| || |||||| || ||||||||||
|| || || || || || || ||
|| || || |||||| |||||| |||||||||||||| |||||| || ||
|| || || || || || ||
|||||| |||||||||||||||||||||| || |||||| || |||||| ||
|| || || || || || || ||
|| |||||| |||||||||||||| || || |||||||||| || ||||||
|| || || || || || || ||
|||||||||| || || |||||||||| |||||| || ||||||||||||||
|| || || || || || || || ||
|| || || || |||||||||||||||||| || || |||||| || ||
|| || || || || || ||
|||||| || |||||| |||||| |||||||||| |||||||||||||| ||
|| || || || || G
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 changes: 1 addition & 1 deletion test/setHeight.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gomaze --seed --height 10
../build/gomaze --seed --height 10
14 changes: 7 additions & 7 deletions test/setHeight.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
S || || || || || || ||
|| |||||| || |||||||||||||| |||||| || || || || ||
|| || || || || || || ||
|||||| |||||| || || |||||||||||||| |||||||||||||| ||
|| || || || ||
|||||||||||||| |||||||||||||| |||||| || || |||||| ||
|| || || || G
S || || || || || ||
|| |||||||||| || || || |||||| |||||| || |||||| ||
|| || || || || || || || ||
|| |||||| |||||| |||||| |||||||||||||| || |||||| ||
|| || || || || || || ||
|| || |||||||||| || || || |||||| |||||| |||||| ||
|| || || || || G
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 changes: 1 addition & 1 deletion test/setSize.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gomaze --seed --height 10 --width 10
../build/gomaze --seed --height 10 --width 10
8 changes: 4 additions & 4 deletions test/setSize.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
||||||||||||||||||
S || || ||
|| || |||||| ||
|| |||||| || ||
|| || ||
|||||| |||||| ||
|||||||||| || ||
|| || ||
|||||| || ||||||
|| G
|||||| || || ||
|| || G
||||||||||||||||||
2 changes: 1 addition & 1 deletion test/setWidth.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gomaze --seed --width 10
../build/gomaze --seed --width 10
38 changes: 19 additions & 19 deletions test/setWidth.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
||||||||||||||||||
S || ||
|| || || || ||
S ||
|||||| ||||||||||
|| || || ||
|||||| |||||| ||
|| || ||
|| |||||| || ||
|| || || ||
|||||| |||||| ||
|| || |||||| ||
|| ||
|| |||||| ||||||
|| || || ||
|||||| || || ||
|| || ||
|||||| || ||||||
|| || || ||
|| |||||||||| ||
|| || || ||
|| || |||||| ||
|| || || || ||
|||||| || || ||
|| || ||
|||||| |||||| ||
|| || || || ||
|| || || || ||
|| |||||| || ||
|| || ||
|| |||||||||| ||
|||||||||| || ||
|| || ||
|||||| || || ||
|| || ||
|| |||||| || ||
|| ||||||||||||||
|| || || ||
|| || || ||||||
|| || || ||
|||||| || || ||
|| || || ||
|| || || || ||
|| || || G
|| ||
|| || || ||||||
|| || || G
||||||||||||||||||

0 comments on commit f43b6e3

Please sign in to comment.