How to make a Word game with buttons using functional React

share link

by Abdul Rawoof A R dot icon Updated: Feb 16, 2023

technology logo
technology logo

Solution Kit Solution Kit  

To make a word game with buttons using functional React, we need a loop that updates the game while we play. This loop is optimized to run the game smoothly, without any lags, and for this purpose, we will use React Native Game Engine to implement this game.


React Native is great for making any game, depending on our choice of games and expertise with the libraries. The feature of high suiting, a reference to the sleeve buttons on a suit that is functional, is known as a Functional Button, mainly used for making games that are run-on buttons. Let's see how the game runs. First, the player has six moves. If the player clicks the buttons, his choices appear on the screen. Then there is only one right combination, which is six buttons(True), and the two wrong ones(False) after the player chooses his six buttons and must click on the button(check) to check if the game is right. If we are right, a text field will appear, saying we got it right or one saying we got it wrong to try again for another turn to win the game.


Also, we have used some important functions in this kit. 'onClick event' executes a certain functionality when the user clicks a button, and this could be when a user submits a form when we change certain content on the web page and other things like that. 'useState' will allow us to track state in a functional component. It generally refers to data or properties that need to be tracked in an application.


Here is an example of how you can make a word game with buttons using Functional React:

Fig : Preview of the output that you will get on running this code from your IDE.

Code

In this solution we're using React library.

import "./styles.css";
import React, { useState } from "react";

// We need to loop over this array to render buttons and answeres
// Id : to handle click on target button
// isRight : weather the answer is right or wrong
// clicked: weather button clicked or not
const initialState = [
  { id: 1, isRight: true, label: "True 1", clicked: false },
  { id: 2, isRight: true, label: "True 2", clicked: false },
  { id: 3, isRight: true, label: "True 3", clicked: false },
  { id: 4, isRight: true, label: "True 4", clicked: false },
  { id: 5, isRight: true, label: "True 5", clicked: false },
  { id: 6, isRight: true, label: "True 6", clicked: false },
  { id: 7, isRight: false, label: "False 7", clicked: false },
  { id: 8, isRight: false, label: "False 8", clicked: false }
];

export default function App() {
  // Button Restart refresh Page
  // reset counter and count to their inital states will reset the game
  function resetGame() {
    setButtons(initialState);
    setCount(6);
    setCorrect(null);
  }

  const [buttons, setButtons] = useState(initialState);

  // Counter
  const [count, setCount] = useState(6);
  const [correct, setCorrect] = useState(null);

  // Click handler will handle both count and buttons changes
  const buttonClickHandler = (id) => {
    if (count === 0) {
      return;
    }
    setCount(count - 1);
   // Update an array of objects
    setButtons(
      buttons.map((item) =>
        item.id === id ? { ...item, clicked: !item.clicked } : item
      )
    );
  };

  // We are counting the clicked buttons which have a property isRight : true
  const checkIfCorrect = () => {
    let correct = buttons.filter(
      (item) => item.clicked === true && item.isRight === true
    ).length;
    if (correct === 6) {
      setCorrect(true)
    } else {
      setCorrect(false)
    }
  };

  return (
    <div className="App">
      <div>
        <button onClick={resetGame} refresh="true">
          RestartNew
        </button>
        <h3>Chances: 6</h3>
        {count}
      </div>

      <div>
        <h2>Answers</h2>
        {buttons.map(
          (button) =>
            button.clicked && <button key={button.id}>{button.label}</button>
        )}
      </div>
      <h2>Buttons Questions!</h2>
      {/* Question buttons */}
      {buttons.map((button) => (
        <button key={button.id} onClick={() => buttonClickHandler(button.id)}>
          {button.label}
        </button>
      ))}

      <br />
      <br />
      <h2>Checker</h2>
      <button onClick={() => checkIfCorrect()}>Check Answers</button>
      <br />
      <br />
      // Check correct and render your components
      {correct != null && correct && <div>correct</div>}
      {correct != null && !correct && <div>Wrong</div>}
    </div>
  );
}

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Copy the code using "copy" button above and paste it in app.js file(remove the earlier code from app.js) and refer demo for styles.css file.
  6. Open the terminal from IDE.
  7. npm start to run the file.


You can also refer this url 'DEMO' for getting the above output.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for 'word game with buttons using functional react' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in Visual Studio Code 1.73.1.
  2. The solution is tested on node v18.12.1 and npm v8.19.2.
  3. React version-18.2.0.


Using this solution, we are able to create word game with buttons using functional React with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to create word game with buttons using functional React.

Dependent Library

reactby facebook

JavaScript doticonstar image 209050 doticonVersion:v18.2.0doticon
License: Permissive (MIT)

The library for web and native user interfaces

Support
    Quality
      Security
        License
          Reuse

            reactby facebook

            JavaScript doticon star image 209050 doticonVersion:v18.2.0doticon License: Permissive (MIT)

            The library for web and native user interfaces
            Support
              Quality
                Security
                  License
                    Reuse

                      You can also search for any dependent libraries on kandi like 'react'.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.

                      See similar Kits and Libraries