test: matching, swapping, launch and linking, push-up chain, lock, points
48 cases, including jump-height bonuses, carried blocks, a manual jump capped at the top of the field, a push that forms a new row of three and re-launches the whole cluster, gray blocks scoring as they leave, and game over only when every column is locked.
@@ -0,0 +1,60 @@
1
'use strict';
2
3
const Planet = require('../src/planet.js');
4
const Rng = require('../src/rng.js');
5
const { GameCore } = require('../src/gameCore.js');
6
7
function makeGame (opts)
8
{
9
opts = opts || {};
10
const planet = opts.planet || Planet.createPlanet(opts.planetOpts || {
11
jumpsToClear: 2,
12
fallTime: 3,
13
riseTime: 1.5
14
});
15
const rng = typeof opts.seed === 'number' ? Rng.create(opts.seed) : (opts.rng || Rng.create(1));
16
const game = new GameCore(planet, rng);
17
if (opts.noSpawn)
18
{ game.spawnInterval = 1e9; }
19
return game;
20
}
21
22
/** Fill a column with `count` still blocks of rotating colors starting at 0. */
23
function fillColumn (game, col, count, colors)
24
{
25
colors = colors || [0, 1, 2, 3, 4];
26
const out = [];
27
for (let r = 0; r < count; r++)
28
{ out.push(game.place(col, r, colors[r % colors.length])); }
29
return out;
30
}
31
32
function placeRow (game, row, cols, color)
33
{
34
const out = [];
35
for (const c of cols)
36
{ out.push(game.place(c, row, color)); }
37
return out;
38
}
39
40
function grayFlying (game)
41
{
42
return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);
43
}
44
45
function almost (a, b, eps)
46
{
47
eps = eps == null ? 1e-6 : eps;
48
return Math.abs(a - b) <= eps;
49
}
50
51
module.exports = {
52
makeGame,
53
fillColumn,
54
placeRow,
55
grayFlying,
56
almost,
57
Planet,
58
Rng,
59
GameCore
60
};
@@ -0,0 +1,60 @@1
'use strict';2
3
const Planet = require('../src/planet.js');4
const Rng = require('../src/rng.js');5
const { GameCore } = require('../src/gameCore.js');6
7
function makeGame (opts)8
{9
opts = opts || {};10
const planet = opts.planet || Planet.createPlanet(opts.planetOpts || {11
jumpsToClear: 2,12
fallTime: 3,13
riseTime: 1.514
});15
const rng = typeof opts.seed === 'number' ? Rng.create(opts.seed) : (opts.rng || Rng.create(1));16
const game = new GameCore(planet, rng);17
if (opts.noSpawn)18
{ game.spawnInterval = 1e9; }19
return game;20
}21
22
/** Fill a column with `count` still blocks of rotating colors starting at 0. */23
function fillColumn (game, col, count, colors)24
{25
colors = colors || [0, 1, 2, 3, 4];26
const out = [];27
for (let r = 0; r < count; r++)28
{ out.push(game.place(col, r, colors[r % colors.length])); }29
return out;30
}31
32
function placeRow (game, row, cols, color)33
{34
const out = [];35
for (const c of cols)36
{ out.push(game.place(c, row, color)); }37
return out;38
}39
40
function grayFlying (game)41
{42
return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);43
}44
45
function almost (a, b, eps)46
{47
eps = eps == null ? 1e-6 : eps;48
return Math.abs(a - b) <= eps;49
}50
51
module.exports = {52
makeGame,53
fillColumn,54
placeRow,55
grayFlying,56
almost,57
Planet,58
Rng,59
GameCore60
};@@ -0,0 +1,76 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7
describe('Column lock and game over', () =>
8
{
9
it('a column filled from floor to top becomes dark gray and locked', () =>
10
{
11
const g = makeGame({ noSpawn: true });
12
fillColumn(g, 3, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);
13
g.checkLocks();
14
assert.equal(g.isColumnLocked(3), true);
15
assert.equal(g.isColumnStill(3), false);
16
for (let r = 0; r < 13; r++)
17
{
18
const b = g.blockAt(3, r);
19
assert.ok(b);
20
assert.equal(b.state, 'locked');
21
assert.equal(b.color, GameCore.COLOR_GRAY);
22
}
23
assert.equal(g.beginDrag(3, 5), false);
24
});
25
26
it('a new spawn never lands in a locked column', () =>
27
{
28
const g = makeGame({ seed: 11, noSpawn: false });
29
g.spawnInterval = 1e9;
30
fillColumn(g, 0, 13, [0, 1]);
31
g.checkLocks();
32
assert.equal(g.isColumnLocked(0), true);
33
for (let i = 0; i < 40; i++)
34
{
35
const spawned = g.trySpawn();
36
assert.ok(spawned);
37
assert.notEqual(spawned.col, 0);
38
g.removeBlock(spawned);
39
}
40
});
41
42
it('a nearly full column (12 blocks) is not locked', () =>
43
{
44
const g = makeGame({ noSpawn: true });
45
fillColumn(g, 2, 12, [0, 1, 2]);
46
g.checkLocks();
47
assert.equal(g.isColumnLocked(2), false);
48
assert.equal(g.isColumnStill(2), true);
49
assert.ok(g.beginDrag(2, 0));
50
g.endDrag();
51
});
52
53
it('when every column is locked the player loses', () =>
54
{
55
const g = makeGame({ noSpawn: true });
56
for (let c = 0; c < 10; c++)
57
{ fillColumn(g, c, 13, [c % 5, (c + 1) % 5, (c + 2) % 5]); }
58
g.tick(0.01);
59
assert.equal(g.state, 'gameover');
60
assert.equal(g.lockedColumnCount(), 10);
61
assert.equal(g.trySpawn(), null);
62
assert.equal(g.beginDrag(0, 0), false);
63
});
64
65
it('game over does not fire while at least one column is open', () =>
66
{
67
const g = makeGame({ noSpawn: true });
68
for (let c = 0; c < 9; c++)
69
{ fillColumn(g, c, 13, [0, 1, 2]); }
70
fillColumn(g, 9, 4, [3, 4]);
71
g.checkLocks();
72
g.checkGameOver();
73
assert.equal(g.state, 'playing');
74
assert.equal(g.lockedColumnCount(), 9);
75
});
76
});
@@ -0,0 +1,76 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');6
7
describe('Column lock and game over', () =>8
{9
it('a column filled from floor to top becomes dark gray and locked', () =>10
{11
const g = makeGame({ noSpawn: true });12
fillColumn(g, 3, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);13
g.checkLocks();14
assert.equal(g.isColumnLocked(3), true);15
assert.equal(g.isColumnStill(3), false);16
for (let r = 0; r < 13; r++)17
{18
const b = g.blockAt(3, r);19
assert.ok(b);20
assert.equal(b.state, 'locked');21
assert.equal(b.color, GameCore.COLOR_GRAY);22
}23
assert.equal(g.beginDrag(3, 5), false);24
});25
26
it('a new spawn never lands in a locked column', () =>27
{28
const g = makeGame({ seed: 11, noSpawn: false });29
g.spawnInterval = 1e9;30
fillColumn(g, 0, 13, [0, 1]);31
g.checkLocks();32
assert.equal(g.isColumnLocked(0), true);33
for (let i = 0; i < 40; i++)34
{35
const spawned = g.trySpawn();36
assert.ok(spawned);37
assert.notEqual(spawned.col, 0);38
g.removeBlock(spawned);39
}40
});41
42
it('a nearly full column (12 blocks) is not locked', () =>43
{44
const g = makeGame({ noSpawn: true });45
fillColumn(g, 2, 12, [0, 1, 2]);46
g.checkLocks();47
assert.equal(g.isColumnLocked(2), false);48
assert.equal(g.isColumnStill(2), true);49
assert.ok(g.beginDrag(2, 0));50
g.endDrag();51
});52
53
it('when every column is locked the player loses', () =>54
{55
const g = makeGame({ noSpawn: true });56
for (let c = 0; c < 10; c++)57
{ fillColumn(g, c, 13, [c % 5, (c + 1) % 5, (c + 2) % 5]); }58
g.tick(0.01);59
assert.equal(g.state, 'gameover');60
assert.equal(g.lockedColumnCount(), 10);61
assert.equal(g.trySpawn(), null);62
assert.equal(g.beginDrag(0, 0), false);63
});64
65
it('game over does not fire while at least one column is open', () =>66
{67
const g = makeGame({ noSpawn: true });68
for (let c = 0; c < 9; c++)69
{ fillColumn(g, c, 13, [0, 1, 2]); }70
fillColumn(g, 9, 4, [3, 4]);71
g.checkLocks();72
g.checkGameOver();73
assert.equal(g.state, 'playing');74
assert.equal(g.lockedColumnCount(), 9);75
});76
});@@ -0,0 +1,168 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js');
6
7
describe('Match detection and launch', () =>
8
{
9
it('does not match two adjacent blocks of the same color', () =>
10
{
11
const g = makeGame({ noSpawn: true });
12
g.place(0, 0, 1);
13
g.place(1, 0, 1);
14
g.place(2, 0, 2);
15
assert.equal(g.findMatchLines().length, 0);
16
g.resolveMatches();
17
assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);
18
});
19
20
it('does not match a diagonal of three', () =>
21
{
22
const g = makeGame({ noSpawn: true });
23
g.place(0, 0, 1);
24
g.place(1, 1, 1);
25
g.place(2, 2, 1);
26
g.place(1, 0, 2);
27
g.place(2, 0, 2);
28
g.place(2, 1, 2);
29
assert.equal(g.findMatchLines().length, 0);
30
});
31
32
it('a horizontal row of 3 turns gray, links, and launches together', () =>
33
{
34
const g = makeGame({ noSpawn: true });
35
const row = placeRow(g, 0, [0, 1, 2], 0);
36
const launched = g.resolveMatches();
37
assert.equal(launched.length, 1);
38
assert.equal(launched[0].blocks.length, 3);
39
const expected = Planet.jumpHeight(g.planet, 3, false);
40
assert.equal(launched[0].jumpHeight, expected);
41
for (const b of row)
42
{
43
assert.equal(b.color, GameCore.COLOR_GRAY);
44
assert.equal(b.state, 'flying');
45
assert.equal(b.clusterId, row[0].clusterId);
46
assert.equal(b.flame, true);
47
assert.ok(b.vy > 0);
48
}
49
assert.ok(row[0].clusterId != null);
50
assert.ok(g.clusters[row[0].clusterId]);
51
});
52
53
it('a vertical column of 3 launches with 120% jump height', () =>
54
{
55
const g = makeGame({ noSpawn: true });
56
const a = g.place(3, 0, 2);
57
const b = g.place(3, 1, 2);
58
const c = g.place(3, 2, 2);
59
const launched = g.resolveMatches();
60
assert.equal(launched.length, 1);
61
const expected = Planet.jumpHeight(g.planet, 3, true);
62
assert.equal(launched[0].jumpHeight, expected);
63
assert.ok(Math.abs(expected - Planet.jumpHeight(g.planet, 3, false) * 1.2) < 1e-12);
64
assert.equal(a.color, GameCore.COLOR_GRAY);
65
assert.equal(b.clusterId, a.clusterId);
66
assert.equal(c.clusterId, a.clusterId);
67
});
68
69
it('a horizontal row of 5 gets a 10% jump-height bonus (2 extra blocks)', () =>
70
{
71
const g = makeGame({ noSpawn: true });
72
placeRow(g, 0, [0, 1, 2, 3, 4], 3);
73
const launched = g.resolveMatches();
74
assert.equal(launched[0].jumpHeight, Planet.jumpHeight(g.planet, 5, false));
75
assert.ok(Math.abs(launched[0].jumpHeight - g.planet.baseJumpHeight * 1.10) < 1e-12);
76
});
77
78
it('everything sitting on top of a match flies with it, keeping its color', () =>
79
{
80
const g = makeGame({ noSpawn: true });
81
placeRow(g, 0, [0, 1, 2], 0);
82
const carried = g.place(1, 1, 4);
83
g.resolveMatches();
84
assert.equal(carried.state, 'flying');
85
assert.equal(carried.color, 4);
86
assert.equal(carried.flame, false);
87
assert.equal(carried.clusterId, g.blockAt(0, 0).clusterId);
88
});
89
90
it('blocks below a vertical match stay on the ground', () =>
91
{
92
const g = makeGame({ noSpawn: true });
93
const below = g.place(2, 0, 1);
94
g.place(2, 1, 3);
95
g.place(2, 2, 3);
96
g.place(2, 3, 3);
97
g.resolveMatches();
98
assert.equal(below.state, 'still');
99
assert.equal(below.color, 1);
100
assert.equal(below.y, 0);
101
assert.equal(g.blockAt(2, 1).state, 'flying');
102
});
103
104
it('two disjoint horizontal matches become two clusters', () =>
105
{
106
const g = makeGame({ noSpawn: true });
107
placeRow(g, 0, [0, 1, 2], 0);
108
placeRow(g, 0, [7, 8, 9], 1);
109
const launched = g.resolveMatches();
110
assert.equal(launched.length, 2);
111
const idA = g.blockAt(0, 0).clusterId;
112
const idB = g.blockAt(7, 0).clusterId;
113
assert.notEqual(idA, idB);
114
});
115
116
it('two matches in the same columns fly as one linked cluster', () =>
117
{
118
const g = makeGame({ noSpawn: true });
119
placeRow(g, 0, [0, 1, 2], 0);
120
placeRow(g, 1, [0, 1, 2], 1);
121
const launched = g.resolveMatches();
122
assert.equal(launched.length, 1);
123
const id = g.blockAt(0, 0).clusterId;
124
assert.equal(g.blockAt(2, 1).clusterId, id);
125
assert.equal(g.clusterMembers(id).length, 6);
126
});
127
128
it('tick itself finds and launches a match after a swap', () =>
129
{
130
const g = makeGame({ noSpawn: true });
131
g.place(0, 0, 1);
132
g.place(0, 1, 2);
133
g.place(1, 0, 1);
134
g.place(2, 0, 2);
135
g.place(2, 1, 1);
136
// After swapping col 2, the 1 at (2,1) goes to (2,0): horizontal 1s at row 0.
137
g.swapTo(2, 1, 0);
138
assert.equal(g.blockAt(2, 0).color, 1);
139
g.tick(0);
140
g.resolveMatches();
141
assert.equal(g.blockAt(0, 0).state, 'flying');
142
assert.equal(g.blockAt(1, 0).state, 'flying');
143
assert.equal(g.blockAt(2, 0).state, 'flying');
144
});
145
});
146
147
describe('Launch physics', () =>
148
{
149
it('reaches the jump height in riseTime, then floats down at constant speed', () =>
150
{
151
const g = makeGame({ noSpawn: true });
152
placeRow(g, 0, [0, 1, 2], 0);
153
const launched = g.resolveMatches();
154
const H = launched[0].jumpHeight;
155
const T = g.planet.riseTime;
156
const startY = 0;
157
g.step(T, 1 / 240);
158
const apex = g.blocks[0].y;
159
assert.ok(Math.abs(apex - (startY + H)) < 0.08, 'apex y=' + apex + ' expected ' + (startY + H));
160
assert.equal(g.clusters[g.blocks[0].clusterId].phase, 'down');
161
const y1 = g.blocks[0].y;
162
g.step(0.2, 1 / 120);
163
const y2 = g.blocks[0].y;
164
const speed = (y1 - y2) / 0.2;
165
assert.ok(Math.abs(speed - g.planet.fallSpeed) < 0.15, 'fall speed ' + speed);
166
assert.ok(y2 < y1, 'must be going down');
167
});
168
});
@@ -0,0 +1,168 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js');6
7
describe('Match detection and launch', () =>8
{9
it('does not match two adjacent blocks of the same color', () =>10
{11
const g = makeGame({ noSpawn: true });12
g.place(0, 0, 1);13
g.place(1, 0, 1);14
g.place(2, 0, 2);15
assert.equal(g.findMatchLines().length, 0);16
g.resolveMatches();17
assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);18
});19
20
it('does not match a diagonal of three', () =>21
{22
const g = makeGame({ noSpawn: true });23
g.place(0, 0, 1);24
g.place(1, 1, 1);25
g.place(2, 2, 1);26
g.place(1, 0, 2);27
g.place(2, 0, 2);28
g.place(2, 1, 2);29
assert.equal(g.findMatchLines().length, 0);30
});31
32
it('a horizontal row of 3 turns gray, links, and launches together', () =>33
{34
const g = makeGame({ noSpawn: true });35
const row = placeRow(g, 0, [0, 1, 2], 0);36
const launched = g.resolveMatches();37
assert.equal(launched.length, 1);38
assert.equal(launched[0].blocks.length, 3);39
const expected = Planet.jumpHeight(g.planet, 3, false);40
assert.equal(launched[0].jumpHeight, expected);41
for (const b of row)42
{43
assert.equal(b.color, GameCore.COLOR_GRAY);44
assert.equal(b.state, 'flying');45
assert.equal(b.clusterId, row[0].clusterId);46
assert.equal(b.flame, true);47
assert.ok(b.vy > 0);48
}49
assert.ok(row[0].clusterId != null);50
assert.ok(g.clusters[row[0].clusterId]);51
});52
53
it('a vertical column of 3 launches with 120% jump height', () =>54
{55
const g = makeGame({ noSpawn: true });56
const a = g.place(3, 0, 2);57
const b = g.place(3, 1, 2);58
const c = g.place(3, 2, 2);59
const launched = g.resolveMatches();60
assert.equal(launched.length, 1);61
const expected = Planet.jumpHeight(g.planet, 3, true);62
assert.equal(launched[0].jumpHeight, expected);63
assert.ok(Math.abs(expected - Planet.jumpHeight(g.planet, 3, false) * 1.2) < 1e-12);64
assert.equal(a.color, GameCore.COLOR_GRAY);65
assert.equal(b.clusterId, a.clusterId);66
assert.equal(c.clusterId, a.clusterId);67
});68
69
it('a horizontal row of 5 gets a 10% jump-height bonus (2 extra blocks)', () =>70
{71
const g = makeGame({ noSpawn: true });72
placeRow(g, 0, [0, 1, 2, 3, 4], 3);73
const launched = g.resolveMatches();74
assert.equal(launched[0].jumpHeight, Planet.jumpHeight(g.planet, 5, false));75
assert.ok(Math.abs(launched[0].jumpHeight - g.planet.baseJumpHeight * 1.10) < 1e-12);76
});77
78
it('everything sitting on top of a match flies with it, keeping its color', () =>79
{80
const g = makeGame({ noSpawn: true });81
placeRow(g, 0, [0, 1, 2], 0);82
const carried = g.place(1, 1, 4);83
g.resolveMatches();84
assert.equal(carried.state, 'flying');85
assert.equal(carried.color, 4);86
assert.equal(carried.flame, false);87
assert.equal(carried.clusterId, g.blockAt(0, 0).clusterId);88
});89
90
it('blocks below a vertical match stay on the ground', () =>91
{92
const g = makeGame({ noSpawn: true });93
const below = g.place(2, 0, 1);94
g.place(2, 1, 3);95
g.place(2, 2, 3);96
g.place(2, 3, 3);97
g.resolveMatches();98
assert.equal(below.state, 'still');99
assert.equal(below.color, 1);100
assert.equal(below.y, 0);101
assert.equal(g.blockAt(2, 1).state, 'flying');102
});103
104
it('two disjoint horizontal matches become two clusters', () =>105
{106
const g = makeGame({ noSpawn: true });107
placeRow(g, 0, [0, 1, 2], 0);108
placeRow(g, 0, [7, 8, 9], 1);109
const launched = g.resolveMatches();110
assert.equal(launched.length, 2);111
const idA = g.blockAt(0, 0).clusterId;112
const idB = g.blockAt(7, 0).clusterId;113
assert.notEqual(idA, idB);114
});115
116
it('two matches in the same columns fly as one linked cluster', () =>117
{118
const g = makeGame({ noSpawn: true });119
placeRow(g, 0, [0, 1, 2], 0);120
placeRow(g, 1, [0, 1, 2], 1);121
const launched = g.resolveMatches();122
assert.equal(launched.length, 1);123
const id = g.blockAt(0, 0).clusterId;124
assert.equal(g.blockAt(2, 1).clusterId, id);125
assert.equal(g.clusterMembers(id).length, 6);126
});127
128
it('tick itself finds and launches a match after a swap', () =>129
{130
const g = makeGame({ noSpawn: true });131
g.place(0, 0, 1);132
g.place(0, 1, 2);133
g.place(1, 0, 1);134
g.place(2, 0, 2);135
g.place(2, 1, 1);136
// After swapping col 2, the 1 at (2,1) goes to (2,0): horizontal 1s at row 0.137
g.swapTo(2, 1, 0);138
assert.equal(g.blockAt(2, 0).color, 1);139
g.tick(0);140
g.resolveMatches();141
assert.equal(g.blockAt(0, 0).state, 'flying');142
assert.equal(g.blockAt(1, 0).state, 'flying');143
assert.equal(g.blockAt(2, 0).state, 'flying');144
});145
});146
147
describe('Launch physics', () =>148
{149
it('reaches the jump height in riseTime, then floats down at constant speed', () =>150
{151
const g = makeGame({ noSpawn: true });152
placeRow(g, 0, [0, 1, 2], 0);153
const launched = g.resolveMatches();154
const H = launched[0].jumpHeight;155
const T = g.planet.riseTime;156
const startY = 0;157
g.step(T, 1 / 240);158
const apex = g.blocks[0].y;159
assert.ok(Math.abs(apex - (startY + H)) < 0.08, 'apex y=' + apex + ' expected ' + (startY + H));160
assert.equal(g.clusters[g.blocks[0].clusterId].phase, 'down');161
const y1 = g.blocks[0].y;162
g.step(0.2, 1 / 120);163
const y2 = g.blocks[0].y;164
const speed = (y1 - y2) / 0.2;165
assert.ok(Math.abs(speed - g.planet.fallSpeed) < 0.15, 'fall speed ' + speed);166
assert.ok(y2 < y1, 'must be going down');167
});168
});@@ -0,0 +1,101 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, placeRow, GameCore, Planet } = require('./helpers.js');
6
7
describe('Points, landing and recolor', () =>
8
{
9
it('blocks that leave the top of the field become one point each and are gone', () =>
10
{
11
const g = makeGame({
12
noSpawn: true,
13
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }
14
});
15
placeRow(g, 0, [0, 1, 2], 0);
16
const carried = g.place(1, 1, 4);
17
g.resolveMatches();
18
assert.equal(g.score, 0);
19
g.step(2, 1 / 120);
20
assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);
21
assert.equal(g.score, 4, '3 rockets + 1 carried');
22
assert.equal(g.blockAt(1, 1), null);
23
assert.ok(!g.blocks.includes(carried) && carried.y >= g.height || g.score === 4);
24
});
25
26
it('a cluster that does not clear the field lands, sits for 3 seconds, then takes random colors', () =>
27
{
28
const g = makeGame({
29
noSpawn: true,
30
planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },
31
seed: 99
32
});
33
placeRow(g, 0, [3, 4, 5], 2);
34
g.resolveMatches();
35
// Jump height is 13/4 = 3.25, they come back down. Step until they land.
36
let landed = false;
37
for (let i = 0; i < 2000; i++)
38
{
39
g.tick(1 / 120);
40
const grayNow = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
41
if (grayNow.length >= 3 && grayNow.every((b) => b.state === 'settling'))
42
{
43
landed = true;
44
break;
45
}
46
}
47
const gray = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
48
assert.ok(landed, 'should have landed as gray, got ' + gray.length + ' states=' + gray.map((b) => b.state + '@' + b.y).join(',') + '\n' + g.snapshot().picture);
49
for (const b of gray)
50
{
51
assert.equal(b.state, 'settling');
52
assert.ok(b.y < 6, 'landed low, y=' + b.y);
53
assert.ok(b.settleTime < 0.05, 'timer starts at landing, t=' + b.settleTime);
54
}
55
g.step(2.9, 1 / 60);
56
assert.ok(gray.every((b) => b.state === 'settling'), 'still settling just before 3s: ' + gray.map((b) => b.state + '/' + b.settleTime).join(','));
57
g.step(0.2, 1 / 60);
58
for (const b of gray)
59
{
60
assert.equal(b.state, 'still');
61
assert.ok(b.color >= 0 && b.color < 5, 'recolored, color=' + b.color);
62
assert.equal(b.clusterId, null);
63
}
64
});
65
66
it('carried colored blocks land as still blocks and do not wait 3 seconds', () =>
67
{
68
const g = makeGame({
69
noSpawn: true,
70
planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }
71
});
72
placeRow(g, 0, [0, 1, 2], 0);
73
const carried = g.place(1, 1, 4);
74
g.resolveMatches();
75
g.step(4, 1 / 120);
76
assert.equal(carried.color, 4);
77
assert.equal(carried.state, 'still');
78
assert.equal(carried.clusterId, null);
79
});
80
81
it('spawn drops a random color into a random open column every second', () =>
82
{
83
const g = makeGame({ seed: 3 });
84
assert.equal(g.blocks.length, 0);
85
g.step(0.99);
86
assert.equal(g.blocks.length, 0);
87
g.step(0.02);
88
assert.equal(g.blocks.length, 1);
89
assert.equal(g.blocks[0].state, 'falling');
90
assert.ok(g.blocks[0].color >= 0 && g.blocks[0].color < 5);
91
assert.ok(g.blocks[0].col >= 0 && g.blocks[0].col < 10);
92
g.step(1);
93
assert.equal(g.blocks.length, 2);
94
// Let the first one land.
95
g.step(g.planet.fallTime + 0.2);
96
const landed = g.blocks.filter((b) => b.state === 'still');
97
assert.ok(landed.length >= 1);
98
for (const b of landed)
99
{ assert.equal(b.y, 0); }
100
});
101
});
@@ -0,0 +1,101 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, placeRow, GameCore, Planet } = require('./helpers.js');6
7
describe('Points, landing and recolor', () =>8
{9
it('blocks that leave the top of the field become one point each and are gone', () =>10
{11
const g = makeGame({12
noSpawn: true,13
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }14
});15
placeRow(g, 0, [0, 1, 2], 0);16
const carried = g.place(1, 1, 4);17
g.resolveMatches();18
assert.equal(g.score, 0);19
g.step(2, 1 / 120);20
assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);21
assert.equal(g.score, 4, '3 rockets + 1 carried');22
assert.equal(g.blockAt(1, 1), null);23
assert.ok(!g.blocks.includes(carried) && carried.y >= g.height || g.score === 4);24
});25
26
it('a cluster that does not clear the field lands, sits for 3 seconds, then takes random colors', () =>27
{28
const g = makeGame({29
noSpawn: true,30
planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },31
seed: 9932
});33
placeRow(g, 0, [3, 4, 5], 2);34
g.resolveMatches();35
// Jump height is 13/4 = 3.25, they come back down. Step until they land.36
let landed = false;37
for (let i = 0; i < 2000; i++)38
{39
g.tick(1 / 120);40
const grayNow = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);41
if (grayNow.length >= 3 && grayNow.every((b) => b.state === 'settling'))42
{43
landed = true;44
break;45
}46
}47
const gray = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);48
assert.ok(landed, 'should have landed as gray, got ' + gray.length + ' states=' + gray.map((b) => b.state + '@' + b.y).join(',') + '\n' + g.snapshot().picture);49
for (const b of gray)50
{51
assert.equal(b.state, 'settling');52
assert.ok(b.y < 6, 'landed low, y=' + b.y);53
assert.ok(b.settleTime < 0.05, 'timer starts at landing, t=' + b.settleTime);54
}55
g.step(2.9, 1 / 60);56
assert.ok(gray.every((b) => b.state === 'settling'), 'still settling just before 3s: ' + gray.map((b) => b.state + '/' + b.settleTime).join(','));57
g.step(0.2, 1 / 60);58
for (const b of gray)59
{60
assert.equal(b.state, 'still');61
assert.ok(b.color >= 0 && b.color < 5, 'recolored, color=' + b.color);62
assert.equal(b.clusterId, null);63
}64
});65
66
it('carried colored blocks land as still blocks and do not wait 3 seconds', () =>67
{68
const g = makeGame({69
noSpawn: true,70
planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }71
});72
placeRow(g, 0, [0, 1, 2], 0);73
const carried = g.place(1, 1, 4);74
g.resolveMatches();75
g.step(4, 1 / 120);76
assert.equal(carried.color, 4);77
assert.equal(carried.state, 'still');78
assert.equal(carried.clusterId, null);79
});80
81
it('spawn drops a random color into a random open column every second', () =>82
{83
const g = makeGame({ seed: 3 });84
assert.equal(g.blocks.length, 0);85
g.step(0.99);86
assert.equal(g.blocks.length, 0);87
g.step(0.02);88
assert.equal(g.blocks.length, 1);89
assert.equal(g.blocks[0].state, 'falling');90
assert.ok(g.blocks[0].color >= 0 && g.blocks[0].color < 5);91
assert.ok(g.blocks[0].col >= 0 && g.blocks[0].col < 10);92
g.step(1);93
assert.equal(g.blocks.length, 2);94
// Let the first one land.95
g.step(g.planet.fallTime + 0.2);96
const landed = g.blocks.filter((b) => b.state === 'still');97
assert.ok(landed.length >= 1);98
for (const b of landed)99
{ assert.equal(b.y, 0); }100
});101
});@@ -0,0 +1,157 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, placeRow, almost, Planet, GameCore } = require('./helpers.js');
6
7
function flyingClusterAt (game, y, colorsByCol)
8
{
9
// Place a grounded match, launch, then teleport the cluster to y.
10
const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b);
11
// Use a dummy match to create a cluster, then rebuild positions/colors.
12
placeRow(game, 0, cols, 0);
13
game.resolveMatches();
14
const clusterId = game.blocks.find((b) => b.clusterId != null).clusterId;
15
const cluster = game.clusters[clusterId];
16
for (const b of game.clusterMembers(clusterId))
17
{ game.removeBlock(b); }
18
// removeBlock drops an empty cluster; put it back before adding new members.
19
game.clusters[clusterId] = cluster;
20
const members = [];
21
for (const col of cols)
22
{
23
const b = game.createBlock(col, y, colorsByCol[col], 'flying');
24
b.clusterId = clusterId;
25
if (colorsByCol[col] === GameCore.COLOR_GRAY)
26
{
27
b.color = GameCore.COLOR_GRAY;
28
b.flame = true;
29
}
30
members.push(b);
31
}
32
cluster.phase = 'hold';
33
cluster.vy = 0;
34
cluster.decel = 0;
35
return { cluster, members };
36
}
37
38
describe('Push-up and chain re-launch', () =>
39
{
40
it('a manual jump hitting a flying cluster pushes that column one tile up, other columns stay', () =>
41
{
42
const g = makeGame({ noSpawn: true });
43
// Flying gray row at y=6, cols 1,2,3.
44
const { cluster, members } = flyingClusterAt(g, 6, { 1: GameCore.COLOR_GRAY, 2: GameCore.COLOR_GRAY, 3: GameCore.COLOR_GRAY });
45
const pushed = members.find((b) => b.col === 2);
46
const left = members.find((b) => b.col === 1);
47
const right = members.find((b) => b.col === 3);
48
const manual = g.place(2, 2, 4);
49
g.startManualLaunch(manual);
50
// Fast-forward until the push has happened (or we time out).
51
let pushedHappened = false;
52
for (let i = 0; i < 400; i++)
53
{
54
g.tick(1 / 120);
55
if (pushed.y >= 6.99)
56
{
57
pushedHappened = true;
58
break;
59
}
60
}
61
assert.ok(pushedHappened, 'expected the hit column to move up, y=' + pushed.y);
62
assert.ok(pushed.y >= 6.99 && pushed.y < 8.5, 'hit column y=' + pushed.y);
63
assert.ok(Math.abs(left.y - 6) < 0.3, 'left should stay, y=' + left.y);
64
assert.ok(Math.abs(right.y - 6) < 0.3, 'right should stay, y=' + right.y);
65
assert.equal(left.clusterId, cluster.id);
66
assert.equal(pushed.clusterId, cluster.id);
67
assert.equal(right.clusterId, cluster.id);
68
});
69
70
it('a push that completes a new row of 3 turns those gray, links them, and re-launches the whole cluster the same height', () =>
71
{
72
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 4, riseTime: 1 } });
73
// Flying: col 0 and 2 have color 1 at y=6, col 1 has color 1 at y=5 (one tile below).
74
// A gray rocket at col 4 y=6 is the original link (disjoint, so "not always touching").
75
const { cluster } = flyingClusterAt(g, 6, {
76
0: 1,
77
1: 1,
78
2: 1,
79
4: GameCore.COLOR_GRAY
80
});
81
// Move col 1 down one tile so they are not yet a horizontal 3 at the same y.
82
const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id);
83
const left = g.blocks.find((b) => b.col === 0 && b.clusterId === cluster.id);
84
const right = g.blocks.find((b) => b.col === 2 && b.clusterId === cluster.id);
85
const gray = g.blocks.find((b) => b.col === 4 && b.clusterId === cluster.id);
86
mid.y = 5;
87
left.y = 6;
88
right.y = 6;
89
gray.y = 6;
90
const originalHeight = cluster.jumpHeight;
91
// Manual in col 1, will push mid from 5 to 6, forming a row of three 1s.
92
const manual = g.place(1, 1, 3);
93
g.startManualLaunch(manual);
94
let relaunched = false;
95
const midStart = mid.y;
96
for (let i = 0; i < 500; i++)
97
{
98
g.tick(1 / 120);
99
if (mid.color === GameCore.COLOR_GRAY && mid.flame && cluster.phase === 'up' && cluster.vy > 1)
100
{
101
relaunched = true;
102
break;
103
}
104
}
105
assert.ok(relaunched, 'expected a re-launch after the new match, mid.color=' + mid.color + ' flame=' + mid.flame + ' phase=' + cluster.phase + ' vy=' + cluster.vy + ' mid.y=' + mid.y);
106
assert.equal(left.color, GameCore.COLOR_GRAY);
107
assert.equal(right.color, GameCore.COLOR_GRAY);
108
assert.equal(mid.color, GameCore.COLOR_GRAY);
109
assert.equal(mid.clusterId, cluster.id);
110
assert.equal(gray.clusterId, cluster.id);
111
assert.equal(cluster.jumpHeight, originalHeight);
112
// The whole cluster (including the distant gray) shares the new upward velocity.
113
assert.ok(cluster.vy > 0);
114
assert.equal(gray.state, 'flying');
115
});
116
117
it('if a re-launch sends every block off the top, that is fine and they become points', () =>
118
{
119
const g = makeGame({
120
noSpawn: true,
121
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }
122
});
123
const { cluster } = flyingClusterAt(g, 11, {
124
0: 1,
125
1: 1,
126
2: 1
127
});
128
const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id);
129
mid.y = 10;
130
g.blocks.find((b) => b.col === 0).y = 11;
131
g.blocks.find((b) => b.col === 2).y = 11;
132
const manual = g.place(1, 6, 2);
133
g.startManualLaunch(manual);
134
g.step(3, 1 / 120);
135
const flying = g.blocks.filter((b) => b.clusterId === cluster.id);
136
assert.equal(flying.length, 0, 'cluster should have left the field');
137
assert.ok(g.score >= 3, 'score=' + g.score);
138
assert.equal(g.state, 'playing');
139
assert.ok(!g.clusters[cluster.id]);
140
});
141
142
it('gray blocks can also disappear off the top and score points', () =>
143
{
144
const g = makeGame({
145
noSpawn: true,
146
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.4 }
147
});
148
placeRow(g, 11, [0, 1, 2], 2);
149
g.resolveMatches();
150
const rockets = g.blocks.filter((b) => b.col <= 2);
151
assert.ok(rockets.length === 3 && rockets.every((b) => b.color === GameCore.COLOR_GRAY && b.state === 'flying'));
152
g.step(2, 1 / 120);
153
assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 0);
154
assert.equal(g.score, 3);
155
assert.equal(g.blocks.length, 0);
156
});
157
});
@@ -0,0 +1,157 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, placeRow, almost, Planet, GameCore } = require('./helpers.js');6
7
function flyingClusterAt (game, y, colorsByCol)8
{9
// Place a grounded match, launch, then teleport the cluster to y.10
const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b);11
// Use a dummy match to create a cluster, then rebuild positions/colors.12
placeRow(game, 0, cols, 0);13
game.resolveMatches();14
const clusterId = game.blocks.find((b) => b.clusterId != null).clusterId;15
const cluster = game.clusters[clusterId];16
for (const b of game.clusterMembers(clusterId))17
{ game.removeBlock(b); }18
// removeBlock drops an empty cluster; put it back before adding new members.19
game.clusters[clusterId] = cluster;20
const members = [];21
for (const col of cols)22
{23
const b = game.createBlock(col, y, colorsByCol[col], 'flying');24
b.clusterId = clusterId;25
if (colorsByCol[col] === GameCore.COLOR_GRAY)26
{27
b.color = GameCore.COLOR_GRAY;28
b.flame = true;29
}30
members.push(b);31
}32
cluster.phase = 'hold';33
cluster.vy = 0;34
cluster.decel = 0;35
return { cluster, members };36
}37
38
describe('Push-up and chain re-launch', () =>39
{40
it('a manual jump hitting a flying cluster pushes that column one tile up, other columns stay', () =>41
{42
const g = makeGame({ noSpawn: true });43
// Flying gray row at y=6, cols 1,2,3.44
const { cluster, members } = flyingClusterAt(g, 6, { 1: GameCore.COLOR_GRAY, 2: GameCore.COLOR_GRAY, 3: GameCore.COLOR_GRAY });45
const pushed = members.find((b) => b.col === 2);46
const left = members.find((b) => b.col === 1);47
const right = members.find((b) => b.col === 3);48
const manual = g.place(2, 2, 4);49
g.startManualLaunch(manual);50
// Fast-forward until the push has happened (or we time out).51
let pushedHappened = false;52
for (let i = 0; i < 400; i++)53
{54
g.tick(1 / 120);55
if (pushed.y >= 6.99)56
{57
pushedHappened = true;58
break;59
}60
}61
assert.ok(pushedHappened, 'expected the hit column to move up, y=' + pushed.y);62
assert.ok(pushed.y >= 6.99 && pushed.y < 8.5, 'hit column y=' + pushed.y);63
assert.ok(Math.abs(left.y - 6) < 0.3, 'left should stay, y=' + left.y);64
assert.ok(Math.abs(right.y - 6) < 0.3, 'right should stay, y=' + right.y);65
assert.equal(left.clusterId, cluster.id);66
assert.equal(pushed.clusterId, cluster.id);67
assert.equal(right.clusterId, cluster.id);68
});69
70
it('a push that completes a new row of 3 turns those gray, links them, and re-launches the whole cluster the same height', () =>71
{72
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 4, riseTime: 1 } });73
// Flying: col 0 and 2 have color 1 at y=6, col 1 has color 1 at y=5 (one tile below).74
// A gray rocket at col 4 y=6 is the original link (disjoint, so "not always touching").75
const { cluster } = flyingClusterAt(g, 6, {76
0: 1,77
1: 1,78
2: 1,79
4: GameCore.COLOR_GRAY80
});81
// Move col 1 down one tile so they are not yet a horizontal 3 at the same y.82
const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id);83
const left = g.blocks.find((b) => b.col === 0 && b.clusterId === cluster.id);84
const right = g.blocks.find((b) => b.col === 2 && b.clusterId === cluster.id);85
const gray = g.blocks.find((b) => b.col === 4 && b.clusterId === cluster.id);86
mid.y = 5;87
left.y = 6;88
right.y = 6;89
gray.y = 6;90
const originalHeight = cluster.jumpHeight;91
// Manual in col 1, will push mid from 5 to 6, forming a row of three 1s.92
const manual = g.place(1, 1, 3);93
g.startManualLaunch(manual);94
let relaunched = false;95
const midStart = mid.y;96
for (let i = 0; i < 500; i++)97
{98
g.tick(1 / 120);99
if (mid.color === GameCore.COLOR_GRAY && mid.flame && cluster.phase === 'up' && cluster.vy > 1)100
{101
relaunched = true;102
break;103
}104
}105
assert.ok(relaunched, 'expected a re-launch after the new match, mid.color=' + mid.color + ' flame=' + mid.flame + ' phase=' + cluster.phase + ' vy=' + cluster.vy + ' mid.y=' + mid.y);106
assert.equal(left.color, GameCore.COLOR_GRAY);107
assert.equal(right.color, GameCore.COLOR_GRAY);108
assert.equal(mid.color, GameCore.COLOR_GRAY);109
assert.equal(mid.clusterId, cluster.id);110
assert.equal(gray.clusterId, cluster.id);111
assert.equal(cluster.jumpHeight, originalHeight);112
// The whole cluster (including the distant gray) shares the new upward velocity.113
assert.ok(cluster.vy > 0);114
assert.equal(gray.state, 'flying');115
});116
117
it('if a re-launch sends every block off the top, that is fine and they become points', () =>118
{119
const g = makeGame({120
noSpawn: true,121
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 }122
});123
const { cluster } = flyingClusterAt(g, 11, {124
0: 1,125
1: 1,126
2: 1127
});128
const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id);129
mid.y = 10;130
g.blocks.find((b) => b.col === 0).y = 11;131
g.blocks.find((b) => b.col === 2).y = 11;132
const manual = g.place(1, 6, 2);133
g.startManualLaunch(manual);134
g.step(3, 1 / 120);135
const flying = g.blocks.filter((b) => b.clusterId === cluster.id);136
assert.equal(flying.length, 0, 'cluster should have left the field');137
assert.ok(g.score >= 3, 'score=' + g.score);138
assert.equal(g.state, 'playing');139
assert.ok(!g.clusters[cluster.id]);140
});141
142
it('gray blocks can also disappear off the top and score points', () =>143
{144
const g = makeGame({145
noSpawn: true,146
planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.4 }147
});148
placeRow(g, 11, [0, 1, 2], 2);149
g.resolveMatches();150
const rockets = g.blocks.filter((b) => b.col <= 2);151
assert.ok(rockets.length === 3 && rockets.every((b) => b.color === GameCore.COLOR_GRAY && b.state === 'flying'));152
g.step(2, 1 / 120);153
assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 0);154
assert.equal(g.score, 3);155
assert.equal(g.blocks.length, 0);156
});157
});@@ -0,0 +1,119 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7
describe('Vertical swapping', () =>
8
{
9
it('dragging a block through a column swaps it with the blocks it passes', () =>
10
{
11
const g = makeGame({ noSpawn: true });
12
const a = g.place(0, 0, 0);
13
const b = g.place(0, 1, 1);
14
const c = g.place(0, 2, 2);
15
assert.ok(g.beginDrag(0, 0));
16
g.moveDrag(2);
17
g.endDrag();
18
assert.equal(a.y, 2);
19
assert.equal(b.y, 0);
20
assert.equal(c.y, 1);
21
assert.equal(g.blockAt(0, 2).color, 0);
22
assert.equal(g.blockAt(0, 0).color, 1);
23
});
24
25
it('swapTo moves a block stepwise and exchanges neighbors', () =>
26
{
27
const g = makeGame({ noSpawn: true });
28
g.place(1, 0, 4);
29
g.place(1, 1, 3);
30
g.place(1, 2, 2);
31
g.place(1, 3, 1);
32
const moved = g.swapTo(1, 0, 3);
33
assert.equal(moved.color, 4);
34
assert.equal(moved.y, 3);
35
assert.equal(g.blockAt(1, 0).color, 3);
36
assert.equal(g.blockAt(1, 1).color, 2);
37
assert.equal(g.blockAt(1, 2).color, 1);
38
});
39
40
it('refuses to drag in a locked or flying column', () =>
41
{
42
const g = makeGame({ noSpawn: true });
43
fillColumn(g, 0, 13, [0]);
44
g.checkLocks();
45
assert.equal(g.isColumnLocked(0), true);
46
assert.equal(g.beginDrag(0, 0), false);
47
48
const g2 = makeGame({ noSpawn: true });
49
g2.place(2, 0, 1);
50
g2.place(3, 0, 1);
51
g2.place(4, 0, 1);
52
g2.place(2, 1, 2);
53
g2.resolveMatches();
54
assert.equal(g2.isColumnStill(2), false);
55
assert.equal(g2.beginDrag(2, 1), false);
56
// A quiet neighboring column is still draggable.
57
g2.place(7, 0, 3);
58
g2.place(7, 1, 4);
59
assert.equal(g2.isColumnStill(7), true);
60
assert.ok(g2.beginDrag(7, 0));
61
g2.endDrag();
62
});
63
64
it('pulling a top block further up starts a manual launch that cannot leave the field', () =>
65
{
66
const g = makeGame({ noSpawn: true });
67
g.place(0, 0, 1);
68
g.place(0, 1, 2);
69
g.place(0, 2, 3);
70
assert.ok(g.beginDrag(0, 2));
71
g.moveDrag(2 + 1.5);
72
assert.equal(g.drag.launchIntent, true);
73
g.endDrag();
74
const block = g.blocks.find((b) => b.color === 3);
75
assert.equal(block.state, 'manual');
76
assert.equal(block.manualPhase, 'up');
77
assert.ok(block.vy > 0);
78
g.step(g.planet.riseTime, 1 / 240);
79
assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);
80
assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);
81
assert.ok(block.y < g.height, 'must not leave the field');
82
assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);
83
g.step(0.5, 1 / 120);
84
assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);
85
assert.ok(block.y < g.height);
86
});
87
88
it('a manual jump from mid-stack still caps at the top cell', () =>
89
{
90
const g = makeGame({ noSpawn: true });
91
g.place(5, 0, 0);
92
g.place(5, 1, 1);
93
const top = g.place(5, 2, 2);
94
g.startManualLaunch(top);
95
g.step(4, 1 / 120);
96
assert.ok(top.y <= g.height - 1 + 1e-9);
97
assert.ok(top.y < g.height);
98
// After coming back down it lands on the remaining stack (y=1, since it left).
99
assert.ok(top.state === 'still' || top.state === 'manual');
100
if (top.state === 'still')
101
{ assert.equal(top.y, 2); }
102
});
103
104
it('a click in the lower half of a cell grabs that cell, not the one above', () =>
105
{
106
const g = makeGame({ noSpawn: true });
107
g.place(0, 0, 0);
108
g.place(0, 1, 1);
109
assert.ok(g.beginDrag(0, 0.4));
110
assert.equal(g.blockById(g.drag.blockId).color, 0);
111
g.endDrag();
112
assert.ok(g.beginDrag(0, 0.9));
113
assert.equal(g.blockById(g.drag.blockId).color, 0);
114
g.endDrag();
115
assert.ok(g.beginDrag(0, 1.1));
116
assert.equal(g.blockById(g.drag.blockId).color, 1);
117
g.endDrag();
118
});
119
});
@@ -0,0 +1,119 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');6
7
describe('Vertical swapping', () =>8
{9
it('dragging a block through a column swaps it with the blocks it passes', () =>10
{11
const g = makeGame({ noSpawn: true });12
const a = g.place(0, 0, 0);13
const b = g.place(0, 1, 1);14
const c = g.place(0, 2, 2);15
assert.ok(g.beginDrag(0, 0));16
g.moveDrag(2);17
g.endDrag();18
assert.equal(a.y, 2);19
assert.equal(b.y, 0);20
assert.equal(c.y, 1);21
assert.equal(g.blockAt(0, 2).color, 0);22
assert.equal(g.blockAt(0, 0).color, 1);23
});24
25
it('swapTo moves a block stepwise and exchanges neighbors', () =>26
{27
const g = makeGame({ noSpawn: true });28
g.place(1, 0, 4);29
g.place(1, 1, 3);30
g.place(1, 2, 2);31
g.place(1, 3, 1);32
const moved = g.swapTo(1, 0, 3);33
assert.equal(moved.color, 4);34
assert.equal(moved.y, 3);35
assert.equal(g.blockAt(1, 0).color, 3);36
assert.equal(g.blockAt(1, 1).color, 2);37
assert.equal(g.blockAt(1, 2).color, 1);38
});39
40
it('refuses to drag in a locked or flying column', () =>41
{42
const g = makeGame({ noSpawn: true });43
fillColumn(g, 0, 13, [0]);44
g.checkLocks();45
assert.equal(g.isColumnLocked(0), true);46
assert.equal(g.beginDrag(0, 0), false);47
48
const g2 = makeGame({ noSpawn: true });49
g2.place(2, 0, 1);50
g2.place(3, 0, 1);51
g2.place(4, 0, 1);52
g2.place(2, 1, 2);53
g2.resolveMatches();54
assert.equal(g2.isColumnStill(2), false);55
assert.equal(g2.beginDrag(2, 1), false);56
// A quiet neighboring column is still draggable.57
g2.place(7, 0, 3);58
g2.place(7, 1, 4);59
assert.equal(g2.isColumnStill(7), true);60
assert.ok(g2.beginDrag(7, 0));61
g2.endDrag();62
});63
64
it('pulling a top block further up starts a manual launch that cannot leave the field', () =>65
{66
const g = makeGame({ noSpawn: true });67
g.place(0, 0, 1);68
g.place(0, 1, 2);69
g.place(0, 2, 3);70
assert.ok(g.beginDrag(0, 2));71
g.moveDrag(2 + 1.5);72
assert.equal(g.drag.launchIntent, true);73
g.endDrag();74
const block = g.blocks.find((b) => b.color === 3);75
assert.equal(block.state, 'manual');76
assert.equal(block.manualPhase, 'up');77
assert.ok(block.vy > 0);78
g.step(g.planet.riseTime, 1 / 240);79
assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);80
assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);81
assert.ok(block.y < g.height, 'must not leave the field');82
assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);83
g.step(0.5, 1 / 120);84
assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);85
assert.ok(block.y < g.height);86
});87
88
it('a manual jump from mid-stack still caps at the top cell', () =>89
{90
const g = makeGame({ noSpawn: true });91
g.place(5, 0, 0);92
g.place(5, 1, 1);93
const top = g.place(5, 2, 2);94
g.startManualLaunch(top);95
g.step(4, 1 / 120);96
assert.ok(top.y <= g.height - 1 + 1e-9);97
assert.ok(top.y < g.height);98
// After coming back down it lands on the remaining stack (y=1, since it left).99
assert.ok(top.state === 'still' || top.state === 'manual');100
if (top.state === 'still')101
{ assert.equal(top.y, 2); }102
});103
104
it('a click in the lower half of a cell grabs that cell, not the one above', () =>105
{106
const g = makeGame({ noSpawn: true });107
g.place(0, 0, 0);108
g.place(0, 1, 1);109
assert.ok(g.beginDrag(0, 0.4));110
assert.equal(g.blockById(g.drag.blockId).color, 0);111
g.endDrag();112
assert.ok(g.beginDrag(0, 0.9));113
assert.equal(g.blockById(g.drag.blockId).color, 0);114
g.endDrag();115
assert.ok(g.beginDrag(0, 1.1));116
assert.equal(g.blockById(g.drag.blockId).color, 1);117
g.endDrag();118
});119
});