Beispiel #1
0
func createSubPlots(root *node.Node, pts plotter.XYs, depth int) {
	if pts == nil {
		pts = make(plotter.XYs, depth+1)
	}

	// fmt.Println("Depth: ", depth)
	pts[depth].X = float64(root.GetX())
	pts[depth].Y = float64(root.GetY())

	if depth == 0 {
		if root.GetX() != theGoalX || root.GetY() != theGoalY {
			return
		}

		pts[depth].X = float64(root.GetX())
		pts[depth].Y = float64(root.GetY())

		total++

		plotutil.AddLinePoints(myPlot, pts)
		return
	}

	for _, item := range root.GetChildren() {
		createSubPlots(item, pts, depth-1)
	}
}
Beispiel #2
0
func getNextLocations(piece byte, allMoves *matrix.DenseMatrix, ellipse *matrix.DenseMatrix, baseNode *node.Node) []*node.Node {
	result := make([]*node.Node, 0, 20)

	x := baseNode.GetX()
	y := baseNode.GetY()
	next := baseNode.GetStep() + 1

	// Start with the single move board
	var singleMove *matrix.DenseMatrix
	switch piece {
	case Pawn:
		singleMove = singlePawnMove
	case Rook:
		singleMove = singleRookMove
	case Knight:
		singleMove = singleKnightMove
	case Bishop:
		singleMove = singleBishopMove
	case Queen:
		singleMove = singleQueenMove
	case King:
		singleMove = singleKingMove
	case Puppy:
		singleMove = singlePuppyMove
	}
	singleMove = shiftMatrix(singleMove, x-8, y-8)
	singleMove = singleMove.GetMatrix(7, 0, 8, 8)

	// fmt.Println("(", x, ", ", y, ")")
	// fmt.Println("Single Move: \n", singleMove)

	// fmt.Println("\nAll Moves: \n", allMoves)
	// fmt.Println("\nEllipse: ", ellipse)

	for i := 0; i < 8; i++ {
		for j := 0; j < 8; j++ {
			// fmt.Println("(", i+1, ", ", 8-j, ") : single: ", singleMove.Get(j, i), "ellipse: ", ellipse.Get(j, i), "all: ", allMoves.Get(j, i))

			if singleMove.Get(j, i) != 0 && ellipse.Get(j, i) != 0 && allMoves.Get(j, i) == float64(next) {
				// fmt.Println("New Child Node: (", i+1, ", ", 8-j, ")")

				var newNode *node.Node
				newNode = new(node.Node)
				newNode.SetX(i + 1)
				newNode.SetY(8 - j)
				newNode.SetStep(next)

				baseNode.AddChild(newNode)
				result = append(result, newNode)
			}
		}
	}

	return result
}
Beispiel #3
0
func GetEllipse(piece byte, startX int, startY int, goalX int, goalY int, maxLength int) {
	start := GenerateMoveBoard(piece, startX, startY)
	result := matrix.Sum(start, GenerateMoveBoard(piece, goalX, goalY))

	theGoalX = goalX
	theGoalY = goalY

	// Get lowest #
	min := float64(maxLength)
	// min := float64(100)
	// for i := 0; i < 8; i++ {
	// 	for j := 0; j < 8; j++ {
	// 		if result.Get(i, j) < min {
	// 			min = result.Get(i, j)
	// 		}
	// 	}
	// }

	// Remove unnecessary #s
	for i := 0; i < 8; i++ {
		for j := 0; j < 8; j++ {
			if result.Get(i, j) > min {
				result.Set(i, j, 0)
			}
		}
	}

	var root *node.Node
	root = new(node.Node)
	root.SetX(startX)
	root.SetY(startY)
	root.SetStep(0)

	// fmt.Println("Ellipse: \n", result)

	nodes := make([][]*node.Node, maxLength+1, maxLength+1)
	nodes[0] = make([]*node.Node, 0, 20)
	nodes[0] = append(nodes[0], root)

	for i := 1; i <= maxLength; i++ {
		// fmt.Println("i: ", i)
		nodes[i] = make([]*node.Node, 0, 20)
		for _, baseNode := range nodes[i-1] {
			// TODO: Improve append to check for duplicate nodes
			nodes[i] = append(nodes[i], getNextLocations(piece, start, result, baseNode)...)
		}
	}

	// fmt.Println(root)

	myPlot, _ = plot.New()
	// Create the board
	createBoard(myPlot)

	createSubPlots(root, nil, maxLength)

	myPlot.X.Min = .5
	myPlot.X.Max = 8.5
	myPlot.Y.Min = .5
	myPlot.Y.Max = 8.5

	// Add the start and end points
	pts := make(plotter.XYs, 2)
	pts[0].X = float64(startX)
	pts[0].Y = float64(startY)
	pts[1].X = float64(goalX)
	pts[1].Y = float64(goalY)
	s, _ := plotter.NewScatter(pts)
	s.GlyphStyle.Color = color.RGBA{R: 0, B: 0, A: 255}
	myPlot.Add(s)

	myPlot.Title.Text = "Total: " + strconv.Itoa(total)

	myPlot.Save(8*vg.Inch, 8*vg.Inch, "results.png")
}