diff --git a/lib/pip_ai/node.rb b/lib/pip_ai/node.rb index c131d7b..2acc654 100644 --- a/lib/pip_ai/node.rb +++ b/lib/pip_ai/node.rb @@ -9,6 +9,7 @@ def initialize(value) end def win_ratio + 1 if @losses == 0 @wins / @losses end @@ -20,9 +21,9 @@ def lose @losses += 1 end - def hit? - win_ratio > 0.5 + def stand? + win_ratio > 0.25 end end end -end +end \ No newline at end of file diff --git a/players/pip_adaptive.rb b/players/pip_adaptive.rb index 6e22b3e..715bdf7 100644 --- a/players/pip_adaptive.rb +++ b/players/pip_adaptive.rb @@ -1,10 +1,57 @@ +require "yaml" require 'pip_ai/node' + +=begin +Blackjack AI (Adaptive 1) +--------------------- + +This AI (Adaptive 1) only makes decisions based on it's own cards, ignoring the dealer completely other than to hit or stand. + +Method: Each iteration, the AI references a tree of old hands to see whether to hit, stand, or split (split not implemented). + +Hand storage tree: + + root + / \ + A 2 .... + / \ .... + A 2 + +Each node has a hash of children, as well as containing a win number and loss number. Children will automatically be created if the hand has never been played. + +Rules: +If the hand has never been played, stand. +If the win ratio (win number/loss number) is high, stand. +Else, hit. + +Possible extra strategies: check avg. win ratio of children, if it is higher then hit. +=end + module ScriptWarsBlackjack module Players # Pip - By user class Pip include PipAI + + def initialize() + @storage_path = File.expand_path("../pip_adaptive_1_storage.yml", __FILE__) + @data = {} + save_data(@storage_path) + File.new(@storage_path) unless File.exists?(@storage_path) + load_data(@storage_path) + end + + def load_data(yml_file) + @data = YAML.load(File.open(yml_file)) + end + + def save_data(yml_file) + File.open(yml_file, "w") do |file| + file.write(@data.to_yaml) + end + end + def place_bet(game_state) # Return your integer bet 0 diff --git a/players/pip_adaptive_1_storage.yml b/players/pip_adaptive_1_storage.yml new file mode 100644 index 0000000..2fbf0ff --- /dev/null +++ b/players/pip_adaptive_1_storage.yml @@ -0,0 +1 @@ +--- {}