From f5b25198a94ca1a3446a3a28e62d1f334ccc998c Mon Sep 17 00:00:00 2001 From: izhigal Date: Fri, 26 Jan 2024 19:35:47 +0800 Subject: [PATCH] yet more formatting --- rlcard/games/doudizhu/game.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/rlcard/games/doudizhu/game.py b/rlcard/games/doudizhu/game.py index 9633abac..f66602ca 100644 --- a/rlcard/games/doudizhu/game.py +++ b/rlcard/games/doudizhu/game.py @@ -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 @@ -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() @@ -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) @@ -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 @@ -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) @@ -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 @@ -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)