Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .#README.md
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# assignment_search
# Morgan Martin and David Watt's assignment_search
Marco? Polo!

[A data structures and algorithms Ruby challenge from the Viking Code School](http://www.vikingcodeschool.com)


# Searching a simple tree of nodes where each Node has an array of child nodes (some_node.children) using DFS.


# Check node to see if it is what we're looking for. Return it if it is. Otherwise add children to stack. Recur.

# Searching the same tree using BFS.

# Check node if it is what we're looking for. Return it if it is. Otherwise add children to queue. Recur.
6 changes: 6 additions & 0 deletions game_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'knights_travails'

describe GameTree do

describe
end
60 changes: 60 additions & 0 deletions knight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require_relative "movetree.rb"

class KnightSearcher
attr_accessor :tree, :current_node
def initialize(movetree)
@tree = movetree
@current_node = nil
end

def find_bfs(coords)
location = nil
@tree.bfs(@tree.root) do |node|
if node.x ==coords[0] && node.y ==coords[1]
location = node
break
end
end
location
end

def path_to_bfs(coords)
moves = []
node = find_bfs(coords)
until node.parent.nil?
moves << [node.x, node.y]
node = node.parent
end
moves << [@tree.root.x, @tree.root.y]
puts "bfs path:"
moves.reverse.each do |move|
puts "(#{move[0]}, #{move[1]})"
end
end

def find_dfs(coords)
location = nil
@tree.dfs(@tree.root) do |node|
if node.x ==coords[0] && node.y ==coords[1]
location = node
break
end
end
location
end

def path_to_dfs(coords)
moves = []
node = find_dfs(coords)
until node.parent.nil?
moves << [node.x, node.y]
node = node.parent
end
moves << [@tree.root.x, @tree.root.y]
puts "dfs path:"
moves.reverse.each do |move|
puts "(#{move[0]}, #{move[1]})"
end
end

end
94 changes: 94 additions & 0 deletions movetree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class Move
attr_accessor :x, :y, :depth, :children, :parent
def initialize(x=0, y=0, depth=0, children=[], parent=nil)
@x = x
@y = y
@depth = depth
@children = children
@parent = parent
end
end

class MoveTree
attr_accessor :max, :root
BOARD_SIZE = 8
VECTORS = [[1, 2], [1, -2], [-1, 2], [-1, -2], [2, 1], [-2, 1], [-2, -1], [2, -1]]

def initialize(start, max_depth)
@max = max_depth
@root = Move.new(start[0], start[1], 0, [], nil)
end

def generate(node)
return if node.depth == @max
moves = viable_moves(node)
moves.each do |move|
node.children[node.children.length] = Move.new(move[0], move[1], node.depth+1, [], node)
end
node.children.each do |child|
generate(child)
end
end

def count_nodes(node)
count = 0
bfs(node) {count += 1}
count
end

def viable_moves(start)
moves = legal_moves(start)
node = start
until node.parent.nil?
if moves.include?([node.parent.x, node.parent.y])
moves.delete([node.parent.x, node.parent.y])
end
node = node.parent
end
moves
end

def legal_moves(node)
moves = []
a = node.x
b = node.y
VECTORS.each do |m, n|
if (0..(BOARD_SIZE-1)).include?(a+m) && (0..(BOARD_SIZE-1)).include?(b+n)
moves << [a+m, b+n]
end
end
return moves
end

def bfs(node)
queue = []
yield(node)
node.children.each do |child|
queue.push(child)
yield(child)
end
until queue.empty?
queue[0].children.each do |child|
queue.push(child)
yield(child)
end
queue.shift
end
end

def dfs(node)
stack = []
yield(node)
node.children.reverse.each do |child|
stack.push(child)
end
until stack.empty?
recent = stack.pop
yield(recent)
recent.children.reverse.each do |child|
stack.push(child)
end
end
end

end