Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional double down action to blackjack #1529

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
23 changes: 19 additions & 4 deletions gym/envs/toy_text/blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ class BlackjackEnv(gym.Env):
by Sutton and Barto.
http://incompleteideas.net/book/the-book-2nd.html
"""
def __init__(self, natural=False):
self.action_space = spaces.Discrete(2)
def __init__(self, natural=False, double_down=False):
if double_down:
self.action_space = spaces.Discrete(3)
else:
self.action_space = spaces.Discrete(2)
self.observation_space = spaces.Tuple((
spaces.Discrete(32),
spaces.Discrete(11),
Expand All @@ -81,6 +84,8 @@ def __init__(self, natural=False):
# Flag to payout 1.5 on a "natural" blackjack win, like casino rules
# Ref: http://www.bicyclecards.com/how-to-play/blackjack/
self.natural = natural
#Flag for allowing doubling down
chisness marked this conversation as resolved.
Show resolved Hide resolved
self.double_down = double_down
# Start the first game
self.reset()

Expand All @@ -90,15 +95,25 @@ def seed(self, seed=None):

def step(self, action):
assert self.action_space.contains(action)
if action: # hit: add a card to players hand and return
if self.double_down:
chisness marked this conversation as resolved.
Show resolved Hide resolved
if action == 2: # double down: bet double and get only 1 card
self.player.append(draw_card(self.np_random))
done = True
if is_bust(self.player):
reward = -2
else:
while sum_hand(self.dealer) < 17:
self.dealer.append(draw_card(self.np_random))
reward = 2 * cmp(score(self.player), score(self.dealer))
if action == 1: # hit: add a card to players hand and return
self.player.append(draw_card(self.np_random))
if is_bust(self.player):
done = True
reward = -1
else:
done = False
reward = 0
else: # stick: play out the dealers hand, and score
elif action == 0: # stick: play out the dealers hand, and score
done = True
while sum_hand(self.dealer) < 17:
self.dealer.append(draw_card(self.np_random))
Expand Down