Skip to content

Commit

Permalink
yet more formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
izhigal committed Jan 26, 2024
1 parent b95e141 commit f5b2519
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions rlcard/games/doudizhu/game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Implement Doudizhu Game class
"""
"""Implement Doudizhu Game class"""
import functools
from heapq import merge
import numpy as np
Expand All @@ -12,9 +11,8 @@


class DoudizhuGame:
"""Provide game APIs for env to run doudizhu and get corresponding state
information.
"""
"""Provide game APIs for env to run doudizhu and get corresponding state information"""

def __init__(self, allow_step_back=False):
self.allow_step_back = allow_step_back
self.np_random = np.random.RandomState()
Expand All @@ -36,8 +34,8 @@ def init_game(self):
for num in range(self.num_players)]

# initialize round to deal cards and determine landlord
self.played_cards = [np.zeros((len(CARD_RANK_STR), ), dtype=np.int32)
for _ in range(self.num_players)]
self.played_cards = [np.zeros((len(CARD_RANK_STR),), dtype=np.int32)
for _ in range(self.num_players)]
self.round = Round(self.np_random, self.played_cards)
self.round.initiate(self.players)

Expand All @@ -64,14 +62,14 @@ def step(self, action):
# TODO: don't record game.round, game.players, game.judger if allow_step_back not set
pass

# perfrom action
# perform action
player = self.players[self.round.current_player]
self.round.proceed_round(player, action)
if action != 'pass':
self.judger.calc_playable_cards(player)
if self.judger.judge_game(self.players, self.round.current_player):
self.winner_id = self.round.current_player
next_id = (player.player_id+1) % len(self.players)
next_id = (player.player_id + 1) % len(self.players)
self.round.current_player = next_id

# get next state
Expand All @@ -89,18 +87,18 @@ def step_back(self):
if not self.round.trace:
return False

#winner_id will be always None no matter step_back from any case
# winner_id will be always None no matter step_back from any case
self.winner_id = None

#reverse round
# reverse round
player_id, cards = self.round.step_back(self.players)

#reverse player
# reverse player
if cards != 'pass':
self.players[player_id].played_cards = self.round.find_last_played_cards_in_trace(player_id)
self.players[player_id].play_back()

#reverse judger.played_cards if needed
# reverse judger.played_cards if needed
if cards != 'pass':
self.judger.restore_playable_cards(player_id)

Expand Down Expand Up @@ -129,7 +127,7 @@ def get_state(self, player_id):

@staticmethod
def get_num_actions():
"""Return the total number of abstract acitons
"""Return the total number of abstract actions
Returns:
int: the total number of abstract actions of doudizhu
Expand Down Expand Up @@ -163,7 +161,8 @@ def is_over(self):
return True

def _get_others_current_hand(self, player):
player_up = self.players[(player.player_id+1) % len(self.players)]
player_down = self.players[(player.player_id-1) % len(self.players)]
others_hand = merge(player_up.current_hand, player_down.current_hand, key=functools.cmp_to_key(doudizhu_sort_card))
player_up = self.players[(player.player_id + 1) % len(self.players)]
player_down = self.players[(player.player_id - 1) % len(self.players)]
others_hand = merge(player_up.current_hand, player_down.current_hand,
key=functools.cmp_to_key(doudizhu_sort_card))
return cards2str(others_hand)

0 comments on commit f5b2519

Please sign in to comment.