Skip to content

Commit

Permalink
udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
daochenzha committed Sep 13, 2022
1 parent 558c3f7 commit 2207acf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
29 changes: 15 additions & 14 deletions rlcard/envs/blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self, config):
self.default_game_config = DEFAULT_GAME_CONFIG
self.game = Game()
super().__init__(config)
self.rank2score = {"A":11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "T":10, "J":10, "Q":10, "K":10}
self.actions = ['hit', 'stand']
self.state_shape = [[2] for _ in range(self.num_players)]
self.action_shape = [None for _ in range(self.num_players)]
Expand Down Expand Up @@ -49,19 +48,8 @@ def _extract_state(self, state):
my_cards = cards[0]
dealer_cards = cards[1]

def get_scores_and_A(hand):
score = 0
has_a = 0
for card in hand:
score += self.rank2score[card[1:]]
if card[1] == 'A':
has_a = 1
if score > 21 and has_a == 1:
score -= 10
return score, has_a

my_score, _ = get_scores_and_A(my_cards)
dealer_score, _ = get_scores_and_A(dealer_cards)
my_score = get_score(my_cards)
dealer_score = get_score(dealer_cards)
obs = np.array([my_score, dealer_score])

legal_actions = OrderedDict({i: None for i in range(len(self.actions))})
Expand Down Expand Up @@ -100,3 +88,16 @@ def _decode_action(self, action_id):
action (str): action for the game
'''
return self.actions[action_id]

rank2score = {"A":11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "T":10, "J":10, "Q":10, "K":10}
def get_score(hand):
score = 0
count_a = 0
for card in hand:
score += rank2score[card[1:]]
if card[1] == 'A':
count_a += 1
while score > 21 and count_a > 0:
count_a -= 1
score -= 10
return score
12 changes: 5 additions & 7 deletions rlcard/games/blackjack/judger.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ def judge_score(self, cards):
score (int): the score of the given cards set
'''
score = 0
has_A = 0
count_a = 0
for card in cards:
card_score = self.rank2score[card.rank]
score += card_score
if card.rank == 'A':
has_A += 1
if score > 21 and has_A > 0:
for _ in range(has_A):
score -= 10
if score < 21:
break
count_a += 1
while score > 21 and count_a > 0:
count_a -= 1
score -= 10
return score

0 comments on commit 2207acf

Please sign in to comment.