@april-cs-npm/arcade-core 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@april-cs-npm/arcade-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
 
5
5
  "description": "npm package for the 2048 logic: move, spawn, hasMoves, score",
6
6
  "main": "src/index.js",
package/src/move.js CHANGED
@@ -1,30 +1,31 @@
1
1
  function mergeRow(row) {
2
- // STEP 1: keep only the non-zero numbers
3
2
  const nums = row.filter((n) => n !== 0);
4
3
 
5
- // STEP 2: merge equal neighbours once
6
4
  const merged = [];
5
+ let points = 0; // track points earned in this row
7
6
  let i = 0;
8
7
  while (i < nums.length) {
9
8
  if (nums[i] === nums[i + 1]) {
10
- merged.push(nums[i] + nums[i + 1]);
11
- i += 2; // skip both so they can't merge again
9
+ const sum = nums[i] + nums[i + 1];
10
+ merged.push(sum);
11
+ points += sum; // add the points earned from this merge
12
+ i += 2;
12
13
  } else {
13
14
  merged.push(nums[i]);
14
15
  i += 1;
15
16
  }
16
17
  }
17
18
 
18
- // STEP 3: pad with zeros until it's 4 long again
19
19
  while (merged.length < 4) {
20
20
  merged.push(0);
21
21
  }
22
22
 
23
- return merged;
23
+ return { row: merged, points }; // return both the new row and the points earned
24
24
  }
25
25
 
26
26
  function slideLeft(board) {
27
- return board.map((row) => mergeRow(row));
27
+ // for each row, merge it and return the new row (ignore points here)
28
+ return board.map((row) => mergeRow(row).row);
28
29
  }
29
30
 
30
31
 
package/src/score.js CHANGED
@@ -1,25 +1,7 @@
1
- import { transpose } from './move.js';
1
+ import { mergeRow, transpose } from './move.js';
2
2
 
3
- // points earned by merges in a single row when it slides left
4
- function rowScore(row) {
5
- const nums = row.filter((n) => n !== 0);
6
- let points = 0;
7
- let i = 0;
8
- while (i < nums.length) {
9
- if (nums[i] === nums[i + 1]) {
10
- points += nums[i] + nums[i + 1]; // a merge happened: add the NEW tile's value
11
- i += 2;
12
- } else {
13
- i += 1;
14
- }
15
- }
16
- return points;
17
- }
18
-
19
- // total points a move would earn in the given direction
3
+ // Calculate the total score for a given board and direction
20
4
  function score(board, direction) {
21
- // reshape the board so `direction` points LEFT, the same way move() does,
22
- // then sum rowScore across all rows.
23
5
  let rows;
24
6
  switch (direction) {
25
7
  case 'left':
@@ -38,13 +20,12 @@ function score(board, direction) {
38
20
  throw new Error(`Invalid direction: ${direction}`);
39
21
  }
40
22
 
41
- // add up the score from every row
42
23
  let total = 0;
43
24
  for (const row of rows) {
44
- total += rowScore(row);
25
+ total += mergeRow(row).points; // add the points for this row
45
26
  }
46
27
  return total;
47
28
  }
48
29
 
49
-
50
30
  export { score };
31
+