memory-game
Installing memory-game library
-
To install memory-game library in your project just run the following command:
npm i @tive-labs/memory-game
Using memory-game library
-
Import the library
- At the beginning of the js file in which you are going to use the library import as follows:
import memoryGame from @tive-labs/memory-game
-
Instantiate memoryGame
- First of all it's necessary instantiate memoryGame as follows:
const mGame = memoryGame();
-
initGame
- Next it's necessary initialize the game using the initGame method.
const init = mGame.initGame('animals', 'easy');
-
initGame receives two parameters, category and level.
- Category must be 'animals' or 'numbers'
- Level must be 'easy' or 'medium' or 'hard'
-
initGame returns an object with an array of hidden cards (cards), an array with the entire cards data, and the length of the array (xLength and yLength):
{ cards, cardsResult, xLength, yLength }
-
selectCard
- To play, it's necessary select cards to discover it using the handleCardSelected method.
let selected = mGame.handleCardSelected(0, 1);
-
handleCardSelected receives two parameters, position x and position y.
- x must be a number representing the x position (vertical position) on the 2d array.
- y must be a number representing the y position (horizontal position) on the 2d array.
-
handleCardSelected returns an object with three properties:
{ cards, selectedCardData, cardMatch, }
- cards: Contains the updated array of cards.
- selectedCardData: Contains an object with the data of the discovered card (id, name, shortName)
- cardMatch: Contains an object with a boolean value (isNewMatched) that indicates if exist a new card match, and an object (matchedCards) with the positions of the matched cards (x_y):
{ isNewMatched, matchedCards: { firstCard, lastCard } }
-
Verify winner
- Another way to verify if exist a winner is using the getIsThereAWinner method.
let isThereAWinner = mGame.getIsThereAWinner()
-
Handling Errors
- Use 'try catch' to handle errors:
try { let selected = mGame.handleCardSelected(0, 1); } catch(error) { console.log(error); }