"Riddle Me This" is an interactive AI-powered riddle game that challenges players with unique, dynamically generated riddles. Built using React and TypeScript, the game leverages Google's Gemini AI to create engaging riddles and evaluate player answers. Players can enjoy the game solo or compete with a friend in a two-player mode.
- AI-generated unique riddles using Google's Gemini API
- Single-player and two-player game modes
- Strategic clue system that affects scoring
- Real-time answer evaluation
- Progressive difficulty with a set number of riddles per game
- Node.js (latest LTS version recommended)
- A Gemini API key from Google AI Studio
-
Clone the repository:
git clone https://github.com/yourusername/riddle-me-this-ai-game.git cd riddle-me-this-ai-game
-
Install dependencies:
npm install
-
Create a
.env.local
file in the root directory and add your Gemini API key:GEMINI_API_KEY=your_gemini_api_key_here
-
Start the development server:
npm run dev
-
Open your browser and navigate to the URL shown in your terminal (typically http://localhost:5173)
- Start a new game by selecting "1 Player"
- Read the riddle and try to solve it
- Use clues if you're stuck, but remember each clue reduces your potential points
- Type your answer and submit
- Play through all five riddles to complete the game
- Select "2 Players" to start a competitive game
- Players take turns answering riddles
- The first player to hit "Hit Me" gets to answer
- If they answer correctly, they earn points
- If they answer incorrectly, the other player gets the points
- The player with the most points after five riddles wins
The project follows a modern React application structure with TypeScript for type safety. Key components include:
-
App Component (
App.tsx
): The main component managing game state, player interactions, and UI rendering. -
Gemini Service (
geminiService.ts
): Handles API communication with Google's Gemini AI for riddle generation and answer validation. -
Types and Constants: Well-defined TypeScript interfaces and game constants that maintain code clarity and consistency.
-
UI Components: Modular components for loading indicators, modals, and game elements.
The application uses React hooks extensively for state management, avoiding the need for additional state management libraries.
-
AI Integration: The application uses Google's Gemini AI to generate unique riddles and evaluate user answers, with careful prompt engineering to ensure quality responses.
-
JSON Parsing: Robust parsing of AI responses with fallback mechanisms to handle various response formats.
-
Session Management: Tracking of riddles to prevent duplicates within and across game sessions.
-
Responsive Design: Tailwind CSS for a responsive and visually appealing interface.
Problem: The Gemini API's response type (response.text
) could potentially be undefined, causing TypeScript errors.
Solution: Implemented null coalescing operators to provide fallback values:
const parsedData = parseGeminiJsonResponse<RiddleData>(response.text || "");
Problem: The AI sometimes returned responses in different formats (with or without markdown code fences, extra text).
Solution: Created a robust parsing function with regex pattern matching and multiple fallback attempts.
Problem: The AI occasionally generated similar or identical riddles.
Solution: Implemented a caching system with similarity detection to ensure variety.
Problem: Ensuring the API key is properly loaded from environment variables.
Solution: Configured Vite to properly expose environment variables to the client-side code.
- Engaging gameplay with AI-generated content ensuring high replayability
- Clean, responsive UI with intuitive controls
- Well-structured codebase with good separation of concerns
- Robust error handling for AI interactions
- State Management: For larger scale, consider using a state management library like Redux or Zustand
- Offline Support: Add caching for previously generated riddles to allow limited offline play
- Accessibility: Enhance keyboard navigation and screen reader support
- Multiplayer: Implement real-time multiplayer using WebSockets
- Difficulty Levels: Add options for different difficulty settings
- Categories: Introduce riddle categories (e.g., wordplay, math, logic)
- Progressive Web App: Convert to a PWA for better mobile experience
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.