rlcard.games.doudizhu

rlcard.games.doudizhu.dealer

Implement Doudizhu Dealer class

class rlcard.games.doudizhu.dealer.DoudizhuDealer(np_random)

Bases: object

Dealer will shuffle, deal cards, and determine players’ roles

deal_cards(players)

Deal cards to players

Parameters

players (list) – list of DoudizhuPlayer objects

determine_role(players)

Determine landlord and peasants according to players’ hand

Parameters

players (list) – list of DoudizhuPlayer objects

Returns

landlord’s player_id

Return type

int

shuffle()

Randomly shuffle the deck

rlcard.games.doudizhu.game

Implement Doudizhu Game class

class rlcard.games.doudizhu.game.DoudizhuGame(allow_step_back=False)

Bases: object

Provide game APIs for env to run doudizhu and get corresponding state information.

static get_num_actions()

Return the total number of abstract acitons

Returns

the total number of abstract actions of doudizhu

Return type

int

get_num_players()

Return the number of players in doudizhu

Returns

the number of players in doudizhu

Return type

int

get_player_id()

Return current player’s id

Returns

current player’s id

Return type

int

get_state(player_id)

Return player’s state

Parameters

player_id (int) – player id

Returns

The state of the player

Return type

(dict)

init_game()

Initialize players and state.

Returns

first state in one game int: current player’s id

Return type

dict

is_over()

Judge whether a game is over

Returns

True(over) / False(not over)

Return type

Bool

step(action)

Perform one draw of the game

Parameters

action (str) – specific action of doudizhu. Eg: ‘33344’

Returns

next player’s state int: next player’s id

Return type

dict

step_back()

Return to the previous state of the game

Returns

True if the game steps back successfully

Return type

(bool)

rlcard.games.doudizhu.judger

Implement Doudizhu Judger class

class rlcard.games.doudizhu.judger.DoudizhuJudger(players, np_random)

Bases: object

Determine what cards a player can play

calc_playable_cards(player)

Recalculate all legal cards the player can play according to his current hand.

Parameters
  • player (DoudizhuPlayer object) – object of DoudizhuPlayer

  • init_flag (boolean) – For the first time, set it True to accelerate the preocess.

Returns

list of string of playable cards

Return type

list

static chain_indexes(indexes_list)

Find chains for solos, pairs and trios by using indexes_list

Parameters

indexes_list – the indexes of cards those have the same count, the count could be 1, 2, or 3.

Returns

[(start_index1, length1), (start_index1, length1), …]

Return type

list of tuples

get_playable_cards(player)

Provide all legal cards the player can play according to his current hand.

Parameters
  • player (DoudizhuPlayer object) – object of DoudizhuPlayer

  • init_flag (boolean) – For the first time, set it True to accelerate the preocess.

Returns

list of string of playable cards

Return type

list

static judge_game(players, player_id)

Judge whether the game is over

Parameters
  • players (list) – list of DoudizhuPlayer objects

  • player_id (int) – integer of player’s id

Returns

True if the game is over

Return type

(bool)

static judge_payoffs(landlord_id, winner_id)
classmethod pair_attachments(cards_count, chain_start, chain_length, size)

Find pair attachments for trio_chain_pair_x and four_two_pair

Parameters
  • cards_count

  • chain_start – the index of start card of the trio_chain or trio or four

  • chain_length – the size of the sequence of the chain, 1 for trio_pair or four_two_pair

  • size – count of pairs for the attachments

Returns

[attachment1, attachment2, …]

Each attachment has two elemnts, the first one contains indexes of attached cards smaller than the index of chain_start, the first one contains indexes of attached cards larger than the index of chain_start

Return type

list of tuples

static playable_cards_from_hand(current_hand)

Get playable cards from hand

Returns

set of string of playable cards

Return type

set

restore_playable_cards(player_id)

restore playable_cards for judger for game.step_back().

Parameters

player_id – The id of the player whose playable_cards need to be restored

classmethod solo_attachments(hands, chain_start, chain_length, size)

Find solo attachments for trio_chain_solo_x and four_two_solo

Parameters
  • hands

  • chain_start – the index of start card of the trio_chain or trio or four

  • chain_length – the size of the sequence of the chain, 1 for trio_solo or four_two_solo

  • size – count of solos for the attachments

Returns

[attachment1, attachment2, …]

Each attachment has two elemnts, the first one contains indexes of attached cards smaller than the index of chain_start, the first one contains indexes of attached cards larger than the index of chain_start

Return type

list of tuples

rlcard.games.doudizhu.player

Implement Doudizhu Player class

class rlcard.games.doudizhu.player.DoudizhuPlayer(player_id, np_random)

Bases: object

Player can store cards in the player’s hand and the role, determine the actions can be made according to the rules, and can perfrom corresponding action

available_actions(greater_player=None, judger=None)

Get the actions can be made based on the rules

Parameters

greater_player (DoudizhuPlayer object) – player who played

current biggest cards.

judger (DoudizhuJudger object): object of DoudizhuJudger

Returns

list of string of actions. Eg: [‘pass’, ‘8’, ‘9’, ‘T’, ‘J’]

Return type

list

property current_hand
get_state(public, others_hands, num_cards_left, actions)
play(action, greater_player=None)

Perfrom action

Parameters
  • action (string) – specific action

  • greater_player (DoudizhuPlayer object) – The player who played current biggest cards.

Returns

If there is a new greater_player, return it, if not, return None

Return type

object of DoudizhuPlayer

play_back()

Restore recorded cards back to self._current_hand

set_current_hand(value)

rlcard.games.doudizhu.round

Implement Doudizhu Round class

class rlcard.games.doudizhu.round.DoudizhuRound(np_random, played_cards)

Bases: object

Round can call other Classes’ functions to keep the game running

static cards_ndarray_to_str(ndarray_cards)
find_last_greater_player_id_in_trace()

Find the last greater_player’s id in trace

Returns

The last greater_player’s id in trace

find_last_played_cards_in_trace(player_id)

Find the player_id’s last played_cards in trace

Returns

The player_id’s last played_cards in trace

initiate(players)

Call dealer to deal cards and bid landlord.

Parameters

players (list) – list of DoudizhuPlayer objects

proceed_round(player, action)

Call other Classes’s functions to keep one round running

Parameters
  • player (object) – object of DoudizhuPlayer

  • action (str) – string of legal specific action

Returns

player who played current biggest cards.

Return type

object of DoudizhuPlayer

step_back(players)

Reverse the last action

Parameters

players (list) – list of DoudizhuPlayer objects

Returns

The last player id and the cards played

update_public(action)

Update public trace and played cards

Parameters

action (str) – string of legal specific action

rlcard.games.doudizhu.utils

Doudizhu utils

class rlcard.games.doudizhu.utils.LocalObjs

Bases: _thread._local

rlcard.games.doudizhu.utils.cards2str(cards)

Get the corresponding string representation of cards

Parameters

cards (list) – list of Card objects

Returns

string representation of cards

Return type

string

rlcard.games.doudizhu.utils.cards2str_with_suit(cards)

Get the corresponding string representation of cards with suit

Parameters

cards (list) – list of Card objects

Returns

string representation of cards

Return type

string

rlcard.games.doudizhu.utils.contains_cards(candidate, target)

Check if cards of candidate contains cards of target.

Parameters
  • candidate (string) – A string representing the cards of candidate

  • target (string) – A string representing the number of cards of target

Returns

boolean

rlcard.games.doudizhu.utils.doudizhu_sort_card(card_1, card_2)

Compare the rank of two cards of Card object

Parameters
  • card_1 (object) – object of Card

  • card_2 (object) – object of card

rlcard.games.doudizhu.utils.doudizhu_sort_str(card_1, card_2)

Compare the rank of two cards of str representation

Parameters
  • card_1 (str) – str representation of solo card

  • card_2 (str) – str representation of solo card

Returns

1(card_1 > card_2) / 0(card_1 = card2) / -1(card_1 < card_2)

Return type

int

rlcard.games.doudizhu.utils.encode_cards(plane, cards)

Encode cards and represerve it into plane.

Parameters

cards (list or str) – list or str of cards, every entry is a

character of solo representation of card

rlcard.games.doudizhu.utils.get_gt_cards(player, greater_player)

Provide player’s cards which are greater than the ones played by previous player in one round

Parameters
  • player (DoudizhuPlayer object) – the player waiting to play cards

  • greater_player (DoudizhuPlayer object) – the player who played current biggest cards.

Returns

list of string of greater cards

Return type

list

Note

  1. return value contains ‘pass’

rlcard.games.doudizhu.utils.get_landlord_score(current_hand)

Roughly judge the quality of the hand, and provide a score as basis to bid landlord.

Parameters

current_hand (str) – string of cards. Eg: ‘56888TTQKKKAA222R’

Returns

score

Return type

int