Skip to content

Commit

Permalink
Keep refactoring autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Oct 14, 2010
1 parent 8f8b91d commit c0c190b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
28 changes: 6 additions & 22 deletions lib/dominion/game.rb
Expand Up @@ -68,17 +68,9 @@ def step

self.prompt = {
:prompt => "action (#{player[:actions]} left)?",
:autocomplete => {
:card_active => lambda {|card|
[*card[:type]].include?(:action) && player[:hand].include?(card)
},
:strategy => lambda {|input|
suggest = input.length == 0 ? nil : board.map(&:first).detect {|x|
prompt[:autocomplete][:card_active][x] && x[:name] =~ /^#{input}/i
}
suggest ? suggest[:name] : nil
}
},
:autocomplete => Input::Autocomplete.cards {|card|
[*card[:type]].include?(:action) && player[:hand].include?(card)
}[self],
:color => :green_back,
:accept => lambda {|input|
self.prompt = nil
Expand All @@ -92,17 +84,9 @@ def step
elsif player[:buys] > 0 # TODO: option to skip copper buys
self.prompt = {
:prompt => "buy (#{treasure(player)}/#{player[:buys]} left)?",
:autocomplete => {
:card_active => lambda {|card|
card[:cost] <= treasure(player)
},
:strategy => lambda {|input|
suggest = input.length == 0 ? nil : board.map(&:first).detect {|x|
prompt[:autocomplete][:card_active][x] && x[:name] =~ /^#{Regexp.escape(input)}/i
}
suggest ? suggest[:name] : nil
}
},
:autocomplete => Input::Autocomplete.cards {|card|
card[:cost] <= treasure(player)
}[self],
:color => :magenta_back,
:accept => lambda {|input|
self.prompt = nil
Expand Down
14 changes: 14 additions & 0 deletions lib/dominion/input.rb
Expand Up @@ -34,6 +34,20 @@ def self.accept_cards(opts)
end

class Autocomplete
def self.cards(&match_func)
lambda {|game| {
:card_active => lambda {|card|
match_func.call(card)
},
:strategy => lambda {|input|
suggest = input.length == 0 ? nil : game.board.map(&:first).detect {|x|
match_func.call(x) && x[:name] =~ /^#{input}/i
}
suggest ? suggest[:name] : nil
}
}}
end

def self.cards_on_board(match_func = lambda {|x| true })
lambda {|game|
{
Expand Down

0 comments on commit c0c190b

Please sign in to comment.