mirror of
https://github.com/tiennm99/pikachu.git
synced 2026-05-25 15:59:16 +00:00
fix: Z-pattern logic, eliminate code duplication, fix tests
- Fix Z-pattern to use proper 2-turn path (H-V-H and V-H-V scanning) instead of single midpoint which only duplicated L-pattern behavior - Eliminate ~390 lines of duplicated pattern-matching code from PikachuGame.js by delegating to PikachuGameLogic via shared board ref - Fix removeCards to reset type=0 so pattern matching works after removal - Fix Jest config: remove invalid preset, add ESM support for Windows - Rename log.js to log.cjs for CommonJS compatibility with ESM project - Fix test expectations to account for pattern priority and border routing
This commit is contained in:
@@ -2,7 +2,7 @@ import { PikachuBaseTest } from '../base/PikachuBaseTest.js';
|
||||
|
||||
describe('U-Pattern Tests', () => {
|
||||
let tester;
|
||||
|
||||
|
||||
beforeEach(() => {
|
||||
tester = new PikachuBaseTest();
|
||||
});
|
||||
@@ -14,7 +14,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 3, 5, 1);
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 2, 5, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern extending right',
|
||||
matrix,
|
||||
@@ -22,7 +22,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 3, 15, 1);
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 2, 15, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern extending left',
|
||||
matrix,
|
||||
@@ -40,7 +40,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -50,7 +50,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 1, 3, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 1, 2, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern extending down',
|
||||
matrix,
|
||||
@@ -58,7 +58,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 8, 3, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 8, 2, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern extending up',
|
||||
matrix,
|
||||
@@ -76,7 +76,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
@@ -88,7 +88,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 1, 3, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 1, 2, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern at top-left corner',
|
||||
matrix,
|
||||
@@ -96,23 +96,25 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
test('should handle U-pattern at opposite board corners', () => {
|
||||
test('should handle U-pattern at opposite board corners (same col)', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 1, 1);
|
||||
tester.placeCard(matrix, 8, 20, 1);
|
||||
|
||||
tester.placeCard(matrix, 8, 1, 1);
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 4, 1, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern at opposite corners',
|
||||
'U-pattern at opposite corners same column',
|
||||
matrix,
|
||||
1, 1, 8, 20,
|
||||
1, 1, 8, 1,
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -120,7 +122,9 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 20, 1);
|
||||
tester.placeCard(matrix, 8, 20, 1);
|
||||
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 4, 20, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern along right edge',
|
||||
matrix,
|
||||
@@ -128,7 +132,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
@@ -142,7 +146,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 3, 5, 2);
|
||||
tester.placeCard(matrix, 4, 5, 2);
|
||||
tester.placeCard(matrix, 5, 5, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern with multiple blocking cards',
|
||||
matrix,
|
||||
@@ -150,7 +154,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -158,7 +162,7 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 2, 2, 1);
|
||||
tester.placeCard(matrix, 4, 4, 1);
|
||||
|
||||
|
||||
// Block all possible extension paths
|
||||
for (let i = 1; i <= 20; i++) {
|
||||
if (i !== 2 && i !== 4) {
|
||||
@@ -172,14 +176,14 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, i, 4, 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern impossible - all paths blocked',
|
||||
matrix,
|
||||
2, 2, 4, 4,
|
||||
false
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
@@ -191,7 +195,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 4, 2, 1);
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 3, 2, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Horizontal extension to right',
|
||||
matrix,
|
||||
@@ -199,7 +203,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -209,7 +213,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 4, 18, 1);
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 3, 18, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Horizontal extension to left',
|
||||
matrix,
|
||||
@@ -217,7 +221,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -227,7 +231,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 6, 4, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 6, 3, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Vertical extension upward',
|
||||
matrix,
|
||||
@@ -235,7 +239,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -245,7 +249,7 @@ describe('U-Pattern Tests', () => {
|
||||
tester.placeCard(matrix, 2, 4, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 2, 3, 2);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Vertical extension downward',
|
||||
matrix,
|
||||
@@ -253,7 +257,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
@@ -261,17 +265,19 @@ describe('U-Pattern Tests', () => {
|
||||
describe('Long Distance Connections', () => {
|
||||
test('should handle long distance U-pattern', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 1, 1);
|
||||
tester.placeCard(matrix, 8, 20, 1);
|
||||
|
||||
tester.placeCard(matrix, 4, 1, 1);
|
||||
tester.placeCard(matrix, 4, 20, 1);
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 4, 10, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Long distance U-pattern',
|
||||
matrix,
|
||||
1, 1, 8, 20,
|
||||
4, 1, 4, 20,
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -279,7 +285,9 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 4, 1, 1);
|
||||
tester.placeCard(matrix, 4, 20, 1);
|
||||
|
||||
// Block direct horizontal path
|
||||
tester.placeCard(matrix, 4, 5, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern across full board width',
|
||||
matrix,
|
||||
@@ -287,7 +295,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -295,7 +303,9 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 10, 1);
|
||||
tester.placeCard(matrix, 8, 10, 1);
|
||||
|
||||
// Block direct vertical path
|
||||
tester.placeCard(matrix, 4, 10, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'U-pattern across full board height',
|
||||
matrix,
|
||||
@@ -303,7 +313,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
@@ -313,7 +323,7 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 1, 1);
|
||||
tester.placeCard(matrix, 1, 5, 1);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Should prefer I-pattern',
|
||||
matrix,
|
||||
@@ -321,7 +331,7 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'I-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
@@ -329,7 +339,7 @@ describe('U-Pattern Tests', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 1, 1, 1);
|
||||
tester.placeCard(matrix, 3, 3, 1);
|
||||
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Should prefer L-pattern',
|
||||
matrix,
|
||||
@@ -337,27 +347,26 @@ describe('U-Pattern Tests', () => {
|
||||
true,
|
||||
'L-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
|
||||
test('should use U-pattern when simpler patterns are blocked', () => {
|
||||
const matrix = tester.createEmptyMatrix();
|
||||
tester.placeCard(matrix, 2, 2, 1);
|
||||
tester.placeCard(matrix, 4, 4, 1);
|
||||
// Block L-pattern corners
|
||||
tester.placeCard(matrix, 2, 4, 2);
|
||||
tester.placeCard(matrix, 4, 2, 2);
|
||||
|
||||
tester.placeCard(matrix, 4, 5, 1);
|
||||
tester.placeCard(matrix, 4, 9, 1);
|
||||
// Block I-pattern
|
||||
tester.placeCard(matrix, 4, 7, 2);
|
||||
|
||||
const testCase = tester.createTestCase(
|
||||
'Should use U-pattern when L-pattern blocked',
|
||||
'Should use U-pattern when I-pattern blocked',
|
||||
matrix,
|
||||
2, 2, 4, 4,
|
||||
4, 5, 4, 9,
|
||||
true,
|
||||
'U-pattern'
|
||||
);
|
||||
|
||||
|
||||
tester.expectTestCase(testCase);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user