Chess Strategy Trainer

Chess Strategy Trainer

Chess Strategy Trainer

Puzzle

Instructions & Controls

Goal:

Puzzle List

These are the puzzles currently loaded in the trainer. (Editing is not currently enabled).

${cst_escapeHTML(puzzle.description)}

Turn: ${puzzle.turn === 'w' ? 'White' : 'Black'}

Initial Position:

Solution:

${puzzle.solution.join(', ')}

`; // Render the board into the clone const pdfBoard = cst_pdfRenderClone.querySelector('#pdf-board'); if(pdfBoard) { // Temporarily set board state to initial puzzle state const currentBoardState = { ...cst_boardState }; // Backup current state const { board: initialBoard } = cst_parseFEN(puzzle.fen); cst_boardState = initialBoard; // Render into pdfBoard pdfBoard.innerHTML = ''; for (let rank = 8; rank >= 1; rank--) { for (let fileIndex = 0; fileIndex < 8; fileIndex++) { const file = FILES[fileIndex]; const squareId = `${file}${rank}`; const square = document.createElement('div'); square.className = `cst-square ${(fileIndex + rank) % 2 === 0 ? 'dark' : 'light'}`; const piece = cst_boardState[squareId]; if (piece) { const pieceSpan = document.createElement('span'); pieceSpan.textContent = PIECE_UNICODE[piece] || '?'; square.appendChild(pieceSpan); } pdfBoard.appendChild(square); } } // Restore board state cst_boardState = currentBoardState; } } /** * Generates and downloads a PDF of the current puzzle */ async function cst_downloadPDF() { if (cst_currentPuzzleIndex === -1) { alert("Please load a puzzle first."); return; } if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') { console.error("CST Tool Error: jsPDF or html2canvas library not loaded."); alert("Error: PDF libraries failed to load. Please check console."); return; } cst_renderPdfClone(); // Create and populate the clone const { jsPDF } = window.jspdf; try { const canvas = await html2canvas(cst_pdfRenderClone, { scale: 2, useCORS: true, }); const imgData = canvas.toDataURL('image/png'); const imgWidth = canvas.width; const imgHeight = canvas.height; const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const ratio = Math.min((pdfWidth - 80) / imgWidth, (pdfHeight - 80) / imgHeight); const w = imgWidth * ratio; const h = imgHeight * ratio; const x = (pdfWidth - w) / 2; const y = 40; // Top margin pdf.addImage(imgData, 'PNG', x, y, w, h); pdf.save(`Chess_Puzzle_${cst_currentPuzzleIndex + 1}.pdf`); } catch (error) { console.error("CST Tool Error: PDF generation failed.", error); alert("An error occurred while generating the PDF. Please try again."); } } // --- EVENT LISTENERS --- // Tab link clicks cst_tabLinks.forEach((link, index) => { link.addEventListener('click', () => cst_switchTab(index)); }); // Next/Prev button clicks if (cst_prevButton) { cst_prevButton.addEventListener('click', () => { if (cst_currentTab > 0) cst_switchTab(cst_currentTab - 1); }); } if (cst_nextButton) { cst_nextButton.addEventListener('click', () => { if (cst_currentTab < cst_tabLinks.length - 1) cst_switchTab(cst_currentTab + 1); }); } // PDF download if (cst_downloadPdfButton) { cst_downloadPdfButton.addEventListener('click', cst_downloadPDF); } // Dashboard Controls if (cst_nextPuzzleButton) { cst_nextPuzzleButton.addEventListener('click', cst_loadNextPuzzle); } if (cst_showSolutionButton) { cst_showSolutionButton.addEventListener('click', cst_showSolution); } if (cst_resetAttemptButton) { cst_resetAttemptButton.addEventListener('click', () => cst_loadPuzzle(cst_currentPuzzleIndex)); // Reload current } // --- INITIALIZATION --- cst_loadPuzzles(); cst_loadNextPuzzle(); // Load the first puzzle cst_renderConfig(); // Populate config tab on load // Set initial tab state cst_tabPanes.forEach((pane, index) => { pane.classList.toggle('hidden', index !== 0); pane.classList.toggle('cst-active', index === 0); }); });