Day 13 Complete

Fancy Maze Map
This commit is contained in:
Brian Buller 2016-12-13 11:27:57 -06:00
parent 46b89e4c56
commit a42f628602
2 changed files with 38 additions and 19 deletions

Binary file not shown.

View File

@ -9,8 +9,11 @@ import (
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
termbox "github.com/nsf/termbox-go"
) )
var tWidth, tHeight int
// Puzzle 1 Input: 1364 31 39 // Puzzle 1 Input: 1364 31 39
// Puzzle 1 Test Input: 10 7 4 // Puzzle 1 Test Input: 10 7 4
func main() { func main() {
@ -26,6 +29,11 @@ func main() {
} }
f := CreateFloor(1, 1, destX, destY, seed) f := CreateFloor(1, 1, destX, destY, seed)
if err := termbox.Init(); err != nil {
panic(err)
}
tWidth, tHeight = termbox.Size()
termbox.Close()
switch mode { switch mode {
case "solve": case "solve":
if f.Solve(f.start.x, f.start.y, 0, true) { if f.Solve(f.start.x, f.start.y, 0, true) {
@ -76,15 +84,7 @@ func (p *Path) Pop() Coord {
return ret return ret
} }
func (p *Path) ContainsCoord(c *Coord) bool { func (p *Path) ContainsCoord(x, y int) bool {
for i := range p.coords {
if p.coords[i].Equals(c) {
return true
}
}
return false
}
func (p *Path) ContainsCoordXY(x, y int) bool {
for i := range p.coords { for i := range p.coords {
if p.coords[i].Is(x, y) { if p.coords[i].Is(x, y) {
return true return true
@ -122,7 +122,7 @@ func CreateFloor(stX, stY, endX, endY, seed int) *Floor {
func (f *Floor) Walk(x, y, dist, maxDist int, print bool) { func (f *Floor) Walk(x, y, dist, maxDist int, print bool) {
wrkCoord := Coord{x, y, dist} wrkCoord := Coord{x, y, dist}
if f.IsWall(x, y) || f.testedPath.ContainsCoordXY(x, y) { if f.IsWall(x, y) || f.testedPath.ContainsCoord(x, y) {
return return
} }
if dist == maxDist { if dist == maxDist {
@ -182,7 +182,7 @@ func (f *Floor) Solve(x, y, dist int, print bool) bool {
if f.end.Is(x, y) { if f.end.Is(x, y) {
return true return true
} }
if f.IsWall(x, y) || f.testedPath.ContainsCoordXY(x, y) { if f.IsWall(x, y) || f.testedPath.ContainsCoord(x, y) {
return false return false
} }
// Test if there is a shorter path to this coordinate // Test if there is a shorter path to this coordinate
@ -266,7 +266,7 @@ func (f *Floor) Solve(x, y, dist int, print bool) bool {
// So we have a hard cutoff at: // So we have a hard cutoff at:
var MaxInt = int(^uint(0) >> 1) var MaxInt = int(^uint(0) >> 1)
if len(f.testedPath.coords) >= MaxInt { if len(f.testedPath.coords) >= MaxInt {
fmt.Println("Couldn't find a path.") fmt.Println("ERROR: Couldn't find a path.")
os.Exit(1) os.Exit(1)
} }
if f.Solve(x+1, y, wrkCoord.dist+1, print) { if f.Solve(x+1, y, wrkCoord.dist+1, print) {
@ -295,19 +295,38 @@ func (f *Floor) Print() {
g := color.New(color.BgGreen).Add(color.FgBlack) g := color.New(color.BgGreen).Add(color.FgBlack)
g.Add(color.Bold) g.Add(color.Bold)
r := color.New(color.BgRed) r := color.New(color.BgRed)
topY, topX := f.end.y+10, f.end.x+10 b := color.New(color.BgBlue).Add(color.BgBlack)
b.Add(color.Bold)
for y := 0; y < topY; y++ { topY, topX := tHeight, tWidth
for x := 0; x < topX; x++ { botY, botX := 0, 0
// We want to center approx 20x20 on our current location
// f.testedPath[len(f.testedPath)-1]?
if len(f.testedPath.coords) > 0 {
cntrCoord := f.testedPath.coords[len(f.testedPath.coords)-1]
if topY < cntrCoord.y+(tHeight/2) {
topY = cntrCoord.y + (tHeight / 2)
}
if topY > tHeight {
botY = topY - tHeight
}
if topX < cntrCoord.x+(tWidth/2) {
topX = cntrCoord.x + (tWidth / 2)
}
if topX > tWidth {
botX = topX - tWidth
}
}
for y := botY; y < topY; y++ {
for x := botX; x < topX; x++ {
if f.dispCoord != nil && f.dispCoord.Is(x, y) { if f.dispCoord != nil && f.dispCoord.Is(x, y) {
g.Print("O") g.Print("O")
} else if f.solvePath.ContainsCoordXY(x, y) { } else if f.solvePath.ContainsCoord(x, y) {
g.Print(".") g.Print(".")
} else if f.testedPath.ContainsCoordXY(x, y) { } else if f.testedPath.ContainsCoord(x, y) {
r.Print(" ") r.Print(" ")
} else { } else {
if f.end.Is(x, y) { if f.end.Is(x, y) {
space.Print("X") b.Print("X")
} else if f.IsWall(x, y) { } else if f.IsWall(x, y) {
wall.Print(" ") wall.Print(" ")
} else { } else {