因此,我试图提示用户为播放器分配一种类型,但是我不确定为什么在打印出机器人的名称之前系统会询问两次该问题。我不确定错误来自哪里,哪个循环
玩家等级:
def get_player_type(player_num: int) -> str:
"""input player type and check if valid """
while True:
type = input(f'Choose the type for Player {player_num}\nEnter Human or Random or Simple: ')
if type.strip().lower() == 'h' or type.strip().lower() == 'r' or type.strip().lower() == 's':
return type.strip().lower()
else:
raise ValueError(f'{type} is not one of Human or Random or Simple. Please try again.')
简单的AI类:
class SimpleAI(Player):
def get_simpleAI_name(player_num: int) -> str):
"""declare names for simple AI"""
name = "SimpleAI " + str(player_num)
return name
游戏类别:
def create_game(path_to_config_file: str) -> "Game":
for player_num in range(1, 3):
if Player.get_player_type(player_num) == 'h':
new_player = HumanPlayer.create_user(player_num, game_config.blank_char, player_list)
player_list.append(new_player)
elif Player.get_player_type(player_num) == 's':
new_AI = SimpleAI.get_simpleAI_name()
print(new_AI)
return Game(game_board, new_player, player_list)
我的代码输出:
Choose the type for Player 1
Enter Human or Random or Simple: s
Choose the type for Player 1
Enter Human or Random or Simple: s
SimpleAI 1
正确的输出应为:
Choose the type for Player 1
Enter Human or Random or Simple: s
SimpleAI 1
You are calling
Player.get_player_type
everytime you try to check it against a value. What you should really do is call this method, store the result in a variable and compare that variable with your values: