puzzle

package
v0.0.0-...-c0d9fdd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NumberRows of the board
	NumberRows = 3
	// NumberColumns of the board
	NumberColumns = 4
)

Variables

This section is empty.

Functions

func AreTheSame

func AreTheSame(a []int, b []int) bool

AreTheSame compares two slices

func Contains

func Contains(s []*Node, n *Node) bool

Contains checks if a slice of Nodes contains a given Node

func Heuristic1

func Heuristic1(p []int) int

Heuristic1 is the number of misplaced tiles

func Heuristic2

func Heuristic2(p []int) int

Heuristic2 sum of permutation inversions + number of misplaced tiles

Types

type Item

type Item struct {
	Node      *Node // The value of the item; arbitrary.
	Heuristic int   // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	Index int // The index of the item in the heap.
}

An Item is something we manage in a priority queue.

type Node

type Node struct {
	Puzzle    []int
	Value     int // the index of the "0" in the puzzle
	Children  []*Node
	Parent    *Node
	Move      string // used for printing files
	Heuristic int
	G         int // the depth of the node in the search tree
}

Node represents a in the search tree

func As

func As(root *Node, heuristic int) []*Node

As algorithm

func BestFirstSearch

func BestFirstSearch(root *Node, heuristic int) []*Node

BestFirstSearch algorithm

func ContainsAndRemove

func ContainsAndRemove(s *[]*Node, n *Node) (bool, *Node)

ContainsAndRemove checks if an slice of Nodes contains a given Node and removes the node if it is theres

func DepthFirstSearch

func DepthFirstSearch(r *Node) []*Node

DepthFirstSearch algorithm

func NewPuzzle

func NewPuzzle(p []int) *Node

NewPuzzle generates the root node

func (*Node) ClonePuzzle

func (n *Node) ClonePuzzle() []int

ClonePuzzle copies the puzzle of a given Node

func (*Node) GenerateMoves

func (n *Node) GenerateMoves()

GenerateMoves generates the possible moves

func (*Node) MoveDown

func (n *Node) MoveDown(p []int, i int)

MoveDown moves the empty tile down by 1 tile

func (*Node) MoveDownLeft

func (n *Node) MoveDownLeft(p []int, i int)

MoveDownLeft moves the empty tile diagonally down-left by 1 tile

func (*Node) MoveDownRight

func (n *Node) MoveDownRight(p []int, i int)

MoveDownRight moves the empty tile diagonally down-right by 1 tile

func (*Node) MoveLeft

func (n *Node) MoveLeft(p []int, i int)

MoveLeft moves the empty tile left by 1 tile

func (*Node) MoveRight

func (n *Node) MoveRight(p []int, i int)

MoveRight moves the empty tile to the right by 1 tile

func (*Node) MoveUp

func (n *Node) MoveUp(p []int, i int)

MoveUp moves the empty tile up by 1 tile

func (*Node) MoveUpLeft

func (n *Node) MoveUpLeft(p []int, i int)

MoveUpLeft moves the empty tile diagonally up-left by 1 tile

func (*Node) MoveUpRight

func (n *Node) MoveUpRight(p []int, i int)

MoveUpRight moves the empty tile diagonally up-right by 1 tile

func (*Node) PathTrace

func (n *Node) PathTrace() []*Node

PathTrace returns a slice of nodes leading to goal

type PriorityQueue

type PriorityQueue []*Item

A PriorityQueue implements heap.Interface and holds Items.

func (PriorityQueue) Contains

func (pq PriorityQueue) Contains(n *Node) (bool, *Item)

Contains checks if it is there

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

Pop an element from the pq

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

Push an element to the pq

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL