示例#1
0
func makeNodeFromState(b *board.Board, parentNode *Node) *Node {
	newNode := &Node{
		g:      parentNode.g + 1,
		h:      b.GetH(),
		key:    b.StateString(),
		parent: parentNode,
		b:      b,
	}
	newNode.f = newNode.g + newNode.h
	return newNode
}
示例#2
0
func main() {
	var b *board.Board
	svr2 := new(solver_2.Solver)
	svr3 := new(solver_3.Solver)
	solv, heur := 0, 0

	flag.Parse()
	plog.Activate(showInfo, showWarning, showError, verbose)
	if size, inp, err := chooseInput(file, stdin); err != nil {
		fmt.Println(err)
		plog.Info.Println("generating map")
		rand.Seed(time.Now().Unix())
		solv, heur = askHeur()
		b, err = generate.GetMap(mapSize, heur)
		handleErr(err)
	} else {
		solv, heur = askHeur()
		b = board.New(size, heur)
		err := b.Input(inp)
		if err != nil {
			fmt.Println(err)
			return
		}
	}
	fmt.Printf("Map %d*%d is solvable.\n", b.Size, b.Size)
	fmt.Println(b)
	if solv == 1 {
		s := solver.New(b)
		s.Solve()
	} else if solv == 2 {
		svr2.Solve_init(b, heur)
		svr2.Solve()
	} else if solv == 3 {
		svr3.Solve_init(b, heur)
		svr3.Solve()
	}
	return
}
示例#3
0
func New(b *board.Board) *Solver {
	s := &Solver{
		BoardStates: make(map[string]*Node),
		OpenList:    new(PriorityQueue),
	}
	heap.Init(s.OpenList)
	currentNode := &Node{
		parent: nil,
		key:    b.StateString(),
		h:      b.GetH(),
		g:      0,
		b:      b,
		open:   true,
	}
	currentNode.f = currentNode.g + currentNode.h
	s.BoardStates[currentNode.key] = currentNode
	heap.Push(s.OpenList, currentNode)
	s.Goal = b.GoalString()
	return s
}