Code Monkey home page Code Monkey logo

15puzzle's Introduction

15 Puzzle

This app generates a solveable 15-puzzle based off of a randomly selected image retrieved from the Unsplash API

Technical Overview

The meat and potatoes of this project is a React app, based off of Create React App (CRA).

In an effort to obscure the api key, a Node server is being used as a proxy for the api call to Unsplash.

The following libraries are also used:

React-redux was purposely not used as it was deemed more appropriate to let each <Puzzle> manage its own internal state.

Getting Started

These instructions will get you a copy of the project up and running on your local machine, and you can get your puzzling on.

Prerequisites

You will need a valid Unsplash API key. You can obtain that here.

Installing

Clone the repo and navigate into the folder:

cd 15puzzle

Install the dependencies for the project by running the following command from the project's root:

npm install

This will install all necessary dependencies.

Create a .env file in /server and place your api key in there. Your folder structure should look like this:

/15puzzle
 /client
 ...
 /server
 ...
 .env
# .env
UNSPLASH_API_KEY=<YOUR_API_KEY_HERE>

At this point, you should be ready to run!

Development

From the project root, go ahead and start the app:

npm start

Build

From the project root, go ahead and run

npm run build-client

You should now have an updated /build folder in /15puzzle/client/

Running a production build

From the project root, go ahead and run the following command which creates a client build, starts the back-end server and runs the client build.

npm run start-prod

How does it work?

Initialization

Populating the <GameBoard>

When a <GameBoard> component mounts, we

  • ping the Unsplash api for a random image
  • persist the url of the received image in <GameBoard>'s state
  • generate an array (to be used as tiles) containing integers [0...15] in a randomized order
    • we repeat this step until a set of tiles is generated that leads to a solveable game

When a <GameBoard> component render()s, we

  • We .map() through pieces and create a <GamePiece> for each element
    • Each <GamePiece> is provided the coordinates (x,y) of where that piece will be positioned in the 4 x 4 square once the puzzle is solved

      • When a <GamePiece> is clicked, its location relative to the blacked out square is calculated and if it borders the blacked out square, the two are swapped.

      • We keep track of the number of inversions on the board at any given time. An inversion is defined as any instance whereby a tile's current index is lower than its index in th solved puzzle.

      • When there are no inversions present, and the blacked out square is in the bottom-right corner of the board...the puzzle is solved!

Indices can be thought of as:

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

Binding images to each <GamePiece>

  • Each <GamePiece> is provided (via props) the url of the retrieved image from Unsplash.
  • Every <GamePiece> uses this image as its background-image
  • The background-position property is used to adjust the visible portion of the background image based on the <GamePiece>'s position on the 4x4 grid when the puzzle is solved

Built With

Future Development Considerations

Testing

  • Create tests to ensure custom components such as <GamePiece> render what they are supposed to, when they are supposed to
  • Incorporate e2e tests in the browser to test overall user experience

Performance & Scalability

  • Review current performance and number of times components are rendering unnecessarily, use shouldComponentUpdate() where helpful

Refactoring

  • Look into cleaning up the main App.js file - perhaps create additional utilities fo "position" and "index" related functions

Authors

  • Barry Ross

References

15puzzle's People

Contributors

barryross avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.