class MoveTreeNode

A node structure for the tree representation of a pieces potential moves. See MoveTree.

Attributes

children[R]
loc[RW]

Public Class Methods

new(loc) click to toggle source

Initialize a node with its location and an empty children array.

@param [Array<Integer>] loc An integer array of length 2 denoting the location the node represents.

# File lib/chess_pieces/move_tree_node.rb, line 14
def initialize(loc)
  @loc = loc
  @children = []
end

Public Instance Methods

==(other) click to toggle source

Define equality between two nodes by same location.

@param [MoveTreeNode] other The other MoveTreeNode to compare the location for equality. @return [true] if the locations match, false otherwise.

# File lib/chess_pieces/move_tree_node.rb, line 61
def ==(other)
  return false unless other.is_a? MoveTreeNode

  @loc == other.loc
end
add_child(loc) click to toggle source

Add a child to the array of children nodes.

@param [Array<Integer>] loc An integer array of length 2 denoting the location of the node to add. @raises [ArgumentError] if loc isn't an Array or MoveTreeNode.

# File lib/chess_pieces/move_tree_node.rb, line 24
def add_child(loc)
  return @children << MoveTreeNode.new(loc) if loc.is_a? Array

  return @children << loc if loc.is_a? MoveTreeNode

  # Raise an error if the argument is not either an array or a MoveTreeNode
  raise(ArgumentError, "Argument is a #{loc.class}; should be Array or MoveTreeNode")
end
remove_child(loc) click to toggle source

Removes a child from the array of children nodes.

@param [Array<Integer>] loc An integer array of length 2 denoting the location of the node to remove, or a MoveTreeNode object with the loc to match. @raises [ArgumentError] if loc isn't an Array or MoveTreeNode.

# File lib/chess_pieces/move_tree_node.rb, line 38
def remove_child(loc)
  return @children.delete(MoveTreeNode.new(loc)) if loc.is_a? Array

  return @children.delete(loc) if loc.is_a? MoveTreeNode

  # Raise an error if the argument is not either an array or a MoveTreeNode
  raise(ArgumentError, "Argument is a #{loc.class}; should be Array or MoveTreeNode")
end
to_s() click to toggle source

Returns a string representation of the node using its location and a list of its children.

@returns [String] String with the node's location and children.

# File lib/chess_pieces/move_tree_node.rb, line 52
def to_s
  "#{@loc}: #{@children}"
end