A real-time, two-player number guessing game built with Node.js, Express, and Socket.IO.
- Each player secretly chooses a 4-digit number with unique digits (e.g., 1234)
- Players take turns guessing each other's numbers
- After each guess, you'll see:
- Correct Digits: How many digits exist in the secret number
- Correct Positions: How many digits are in the correct position
- First player to guess correctly wins
npm install
npm start
The game will be available at http://localhost:3000
- Create a Session: Click "Create New Session" to start a game
- Share Code: Share the 6-character session code with your friend
- Join Session: Your friend enters the code to join
- Choose Numbers: Both players select their secret 4-digit numbers
- Play: Take turns guessing until someone wins
ai-coding-challenge/
├── server.js # Express + Socket.IO server
├── package.json # Dependencies and scripts
├── public/
│ ├── index.html # Game UI
│ ├── styles.css # Styling
│ └── game.js # Client-side game logic
└── README.md
- Multi-session support: Handles multiple concurrent games
- Real-time communication: WebSocket-based instant updates
- Game logic: Validates numbers, checks guesses, manages turns
- In-memory storage: Fast session management
- Single-page application: Smooth screen transitions
- Real-time updates: Instant feedback on all actions
- Responsive design: Works on desktop and mobile
- User-friendly: Clear visual feedback and intuitive flow
- Session Management: Create and join games with unique codes
- Real-time Sync: WebSocket-based instant updates
- Multiple Sessions: Support for unlimited concurrent games
- Turn Management: Automatic turn alternation
- Input Validation: Ensures valid 4-digit unique numbers
- Game History: View all guesses and results
- Responsive UI: Works on all devices
- Error Handling: Graceful disconnection handling
- Scalable Architecture: Ready for production deployment
- Backend: Node.js, Express.js
- Real-time: Socket.IO
- Frontend: HTML5, CSS3, Vanilla JavaScript
- Deployment: Render, Railway, or Heroku
npm install
npm run dev
- Open
http://localhost:3000in one browser window - Create a session and copy the code
- Open
http://localhost:3000in another browser window (or incognito) - Join with the code and play
- Must be exactly 4 digits
- All digits must be unique
- Only numeric characters allowed
- Correct Digits: Counts how many digits from the guess exist anywhere in the secret number
- Correct Positions: Counts how many digits are in the exact right position
- Example: Secret = 1234, Guess = 1543
- Correct Digits: 4 (all digits exist)
- Correct Positions: 2 (1 and 4 are in correct positions)
The current architecture supports:
- Multiple concurrent game sessions
- Real-time WebSocket connections
- In-memory session storage
For production scale:
- Database: Add Redis for session persistence
- Load Balancing: Use Socket.IO Redis adapter for multiple instances
- CDN: Serve static assets via CDN
- Monitoring: Add logging and analytics
MIT License - Feel free to use for your projects.
Contributions welcome! Feel free to open issues or submit pull requests.