Beispiel #1
0
func Search(start gocube.CubieCube, heuristic EdgesHeuristic,
	d int) []gocube.Move {
	if d == 0 {
		if IsSolved(start) {
			return []gocube.Move{}
		} else {
			return nil
		}
	} else if heuristic.Lookup(start) > d {
		return nil
	}
	for m := 0; m < 18; m++ {
		newCube := start
		newCube.Move(gocube.Move(m))
		if solution := Search(newCube, heuristic, d-1); solution != nil {
			return append([]gocube.Move{gocube.Move(m)}, solution...)
		}
	}
	return nil
}
Beispiel #2
0
func solve2x2x3(start gocube.CubieCube, depth, lastFace int) []gocube.Move {
	if depth == 0 {
		if solved, _ := Is2x2x3Solved(start); solved {
			return []gocube.Move{}
		} else {
			return nil
		}
	}
	for m := 0; m < 18; m++ {
		move := gocube.Move(m)
		face := move.Face()
		if face == lastFace {
			continue
		}
		newCube := start
		newCube.Move(move)
		if solution := solve2x2x3(newCube, depth-1, face); solution != nil {
			return append([]gocube.Move{move}, solution...)
		}
	}
	return nil
}
Beispiel #3
0
func solve2x2x2Search(start gocube.CubieCube, depth, lastFace int,
	previousMoves []gocube.Move, output chan<- []gocube.Move) {
	if depth == 0 {
		if solved, _ := Is2x2x2Solved(start); solved {
			newArray := make([]gocube.Move, len(previousMoves))
			copy(newArray, previousMoves)
			output <- newArray
		}
		return
	}
	for m := 0; m < 18; m++ {
		move := gocube.Move(m)
		face := move.Face()
		if face == lastFace {
			continue
		}
		newCube := start
		newCube.Move(move)
		previousMoves := append(previousMoves, move)
		solve2x2x2Search(newCube, depth-1, face, previousMoves, output)
		previousMoves = previousMoves[:len(previousMoves)-1]
	}
}
Beispiel #4
0
func NewEdgesHeuristic(maxDepth int) EdgesHeuristic {
	res := EdgesHeuristic{map[string]int{}, maxDepth}
	queue := []EdgesHeuristicNode{EdgesHeuristicNode{gocube.SolvedCubieEdges(),
		HashEdges(gocube.SolvedCubieEdges()), 0}}
	for len(queue) > 0 {
		node := queue[0]
		queue = queue[1:]
		if _, ok := res.Mapping[node.Hash]; ok {
			continue
		}
		res.Mapping[node.Hash] = node.Depth
		if node.Depth == maxDepth {
			continue
		}
		for move := 0; move < 18; move++ {
			newEdges := node.Edges
			newEdges.Move(gocube.Move(move))
			hash := HashEdges(newEdges)
			queue = append(queue, EdgesHeuristicNode{newEdges, hash,
				node.Depth + 1})
		}
	}
	return res
}
Beispiel #5
0
func solveAllButL5C(start gocube.CubieCube, depth, lastFace int) []gocube.Move {
	if depth == 0 {
		if solved := IsAllButL5CSolved(start); solved {
			return []gocube.Move{}
		} else {
			return nil
		}
	} else if globalEdgesHeuristic.Lookup(start.Edges) > depth {
		return nil
	}
	for m := 0; m < 18; m++ {
		move := gocube.Move(m)
		face := move.Face()
		if face == lastFace {
			continue
		}
		newCube := start
		newCube.Move(move)
		if solution := solveAllButL5C(newCube, depth-1, face); solution != nil {
			return append([]gocube.Move{move}, solution...)
		}
	}
	return nil
}