Open In App

Word Guess Game using React

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we will create an Interactive Word Guess Game using ReactJS. Word Guess game is basically a guessing game, where a hint will be given based on the hint you have to guess the word.

This project basically implements functional components and manages the state accordingly. This Game allows users to interact with the game and guess the hidden words by selecting letters, motive of this fun game is to test the vocabulary in terms of an interactive game. The game provides a hint system where players can request a hint that reveals a single letter in the hidden word. This feature assists players who may be stuck or need a little help to progress. A game over screen is displayed when the user makes too many incorrect guesses.

Let’s have an interactive look at what our final project will look like:

Word Guess Game using React

Technologies Used/Pre-requisites

Approach:

The developed Word Guess Game obeys a functional component-based approach to create an interactive and friendly engaging word guessing game. The game consists of ReactJS’s efficient stat management capabilities and features for a smooth and responsive user interface. Users can select letters from an on-screen displayed English alphabet to guess the hidden word, the correct guesses update the word display and incorrect guesses decrement the remaining guesses count. The developed game also consists of features like Hint, which helps the user to get some hints about the hidden word. The game ends with the Winning and Losing scenarios according to the user’s play.

Steps to create the application:

Step 1: Set up React project using the below command in VSCode IDE.

npx create-react-app <<name of project>>

Step 2: Navigate to the newly created project folder by executing the below command.

cd <<Name_of_project>>

Project Structure:

Project Structure

The dependencies in package.json will look like this:

{
"name": "word-game-gfg",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},

Example: Insert the below code in the App.js and App.css files mentioned in the above directory structure.

  • App.js: This file contains the entire logic, behavior, and structure of the game. There is a hook used (useState) that manages the state of the application. Various buttons are been created for controlling the application.
  • App.css: This file is used to provide beautiful styling to the application, various effects, colors, and width-height management are done through this file.
CSS
/* App.css */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

h1 {
  font-size: 36px;
  margin-bottom: 30px;
  color: rgb(21, 228, 2);
}

.word-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 50px;
}

.letter {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60px;
  height: 60px;
  margin: 0 5px;
  border-radius: 50%;
  font-size: 24px;
  font-weight: bold;
  color: #fff;
  background-color: #333;
  opacity: 1;
  transition: opacity 0.2s ease-in-out;
}

.letter.visible {
  opacity: 1;
}

.button-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.guess-section {
  margin-bottom: 30px;
}

.restart-button,
.remove-button {
  padding: 12px 20px;
  margin-right: 10px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  background-color: #f44336;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.remove-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.restart-button:hover,
.remove-button:hover {
  background-color: #d32f2f;
}

.restart-button:focus,
.remove-button:focus {
  outline: none;
}

.letter-selection {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 30px;
}

.letter-button {
  padding: 10px 15px;
  margin: 5px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  background-color: #2196f3;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.letter-button.selected {
  background-color: #1976d2;
}

.letter-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.hints {
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.hint-button {
  padding: 12px 20px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  background-color: #4caf50;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.hint-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.hint-button:hover {
  background-color: #388e3c;
}

.hint-button:focus {
  outline: none;
}

.message {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.guess-button {
  padding: 12px 30px;
  margin-top: 20px;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  background-color: #ff5722;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.guess-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.guess-button:hover {
  background-color: #e64a19;
}

.guess-button:focus {
  outline: none;
}
.word-description {
  font-size: 18px;
  font-style: bold;
  color: rgb(0, 0, 0);
  margin-bottom: 20px;
}
JavaScript

Steps to run the application:

1. Execute the following command in the terminal.

npm start

2. Open the web browser and type the following URL in the address bar.

http://localhost:3000/

Output:



Next Article

Similar Reads

three90RightbarBannerImg