The making of GuessTheColor Game
Tags: devjournal, nextjs, design, color
Published: 2022-11-01 14:48:52

A nextJS game with its objective of identifying as many colors as you can in a given amount of time using RGB color codes.

In this article, I briefly describe how I built [this game](https://tebza.dev/colordash).

Source code available on [github.](https://github.com/TebzaTheMan/ColorDash)

## UI Design

When I started working on this project, only had an idea of the gameplay, where there is an RGB value and colors displayed on the screen, and the player has to select the color that matches the RGB value.
I also thought of a time constraint. With that in mind, I listed the UI design goals:
* Display the RGB code to be guessed
*  6 random colors to choose from
*  score
*  timer

I decided to use [ChakraUI](https://chakra-ui.com/) for the UI; this decision rests on the fact that it is easy to use and you can create beautiful prototypes faster.

I forked the [chakra Figma UI kit](https://www.figma.com/community/file/971408767069651759), played around with the components, and followed the design goals.

Here are the final versions

Start page

![design_start_page.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rk6hy74en1423ze7b4gw.png)

Play page

![design_play_page.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3juvykap27kmnlmhos3.png)

Game over modal

![design_GameOver.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtvdan4ya42i2py3it49.png)

sidenote: previous iterations are also available on the [Figma file](https://www.figma.com/file/B3N8tEuQPTJCiH85t6QyoA/GuessTheColor?node-id=873%3A9291) 🙈

## Development 
Now that I knew what I wanted to build, I decided on the tech stack. 

The project follows the [bullet-proof react](https://github.com/alan2207/bulletproof-react) architecture principles. This is an amazing resource that teaches you how to build scalable react apps, so much so that I created a [nextjs-starter-pack](https://github.com/TebzaTheMan/nextjs-starter-pack) template based on these principles.


- For the front end, I used ... wait for it .... NextJS.
- ChakraUI for the UI 
- Typescript for static testing
- For all the user storage needs like saving the high score, LocalStorage to the rescue 🚑.
- Vercel for deployment!
- Deciding to use the [Git Feature Branch Workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow) -  which is basically creating a branch for each feature and then merging to the main branch, influenced my coding action plan.

I broke up my design into small features.
Started with the start page and broke it down into these components: 
* Highscore
* Name of the game
* Play Button

The play page 
- Infobar
- color blocks
- game over modal

here's my commit history 🙈


![commit history.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar1y1m4a9k4j3ipsmspu.jpg)



***
The favorite feature I loved to implement was the random color blocks, and ironically, that's where my most annoying bug also came from

Seeing these random colors pop up was pretty cool ...


![3 mobile phone views](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl62fxfu72ki454bm1bt.jpg)



 but also a nasty error popped up every time I reloaded the page.


![reloading play page crashes.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wcq8iwnhknnjef4pkdm.jpg)



I found myself reading about  [Schrodinger's cat](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat) 😂😭. It was used to explain how React Hydration works in this [awesome article](https://www.joshwcomeau.com/react/the-perils-of-rehydration/). 

So the cause of my pain was the random generation of the colors. On the server, it would generate a set of colors and pick a random color and display its RGB code, and also the client would generate another set of colors and pick another random color's RGB code to be displayed. This caused a mismatch. The best solution was randomly picking the color to be guessed only on the client side.


UseEffect( ) to the rescue!  🚑

[Colorblocks.tsx](https://github.com/TebzaTheMan/ColorDash/blob/main/features/colorblocks/components/Colorblocks.tsx)
 ```javascript
useEffect(() => {
    setCorrectColor(colors[Math.floor(Math.random() * NUM_COLORS)]);
  }, [colors]);

```
## Remarks
If you want to 'finish' a side project, don't try to build it to its full potential in one go. Rather simplify it to its bare minimum and add new features later on.

## What's next?

The game is at its bare minimum, and I have a few( a lot )  features I want to add
some of them are
- Help / how to play page
- Dark mode 🌙 because why not?
- A penalty system since you have 3 tries. If you get the color right on your first try, you get 10 points, 2nd try is 5 points, and 3rd try is 2 points.
- Difficulty modes
something like a beginner, Amateur, and expert mode


## Conclusion
Really enjoyed building this project! Give the game a go and see if you can beat my high score of 60 😂. 
You can find my tips on identifying the colors from an RGB value [here](https://tebza.dev/how-to-identify-color-from-an-rgb-value)✌️.