Dev.to WebDev 🛠 Dev 👁 2 📖 1 min read

Building a Zero-Dependency 3x3 Grid Simulator: Fixed Paylines vs Adjacent-Reel Ways in Vanilla JS

When explaining probability and grid-matching mechanics to newcomers, static tables rarely click. One common point of confusion in 3×3 reel grids is how fixed paylines and adjacent-reel ways evaluate the exact same board

When explaining probability and grid-matching mechanics to newcomers, static tables rarely click. One common point of confusion in 3×3 reel grids is how fixed paylines and adjacent-reel ways evaluate the exact same board completely differently.

In this short walkthrough, we build the core calculation logic in ~25 lines of zero-dependency vanilla JavaScript.

1. Modeling the 3×3 Grid

Represent a 3-row × 3-column grid as a flat array of 9 booleans (or symbol IDs), where index r * 3 + c corresponds to row r and column (reel) c:

// 5 classic fixed paylines: 3 horizontal rows + 2 diagonals
const FIXED_LINES = [
  [0, 1, 2], // top row
  [3, 4, 5], // middle row
  [6, 7, 8], // bottom row
  [0, 4, 8], // main diagonal
  [2, 4, 6], // anti-diagonal
];

function countFixedPaylines(grid, targetSymbol) {
  return FIXED_LINES.filter(line =>
    line.every(idx => grid[idx] === targetSymbol)
  ).length;
}

2. Counting Adjacent-Reel Ways (Left-to-Right Combinatorics)

Unlike fixed paylines, ways-to-win ignores row alignment and simply multiplies the count of matching symbols on each consecutive reel starting from reel 0:

function countAdjacentWays(grid, targetSymbol) {
  const perReel = [0, 1, 2].map(col =>
    [0, 1, 2].reduce((acc, row) => acc + (grid[row * 3 + col] === targetSymbol ? 1 : 0), 0)
  );
  // If any reel has 0 matches, total 3-of-a-kind ways is 0
  return perReel[0] * perReel[1] * perReel[2];
}

3. Why the Same Board Can Yield 0 Lines or 12 Ways

Consider a board where Reel 1 has 2 matching symbols (top & bottom), Reel 2 has 2 matching symbols (top & bottom), and Reel 3 has 3 matching symbols (all three rows):

  • If the middle cell on Reel 2 is empty, diagonal routes break—so countFixedPaylines() can return far fewer lines (or 0 if the pattern misses all 5 preset routes).
  • Meanwhile, countAdjacentWays() multiplies 2 × 2 × 3 = 12 distinct left-to-right combinations.

Live Demo & Reference

You can toggle cells interactively and see both counters update side by side in our zero-dependency interactive Paylines vs Ways simulator.

Note: Shared purely for educational, algorithmic, and UI prototyping reference (18+, informational only).

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.