Task description Thought Process
- Key Features
- Getting Started
- Slot Class Documentation
- Constructor
- Public Methods
- Private Methods
- Example
- Unit Tests
Key features:
- Initiates a slot machine object with class "Slot" which accepts the current slot machine configuration from
configuration.tsfile. - The user is able to subscribe / unsubscribe to certain paylines before spinning.
- After each spin the algorithm matches the visible reels against the payline pattern the user has subscribed to and logs whether there are matches, for which symbols, how many matches and what is the prize for the given payline match.
To get started with this project, follow these steps:
- Clone the repository.
- Install dependencies with
npm install. - Open the terminal in
Slot Machine\dist\directory. - Type
npx tsc - run index.js from the
distfolder with commandnode index.jsin the terminal. - After changing
index.ts, repeat steps 3 - 5.
The Slot class represents a slot machine object with customizable parameters such as the number of reels, rows, symbols, and paylines.
reelsCount: The number of reels in the slot machine.rowsCount: The number of visible rows in the slot machine (display).symbols: An interface defining the symbols and their respective prizes for the N'th match against the payline patterns.lines: A 2D matrix representing the payline patterns of the slot machine.- for instance [0,0,0,0,0] represents a line pattern and [0,1,0,1,0] represents a zig-zag pattern.
reels: A 2D matrix representing the initial configuration of reels - each reel is an array with N symbols.
spin()- Spins the slot machine and returns the visible symbols on each reel. It iterates through reels and calls the private methodspinReel(reel: number[], rowsCount: number)for each one. The result from spinReel() is being saved in an array, which is pushed to the 2D matrix visibleReels: number[][]
const visible: number[] = this.spinReel(reel, this.#rowsCount);
visibleReels.push(visible);RETURNS visibleReels:number[][]: An array of arrays representing the visible symbols on each reel after spinning.
subscribeToPayline(paylineIndex: number)- Subscribes to a specific payline by its index. Throws an error if user choose an index that do not exist (The error to be handled in a future endpoint).
For instance if we have the below lines configuration:
lines: [
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[0, 1, 0, 1, 0],
[1, 2, 1, 2, 1],
],and the user subscribes to pattern 3, this means they subscribed to pattern [0, 1, 0, 1, 0]
Example:
slotMachine.subscribeToPayline(3);unsubscribePayline(paylineIndex: number)- Unsubscribes from a specific payline by its index. Throws an error if user unsubscribes from payline that he hasn't subscribed to.
Example:
slotMachine.unsubscribePayline(3);spinReel(index: number, rows: number, reel: number[])- Spins a single reel and returns the visible symbols of that reel.
It chooses an index with Math.random() and then implements the circular index array algorithm (index rotation when index reaches the end of the array).
RETURNS : number[]: An array of numbers representing the visible symbols on the current reel after spinning.
calculatePaylines(visibleReels: number[][])- Calculates the matches and winnings for each subscribed payline. It iterates through the subscriptions array and calls method:
subscription.matchPattern(visibleReels)which mathes against the current subscription payline pattern (Line, ZigZag etc.) depending on whether the user has subscribed to it. For instance if user subscribes to payline 1 and has 3 matches from symbol 4, the function will log the below:
If we assume that the visible reels after the spin are: [ [ 4, <4>, 8 ], [ 1, <4>, 4 ], [ 5, <4>, 4 ], [ 5, 5, 7 ], [ 3, 3, 9 ] ] and the subscribed payline is 1 - [1, 1, 1, 1, 1] (matching pattern on the second row of the visible reels) , then we have 3 matches with matching symbol 4.
We use the matchingSymbol and matches from the result: ResultInterface so we can retrieve the symbol and it's respective prize accordingly (matches used as index ) from the symbols.
symbols {4: [0, 0, 40, 80, 200]} The final result that the method logs is: ''From payline 1 you have 3 matches for symbol 4 and you win 40$''
matchZigZagPattern(visibleReels: number[][], paylineArr: number[]): ResultInterface- Matches the symbols in a zig-zag pattern and calculates winnings.
Returns an object with information of the matchingSymbol and how many consecutive matches there are.
For example, zig-zag patterns are represented like:
[0, 1, 0, 1, 0]
[1, 2, 1, 2, 1]RETURNS ResultInterface: { matches, matchingSymbol }
matchLinePattern(visibleReels: number[][], paylineArr: number[]): ResultInterface- Matches the symbols in a straight line pattern and calculates winnings.
Returns an object with information of the matchingSymbol and how many consecutive matches there are.
For example, line patterns are represented like:
[0, 0, 0, 0, 0]
[1, 1, 1, 1, 1]RETURNS ResultInterface: { matches, matchingSymbol }
const lineZeroPattern = new Line(0, configuration.lines[0]);
const lineOnePattern = new Line(1, configuration.lines[1]);
const lineTwoPattern = new Line(2, configuration.lines[2]);
const zeroOnePattern = new ZigZag(3, configuration.lines[3]);
const oneTwoPattern = new ZigZag(4, configuration.lines[4]);
const subscriptions:PatternInterface[] = [lineZeroPattern, lineOnePattern, lineTwoPattern, zeroOnePattern, oneTwoPattern];
const slotMachine = new Slot(configuration.reelsCount, configuration.rowsCount, configuration.symbols, configuration.lines, configuration.reels, subscriptions);
slotMachine.spin();Unit tests are in src/unit-tests directory. There will be a separate unit-test file for each function.
Currently there are only tests for spinReel logic.
- Go to
src\unit-testsand open the terminal; - In the terminal write command
mocha {filename};