Skip to content

joaopege1/termo_game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Word Guessing Game API

Overview

This is a simple word guessing game API built with FastAPI, inspired by games like Wordle. The game allows setting a secret word (the "answer") and then making guesses. For each guess:

  • It provides feedback on letters that are correct and in the right position (as a list of 1s and 0s).
  • It can also provide a list of letters that are in the secret word but in the wrong position.

The API uses global state for the secret word and tracked correct letters, so it's best suited for single-session play (e.g., one player at a time). Note: The current implementation has some limitations, such as no built-in attempt tracking across multiple API calls and persistent global state for correct letters, which may require manual resets in a production setup.

Features

  • Set a secret word via POST request.
  • Guess a word and get positional feedback (correct letter and position).
  • Get letters that are correct but in the wrong position.
  • Basic error handling for game operations.

Requirements

  • Python 3.8+
  • FastAPI
  • Uvicorn (for running the server)

Usage

1. Set the Secret Word

Use the POST endpoint to define the secret word. All operations are case-insensitive (converted to lowercase).

  • Endpoint: POST /game/answer

  • Request Body: JSON with "answer" key (e.g., {"answer": "apple"})

  • Response: Success message and the new answer.

  • Example (using curl):

    curl -X POST "http://127.0.0.1:8000/game/answer" -H "Content-Type: application/json" -d '{"answer": "apple"}'
    

    Response:

    {
      "message": "Answer updated successfully",
      "new_answer": "apple"
    }

2. Make a Guess

Guess a word of the same length as the secret. The API returns a list indicating correct positions (1 for correct letter and position, 0 otherwise). If the guess is fully correct, it congratulates you.

  • Endpoint: GET /game?word=<your_guess>

  • Query Parameter: word (string, e.g., word=apric)

  • Response:

    • If correct: Congratulation message.
    • If incorrect: List of matching indicators (e.g., [1, 0, 0, 1, 0]).
    • Note: The code includes a loop for up to 5 attempts, but due to early returns, it effectively processes a single guess per call. Multiple guesses require multiple API calls.
  • Example (assuming secret is "apple"):

    curl "http://127.0.0.1:8000/game?word=apric"
    

    Response (example):

    {
      "matching characters and index": [1, 0, 0, 1, 0]
    }

    If fully correct:

    {
      "message": "Congratulations! You've guessed the word correctly."
    }

3. Check Letters in Wrong Positions

For a given guess, get a list of letters that appear in the secret word but are not in the correct position (based on prior correct position tracking).

  • Endpoint: GET /game/wrong-position?word=<your_guess>

  • Query Parameter: word (string)

  • Response: List of letters in the wrong position.

  • Example (assuming secret is "apple" and some correct positions tracked):

    curl "http://127.0.0.1:8000/game/wrong-position?word=apric"
    

    Response (example):

    {
      "letters in the word but in wrong position": ["p"]
    }

Game Logic Details

  • Correct Position Check: Compares each character of the guess to the secret word. Returns a list where 1 indicates a match in letter and position, 0 otherwise.
  • Wrong Position Check: Identifies letters in the guess that are in the secret but not already marked as correctly positioned (tracked globally).
  • Winning Condition: The guess matches the secret exactly (all 1s in the position list).
  • Limitations:
    • Global state: Correct letters (positions) are accumulated across calls, which may lead to unexpected behavior in multi-user scenarios. Consider resetting right_letters manually if needed.
    • No automatic attempt limiting across sessions; the 5-attempt loop in the code doesn't persist state between calls.
    • Assumes guesses are the same length as the secret; no validation for length mismatch.
    • Error handling: Returns 500 for internal errors.

Examples

  1. Set secret: POST /game/answer with {"answer": "hello"}.
  2. Guess "holla": GET /game?word=holla → Might return [1, 0, 1, 1, 0].
  3. Check wrong positions for "holla": GET /game/wrong-position?word=holla → Might return ["l"] (depending on state).
  4. Correct guess "hello": GET /game?word=hello → Congratulation message.

Contributing

Feel free to fork and improve! Suggestions: Add attempt tracking, reset state endpoint, length validation, or multiplayer support.

About

This is a simple word guessing game API built with FastAPI, inspired by games like Wordle. The game allows setting a secret word (the "answer") and then making guesses.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages