Added Force Quit to intcode processor
This commit is contained in:
@@ -8,10 +8,13 @@ import (
|
||||
"time"
|
||||
|
||||
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
|
||||
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
var auto bool
|
||||
|
||||
var maze *Maze
|
||||
|
||||
func main() {
|
||||
progFileName := "input"
|
||||
|
||||
@@ -19,20 +22,49 @@ func main() {
|
||||
auto = true
|
||||
}
|
||||
prog := intcode.ReadIntCodeFile(progFileName)
|
||||
maze = NewMaze()
|
||||
|
||||
play(prog)
|
||||
}
|
||||
|
||||
func play(prog []int) {
|
||||
p := intcode.NewProgram(prog)
|
||||
go func() {
|
||||
var roadTaken []helpers.Coordinate
|
||||
for {
|
||||
time.Sleep(500)
|
||||
fmt.Println(helpers.CLEAR_SCREEN)
|
||||
maze.Print()
|
||||
for !p.NeedsInput() {
|
||||
time.Sleep(1)
|
||||
}
|
||||
var movingDir int
|
||||
var moveToCoord *helpers.Coordinate
|
||||
if auto {
|
||||
var picked bool
|
||||
directions := []int{DIR_N, DIR_E, DIR_S, DIR_W}
|
||||
// If we have an unexplored location, try it
|
||||
for _, tryDir := range directions {
|
||||
v := maze.GetCoord(maze.GetDirFromBot(tryDir))
|
||||
if v == MAZE_UNKNOWN {
|
||||
movingDir = tryDir
|
||||
picked = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !picked {
|
||||
movingDir = maze.GetDirectionToLast()
|
||||
if movingDir == -1 {
|
||||
fmt.Println("Maze Created")
|
||||
p.ForceQuit()
|
||||
return
|
||||
}
|
||||
picked = true
|
||||
}
|
||||
moveToCoord = maze.GetDirFromBot(movingDir)
|
||||
} else {
|
||||
var gotInput bool
|
||||
for !gotInput {
|
||||
for movingDir == 0 {
|
||||
fmt.Print("Input (vimlike): ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
inp, err := reader.ReadString('\n')
|
||||
@@ -41,38 +73,40 @@ func play(prog []int) {
|
||||
}
|
||||
inp = strings.TrimSpace(inp)
|
||||
switch inp {
|
||||
case "h":
|
||||
p.Input(3)
|
||||
gotInput = true
|
||||
case "j":
|
||||
p.Input(2)
|
||||
gotInput = true
|
||||
case "k":
|
||||
p.Input(1)
|
||||
gotInput = true
|
||||
case "l":
|
||||
p.Input(4)
|
||||
gotInput = true
|
||||
case "h": // West
|
||||
movingDir = DIR_W
|
||||
moveToCoord = maze.bot.GetWestCoord()
|
||||
case "j": // South
|
||||
movingDir = DIR_S
|
||||
moveToCoord = maze.bot.GetSouthCoord()
|
||||
case "k": // North
|
||||
movingDir = DIR_N
|
||||
moveToCoord = maze.bot.GetNorthCoord()
|
||||
case "l": // East
|
||||
movingDir = DIR_E
|
||||
moveToCoord = maze.bot.GetEastCoord()
|
||||
}
|
||||
}
|
||||
}
|
||||
p.Input(movingDir)
|
||||
for !p.NeedsOutput() {
|
||||
time.Sleep(1)
|
||||
}
|
||||
moveRes := p.Output()
|
||||
maze.SetCoord(moveToCoord, moveRes)
|
||||
switch moveRes {
|
||||
case 0: // Hit a wall
|
||||
fmt.Println("WALL")
|
||||
case 1: // Moved
|
||||
fmt.Println("OK")
|
||||
case 2: // Moved and done
|
||||
fmt.Println("DONE")
|
||||
case 1, 2: // Moved
|
||||
roadTaken = append(roadTaken, *maze.bot)
|
||||
maze.MoveBot(movingDir)
|
||||
}
|
||||
if p.State() == intcode.RET_DONE || p.State() == intcode.RET_ERR {
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
ret := p.Run()
|
||||
if ret == intcode.RET_DONE {
|
||||
|
||||
maze.Print()
|
||||
} else if ret == intcode.RET_ERR {
|
||||
fmt.Println("ERROR")
|
||||
fmt.Println(p.Error())
|
||||
|
||||
Reference in New Issue
Block a user