Skip to content

Commit 902728d

Browse files
committed
Enhance game state management and improve board visuals
- Updated the updateBoardVisuals function in game.js to include king check status for better visual feedback. - Added getters for game state variables in game.js to facilitate easier access in tests and improve encapsulation. - Refactored test setup in game-flow.test.js and game-moves.test.js to utilize new getters for game state, enhancing clarity and maintainability. - Removed unnecessary console logs from pieces.js to clean up the initialization process.
1 parent 1d7fcb5 commit 902728d

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

js/game.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function resetGame() {
8888
renderPieces(boardState);
8989
updateStatusDisplay();
9090
updateMoveHistoryDisplay();
91-
updateBoardVisuals(boardState);
91+
updateBoardVisuals(boardState, findKingInCheck(currentPlayer));
9292
}
9393

9494
/**
@@ -916,6 +916,15 @@ if (typeof module !== 'undefined' && module.exports) {
916916
isStalemate,
917917
canCastle,
918918
generateNotation,
919-
generateNotationWithDisambiguation
919+
generateNotationWithDisambiguation,
920+
// Export state variables for test access
921+
get boardState() { return boardState; },
922+
get currentPlayer() { return currentPlayer; },
923+
get moveHistory() { return moveHistory; },
924+
get isGameOver() { return isGameOver; },
925+
get kingPositions() { return kingPositions; },
926+
get currentStatus() { return currentStatus; },
927+
get enPassantTarget() { return enPassantTarget; },
928+
get positionHistory() { return positionHistory; }
920929
};
921930
}

js/pieces.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,21 @@ function isPseudoLegalMove(piece, startRow, startCol, endRow, endCol, isCapture)
103103
* @returns {Array<Array<Piece|null>>} A 2D array representing the board state.
104104
*/
105105
function getInitialPieces() {
106-
console.log("Initializing pieces...");
107106
const board = Array(8).fill(null).map(() => Array(8).fill(null));
108107

109108
// Place pawns
110109
for (let col = 0; col < 8; col++) {
111110
board[1][col] = new Piece(PAWN, BLACK);
112111
board[6][col] = new Piece(PAWN, WHITE);
113-
console.log(`Created pawns at: [1,${col}] (Black) and [6,${col}] (White)`);
114112
}
115113

116114
// Place other pieces
117115
const backRank = [ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK];
118116
for (let col = 0; col < 8; col++) {
119117
board[0][col] = new Piece(backRank[col], BLACK);
120118
board[7][col] = new Piece(backRank[col], WHITE);
121-
console.log(`Created ${backRank[col]} at: [0,${col}] (Black) and [7,${col}] (White)`);
122119
}
123120

124-
// Log the first row to validate
125-
console.log("First row (black pieces):", board[0].map(p => p ? p.type : 'null'));
126-
console.log("Last row (white pieces):", board[7].map(p => p ? p.type : 'null'));
127-
128121
return board;
129122
}
130123

tests/integration/game-flow.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,48 @@ describe('integration: game flow', () => {
5959
initializeGame = game.initializeGame;
6060
handleBoardClick = game.handleBoardClick;
6161

62+
// Set up game state globals that board.js functions expect
63+
// Use getters to always reference current state
64+
Object.defineProperty(global, 'moveHistory', {
65+
get: () => game.moveHistory,
66+
configurable: true
67+
});
68+
Object.defineProperty(global, 'boardState', {
69+
get: () => game.boardState,
70+
configurable: true
71+
});
72+
Object.defineProperty(global, 'currentPlayer', {
73+
get: () => game.currentPlayer,
74+
configurable: true
75+
});
76+
Object.defineProperty(global, 'isGameOver', {
77+
get: () => game.isGameOver,
78+
configurable: true
79+
});
80+
Object.defineProperty(global, 'kingPositions', {
81+
get: () => game.kingPositions,
82+
configurable: true
83+
});
84+
Object.defineProperty(global, 'currentStatus', {
85+
get: () => game.currentStatus,
86+
configurable: true
87+
});
88+
Object.defineProperty(global, 'enPassantTarget', {
89+
get: () => game.enPassantTarget,
90+
configurable: true
91+
});
92+
Object.defineProperty(global, 'positionHistory', {
93+
get: () => game.positionHistory,
94+
configurable: true
95+
});
96+
6297
// Also attach to window for code that expects window.*
6398
window.initializeGame = initializeGame;
6499
window.handleBoardClick = handleBoardClick;
100+
Object.defineProperty(window, 'moveHistory', {
101+
get: () => game.moveHistory,
102+
configurable: true
103+
});
65104

66105
initializeGame();
67106
});

tests/unit/game-moves.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,49 @@ describe('game core', () => {
5151
handleBoardClick = game.handleBoardClick;
5252
generateLegalMovesForPiece = game.generateLegalMovesForPiece;
5353

54+
// Set up game state globals that board.js functions expect
55+
// Use getters to always reference current state
56+
Object.defineProperty(global, 'moveHistory', {
57+
get: () => game.moveHistory,
58+
configurable: true
59+
});
60+
Object.defineProperty(global, 'boardState', {
61+
get: () => game.boardState,
62+
configurable: true
63+
});
64+
Object.defineProperty(global, 'currentPlayer', {
65+
get: () => game.currentPlayer,
66+
configurable: true
67+
});
68+
Object.defineProperty(global, 'isGameOver', {
69+
get: () => game.isGameOver,
70+
configurable: true
71+
});
72+
Object.defineProperty(global, 'kingPositions', {
73+
get: () => game.kingPositions,
74+
configurable: true
75+
});
76+
Object.defineProperty(global, 'currentStatus', {
77+
get: () => game.currentStatus,
78+
configurable: true
79+
});
80+
Object.defineProperty(global, 'enPassantTarget', {
81+
get: () => game.enPassantTarget,
82+
configurable: true
83+
});
84+
Object.defineProperty(global, 'positionHistory', {
85+
get: () => game.positionHistory,
86+
configurable: true
87+
});
88+
5489
// Also attach to window for code that expects window.*
5590
window.initializeGame = initializeGame;
5691
window.handleBoardClick = handleBoardClick;
5792
window.generateLegalMovesForPiece = generateLegalMovesForPiece;
93+
Object.defineProperty(window, 'moveHistory', {
94+
get: () => game.moveHistory,
95+
configurable: true
96+
});
5897

5998
initializeGame();
6099
});

0 commit comments

Comments
 (0)