From 7843a6a3f6082d7acb14c09ec491bdbdb53efa72 Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Tue, 11 Aug 2015 13:06:10 -0400 Subject: [PATCH 1/3] Framework for storage/loading of YAML hand data --- players/pip_adaptive.rb | 21 +++++++++++++++++++++ players/pip_adaptive_storage.yml | 1 + 2 files changed, 22 insertions(+) create mode 100644 players/pip_adaptive_storage.yml diff --git a/players/pip_adaptive.rb b/players/pip_adaptive.rb index 6e22b3e..1e79f60 100644 --- a/players/pip_adaptive.rb +++ b/players/pip_adaptive.rb @@ -1,10 +1,31 @@ +require "yaml" require 'pip_ai/node' + module ScriptWarsBlackjack module Players # Pip - By user class Pip include PipAI + + def initialize() + @storage_path = File.expand_path("../pip_adaptive_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_storage.yml b/players/pip_adaptive_storage.yml new file mode 100644 index 0000000..2fbf0ff --- /dev/null +++ b/players/pip_adaptive_storage.yml @@ -0,0 +1 @@ +--- {} From 9a1c8aba602ff4102cf0ad04e7c17b6c62eeac1f Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Tue, 11 Aug 2015 13:28:48 -0400 Subject: [PATCH 2/3] Catch divide-by-zero, rename yml file, refactor --- lib/pip_ai/node.rb | 5 +++-- players/pip_adaptive.rb | 2 +- .../{pip_adaptive_storage.yml => pip_adaptive_1_storage.yml} | 0 3 files changed, 4 insertions(+), 3 deletions(-) rename players/{pip_adaptive_storage.yml => pip_adaptive_1_storage.yml} (100%) diff --git a/lib/pip_ai/node.rb b/lib/pip_ai/node.rb index c131d7b..911e3a4 100644 --- a/lib/pip_ai/node.rb +++ b/lib/pip_ai/node.rb @@ -9,6 +9,7 @@ def initialize(value) end def win_ratio + 0 if @losses == 0 @wins / @losses end @@ -20,9 +21,9 @@ def lose @losses += 1 end - def hit? + def stand? win_ratio > 0.5 end end end -end +end \ No newline at end of file diff --git a/players/pip_adaptive.rb b/players/pip_adaptive.rb index 1e79f60..836562b 100644 --- a/players/pip_adaptive.rb +++ b/players/pip_adaptive.rb @@ -9,7 +9,7 @@ class Pip include PipAI def initialize() - @storage_path = File.expand_path("../pip_adaptive_storage.yml", __FILE__) + @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) diff --git a/players/pip_adaptive_storage.yml b/players/pip_adaptive_1_storage.yml similarity index 100% rename from players/pip_adaptive_storage.yml rename to players/pip_adaptive_1_storage.yml From fee642341d51ca1d1f9433017ea550e4b09f44f4 Mon Sep 17 00:00:00 2001 From: Benjamin Ward Date: Tue, 11 Aug 2015 13:44:00 -0400 Subject: [PATCH 3/3] Add strategy in comment, fix node based on strategy --- lib/pip_ai/node.rb | 4 ++-- players/pip_adaptive.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/pip_ai/node.rb b/lib/pip_ai/node.rb index 911e3a4..2acc654 100644 --- a/lib/pip_ai/node.rb +++ b/lib/pip_ai/node.rb @@ -9,7 +9,7 @@ def initialize(value) end def win_ratio - 0 if @losses == 0 + 1 if @losses == 0 @wins / @losses end @@ -22,7 +22,7 @@ def lose end def stand? - win_ratio > 0.5 + win_ratio > 0.25 end end end diff --git a/players/pip_adaptive.rb b/players/pip_adaptive.rb index 836562b..715bdf7 100644 --- a/players/pip_adaptive.rb +++ b/players/pip_adaptive.rb @@ -1,6 +1,32 @@ 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