# Guess Who Environment Documentation

## Overview

The Guess Who Environment is a single-player adaptation of the two-player version we are familiar with. It is question-driven where a player aims to deduce the target character selected by the gamemaster. The gamemaster, configurable in behavior, responds to each question with "Yes", "No" or "I don't know" based on the target character, guiding the player toward a correct guess. The environment tracks player moves, validates guesses (formatted as [name]), and updates the game state for each interaction, offering a structured and engaging experience that challenges players' deductive reasoning and strategic questioning skills. 

## Action Space
- **Format:** Actions are strings that can either be a question or a final guess that is formatted as [name]. For example:
- **Example:**
    - To ask a question: "Does the character have a blue eyes?"
    - To make a final guess: [Tom]
- **Notes:** Players can ask any yes-or-no question, and the gamemaster will respond with "Yes", "No" or "I don't know". To submit a final guess, the player must enclose their guess in square brackets (e.g. [Tom]). Incorrectly formatted guesses will be marked as a normal question. 

## Observation Space
**Reset Observation:**
On reset, the observation provides the initial prompt. For example:
```plaintext
[GAME] You are Player 0. You are playing Guess Who.
The gamemaster has chosen one target character from the list of characters that you will be shown below.
You have to guess the target character by asking yes-or-no questions about the target character's traits.
You can ask questions like 'Is the character male?' or 'Does the character have a beard?'.
You can also guess the name of the target character at any time by ensuring that you wrap their name in square brackets, e.g. [Zach].
As you play, the history of your questions and gamemaster's responses will be displayed.Here is the list of characters you can ask questions about:
1. Alex is a middle-aged male with short brown hair and brown eyes. Alex has a fair complexion, light skin tone, and wide smile. They wear glasses, have mustache facial hair, and their clothing style is casual. Alex has straight hair texture, round glasses style, a pointed nose, medium ears, and dimples on their cheeks.

2. Alfred is a elderly male with bald white hair and blue eyes. Alfred has a fair complexion, fair skin tone, and closed-lip smile. They wear no accessories, have beard facial hair, and their clothing style is formal. Alfred has none hair texture, none glasses style, a broad nose, large ears, and none on their cheeks.

3. Anita is a young female with long blonde hair and blue eyes. Anita has a fair complexion, olive skin tone, and wide smile. They wear earrings, have none facial hair, and their clothing style is casual. Anita has wavy hair texture, none glasses style, a round nose, small ears, and freckles on their cheeks.

4. Anne is a middle-aged female with long red hair and blue eyes. Anne has a olive complexion, fair skin tone, and neutral smile. They wear no accessories, have none facial hair, and their clothing style is sporty. Anne has curly hair texture, none glasses style, a pointed nose, medium ears, and dimples on their cheeks.

5. Bernard is a elderly male with bald brown hair and brown eyes. Bernard has a dark complexion, olive skin tone, and closed-lip smile. They wear no accessories, have mustache facial hair, and their clothing style is formal. Bernard has none hair texture, none glasses style, a round nose, medium ears, and none on their cheeks.

6. Bill is a young male with short blonde hair and brown eyes. Bill has a olive complexion, light skin tone, and wide smile. They wear no accessories, have none facial hair, and their clothing style is casual. Bill has straight hair texture, none glasses style, a pointed nose, large ears, and none on their cheeks.

7. Charles is a middle-aged male with short brown hair and blue eyes. Charles has a olive complexion, fair skin tone, and neutral smile. They wear hat, have none facial hair, and their clothing style is sporty. Charles has wavy hair texture, none glasses style, a broad nose, medium ears, and freckles on their cheeks.

8. Claire is a middle-aged female with short black hair and brown eyes. Claire has a fair complexion, olive skin tone, and closed-lip smile. They wear no accessories, have none facial hair, and their clothing style is formal. Claire has straight hair texture, none glasses style, a pointed nose, medium ears, and none on their cheeks.

9. David is a young male with short blonde hair and brown eyes. David has a olive complexion, olive skin tone, and wide smile. They wear no accessories, have mustache facial hair, and their clothing style is casual. David has straight hair texture, none glasses style, a round nose, medium ears, and dimples on their cheeks.

10. Eric is a middle-aged male with short blonde hair and blue eyes. Eric has a fair complexion, light skin tone, and neutral smile. They wear no accessories, have beard facial hair, and their clothing style is formal. Eric has curly hair texture, none glasses style, a pointed nose, small ears, and none on their cheeks.

11. Frans is a elderly male with short red hair and brown eyes. Frans has a dark complexion, light skin tone, and closed-lip smile. They wear glasses, have mustache facial hair, and their clothing style is casual. Frans has straight hair texture, square glasses style, a broad nose, large ears, and none on their cheeks.

12. George is a middle-aged male with bald white hair and blue eyes. George has a olive complexion, olive skin tone, and neutral smile. They wear glasses, have none facial hair, and their clothing style is formal. George has none hair texture, round glasses style, a pointed nose, medium ears, and freckles on their cheeks.

13. Herman is a young male with short brown hair and brown eyes. Herman has a fair complexion, light skin tone, and wide smile. They wear no accessories, have beard facial hair, and their clothing style is sporty. Herman has wavy hair texture, none glasses style, a round nose, small ears, and none on their cheeks.

14. Joe is a young male with curly black hair and brown eyes. Joe has a dark complexion, fair skin tone, and closed-lip smile. They wear hat, have mustache facial hair, and their clothing style is casual. Joe has curly hair texture, none glasses style, a broad nose, medium ears, and dimples on their cheeks.

15. Maria is a elderly female with long brown hair and brown eyes. Maria has a fair complexion, olive skin tone, and wide smile. They wear glasses, have none facial hair, and their clothing style is formal. Maria has straight hair texture, round glasses style, a pointed nose, small ears, and none on their cheeks.

16. Max is a young male with short blonde hair and blue eyes. Max has a light complexion, fair skin tone, and neutral smile. They wear no accessories, have none facial hair, and their clothing style is sporty. Max has wavy hair texture, none glasses style, a broad nose, medium ears, and freckles on their cheeks.

17. Paul is a elderly male with bald white hair and brown eyes. Paul has a fair complexion, olive skin tone, and closed-lip smile. They wear no accessories, have none facial hair, and their clothing style is formal. Paul has none hair texture, none glasses style, a round nose, large ears, and dimples on their cheeks.

18. Peter is a middle-aged male with curly black hair and blue eyes. Peter has a olive complexion, light skin tone, and neutral smile. They wear hat, have none facial hair, and their clothing style is casual. Peter has curly hair texture, none glasses style, a pointed nose, medium ears, and none on their cheeks.

19. Philip is a young male with short red hair and brown eyes. Philip has a dark complexion, fair skin tone, and wide smile. They wear no accessories, have none facial hair, and their clothing style is sporty. Philip has straight hair texture, none glasses style, a round nose, small ears, and freckles on their cheeks.

20. Richard is a middle-aged male with short brown hair and blue eyes. Richard has a olive complexion, light skin tone, and closed-lip smile. They wear glasses, have beard facial hair, and their clothing style is formal. Richard has straight hair texture, square glasses style, a broad nose, large ears, and none on their cheeks.

21. Robert is a middle-aged male with short brown hair and brown eyes. Robert has a fair complexion, olive skin tone, and neutral smile. They wear glasses, have none facial hair, and their clothing style is casual. Robert has wavy hair texture, round glasses style, a pointed nose, medium ears, and dimples on their cheeks.

22. Sam is a young male with short brown hair and brown eyes. Sam has a fair complexion, dark skin tone, and closed-lip smile. They wear no accessories, have mustache facial hair, and their clothing style is sporty. Sam has curly hair texture, none glasses style, a broad nose, small ears, and none on their cheeks.

23. Susan is a middle-aged female with short blonde hair and blue eyes. Susan has a olive complexion, light skin tone, and wide smile. They wear earrings, have none facial hair, and their clothing style is formal. Susan has straight hair texture, none glasses style, a round nose, large ears, and freckles on their cheeks.

24. Tom is a young male with short blonde hair and blue eyes. Tom has a fair complexion, olive skin tone, and neutral smile. They wear no accessories, have none facial hair, and their clothing style is sporty. Tom has curly hair texture, none glasses style, a pointed nose, medium ears, and none on their cheeks.
```

**Step Observation:**
After each step, the environment returns the action (e.g. "Yes", "No", "I don't know"). For example:
```plaintext
[Player 0] Is the character male?
[GAME] Yes
```

By default, the environment returns observations in the following format:
```python
{
  player_id: int : [
    (sender_id: int, message: str),
    (sender_id: int, message: str),
    ...
  ]
}
```

**Note:** _Should the judge be unable to interpret the questions and provide one of the three assigned options - "yes", "no" or "I don't know" - the judge will respond with "I'm sorry, I don't understand. Please try asking again." in an effort to get the player to rephrase its action._

## Gameplay
**Game Setup:** The game begins with the `reset` method selecting a target character from a predefined character list. The player then initiates the game by asking yes-or-no questions to uncover hints about the target character.

**Turns:** Players take turns by either asking questions or making a final guess. Each turn, the player can ask questions like, “Does the character have blue eyes?” The gamemaster will respond with "Yes," "No," or "I don’t know," providing clues for deducing the character. To submit a final guess of the character, players must wrap it in square brackets (e.g., [Tom]). The environment tracks questions asked, and the correctness of the final guess.

**Objective:** The player’s goal is to identify the target character using the information gleaned from the gamemaster's responses. By formulating strategic questions, the player can narrow down the possibilities and improve their chances of guessing correctly.

**Winning Condition:** The game is won if the player successfully guesses the target character within the allowable turns of the game - 40 turns by default. If the player’s guess matches the target character, they receive a congratulatory message, confirming the win. If incorrect, the player loses the game.


## Key Rules:

- **Valid Moves:**
    - For the submission of final guesses, the player must follow the structure of [Tom].
    - The player is allowed to think through its questions, as well as also ask questions that are a combination of features.

- **Invalid Moves:**
    - A move is invalid if the player makes the wrong guess of the target character.

## Rewards
| Outcome          | Reward for Player |
|------------------|:-----------------:|
| **Win**          |       `+1`        |
| **Lose**         |       `0`         |
| **Invalid Move** |       `-1`        |

## Variants

| Env-id                |
|-----------------------|
| `GuessWho-v0`         |


## Example Usage

```python
import textarena as ta

## initializa the environment
env = ta.make("GuessWho-v0")

## Wrap the environment for easier observation handling
env = ta.wrappers.LLMObservationWrapper(env=env)

## Wrap the environment for pretty rendering
env = ta.wrappers.PrettyRenderWrapper(env=env)

## initalize agents
agent0  = ta.basic_agents.GPTAgent(model_name="gpt-4o-mini")

## reset the environment to start a new game
observations = env.reset(seed=490)

## Write the game loop
done = False
while not done:
    for player_id, agent in enumerate([agent0]):
        ## get the current observation for the player
        obs = observations[player_id]

        ## Get the agent to use the observation and make an action
        action = agent(obs) 

        ## use the action and execute in the environment
        observation, rewards, truncated, terminated, info = env.step(player_id, action)

        ## render the environment
        env.render()

        ## check if the game has ended
        done = truncated or terminated

## Finally, print the game results
for player_id, agent in enumerate([agent0]):
    print(f"{agent.agent_identifier}: {rewards[player_id]}")
print(f"Reason: {info['reason']}")
```

## Troubleshooting

**Repeated Question:**

- **Issue**: Due to the open response, the player has given questions that are closely similar to earlier ones.
- **Solution**: Insert into the player's initial prompt the strategy of seeking a diverse range of questions.

**Gamemaster Response Missing or Incorrect:**

- **Issue**: The gamemaster doesn’t respond or provides an unclear answer to a valid question.
- **Solution**: Check that _generate_gamemaster_response correctly processes the player’s question and that self.gamemaster.respond_to_action is functioning as expected.


## Version History
- **v0**
  - Initial release 


### Contact
If you have questions or face issues with this specific environment, please reach out directly to bobby_cheng@i2r.a-star.edu.sg