DEV Community

Cover image for Building a Mind-Reading AI: Rock Paper Scissors with Redis 8's Real-Time Magic
depa panjie purnama
depa panjie purnama Subscriber

Posted on

Building a Mind-Reading AI: Rock Paper Scissors with Redis 8's Real-Time Magic

Redis AI Challenge: Real-Time AI Innovators

This is a submission for the Redis AI Challenge: Real-Time AI Innovators.

homepage

What I Built

I created Rock Paper Scissors Mind Reader - an AI-powered game where a machine learning model attempts to predict your next move in real-time by analyzing your playing patterns. The twist? The AI explains its reasoning, showing you exactly how it's learning to read your mind!

Key Features:

  • Real-time AI Predictions: The AI analyzes your patterns and predicts your next move before you make it
  • Explainable AI: See exactly why the AI made its prediction with detailed reasoning
  • Pattern Visualization: Watch your playing patterns emerge in real-time
  • Global Leaderboard: Compete with players worldwide
  • Client-Side API Keys: Secure, privacy-focused design where players use their own Gemini API keys

The game creates a fascinating psychological battle where you try to be unpredictable while the AI gets smarter with every move!

Demo

  • Live Demo: https://rock-paper-scissors-web-ebon.vercel.app
  • Video:
  • Screenshot: the game
  • GitHub repo:

    GitHub logo depapp / rock-paper-scissors

    An innovative real-time AI-powered prediction game where players battle against an AI that learns their patterns and tries to predict their next move in Rock Paper Scissors. Built for the Redis "Real-Time AI Innovators" challenge.

    🧠 Rock Paper Scissors Mind Reader - AI Prediction Game

    An innovative real-time AI-powered prediction game where players battle against an AI that learns their patterns and tries to predict their next move in Rock Paper Scissors. Built for the Redis "Real-Time AI Innovators" challenge.

    🎮 Game Concept

    Players choose between Rock ✊, Paper 📄, or Scissors ✂️ while an AI analyzes their patterns in real-time and predicts their next choice. The AI's prediction is hidden until after the player makes their choice, creating true suspense. The AI gets smarter with each move, learning from:

    • Frequency patterns
    • Sequential patterns
    • Complex multi-step patterns
    • Meta-patterns (trying to be unpredictable)
    • Psychological patterns (panic choices, pressure responses)

    🚀 Key Features

    Real-Time AI Learning

    • Pattern Recognition: AI analyzes player behavior in real-time
    • Adaptive Difficulty: AI confidence grows as it learns your patterns
    • Explainable AI: AI explains its reasoning for each prediction
    • Multiple

How I Used Redis 8

Redis 8 is the backbone of this application, powering every real-time feature. Here's how I leveraged its capabilities:

1. Vector Search for Pattern Matching

// Find players with similar playing patterns
const similarPlayers = await redis.ft.search('idx:profiles', 
  `*=>[KNN 10 @embedding $vec]`
);
Enter fullscreen mode Exit fullscreen mode

I use Redis Vector Search to find players with similar patterns, helping the AI learn from collective behavior.

2. Semantic Caching for AI Predictions

// Cache AI analysis for similar pattern combinations
await redis.setex(`analysis:${patternHash}`, 300, aiPrediction);
Enter fullscreen mode Exit fullscreen mode

Frequently occurring patterns are cached, reducing API calls and providing instant predictions.

3. Redis Streams for Move History

// Record every move for pattern analysis
await redis.xadd(`moves:${gameId}`, '*', {
  move: playerMove,
  prediction: aiPrediction,
  timestamp: Date.now()
});
Enter fullscreen mode Exit fullscreen mode

Every game move is streamed in real-time, creating a comprehensive dataset for pattern analysis.

4. Sorted Sets for Global Leaderboard

// Update player rankings in real-time
await redis.zadd('leaderboard', {
  score: winRate * 1000 + totalWins,
  value: username
});
Enter fullscreen mode Exit fullscreen mode

The leaderboard updates instantly as games finish, showing top players globally.

5. Pub/Sub for Real-Time Updates

// Broadcast game events to all connected clients
redis.publish('game:updates', JSON.stringify({
  type: 'leaderboard_update',
  data: newRankings
}));
Enter fullscreen mode Exit fullscreen mode

Live updates keep all players synchronized with the latest statistics.

6. Time-Series for Performance Metrics

// Track AI accuracy over time
await redis.ts.add('ai:accuracy', '*', accuracyScore);
Enter fullscreen mode Exit fullscreen mode

Monitor how the AI's prediction accuracy improves as it learns more patterns.

7. Hash Storage for Game State

// Store complex game state efficiently
await redis.hset(`game:${gameId}`, {
  playerPattern: JSON.stringify(patterns),
  aiConfidence: confidence,
  moveHistory: JSON.stringify(history)
});
Enter fullscreen mode Exit fullscreen mode

Technical Architecture

Stack:

  • Frontend: Next.js 14, TypeScript, Tailwind CSS, Framer Motion
  • Backend: Node.js, Express, Socket.io, TypeScript
  • AI: Google Gemini for pattern analysis and explanations
  • Database: Redis 8 (Cloud)
  • Architecture: Monorepo with shared types

Security & Privacy:

  • Client-side API key management (keys never touch the server)
  • Local storage for user preferences
  • No personal data collection

Challenges and Learnings

Building this project taught me:

  1. How to effectively combine multiple Redis features for real-time AI
  2. The importance of client-side security for API keys
  3. Creating engaging visualizations for complex data
  4. Balancing AI intelligence with game enjoyment

Future Enhancements

  • Tournament mode with brackets
  • More sophisticated pattern detection algorithms
  • Mobile app version
  • AI difficulty levels
  • Pattern sharing between friends

This project showcases how Redis 8's real-time capabilities can create engaging AI-powered experiences. By combining vector search, caching, streams, and pub/sub, I've built a game that's not just fun to play, but also demonstrates the future of real-time AI applications.

The mind-reading aspect creates a unique psychological challenge that keeps players coming back, while the technical implementation shows the power of Redis as a complete real-time data platform for AI applications.

Top comments (0)