Hangman is a popular game of guessing. One player thinks of a word, and then provides the participants with a hint about the word, as well as how many letters it contains. It was super fun at school during recess times. But guess what? It can still be fun if you play against a machine and collaboratively with your friends. Meet Hangman game with Redis.
This is the Hangman game written in JavaScript for the frontend, and uses Java and Redis for the backend. Once deployed, multiple users can play simultaneously, while a real-time scoreboard is computed based on the plays.
To deploy this game locally, you will need to have the following pre-requisites:
- Docker: to pull images and create the containers with the services.
Go to the root directory from this project where the file docker-compose.yml
is located and execute the following command:
docker compose up -d
Once the services finish starting up, you can access the game in the following URL:
http://localhost:8080/hangman-game-redis
To stop the services, execute the following command:
docker compose down
To deploy this game on AWS, you will need to have the following pre-requisites:
- Java 17: to compile and build Lambda functions and backend APIs.
- Maven: to build the project and create the deployment archive.
- Terraform: to create all the resources and required configuration.
- Redis Cloud: for the Redis database to store the game data.
- AWS Account: for resources like S3, Lambda, and API Gateway.
During deployment, the code will create a new subscription and database in your Redis Cloud account. By default, the subscription will be based on the 30 MB Essentials plan, which is free. This allows you to play with the game without paying a single dime to Redis. If you are wondering whether a 30MB
database size will suffice for you to play the game with thousands of users simultaneously, the answer is yes. In fact, this plan allows you to handle 500,000
users approximately. For anything beyond this, consider a plan with a larger capacity.
In order for the code to create these resources, it needs to know how to interact with your Redis Cloud account. Therefore, you must export valid API keys from your Redis Cloud account as environment variables before moving further. You can find instructions about how to create your own API keys here. Then export the following environment variables:
export REDISCLOUD_ACCESS_KEY=<YOUR_REDISCLOUD_ACCESS_KEY>
export REDISCLOUD_SECRET_KEY=<YOUR_REDISCLOUD_SECRET_KEY>
To deploy the game, go to the cloud
directory, and execute the following command:
sh deploy.sh
This will start with the compilation of the source-code and the download of the dependencies from Terraform. Keep in mind that in order for the Terraform code to create cloud resources, your AWS credentials must be already set in your environment.
From this point on, Terraform will start the deployment of the game on AWS. It may take a few minutes to complete. When it finishes, you will see an output like this:
Apply complete! Resources: 94 added, 0 changed, 0 destroyed.
Outputs:
Game = "http://hangman-game-redis-uniquestring.s3-website-us-east-1.amazonaws.com"
This is the URL of the game. Share this URL with everybody wanting to play. To undeploy the game and delete the resources created, go to the cloud
directory, and execute the following command:
sh undeploy.sh
The first thing you will see after opening the game is a text box asking you to specify your player's name. This is a required step. Your name is required to compute the scoreboard.
At any time, you can set the player name again by pressing Shift + P. To close the set player name window, press the ESC key.
After confirming your name, the game begins. The game is self-explanatory. All you have to do is read the hint provided, and try to guess what word is being asked. Use your mouse or your keyboard to select the characters representing the letter from your guess. If you enter a wrong letter, the hangman will start to build. You have 6 guesses per game. If you find the word, congratulations. A new point will be added to your score. Otherwise, your score will not add up.
Whether you win or lose, you will always be presented with the option to play again. Just click in the play again button and try one more time. A new word will be picked to you.
To see the scoreboard, press Shift + S.
To close the scoreboard window, press the ESC key.
The game also makes available some interesting metrics about the plays from users. To see the metrics, press Shift + M.
To close the metrics window, press the ESC key.
Hangman Game with Redis is licensed under the MIT license.