Created
January 12, 2013 02:03
-
-
Save rsepassi/4515661 to your computer and use it in GitHub Desktop.
w1d5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KnightsPath | |
def initialize(start, finish) | |
@start = convert_chess_position(start) | |
@finish = convert_chess_position(finish) | |
@queue = [KnightNode.new(@start, nil)] | |
end | |
def find_path | |
past_moves = [] | |
while knight = @queue.shift | |
break if knight.position == @finish | |
past_moves << knight.position | |
next_moves = possible_next_moves(knight.position) | |
prune_moves(next_moves, past_moves) | |
@queue += create_knight_nodes(next_moves, knight) | |
end | |
knight.position == @finish ? print_path(knight) : puts("No path exists.") | |
end | |
private | |
def print_path(knight) | |
print_path(knight.parent) unless knight.parent.nil? | |
puts convert_chess_position(knight.position) | |
end | |
def prune_moves(next_moves, past_moves) | |
next_moves.delete_if { |pos| past_moves.include?(pos) } | |
@queue.each do |knight| | |
next_moves.delete_if { |pos| knight.position == pos } | |
end | |
end | |
def create_knight_nodes(positions, parent) | |
positions.map { |pos| KnightNode.new(pos, parent) } | |
end | |
def convert_chess_position(position) | |
if position.is_a?(String) | |
[('a'..'h').to_a.index(position[0]), position[1].to_i - 1] | |
else | |
"#{('a'..'h').to_a[position[0]]}#{(position[1] + 1).to_s}" | |
end | |
end | |
def possible_next_moves(position) | |
all_possibles = [ | |
[position[0] + 2, position[1] + 1], | |
[position[0] + 2, position[1] - 1], | |
[position[0] - 2, position[1] + 1], | |
[position[0] - 2, position[1] - 1], | |
[position[0] + 1, position[1] + 2], | |
[position[0] + 1, position[1] - 2], | |
[position[0] - 1, position[1] + 2], | |
[position[0] - 1, position[1] - 2]] | |
all_possibles.select do |pos| | |
(0..7).include?(pos[0]) && (0..7).include?(pos[1]) | |
end | |
end | |
end | |
class KnightNode | |
attr_reader :position, :parent | |
def initialize(position, parent) | |
@position, @parent = position, parent | |
end | |
end | |
# Tests: | |
game = KnightsPath.new('e4','h6') | |
game.find_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'jumpstart_auth' | |
class MicroBlogger | |
attr_reader :client | |
def initialize | |
puts "Initializing" | |
@client = JumpstartAuth.twitter | |
end | |
def tweet(message) | |
message.size <= 140 ? @client.update(message) : puts("You are stupid") | |
end | |
def dm(target, message) | |
puts "Trying to send #{target} this direct message: " | |
puts message | |
if followers_list.include?(target) | |
tweet("d #{target} #{message}") | |
else | |
puts "Error: You can only DM people who follow you." | |
end | |
end | |
def followers_list | |
@client.followers.map { |follower| follower.screen_name } | |
end | |
def spam_my_followers(message) | |
followers_list.each { |target| dm(target, message) } | |
end | |
def everyones_last_tweet | |
friends = @client.friends.sort_by { |a, b| a.screen_name <=> b.screen_name } | |
friends.each do |friend| | |
puts "#{friend.screen_name}: #{friend.status.text}" | |
puts "posted: #{friend.status.created_at.strftime("%A, %b %d")}" | |
end | |
end | |
def shorten(url) | |
puts "Shortening #{url}" | |
end | |
def run | |
puts "Welcome to the JSL Twitter Client!" | |
command = "" | |
until command == 'q' | |
printf "enter command: " | |
parts = gets.chomp.split | |
command = parts[0] | |
case command | |
when 'q' | |
puts "Goodbye!" | |
when 't' | |
tweet(parts[1..-1].join(' ')) | |
when 'dm' | |
dm(parts[1], parts[2..-1].join(' ')) | |
when 'spam' | |
spam_my_followers(parts[1..-1].join(' ')) | |
when 'elt' | |
everyones_last_tweet | |
when 's' | |
shorten(parts[1..-1].join) | |
else | |
puts "Sorry, I don't know how to #{command}" | |
end | |
end | |
end | |
end | |
# execution script: | |
blogger = MicroBlogger.new | |
blogger.tweet("MicroBlogger Initialized") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### NOT FINISHED, DOESN'T DO SHIT AT THE MOMENT ### | |
class TTTNode | |
attr_reader :board, :parent, :mover | |
def initialize(board, parent, mover) | |
@board, @parent, @mover = board, parent, mover | |
end | |
def children | |
@children ||= create_children | |
end | |
def create_children | |
new_children = [] | |
new_boards = next_possible_boards(self.board) | |
new_boards.each do |board| | |
new_children << TTTNode.new(board, self, other_mover) | |
end | |
new_children | |
end | |
def other_mover | |
self.mover == 1 ? 0 : 1 | |
end | |
def next_possible_boards(board) | |
new_boards = [] | |
board.each_with_index do |pos, i| | |
if board[i].nil? | |
new_board = board.dup | |
new_board[i] = @mover | |
new_boards << new_board | |
end | |
end | |
new_boards | |
end | |
end | |
class TTTComputer | |
def initialize(board, mover) | |
@board, @mover = mover | |
@initial_node = TTTNode.new(board, nil, mover) | |
end | |
def best_move | |
winning_boards = Hash.new(0) | |
# Keys are the possible moves (indices) | |
# Values are the number of winning boards in children path | |
end | |
def winning_board?(board, mover) | |
return false if board.compact.lenght != 9 | |
wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], | |
[0, 3, 6], [1, 4, 7], [2, 5, 8], | |
[0, 4, 8], [2, 4, 6]] | |
wins.each do |win| | |
in_a_row = [board[win[0]], board[win[1]], board[win[2]]] | |
return true if in_a_row.all? { |i| i == mover } | |
end | |
return false | |
end | |
def tie_board?(board, mover) | |
!winning_board?(board, mover) && !winning_board(board, other_mover) | |
end | |
def other_mover | |
@mover == 1 ? 0 : 1 | |
end | |
end | |
def possible_moves(board) | |
# indices with nil | |
end | |
initial_node = TTTNode.new(Array.new(2,nil), nil, 1) | |
#initial_node.children.each {|c| c.children.each {|a| p a.board}} | |
# initial_node.children.each do |child| | |
# child.children | |
# end | |
def print_all(node) | |
p node.board | |
return if node.board.compact.length == 2 | |
node.children.each { |child| print_all(child) } | |
end | |
print_all(initial_node) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode | |
attr_accessor :value, :parent | |
attr_reader :left_child, :right_child | |
def initialize(value) | |
@left_child, @right_child = nil, nil | |
@value = value | |
end | |
def left_child=(node) | |
@left_child.parent = nil unless @left_child.nil? | |
node.parent = self | |
@left_child = node | |
end | |
def right_child=(node) | |
@right_child.parent = nil unless @right_child.nil? | |
node.parent = self | |
@right_child = node | |
end | |
def dfs(value) | |
return self if self.value == value | |
if @left_child.nil? && @right_child.nil? | |
return nil | |
elsif @right_child.nil? | |
@left_child.dfs(value) | |
elsif @left_child.nil? | |
@right_child.dfs(value) | |
else | |
@left_child.dfs(value) || @right_child.dfs(value) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WordChains | |
attr_reader :dictionary | |
attr_accessor :queue | |
def initialize(filename) | |
@initial_word, @final_word = ARGV | |
@dictionary = build_dictionary(filename) | |
@queue = [Node.new(@initial_word, nil)] | |
raise_error unless words_in_dictionary? | |
end | |
def find_chain | |
word = '' | |
tried_words = [] | |
while word = @queue.shift | |
tried_words << word.value | |
break if is_final_word?(word.value) | |
neighbors = get_words_one_letter_away(word.value) | |
neighbors.select! { |n| !tried_words.include?(n) } | |
@queue.each do |queue_word| | |
neighbors.delete_if { |n| n == queue_word.value } | |
end | |
@queue += create_nodes(neighbors, word) | |
end | |
word.nil? ? puts("No chain exists.") : print_parents(word) | |
end | |
private | |
def raise_error | |
raise ArgumentError, "Sorry, one of these words is not in my dictionary." | |
end | |
def words_in_dictionary? | |
dictionary.include?(@initial_word) && dictionary.include?(@final_word) | |
end | |
def print_parents(word) | |
print_parents(word.parent) unless word.parent.nil? | |
puts word.value | |
end | |
def is_final_word?(word) | |
word == @final_word | |
end | |
def get_words_one_letter_away(word) | |
neighbors =[] | |
word.split("").each_with_index do |letter, i| | |
regex = word.split("") | |
regex[i] = "[^#{letter}]" | |
neighbors << match_words(regex.join) | |
end | |
neighbors.flatten | |
end | |
def create_nodes(words, parent) | |
words.map { |word| Node.new(word, parent) } | |
end | |
def match_words(regex) | |
dictionary.select { |dic_word| dic_word.match(Regexp.new(regex)) } | |
end | |
def build_dictionary(filename) | |
File.readlines(filename).map(&:chomp).select do |word| | |
word.length == @initial_word.length | |
end | |
end | |
end | |
class Node | |
attr_reader :parent, :value | |
def initialize(value, parent) | |
@value, @parent = value, parent | |
end | |
end | |
chain = WordChains.new('2of4brif.txt') | |
chain.find_chain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment