diff --git a/app/routes/main_routes.py b/app/routes/main_routes.py index fcca6a5..1d4d84d 100644 --- a/app/routes/main_routes.py +++ b/app/routes/main_routes.py @@ -23,6 +23,52 @@ MAX_BOARD_SIZE = 13 main_bp = Blueprint("main", __name__) +def create_empty_board(board_size): + """Create a square board matrix with no placed pieces.""" + return [[0 for _ in range(board_size)] for _ in range(board_size)] + + +def build_board_from_positions(piece_positions, board_size): + """Convert the legacy one-piece-per-row format into a board matrix.""" + board = create_empty_board(board_size) + + for row, col in enumerate(piece_positions): + if isinstance(col, int) and 0 <= col < board_size: + board[row][col] = 1 + + return board + + +def normalize_board(candidate_board, board_size): + """Normalize stored boards so legacy and matrix formats can both be used.""" + if not isinstance(candidate_board, list) or len(candidate_board) != board_size: + return create_empty_board(board_size) + + if all(isinstance(row, list) and len(row) == board_size for row in candidate_board): + normalized_board = create_empty_board(board_size) + + for row_index, row in enumerate(candidate_board): + for col_index, value in enumerate(row): + normalized_board[row_index][col_index] = 1 if value else 0 + + return normalized_board + + return build_board_from_positions(candidate_board, board_size) + + +def iter_piece_positions(board): + """Yield every occupied square from the normalized board matrix.""" + for row_index, row in enumerate(board): + for col_index, value in enumerate(row): + if value: + yield row_index, col_index + + +def count_placed_pieces(board): + """Count how many pieces are currently placed on the board.""" + return sum(1 for _ in iter_piece_positions(board)) + + def parse_board_size(raw_size, default=DEFAULT_BOARD_SIZE): """Convert a user-supplied board size into a safe integer range.""" try: @@ -119,7 +165,7 @@ def build_queens_trace(board_size): "type": "solution", "row": None, "col": None, - "board": queen_positions[:], + "board": build_board_from_positions(queen_positions, board_size), "message": f"Lösung gefunden. Alle {board_size} Damen stehen ohne Konflikte." }) return True @@ -136,7 +182,7 @@ def build_queens_trace(board_size): "type": "place", "row": row, "col": col, - "board": queen_positions[:], + "board": build_board_from_positions(queen_positions, board_size), "message": f"Dame auf {position} gesetzt." }) @@ -148,7 +194,7 @@ def build_queens_trace(board_size): "type": "remove", "row": row, "col": col, - "board": queen_positions[:], + "board": build_board_from_positions(queen_positions, board_size), "message": f"Backtracking: Dame von {position} entfernt." }) @@ -159,7 +205,7 @@ def build_queens_trace(board_size): return { "mode": "queens", "board_size": board_size, - "initial_board": [-1] * board_size, + "initial_board": create_empty_board(board_size), "steps": steps, "solved": solved } @@ -168,11 +214,11 @@ def build_queens_trace(board_size): def build_rooks_trace(board_size): """Build a simple placement trace for the rook mode.""" rook_positions = solve_rooks(board_size) - board = [-1] * board_size + board = create_empty_board(board_size) steps = [] for row, col in enumerate(rook_positions): - board[row] = col + board[row][col] = 1 position = get_position_label(row, col, board_size) # As with the queens trace, each step includes a full board snapshot for # straightforward frontend rendering. @@ -180,7 +226,7 @@ def build_rooks_trace(board_size): "type": "place", "row": row, "col": col, - "board": board[:], + "board": [board_row[:] for board_row in board], "message": f"Turm auf {position} gesetzt." }) @@ -188,14 +234,14 @@ def build_rooks_trace(board_size): "type": "solution", "row": None, "col": None, - "board": board[:], + "board": [board_row[:] for board_row in board], "message": f"Loesung gefunden. Alle {board_size} Tuerme stehen ohne Konflikte." }) return { "mode": "rooks", "board_size": board_size, - "initial_board": [-1] * board_size, + "initial_board": create_empty_board(board_size), "steps": steps, "solved": True } @@ -203,7 +249,7 @@ def build_rooks_trace(board_size): def serialize_game_state(game_state): """Convert a database model into JSON-friendly data for the frontend.""" - board = json.loads(game_state.board) + board = normalize_board(json.loads(game_state.board), game_state.board_size) return { "id": game_state.id, @@ -213,7 +259,7 @@ def serialize_game_state(game_state): "save_name": game_state.save_name, "save_note": game_state.save_note, "is_favorite": game_state.is_favorite, - "pieces_placed": sum(1 for column in board if column != -1), + "pieces_placed": count_placed_pieces(board), "step_count": game_state.step_count, "elapsed_seconds": game_state.elapsed_seconds, "is_solved": game_state.is_solved, @@ -285,17 +331,17 @@ def check(): row = data["row"] col = data["col"] piece_mode = data.get("mode", "queens") + normalized_board = normalize_board(board, len(board)) - # The board is stored as a list where the index is the row and the value is - # the column of the placed piece. -1 means the row is still empty. - for current_row, current_col in enumerate(board): - if current_col == -1 or current_row == row: + for current_row, current_col in iter_piece_positions(normalized_board): + if current_row == row and current_col == col: continue + same_row = current_row == row same_column = current_col == col same_diagonal = abs(current_col - col) == abs(current_row - row) - if same_column: + if same_row or same_column: return jsonify({"valid": False}) # Diagonal conflicts only matter for queens, not for rooks. @@ -425,7 +471,7 @@ def load_game(): # If the user has never saved a game, return a clean default board. if not latest_game: - return jsonify({"board": [-1] * DEFAULT_BOARD_SIZE, "mode": "queens"}) + return jsonify({"board": create_empty_board(DEFAULT_BOARD_SIZE), "mode": "queens"}) return jsonify(serialize_game_state(latest_game)) diff --git a/app/static/css/dashboard.css b/app/static/css/dashboard.css index 75accb6..9a0050d 100644 --- a/app/static/css/dashboard.css +++ b/app/static/css/dashboard.css @@ -483,9 +483,17 @@ textarea { line-height: 1.55; } -.board { +.board-stage { + display: grid; + gap: 14px; + justify-items: center; + margin-top: 10px; +} + +.board-shell { overflow-x: auto; padding-bottom: 8px; + width: 100%; } .board-grid { @@ -544,30 +552,30 @@ textarea { background: #d65555 !important; } -.solver-panel { - margin-top: 18px; - padding: 18px; - border-radius: 12px; - border: 1px solid rgba(31, 49, 64, 0.1); - background: rgba(255, 250, 242, 0.74); - text-align: left; -} - -.solver-panel-header { - display: flex; - justify-content: space-between; - align-items: flex-start; - gap: 16px; -} - -.solver-panel h3 { - margin-bottom: 8px; -} - .solver-message { margin: 0; } +.board-navigator { + display: flex; + align-items: center; + justify-content: center; + flex-wrap: wrap; + gap: 10px; + width: min(100%, 620px); +} + +.board-nav-button { + min-width: 150px; +} + +.solver-message-inline { + margin: 0 auto; + max-width: 760px; + text-align: center; + font-size: 15px; +} + .solver-step-info { margin: 0; padding: 8px 12px; @@ -579,63 +587,31 @@ textarea { white-space: nowrap; } -.solver-groups { - display: grid; - grid-template-columns: minmax(0, 1.35fr) minmax(260px, 0.65fr); - gap: 16px; - margin-top: 18px; -} - -.solver-group { - padding: 18px; - border: 1px solid rgba(31, 49, 64, 0.1); - background: rgba(255, 255, 255, 0.58); -} - -.solver-group h4 { - margin: 0 0 8px; - color: #182029; - font-size: 1.15rem; -} - -.solver-group-text { - margin: 0; - font-size: 15px; -} - -.solver-controls { - display: grid; - grid-template-columns: repeat(3, minmax(0, 1fr)); - gap: 10px; - margin-top: 16px; -} - -.instant-solver-actions { - margin-top: 16px; -} - -.instant-solver-actions .btn { - width: 100%; -} - .solver-speed { display: grid; - grid-template-columns: auto 1fr auto; - align-items: center; + grid-template-columns: minmax(0, 1fr) auto; + grid-template-areas: + "label label" + "input value"; + align-items: end; gap: 12px; margin-top: 16px; } .solver-speed input[type="range"] { + grid-area: input; width: 100%; + min-width: 0; } .solver-speed-label { + grid-area: label; font-weight: 700; color: var(--navy); } .solver-speed-value { + grid-area: value; min-width: 62px; text-align: right; color: var(--muted); @@ -650,19 +626,6 @@ textarea { gap: 10px; } -.controls-save-row { - display: grid; - grid-template-columns: repeat(2, minmax(0, 1fr)); - gap: 10px; - width: min(420px, 100%); -} - -.settings-sidebar-actions { - margin-top: 18px; - display: grid; - gap: 10px; -} - .section-header, .history-header, .sidebar-header, @@ -786,6 +749,34 @@ textarea { width: min(400px, 94vw); } +.settings-section { + margin-top: 22px; + padding-top: 22px; + border-top: 1px solid rgba(31, 49, 64, 0.1); +} + +.settings-section:first-of-type { + margin-top: 0; + padding-top: 0; + border-top: 0; +} + +.settings-section h4 { + margin: 0; + color: #182029; + font-size: 1.15rem; +} + +.settings-section .section-kicker { + margin-bottom: 10px; +} + +.settings-action-grid { + display: grid; + gap: 10px; + margin-top: 16px; +} + .sidebar-label { display: block; margin-top: 18px; @@ -980,14 +971,6 @@ textarea { } @media (max-width: 980px) { - .solver-groups { - grid-template-columns: 1fr; - } - - .solver-controls { - grid-template-columns: 1fr; - } - .game-card-header, .history-header, .section-header { @@ -1040,7 +1023,7 @@ textarea { padding-right: 8px; } - .board { + .board-shell { margin-left: -4px; margin-right: -4px; padding-bottom: 4px; @@ -1061,6 +1044,10 @@ textarea { grid-template-columns: 1fr; } + .board-navigator { + width: 100%; + } + .mode-switch .btn { min-width: min(100%, 280px); } @@ -1070,14 +1057,11 @@ textarea { } .controls>.btn, + .board-nav-button, .save-point-actions .btn { width: 100%; } - .controls-save-row { - width: 100%; - } - .sidebar { width: min(100%, calc(100vw - 24px)); } diff --git a/app/static/js/dashboard.js b/app/static/js/dashboard.js index 60aaa41..efd97f2 100644 --- a/app/static/js/dashboard.js +++ b/app/static/js/dashboard.js @@ -1,9 +1,14 @@ +// Board configuration const DEFAULT_BOARD_SIZE = 8; const MIN_BOARD_SIZE = 4; const MAX_BOARD_SIZE = 13; -const EMPTY_CELL = -1; +const EMPTY_CELL = 0; +const OCCUPIED_CELL = 1; const GAME_STORAGE_KEY = "eight-queens-dashboard-session-v1"; +const AppLanguage = window.AppLanguage; + +// Mutable game and solver state let mode = "queens"; let boardSize = DEFAULT_BOARD_SIZE; let pendingBoardSize = DEFAULT_BOARD_SIZE; @@ -21,7 +26,9 @@ let solverSpeedMs = 320; let solverLoading = false; let solverRequestId = 0; let currentSolverHighlight = null; +let solverInitialBoard = createEmptyBoard(boardSize); +// Cached DOM references const boardDiv = document.getElementById("board"); const statusMessage = document.getElementById("statusMessage"); const queenList = document.getElementById("queenList"); @@ -46,8 +53,10 @@ const btnQueens = document.getElementById("btnQueens"); const btnRooks = document.getElementById("btnRooks"); const btnPrepareSolve = document.getElementById("btnPrepareSolve"); const btnInstantSolve = document.getElementById("btnInstantSolve"); +const btnPrevStep = document.getElementById("btnPrevStep"); const btnNextStep = document.getElementById("btnNextStep"); const btnPlayPause = document.getElementById("btnPlayPause"); +const boardNavigator = document.getElementById("boardNavigator"); const solverMessage = document.getElementById("solverMessage"); const solverStepInfo = document.getElementById("solverStepInfo"); const solverSpeedInput = document.getElementById("solverSpeedInput"); @@ -78,7 +87,9 @@ const timeHistoryTableBody = document.getElementById("timeHistoryTableBody"); const infoModal = document.getElementById("infoModal"); const infoModalBackdrop = document.getElementById("infoModalBackdrop"); -const SUPPORTED_LANGUAGES = ["de", "en"]; +// ----------------------------------------------------------------------------- +// Translation dictionaries and language-specific copy + let currentLanguage = "de"; const MODE_COPY = { @@ -88,22 +99,22 @@ const MODE_COPY = { plural: "Damen", eyebrow: "Damen-Modus", gameTitle: "Damen-Spielbrett", - boardText: "Platziere Damen so, dass keine zwei dieselbe Spalte oder Diagonale teilen.", - infoTitle: "Spielregeln und Acht-Damen-Problem", - infoRuleText: "Im Damen-Modus dürfen sich Damen weder in derselben Spalte noch diagonal angreifen. Mit einem Klick setzt oder entfernst du eine Figur.", - infoProblemHeading: "Was ist das Acht-Damen-Problem?", - infoProblemText: "Das klassische Acht-Damen-Problem fragt nach einer Anordnung von acht Damen auf einem 8x8-Brett, bei der sich keine zwei Damen schlagen. Es ist ein bekanntes Beispiel für kombinatorische Suche und Backtracking." + boardText: "Platziere Damen frei auf dem Brett. Konflikte entstehen in derselben Reihe, Spalte oder Diagonale.", + infoTitle: "Spielanleitung", + infoRuleText: "In diesem Modus spielst du mit Damen. Eine Dame ist die Figur mit dem Symbol ♛. Sie darf keine andere Dame in derselben waagerechten Reihe, senkrechten Spalte oder schrägen Diagonale treffen. Mit einem Klick setzt du eine Dame auf ein freies Feld oder entfernst sie wieder.", + infoProblemHeading: "Was ist eine Dame?", + infoProblemText: "Eine Dame ist eine Schachfigur. In diesem Spiel musst du keine Schachregeln lernen, sondern nur wissen: Eine Dame kann gerade und schräg über das Brett wirken. Darum müssen Damen genug Abstand haben, damit sie sich nicht gegenseitig erreichen." }, rooks: { singular: "Turm", plural: "Türme", eyebrow: "Türme-Modus", gameTitle: "Türme-Spielbrett", - boardText: "Platziere Türme so, dass keine zwei dieselbe Spalte teilen.", - infoTitle: "Spielregeln und Acht-Türme-Problem", - infoRuleText: "Im Türme-Modus dürfen sich Türme nicht in derselben Spalte stehen. Diagonalen spielen hier keine Rolle. Mit einem Klick setzt oder entfernst du eine Figur.", - infoProblemHeading: "Was ist das Acht-Türme-Problem?", - infoProblemText: "Beim Acht-Türme-Problem suchst du eine Anordnung von acht Türmen auf einem 8x8-Brett, bei der sich keine zwei Türme angreifen. Es ist eine einfachere Variante, um systematische Suche und Konfliktprüfung sichtbar zu machen." + boardText: "Platziere Türme frei auf dem Brett. Konflikte entstehen in derselben Reihe oder Spalte.", + infoTitle: "Spielanleitung", + infoRuleText: "In diesem Modus spielst du mit Türmen. Ein Turm ist die Figur mit dem Symbol ♜. Er darf keinen anderen Turm in derselben waagerechten Reihe oder senkrechten Spalte treffen. Schräge Diagonalen sind hier egal. Mit einem Klick setzt du einen Turm auf ein freies Feld oder entfernst ihn wieder.", + infoProblemHeading: "Was ist ein Turm?", + infoProblemText: "Ein Turm ist eine Schachfigur. Für dieses Spiel reicht eine einfache Regel: Ein Turm wirkt nur gerade nach oben, unten, links und rechts. Darum dürfen zwei Türme nicht in derselben Reihe oder Spalte stehen." } }, en: { @@ -112,22 +123,22 @@ const MODE_COPY = { plural: "Queens", eyebrow: "Queens Mode", gameTitle: "Queens Board", - boardText: "Place queens so that no two share the same column or diagonal.", - infoTitle: "Rules and the Eight Queens Puzzle", - infoRuleText: "In queens mode, queens may not attack each other in the same column or on diagonals. Click once to place or remove a piece.", - infoProblemHeading: "What is the Eight Queens puzzle?", - infoProblemText: "The classic Eight Queens puzzle asks for eight queens on an 8x8 board so that no two queens attack each other. It is a well-known example of combinatorial search and backtracking." + boardText: "Place queens freely on the board. Conflicts happen in the same row, column, or diagonal.", + infoTitle: "How To Play", + infoRuleText: "In this mode you play with queens. A queen is the piece shown with the symbol ♛. It must not reach another queen in the same horizontal row, vertical column, or diagonal line. Click once to place a queen on an empty square or remove it again.", + infoProblemHeading: "What is a queen?", + infoProblemText: "A queen is a chess piece. You do not need full chess knowledge here. You only need to know that a queen can affect the board in straight and diagonal lines. That is why queens need enough distance so they cannot reach each other." }, rooks: { singular: "Rook", plural: "Rooks", eyebrow: "Rooks Mode", gameTitle: "Rooks Board", - boardText: "Place rooks so that no two share the same column.", - infoTitle: "Rules and the Eight Rooks Puzzle", - infoRuleText: "In rooks mode, rooks may not stand in the same column. Diagonals do not matter here. Click once to place or remove a piece.", - infoProblemHeading: "What is the Eight Rooks puzzle?", - infoProblemText: "In the Eight Rooks puzzle, you arrange eight rooks on an 8x8 board so that no two rooks attack each other. It is a simpler variant that helps visualize systematic search and conflict checking." + boardText: "Place rooks freely on the board. Conflicts happen in the same row or column.", + infoTitle: "How To Play", + infoRuleText: "In this mode you play with rooks. A rook is the piece shown with the symbol ♜. It must not reach another rook in the same horizontal row or vertical column. Diagonal lines do not matter here. Click once to place a rook on an empty square or remove it again.", + infoProblemHeading: "What is a rook?", + infoProblemText: "A rook is a chess piece. For this game you only need one simple idea: a rook affects the board only in straight lines up, down, left, and right. That is why two rooks must not stand in the same row or column." } } }; @@ -142,11 +153,11 @@ const translations = { accountShortcutAria: "Konto und Login-Einstellungen öffnen", introKicker: "Puzzle-Modus", introTitle: "Wähl deinen Modus und löse das Brett auf deine Art.", - introText: "Wechsle zwischen Damen und Türmen, nutze Animation oder Sofort-Lösung und öffne die Brett-Einstellungen jederzeit ueber den linken Seiten-Tab.", + introText: "Wechsle zwischen Damen und Türmen, nutze Animation oder Sofort-Lösung und öffne die Spiel-Einstellungen jederzeit ueber den linken Seiten-Tab.", modeSwitchAria: "Spielmodus", btnQueens: "Damen", btnRooks: "Türme", - infoButton: "Regeln & Hilfe", + infoButton: "Spielanleitung", gameStatsAria: "Spielstatus", stepsLabel: "Schritte", timeLabel: "Zeit", @@ -160,26 +171,35 @@ const translations = { animationSolverText: "Bereite den Lösungsweg vor, gehe ihn manuell Schritt für Schritt durch oder spiele die Animation automatisch ab.", instantSolverTitle: "Sofort-Lösung", instantSolverText: "Lade direkt eine gültige Lösung, ohne Zwischenschritte oder Animation.", - defaultSolverMessage: "Bereite einen Ablauf vor und spiele ihn Schritt für Schritt ab.", + defaultSolverMessage: "Lade eine Animation und steuere sie am Brett oder spiele sie automatisch ab.", solverReady: ({ modeLabel }) => `Solver für ${modeLabel} bereit. ${t("defaultSolverMessage")}`, solverSpeedLabel: "Animationsgeschwindigkeit", - btnPrepareSolve: "Animation vorbereiten", - btnPrepareSolveLoading: "Animation wird vorbereitet...", - btnNextStep: "Nächster Schritt", + btnPrepareSolve: "Animation laden", + btnPrepareSolveLoading: "Animation wird geladen...", + btnPrevStep: "Zurück", + btnNextStep: "Vorwärts", btnPlayPause: "Animation abspielen", btnPlayReplay: "Animation erneut abspielen", btnPlayPauseRunning: "Animation pausieren", btnInstantSolve: "Sofort lösen", btnReset: "Brett zurücksetzen", - settingsTrigger: "Brett-Einstellungen", - settingsSidebarTitle: "Brett-Einstellungen", + settingsTrigger: "Spiel-Einstellungen", + settingsSidebarTitle: "Spiel-Einstellungen", settingsSidebarClose: "Schliessen", + boardSettingsKicker: "Brett", + boardSettingsTitle: "Brettgrösse", settingsSidebarText: "Passe die Brettgroesse zwischen 4 und 13 an.", boardSizeInputLabel: "Grösse", applyBoardSizeButton: "Grösse anwenden", settingsSidebarHint: "Beim ändern wird das Brett zur Sicherheit zurückgesetzt. Auf dem Handy ist 8x8 eingeplant, grössere Bretter können abgeschnitten wirken.", + saveSettingsKicker: "Speichern", + saveSettingsTitle: "Fortschritt speichern", + saveSettingsText: "Öffne hier die Optionen zum Speichern oder Laden deiner Save-Points.", + solutionSettingsKicker: "Lösung", + solutionSettingsTitle: "Lösungswerkzeuge", + solutionSettingsText: "Lade eine Animation, steuere sie am Brett mit Vorwärts und Zurück oder spiele sie automatisch ab.", btnSave: "Speichern", - btnLoad: "Save-Points", + btnLoad: "Laden", saveSidebarTitle: "Speicherpunkt", saveSidebarClose: "Schliessen", saveSidebarText: "Gib dem Spielstand einen klaren Namen und optional eine kurze Notiz.", @@ -214,8 +234,8 @@ const translations = { historyHeadStatus: "Status", historyHeadSaved: "Gespeichert", infoModalClose: "Schliessen", - infoModalIntro: "Setze pro Reihe genau eine Figur auf das Brett. Eine gültige Lösung hat keine Konflikte.", - infoModalModeHeading: "So funktioniert der aktuelle Modus", + infoModalIntro: "Das Schachbrett besteht aus vielen quadratischen Feldern. Jedes Feld kann leer sein oder genau eine Figur enthalten. Deine Aufgabe ist es, Figuren auf das Brett zu setzen, ohne dass sie sich gegenseitig angreifen. Das Puzzle ist erst gelöst, wenn genau so viele Figuren auf dem Brett stehen wie das Brett Felder pro Seite hat, also zum Beispiel 8 Figuren auf einem 8x8-Brett.", + infoModalModeHeading: "Grundregeln", infoModalTimeHeading: "Was zeigen Zeit und Schritte?", infoModalTimeText: "Der Schrittzähler erfasst deine Züge auf dem Brett. Die Zeit läuft für das aktuelle Spiel weiter, auch wenn du die Seite neu lädst. Beim Speichern werden Zeit und Schritte zusammen mit dem Save-Point abgelegt.", pieceListTitle: ({ modeLabel }) => `Positionen der ${modeLabel}`, @@ -243,15 +263,16 @@ const translations = { solverDiscardedManual: "Solver-Ablauf verworfen, weil das Brett manuell geändert wurde.", solverDiscardedStatus: "Solver-Ablauf verworfen. Bereite ihn bei Bedarf neu vor.", solvedPerfect: ({ duration }) => `Perfekt gelöst in ${duration}.`, - animationReset: "Animations-Ablauf zurückgesetzt. Starte die Animation erneut oder gehe weiter mit Nächster Schritt.", - animationPreparing: ({ modeLabel, size }) => `Berechne den Animations-Ablauf für ${modeLabel.toLowerCase()} auf ${size} x ${size}...`, + animationReset: "Animations-Ablauf zurückgesetzt. Nutze die Schritt-Buttons am Brett oder starte die Animation erneut.", + animationAtStart: "Anfang des Animations-Ablaufs erreicht.", + animationPreparing: ({ modeLabel, size }) => `Lade den Animations-Ablauf für ${modeLabel.toLowerCase()} auf ${size} x ${size}...`, animationPreparingStatus: ({ size }) => `Animations-Solver berechnet die Schritte fuer ${size} x ${size}.`, noSolution: "Keine Lösung gefunden.", noSolutionStatus: "Keine Lösung fuer dieses Brett gefunden.", animationReady: ({ steps, queensMode }) => queensMode - ? `Animations-Ablauf bereit mit ${steps} Schritten. Nutze Nächster Schritt oder Animation abspielen, um das Backtracking zu verfolgen.` - : `Animations-Ablauf bereit mit ${steps} Schritten. Nutze Nächster Schritt oder Animation abspielen.`, - animationReadyStatus: "Animations-Solver bereit. Nutze Nächster Schritt oder Animation abspielen.", + ? `Animations-Ablauf bereit mit ${steps} Schritten. Nutze die Vor- und Zurück-Buttons am Brett oder spiele die Animation ab, um das Backtracking zu verfolgen.` + : `Animations-Ablauf bereit mit ${steps} Schritten. Nutze die Vor- und Zurück-Buttons am Brett oder spiele die Animation ab.`, + animationReadyStatus: "Animations-Solver bereit. Nutze die Schritt-Buttons am Brett oder spiele die Animation ab.", animationLoadError: "Der Solver-Ablauf konnte nicht geladen werden.", animationFinishedStatus: ({ size }) => `Loesung für ${size} x ${size} Schritt fuer Schritt aufgebaut.`, animationAlreadyFinished: "Der Animations-Ablauf ist bereits abgeschlossen.", @@ -293,11 +314,11 @@ const translations = { accountShortcutAria: "Open account and login settings", introKicker: "Puzzle Mode", introTitle: "Choose your mode and solve the board your way.", - introText: "Switch between queens and rooks, use animation or instant solve, and open board settings anytime from the left side tab.", + introText: "Switch between queens and rooks, use animation or instant solve, and open game settings anytime from the left side tab.", modeSwitchAria: "Game mode", btnQueens: "Queens", btnRooks: "Rooks", - infoButton: "Rules & Help", + infoButton: "How to play?", gameStatsAria: "Game status", stepsLabel: "Steps", timeLabel: "Time", @@ -311,24 +332,33 @@ const translations = { animationSolverText: "Prepare the solving path, step through it manually, or play the animation automatically.", instantSolverTitle: "Instant Solve", instantSolverText: "Load a valid solution directly, without intermediate steps or animation.", - defaultSolverMessage: "Prepare a solving trace and play it back step by step.", + defaultSolverMessage: "Load an animation and control it from the board or play it automatically.", solverReady: ({ modeLabel }) => `Solver for ${modeLabel} is ready. ${t("defaultSolverMessage")}`, solverSpeedLabel: "Animation speed", - btnPrepareSolve: "Prepare animation", - btnPrepareSolveLoading: "Preparing animation...", - btnNextStep: "Next step", + btnPrepareSolve: "Load animation", + btnPrepareSolveLoading: "Loading animation...", + btnPrevStep: "Back", + btnNextStep: "Forward", btnPlayPause: "Play animation", btnPlayReplay: "Replay animation", btnPlayPauseRunning: "Pause animation", btnInstantSolve: "Solve instantly", btnReset: "Reset board", - settingsTrigger: "Board settings", - settingsSidebarTitle: "Board Settings", + settingsTrigger: "Game settings", + settingsSidebarTitle: "Game settings", settingsSidebarClose: "Close", + boardSettingsKicker: "Board", + boardSettingsTitle: "Board size", settingsSidebarText: "Adjust the board size between 4 and 13.", boardSizeInputLabel: "Size", applyBoardSizeButton: "Apply size", settingsSidebarHint: "Changing the size resets the board for safety. On phones, 8x8 is the intended maximum for a reliable fit and larger boards may appear cropped.", + saveSettingsKicker: "Saving", + saveSettingsTitle: "Save progress", + saveSettingsText: "Open the options for saving your progress or loading save points here.", + solutionSettingsKicker: "Solution", + solutionSettingsTitle: "Solving tools", + solutionSettingsText: "Load an animation, control it from the board with back and forward, or play it automatically.", btnSave: "Save", btnLoad: "Save points", saveSidebarTitle: "Save Point", @@ -365,8 +395,8 @@ const translations = { historyHeadStatus: "Status", historyHeadSaved: "Saved", infoModalClose: "Close", - infoModalIntro: "Place exactly one piece in each row. A valid solution has no conflicts.", - infoModalModeHeading: "How the current mode works", + infoModalIntro: "The chessboard is made of many square fields. Each field can either stay empty or hold exactly one piece. Your task is to place pieces on the board without letting them attack each other. The puzzle is only solved when the board contains exactly as many pieces as it has squares on one side, for example 8 pieces on an 8x8 board.", + infoModalModeHeading: "Basic rules", infoModalTimeHeading: "What do time and steps show?", infoModalTimeText: "The step counter records your moves on the board. Time keeps running for the current game even if you reload the page. When saving, time and steps are stored together with the save point.", pieceListTitle: ({ modeLabel }) => `${modeLabel} positions`, @@ -394,15 +424,16 @@ const translations = { solverDiscardedManual: "Solver trace discarded because the board was changed manually.", solverDiscardedStatus: "Solver trace discarded. Prepare it again if needed.", solvedPerfect: ({ duration }) => `Perfectly solved in ${duration}.`, - animationReset: "Animation trace reset. Start the animation again or continue with Next step.", - animationPreparing: ({ modeLabel, size }) => `Preparing the animation trace for ${modeLabel.toLowerCase()} on ${size} x ${size}...`, + animationReset: "Animation trace reset. Use the step buttons on the board or start the animation again.", + animationAtStart: "Reached the start of the animation trace.", + animationPreparing: ({ modeLabel, size }) => `Loading the animation trace for ${modeLabel.toLowerCase()} on ${size} x ${size}...`, animationPreparingStatus: ({ size }) => `Animation solver is preparing the steps for ${size} x ${size}.`, noSolution: "No solution found.", noSolutionStatus: "No solution found for this board.", animationReady: ({ steps, queensMode }) => queensMode - ? `Animation trace ready with ${steps} steps. Use Next step or Play animation to follow the backtracking process.` - : `Animation trace ready with ${steps} steps. Use Next step or Play animation.`, - animationReadyStatus: "Animation solver ready. Use Next step or Play animation.", + ? `Animation trace ready with ${steps} steps. Use the back and forward buttons on the board or play the animation to follow the backtracking process.` + : `Animation trace ready with ${steps} steps. Use the back and forward buttons on the board or play the animation.`, + animationReadyStatus: "Animation solver ready. Use the step buttons on the board or play the animation.", animationLoadError: "The solver trace could not be loaded.", animationFinishedStatus: ({ size }) => `Solution for ${size} x ${size} was built step by step.`, animationAlreadyFinished: "The animation trace is already complete.", @@ -437,42 +468,8 @@ const translations = { } }; -function createEmptyBoard(size) { - return Array(size).fill(EMPTY_CELL); -} - -function clampBoardSize(size) { - return Math.min(MAX_BOARD_SIZE, Math.max(MIN_BOARD_SIZE, Number(size))); -} - -function getColumnLabel(index) { - return String.fromCharCode(65 + index); -} - -function normalizeLanguage(language) { - const shortLanguage = String(language || "").trim().slice(0, 2).toLowerCase(); - return SUPPORTED_LANGUAGES.includes(shortLanguage) ? shortLanguage : "de"; -} - -function getStoredLanguage() { - try { - return window.localStorage.getItem("preferredLanguage"); - } catch (error) { - return null; - } -} - -function storeLanguage(language) { - try { - window.localStorage.setItem("preferredLanguage", language); - } catch (error) { - return; - } -} - -function detectBrowserLanguage() { - return normalizeLanguage(navigator.language || navigator.userLanguage || "de"); -} +// ----------------------------------------------------------------------------- +// Translation helpers function t(key, params = {}) { const value = translations[currentLanguage][key]; @@ -483,87 +480,151 @@ function getDefaultSolverMessage() { return t("defaultSolverMessage"); } -function applyTextTranslations() { - document.documentElement.lang = currentLanguage; - document.body.dataset.language = currentLanguage; - - document.querySelectorAll("[data-i18n]").forEach((element) => { - const key = element.dataset.i18n; - const translatedText = t(key); - - if (translatedText === undefined) { - return; - } - - if (element.tagName === "TITLE" || String(translatedText).includes(" { - const translatedText = t(element.dataset.i18nPlaceholder); - - if (translatedText === undefined) { - return; - } - - element.setAttribute("placeholder", translatedText); - }); - - document.querySelectorAll("[data-i18n-aria-label]").forEach((element) => { - const translatedText = t(element.dataset.i18nAriaLabel); - - if (translatedText === undefined) { - return; - } - - element.setAttribute("aria-label", translatedText); - }); -} - -function setLanguage(language) { - currentLanguage = normalizeLanguage(language); - applyTextTranslations(); - - langSwitch.textContent = currentLanguage.toUpperCase(); - langSwitch.setAttribute("aria-label", t("languageSwitchAria")); - langSwitch.title = currentLanguage === "de" ? "Switch to English" : "Zu Deutsch wechseln"; - - updateSettingsTrigger(); - updateModeContent(); - updateQueenList(); - updateProgressDisplay(); - updateSolverSpeed(solverSpeedMs); - updateSolverControls(); - - if (!solverLoading && !hasPreparedSolverTrace()) { - setSolverMessage(t("solverReady", { modeLabel: getModeLabel(mode) })); - } - - storeLanguage(currentLanguage); -} - function getModeCopy(selectedMode = mode, language = currentLanguage) { return MODE_COPY[language][selectedMode === "rooks" ? "rooks" : "queens"]; } +function getPieceLabel(selectedMode) { + return getModeCopy(selectedMode).singular; +} + +function getModeLabel(selectedMode) { + return getModeCopy(selectedMode).plural; +} + +function applyTextTranslations() { + AppLanguage.applyTranslations({ + translate: (key) => t(key), + allowHtml: (element, translatedText) => element.tagName === "TITLE" || String(translatedText).includes(" Array(size).fill(EMPTY_CELL)); +} + +function clampBoardSize(size) { + return Math.min(MAX_BOARD_SIZE, Math.max(MIN_BOARD_SIZE, Number(size))); +} + +function getColumnLabel(index) { + return String.fromCharCode(65 + index); +} + +function cloneBoard(selectedBoard) { + return selectedBoard.map((row) => row.slice()); +} + +function getCellKey(row, col) { + return `${row}:${col}`; +} + function sanitizeBoard(candidateBoard, size) { if (!Array.isArray(candidateBoard) || candidateBoard.length !== size) { return createEmptyBoard(size); } - return candidateBoard.map((value) => { + if (candidateBoard.every((row) => Array.isArray(row) && row.length === size)) { + return candidateBoard.map((row) => row.map((value) => { + if (value === true) { + return OCCUPIED_CELL; + } + + const numericValue = Number(value); + return numericValue === OCCUPIED_CELL ? OCCUPIED_CELL : EMPTY_CELL; + })); + } + + const normalizedBoard = createEmptyBoard(size); + + candidateBoard.forEach((value, row) => { const numericValue = Number(value); - if (!Number.isInteger(numericValue)) { - return EMPTY_CELL; + if (Number.isInteger(numericValue) && numericValue >= 0 && numericValue < size) { + normalizedBoard[row][numericValue] = OCCUPIED_CELL; } - - return numericValue >= 0 && numericValue < size ? numericValue : EMPTY_CELL; }); + + return normalizedBoard; +} + +function getOccupiedPositions(selectedBoard = board) { + const positions = []; + + for (let row = 0; row < selectedBoard.length; row++) { + for (let col = 0; col < selectedBoard[row].length; col++) { + if (selectedBoard[row][col] === OCCUPIED_CELL) { + positions.push({ row, col }); + } + } + } + + return positions; +} + +function extractSolverBoard(payload, size) { + if (!Array.isArray(payload)) { + return createEmptyBoard(size); + } + + if (payload.length === size) { + return sanitizeBoard(payload, size); + } + + if (payload.length > 0) { + return sanitizeBoard(payload[0], size); + } + + return createEmptyBoard(size); } function formatDuration(totalSeconds) { @@ -585,6 +646,7 @@ function getElapsedMilliseconds() { return elapsedBeforeTimerStartMs + Math.max(0, Date.now() - timerStartedAtMs); } +// Keep a minimal local snapshot so a refresh can restore the current run. function persistCurrentSession() { try { window.localStorage.setItem( @@ -603,6 +665,9 @@ function persistCurrentSession() { } } +// ----------------------------------------------------------------------------- +// Timer and UI synchronization + function updateProgressDisplay() { stepCounterValue.textContent = String(stepCount); timerValue.textContent = formatDuration(Math.floor(getElapsedMilliseconds() / 1000)); @@ -661,6 +726,8 @@ function ensureFreshRunAfterSolvedBoard() { resetProgress(); } +// These helpers keep the static labels and the dynamic dashboard widgets aligned +// with the currently selected mode, language, and board size. function updateModeButtons() { btnQueens.classList.toggle("active", mode === "queens"); btnRooks.classList.toggle("active", mode === "rooks"); @@ -719,6 +786,9 @@ function setSolverMessage(message = getDefaultSolverMessage()) { solverMessage.textContent = message; } +// ----------------------------------------------------------------------------- +// Rendering and solver state helpers + function updateSolverSpeed(rawValue) { solverSpeedMs = Number(rawValue); solverSpeedInput.value = String(solverSpeedMs); @@ -730,6 +800,8 @@ function updateSolverSpeed(rawValue) { } } +// These render helpers are reused by manual play, solver playback, +// save-point loading, and session restore. function updateBoardLayout() { let cellSize = Math.max(32, Math.min(60, Math.floor(480 / boardSize))); let labelSize = cellSize; @@ -746,6 +818,29 @@ function updateBoardLayout() { boardDiv.style.setProperty("--label-size", `${labelSize}px`); } +function updateQueenList() { + queenList.innerHTML = ""; + const positions = getOccupiedPositions(); + + positions.forEach(({ row, col }, index) => { + const position = `${getColumnLabel(col)}${boardSize - row}`; + const li = document.createElement("li"); + + li.textContent = `${getPieceLabel(mode)} ${index + 1}: ${position}`; + queenList.appendChild(li); + }); + + const placedPieces = positions.length; + pieceCountBadge.textContent = t("pieceCount", { count: placedPieces }); + + if (placedPieces === 0) { + const emptyItem = document.createElement("li"); + emptyItem.className = "empty"; + emptyItem.textContent = t("pieceEmpty", { modeLabel: getModeLabel(mode) }); + queenList.appendChild(emptyItem); + } +} + function isSolverPlaying() { return solverTimerId !== null; } @@ -771,11 +866,14 @@ function stopSolverPlayback(refreshControls = true) { function updateSolverControls() { const currentStep = Math.max(0, solverStepIndex + 1); + const hasTrace = hasPreparedSolverTrace(); btnPrepareSolve.disabled = solverLoading; btnInstantSolve.disabled = solverLoading; - btnNextStep.disabled = solverLoading || hasCompletedSolverTrace(); + btnPrevStep.disabled = solverLoading || !hasTrace || solverStepIndex < 0; + btnNextStep.disabled = solverLoading || !hasTrace || hasCompletedSolverTrace(); btnPlayPause.disabled = solverLoading; + boardNavigator.hidden = !hasTrace; btnPrepareSolve.textContent = solverLoading ? t("btnPrepareSolveLoading") : t("btnPrepareSolve"); @@ -801,21 +899,45 @@ function discardSolverTrace(message = getDefaultSolverMessage()) { solverSteps = []; solverStepIndex = -1; currentSolverHighlight = null; + solverInitialBoard = createEmptyBoard(boardSize); setSolverMessage(message); updateSolverControls(); } +function syncProgressAfterBoardStateChange() { + if (isBoardSolved()) { + finishSolvedRun(); + return; + } + + if (isSolved) { + resetProgress({ + stepCountValue: stepCount, + elapsedMs: getElapsedMilliseconds(), + solved: false + }); + return; + } + + updateProgressDisplay(); + persistCurrentSession(); +} + function rewindSolverTrace() { stopSolverPlayback(false); solverStepIndex = -1; currentSolverHighlight = null; - board = createEmptyBoard(boardSize); + board = sanitizeBoard(solverInitialBoard, boardSize); setSolverMessage(t("animationReset")); drawBoard(); + syncProgressAfterBoardStateChange(); updateSolverControls(); } +// ----------------------------------------------------------------------------- +// Board rules, rendering, and player interaction + function setMode(newMode) { if (mode === newMode) { return; @@ -831,34 +953,25 @@ function setMode(newMode) { }); } -function closeAllPanels() { - toggleSidebar(false); - toggleSavePanel(false); - toggleSavePointsPanel(false); - toggleInfoModal(false); -} - function getConflicts(selectedBoard = board, selectedMode = mode) { - const conflicts = []; + const conflicts = new Set(); + const positions = getOccupiedPositions(selectedBoard); - for (let rowA = 0; rowA < boardSize; rowA++) { - if (selectedBoard[rowA] === EMPTY_CELL) { - continue; - } + for (let indexA = 0; indexA < positions.length; indexA++) { + const positionA = positions[indexA]; - for (let rowB = rowA + 1; rowB < boardSize; rowB++) { - if (selectedBoard[rowB] === EMPTY_CELL) { - continue; - } - - const colA = selectedBoard[rowA]; - const colB = selectedBoard[rowB]; - const sameColumn = colA === colB; - const sameDiagonal = Math.abs(colA - colB) === Math.abs(rowA - rowB); - const hasConflict = selectedMode === "rooks" ? sameColumn : sameColumn || sameDiagonal; + for (let indexB = indexA + 1; indexB < positions.length; indexB++) { + const positionB = positions[indexB]; + const sameRow = positionA.row === positionB.row; + const sameColumn = positionA.col === positionB.col; + const sameDiagonal = Math.abs(positionA.col - positionB.col) === Math.abs(positionA.row - positionB.row); + const hasConflict = selectedMode === "rooks" + ? sameRow || sameColumn + : sameRow || sameColumn || sameDiagonal; if (hasConflict) { - conflicts.push([rowA, colA], [rowB, colB]); + conflicts.add(getCellKey(positionA.row, positionA.col)); + conflicts.add(getCellKey(positionB.row, positionB.col)); } } } @@ -867,8 +980,7 @@ function getConflicts(selectedBoard = board, selectedMode = mode) { } function isBoardSolved(selectedBoard = board, selectedMode = mode) { - const allPlaced = selectedBoard.every((col) => col !== EMPTY_CELL); - return allPlaced && getConflicts(selectedBoard, selectedMode).length === 0; + return getPlacedPieceCount(selectedBoard) === boardSize && getConflicts(selectedBoard, selectedMode).size === 0; } function finishSolvedRun() { @@ -883,6 +995,8 @@ function finishSolvedRun() { persistCurrentSession(); } +// The board is always rendered from state so manual moves, restored sessions, +// and solver playback all share the same visual path. function drawBoard() { boardDiv.innerHTML = ""; boardDiv.className = "board-grid"; @@ -916,10 +1030,10 @@ function drawBoard() { ); } - if (board[row] === col) { + if (board[row][col] === OCCUPIED_CELL) { cell.textContent = mode === "queens" ? "♛" : "♜"; - if (conflicts.some(([conflictRow, conflictCol]) => conflictRow === row && conflictCol === col)) { + if (conflicts.has(getCellKey(row, col))) { cell.classList.add("invalid"); } } @@ -949,7 +1063,7 @@ function placeQueen(row, col) { } currentSolverHighlight = null; - board[row] = board[row] === col ? EMPTY_CELL : col; + board[row][col] = board[row][col] === OCCUPIED_CELL ? EMPTY_CELL : OCCUPIED_CELL; stepCount += 1; drawBoard(); updateProgressDisplay(); @@ -962,6 +1076,30 @@ function placeQueen(row, col) { } } +function resetBoard(options = {}) { + const { + statusMessageText = "", + statusColor = "", + solverMessageText = t("solverReady", { modeLabel: getModeLabel(mode) }) + } = options; + + discardSolverTrace(solverMessageText); + board = createEmptyBoard(boardSize); + currentSolverHighlight = null; + resetProgress(); + + if (statusMessageText) { + setStatus(statusMessageText, statusColor); + } else { + setStatus(); + } + + drawBoard(); +} + +// ----------------------------------------------------------------------------- +// Panels and board settings + function previewBoardSize(size) { pendingBoardSize = clampBoardSize(size); sidebarBoardSizeValue.textContent = pendingBoardSize; @@ -1028,12 +1166,8 @@ function toggleInfoModal(forceOpen) { infoModal.setAttribute("aria-hidden", String(!shouldOpen)); } -function getPieceLabel(selectedMode) { - return getModeCopy(selectedMode).singular; -} - -function getPlacedPieceCount() { - return board.filter((col) => col !== EMPTY_CELL).length; +function getPlacedPieceCount(selectedBoard = board) { + return getOccupiedPositions(selectedBoard).length; } function resetSaveForm() { @@ -1051,17 +1185,25 @@ function openSavePanel() { toggleSavePanel(true); } +function closeAllPanels() { + toggleSidebar(false); + toggleSavePanel(false); + toggleSavePointsPanel(false); + toggleInfoModal(false); +} + +// ----------------------------------------------------------------------------- +// Save point and history helpers + function getBoardPreview(savedBoard, limit = 4) { + const boardSizeForPreview = Array.isArray(savedBoard) ? savedBoard.length : DEFAULT_BOARD_SIZE; + const normalizedBoard = sanitizeBoard(savedBoard, boardSizeForPreview); const preview = []; - for (let row = 0; row < savedBoard.length; row++) { - if (savedBoard[row] === EMPTY_CELL) { - continue; - } + for (const { row, col } of getOccupiedPositions(normalizedBoard)) { + preview.push(`${getColumnLabel(col)}${boardSizeForPreview - row}`); - preview.push(`${getColumnLabel(savedBoard[row])}${savedBoard.length - row}`); - - if (preview.length === limit) { + if (preview.length >= limit) { break; } } @@ -1098,10 +1240,6 @@ function createSavePointActionButton(label, className, onClick) { return button; } -function getModeLabel(selectedMode) { - return getModeCopy(selectedMode).plural; -} - function renderSavePoints(savePoints) { savePointsList.innerHTML = ""; @@ -1211,6 +1349,8 @@ async function openSavePointsPanel() { toggleSavePointsPanel(true); } +// Server-loaded save points and locally restored sessions both flow through +// this helper so the dashboard only has one "hydrate state into UI" path. function applyLoadedGame(data) { discardSolverTrace(t("solverReady", { modeLabel: getModeLabel(data.mode || "queens") })); @@ -1235,6 +1375,9 @@ function applyLoadedGame(data) { persistCurrentSession(); } +// ----------------------------------------------------------------------------- +// Solver execution + function applySolverStep(step) { board = sanitizeBoard(step.board, boardSize); @@ -1259,7 +1402,7 @@ function applySolverStep(step) { setSolverMessage(step.message || getDefaultSolverMessage()); drawBoard(); - persistCurrentSession(); + syncProgressAfterBoardStateChange(); updateSolverControls(); } @@ -1320,10 +1463,13 @@ function startSolverPlayback(stepImmediately = true) { scheduleNextSolverPlaybackStep(); } +// requestId prevents older async responses from overwriting a newer solver run +// when the user changes size, mode, or solver action quickly. async function prepareSolutionTrace() { const endpoint = mode === "rooks" ? "/solve_rooks_trace" : "/solve_trace"; const requestId = ++solverRequestId; + toggleSidebar(false); solverLoading = true; stopSolverPlayback(false); solverSteps = []; @@ -1348,20 +1494,24 @@ async function prepareSolutionTrace() { solverSteps = Array.isArray(trace.steps) ? trace.steps : []; solverStepIndex = -1; - board = Array.isArray(trace.initial_board) ? trace.initial_board.slice() : createEmptyBoard(boardSize); + solverInitialBoard = Array.isArray(trace.initial_board) + ? sanitizeBoard(trace.initial_board, boardSize) + : createEmptyBoard(boardSize); + board = cloneBoard(solverInitialBoard); currentSolverHighlight = null; if (!trace.solved || solverSteps.length === 0) { setSolverMessage(t("noSolution")); setStatus(t("noSolutionStatus"), "red"); drawBoard(); - persistCurrentSession(); + syncProgressAfterBoardStateChange(); return false; } setSolverMessage(t("animationReady", { steps: solverSteps.length, queensMode: mode === "queens" })); setStatus(t("animationReadyStatus"), "#2563eb"); drawBoard(); + syncProgressAfterBoardStateChange(); return true; } catch (error) { if (requestId !== solverRequestId) { @@ -1385,11 +1535,7 @@ async function nextSolverStep() { } if (!hasPreparedSolverTrace()) { - const prepared = await prepareSolutionTrace(); - - if (!prepared) { - return; - } + return; } if (isSolverPlaying()) { @@ -1401,6 +1547,35 @@ async function nextSolverStep() { } } +function previousSolverStep() { + if (solverLoading || !hasPreparedSolverTrace()) { + return; + } + + if (isSolverPlaying()) { + stopSolverPlayback(false); + } + + if (solverStepIndex < 0) { + setStatus(t("animationAtStart"), "#2563eb"); + return; + } + + solverStepIndex -= 1; + + if (solverStepIndex >= 0) { + applySolverStep(solverSteps[solverStepIndex]); + return; + } + + currentSolverHighlight = null; + board = sanitizeBoard(solverInitialBoard, boardSize); + setSolverMessage(t("animationAtStart")); + drawBoard(); + syncProgressAfterBoardStateChange(); + updateSolverControls(); +} + async function toggleSolverPlayback() { if (solverLoading) { return; @@ -1440,11 +1615,13 @@ async function instantSolve() { const endpoint = mode === "rooks" ? "/solve_rooks" : "/solve"; const requestId = ++solverRequestId; + toggleSidebar(false); solverLoading = true; stopSolverPlayback(false); solverSteps = []; solverStepIndex = -1; currentSolverHighlight = null; + solverInitialBoard = createEmptyBoard(boardSize); setSolverMessage(t("instantLoading", { modeLabel: getModeLabel(mode), size: boardSize })); setStatus(t("instantLoadingStatus"), "#2563eb"); updateSolverControls(); @@ -1462,7 +1639,7 @@ async function instantSolve() { return; } - board = mode === "queens" && Array.isArray(solution[0]) ? solution[0].slice() : solution.slice(); + board = extractSolverBoard(solution, boardSize); currentSolverHighlight = null; finishSolvedRun(); setSolverMessage(t("instantLoaded")); @@ -1484,55 +1661,8 @@ async function instantSolve() { } } -function resetBoard(options = {}) { - const { - statusMessageText = "", - statusColor = "", - solverMessageText = t("solverReady", { modeLabel: getModeLabel(mode) }) - } = options; - - discardSolverTrace(solverMessageText); - board = createEmptyBoard(boardSize); - currentSolverHighlight = null; - resetProgress(); - - if (statusMessageText) { - setStatus(statusMessageText, statusColor); - } else { - setStatus(); - } - - drawBoard(); -} - -function updateQueenList() { - queenList.innerHTML = ""; - let count = 1; - - for (let row = 0; row < boardSize; row++) { - if (board[row] === EMPTY_CELL) { - continue; - } - - const col = board[row]; - const position = `${getColumnLabel(col)}${boardSize - row}`; - const li = document.createElement("li"); - - li.textContent = `${getPieceLabel(mode)} ${count}: ${position}`; - queenList.appendChild(li); - count += 1; - } - - const placedPieces = count - 1; - pieceCountBadge.textContent = t("pieceCount", { count: placedPieces }); - - if (placedPieces === 0) { - const emptyItem = document.createElement("li"); - emptyItem.className = "empty"; - emptyItem.textContent = t("pieceEmpty", { modeLabel: getModeLabel(mode) }); - queenList.appendChild(emptyItem); - } -} +// ----------------------------------------------------------------------------- +// Save point persistence actions async function saveGame() { const response = await fetch("/save", { @@ -1623,6 +1753,9 @@ async function deleteSavePoint(savePointId) { setStatus(t("deleteSuccess"), "#2563eb"); } +// ----------------------------------------------------------------------------- +// Persistence and startup + function restoreSessionFromStorage() { try { const rawSession = window.localStorage.getItem(GAME_STORAGE_KEY); @@ -1657,11 +1790,12 @@ function restoreSessionFromStorage() { } } +// Boot order: translate UI, restore any local session, then load history data. async function initializeDashboard() { pendingBoardSize = boardSize; updateModeButtons(); updateBoardSizeDisplays(boardSize); - setLanguage(getStoredLanguage() || detectBrowserLanguage()); + languageController.init(); updateSettingsTrigger(false); setSolverMessage(t("solverReady", { modeLabel: getModeLabel(mode) })); @@ -1674,18 +1808,6 @@ async function initializeDashboard() { await refreshTimeHistory(); } -if (langSwitch) { - langSwitch.addEventListener("click", async () => { - setLanguage(currentLanguage === "de" ? "en" : "de"); - resetSaveForm(); - await refreshTimeHistory(); - - if (savePointsSidebar.classList.contains("open")) { - await refreshSavePoints(); - } - }); -} - window.addEventListener("keydown", (event) => { if (event.key === "Escape") { closeAllPanels(); diff --git a/app/static/js/index.js b/app/static/js/index.js index c2e7b1f..4288f7e 100644 --- a/app/static/js/index.js +++ b/app/static/js/index.js @@ -1,61 +1,65 @@ +// settings for preview board const HERO_BOARD_SIZE = 8; const heroBoard = document.getElementById("heroBoard"); + +//translation for English and German +const AppLanguage = window.AppLanguage; const langSwitch = document.getElementById("langSwitch"); -const supportedLanguages = ["de", "en"]; const initialLanguage = window.LANDING_INITIAL_LANGUAGE || "de"; +let currentLanguage = initialLanguage; const translations = { de: { "meta.title": "8-Damen- und 8-Türme Solver", "nav.brand": "8-Damen- und 8-Türme Solver", "nav.problem": "Problem", - "nav.features": "Features", + "nav.features": "Funktionen", "nav.journey": "Ablauf", - "nav.login": "Login", - "nav.signup": "Create Account", + "nav.login": "Anmelden", + "nav.signup": "Account erstellen", "hero.eyebrow": "Interaktives Lernen mit Strategie, Logik und Backtracking", "hero.title": "Entdecke das Acht-Damen- und Acht-Türme-Problem.", - "hero.text": "Diese App macht aus einem klassischen Informatik- und Mathematikproblem ein interaktives Erlebnis: Setze Damen oder Türme, verfolge den Solver Schritt fuer Schritt, miss deine Zeit und speichere clevere Zwischenstände.", + "hero.text": "Diese App macht aus einem klassischen Informatik- und Mathematikproblem ein interaktives Erlebnis: Setze Damen oder Türme, verfolge den Solver Schritt für Schritt, miss deine Zeit und speichere clevere Zwischenstände.", "hero.primaryCta": "Jetzt starten", - "hero.secondaryCta": "Bereits Account? Login", + "hero.secondaryCta": "Bereits ein Account? Anmelden", "hero.fact1Title": "Skalierbar", "hero.fact1Text": "Brettgrösse flexibel anpassen", "hero.fact2Title": "Schritt für Schritt", "hero.fact2Text": "Solver mit Animation", - "hero.fact3Title": "Save-Points", + "hero.fact3Title": "Speicherstände", "hero.fact3Text": "Zeit und Fortschritt behalten", - "hero.solverLabel": "Solver", - "hero.solverValue": "Backtracking live", - "hero.challengeLabel": "Challenge", + "hero.solverLabel": "Lösungshelfer", + "hero.solverValue": "Backtracking in Echtzeit", + "hero.challengeLabel": "Herausforderung", "hero.challengeValue": "Zeit und Schritte messen", - "problem.eyebrow": "Warum dieses Problem spannend ist", - "problem.title": "Die Acht-Damen- und Acht-Türme-Probleme sind mehr als Schachrätsel.", - "problem.text": "Beim Acht-Damen-Problem suchst du eine Anordnung von acht Damen auf einem Schachbrett, sodass sich keine zwei Damen schlagen. Beim Acht-Tuerme-Problem gilt dieselbe Idee für Türme, nur mit einfacheren Konfliktregeln. Beide Aufgaben zeigen, wie Mustererkennung, systematische Suche und Backtracking funktionieren.

Gerade beim Damenproblem wird schnell sichtbar, warum Strategie wichtig ist: Es gibt unzählige moegliche Platzierungen, aber nur sehr wenige wirklich gültige Lösungen. Genau das macht diese App spannend, weil man nicht nur das Ergebnis sieht, sondern auch den Weg dorthin.", + "problem.eyebrow": "Was ist das Acht-Damen-Problem?", + "problem.title": "Eine klassische schachmathematische Aufgabe mit überraschend vielen Lösungen.", + "problem.text": "Das Acht-Damen-Problem ist eine schachmathematische Aufgabe: Acht Damen sollen auf einem Schachbrett so aufgestellt werden, dass keine zwei Damen dieselbe Reihe, Spalte oder Diagonale teilen. Das Problem lässt sich auf beliebige Brettgrössen verallgemeinern, indem n Damen auf einem n x n-Brett platziert werden.

Für das klassische 8x8-Brett gibt es 92 Lösungen. Zählt man Stellungen, die nur durch Spiegelung oder Drehung entstehen, als gleich, bleiben 12 eindeutige Lösungen übrig.

Quelle: de-academic.com", "features.eyebrow": "Was die Applikation bietet", "features.title": "Von der Idee bis zur gelösten Stellung.", "features.card1Title": "Interaktives Brett", "features.card1Text": "Setze Damen oder Türme direkt auf dem Brett und prüfe, ob deine Anordnung konfliktfrei ist.", "features.card2Title": "Solver mit Animation", - "features.card2Text": "Beobachte den Lösungsweg Schritt fuer Schritt und verstehe, wie Backtracking arbeitet.", + "features.card2Text": "Beobachte den Lösungsweg Schritt für Schritt und verstehe, wie Backtracking arbeitet.", "features.card3Title": "Zeit und Schritte", "features.card3Text": "Miss deinen Fortschritt während des Spiels und vergleiche unterschiedliche Versuche.", - "features.card4Title": "Save-Points", - "features.card4Text": "Speichere Zwischenstände mit Notizen, Zeiten und Schrittzahl fuer späteres Weiterarbeiten.", + "features.card4Title": "Speicherstände", + "features.card4Text": "Speichere Zwischenstände mit Notizen, Zeiten und Schrittzahl für späteres Weiterarbeiten.", "features.card5Title": "Skalierbare Bretter", "features.card5Text": "Passe die Brettgrösse an und erkunde die Probleme nicht nur auf dem klassischen 8x8-Brett.", "journey.eyebrow": "So funktioniert dein Einstieg", "journey.title": "In wenigen Schritten vom ersten Besuch zur eigenen Lösung.", "journey.step1Title": "Account erstellen", - "journey.step1Text": "Registriere dich und erhalte Zugang zu Dashboard, Save-Points und deinem Fortschritt.", + "journey.step1Text": "Registriere dich und erhalte Zugang zum Dashboard, zu Speicherständen und zu deinem Fortschritt.", "journey.step2Title": "Spielmodus wählen", "journey.step2Text": "Arbeite mit Damen oder Türmen und passe die Brettgrösse deinem Ziel an.", "journey.step3Title": "Lösen oder analysieren", - "journey.step3Text": "Teste eigene Ideen oder lass dir die Lösung visuell vom Solver lösen lassen, dabei wird 1:1 gezeigt wie der Solver vorgeht.", + "journey.step3Text": "Teste eigene Ideen oder lass dir die Lösung visuell vom Solver zeigen, damit du den Lösungsweg besser nachvollziehen kannst.", "cta.eyebrow": "Bereit für den ersten Versuch?", "cta.title": "Mach aus einem bekannten Problem ein sichtbares Lernerlebnis.", "cta.text": "Starte mit einem Account, speichere deine Zwischenstände und finde heraus, wie schnell du eine konfliktfreie Lösung erreichst.", - "cta.primaryCta": "Create Account", - "cta.secondaryCta": "Login", + "cta.primaryCta": "Account erstellen", + "cta.secondaryCta": "Anmelden", "footerText": "Schulprojekt rund um das Acht-Damen- und Acht-Türme-Problem.", "footerPrivacy": "Datenschutzerklärung", "footerImprint": "Impressum", @@ -67,13 +71,13 @@ const translations = { "nav.problem": "Problem", "nav.features": "Features", "nav.journey": "Journey", - "nav.login": "Login", + "nav.login": "Sign in", "nav.signup": "Create Account", "hero.eyebrow": "Interactive learning with strategy, logic, and backtracking", "hero.title": "Discover the Eight Queens and Eight Rooks puzzle.", "hero.text": "This app turns a classic computer science and mathematics problem into an interactive experience: place queens or rooks, follow the solver step by step, track your time, and save clever checkpoints.", "hero.primaryCta": "Get Started", - "hero.secondaryCta": "Already have an account? Login", + "hero.secondaryCta": "Already have an account? Sign in", "hero.fact1Title": "Scalable", "hero.fact1Text": "Adjust the board size freely", "hero.fact2Title": "Step by step", @@ -84,9 +88,9 @@ const translations = { "hero.solverValue": "Backtracking in motion", "hero.challengeLabel": "Challenge", "hero.challengeValue": "Measure time and steps", - "problem.eyebrow": "Why this puzzle matters", - "problem.title": "The Eight Queens and Eight Rooks problems are more than chess puzzles.", - "problem.text": "In the Eight Queens problem, you search for a way to place eight queens on a chessboard so that no two queens can attack each other. The Eight Rooks problem follows the same idea for rooks, but with simpler conflict rules. Both tasks show how pattern recognition, systematic search, and backtracking work.

The queens puzzle quickly makes it obvious why strategy matters: there are countless possible placements, but only a few truly valid solutions. That is what makes this app interesting, because you do not just see the result, you also see the path to it.", + "problem.eyebrow": "What is the Eight Queens problem?", + "problem.title": "A classic chess-related math puzzle with surprisingly many solutions.", + "problem.text": "The Eight Queens problem is a chess-related mathematical puzzle: eight queens must be placed on a chessboard so that no two queens share the same row, column, or diagonal. The idea can be generalized to boards of any size by placing n queens on an n x n board.

On the classic 8x8 board there are 92 solutions. If positions that only differ by reflection or rotation are counted as the same, 12 unique solutions remain.

Source: de-academic.com", "features.eyebrow": "What the application offers", "features.title": "From the first idea to a solved position.", "features.card1Title": "Interactive board", @@ -111,7 +115,7 @@ const translations = { "cta.title": "Turn a famous puzzle into a visible learning experience.", "cta.text": "Start with an account, save your checkpoints, and find out how quickly you can reach a conflict-free solution.", "cta.primaryCta": "Create Account", - "cta.secondaryCta": "Login", + "cta.secondaryCta": "Sign in", "footerText": "School project about the Eight Queens and Eight Rooks problem.", "footerPrivacy": "Privacy Policy", "footerImprint": "Imprint", @@ -119,6 +123,43 @@ const translations = { } }; +function t(key) { + return translations[currentLanguage]?.[key] ?? null; +} + +function applyTranslations(language) { + currentLanguage = language; + + AppLanguage.applyTranslations({ + translate: (key) => t(key), + allowHtml: (element, translatedText) => element.tagName === "TITLE" || String(translatedText).includes(" { - const key = element.dataset.i18n; - const translatedText = dictionary[key]; - - if (!translatedText) { - return; - } - - if (element.tagName === "TITLE" || translatedText.includes(" { - const key = element.dataset.i18nAriaLabel; - const translatedText = dictionary[key]; - - if (!translatedText) { - return; - } - - element.setAttribute("aria-label", translatedText); - }); - - if (langSwitch) { - langSwitch.textContent = resolvedLanguage.toUpperCase(); - langSwitch.setAttribute("aria-label", resolvedLanguage === "de" ? "Switch language to English" : "Sprache auf Deutsch wechseln"); - langSwitch.title = resolvedLanguage === "de" ? "Switch to English" : "Zu Deutsch wechseln"; - } - - storeLanguage(resolvedLanguage); -} - -function getPreferredLanguage() { - return normalizeLanguage(getStoredLanguage() || detectBrowserLanguage() || initialLanguage); -} - function createHeroBoard() { if (!heroBoard) { return; @@ -241,16 +208,8 @@ function rotatePattern() { applyPattern(queenPatterns[currentPatternIndex]); } -if (langSwitch) { - langSwitch.addEventListener("click", () => { - const nextLanguage = document.documentElement.lang === "de" ? "en" : "de"; - setLanguage(nextLanguage); - }); -} - createHeroBoard(); applyPattern(queenPatterns[currentPatternIndex]); -setLanguage(getPreferredLanguage()); if (heroBoard) { window.setInterval(rotatePattern, 2200); diff --git a/app/static/js/language.js b/app/static/js/language.js new file mode 100644 index 0000000..843b0e7 --- /dev/null +++ b/app/static/js/language.js @@ -0,0 +1,261 @@ +// handels the language translation + +(function attachAppLanguage(window, document) { + const DEFAULT_LANGUAGE = "de"; + const DEFAULT_STORAGE_KEY = "preferredLanguage"; + const DEFAULT_SUPPORTED_LANGUAGES = ["de", "en"]; + + function resolveSupportedLanguages(supportedLanguages = DEFAULT_SUPPORTED_LANGUAGES) { + const normalizedLanguages = Array.isArray(supportedLanguages) + ? supportedLanguages + .map((language) => String(language || "").trim().slice(0, 2).toLowerCase()) + .filter(Boolean) + : []; + + return normalizedLanguages.length > 0 ? normalizedLanguages : DEFAULT_SUPPORTED_LANGUAGES.slice(); + } + + function normalizeLanguage( + language, + { + supportedLanguages = DEFAULT_SUPPORTED_LANGUAGES, + defaultLanguage = DEFAULT_LANGUAGE + } = {} + ) { + const normalizedSupportedLanguages = resolveSupportedLanguages(supportedLanguages); + const normalizedDefaultLanguage = normalizedSupportedLanguages.includes(defaultLanguage) + ? defaultLanguage + : normalizedSupportedLanguages[0] || DEFAULT_LANGUAGE; + const shortLanguage = String(language || "").trim().slice(0, 2).toLowerCase(); + + return normalizedSupportedLanguages.includes(shortLanguage) ? shortLanguage : normalizedDefaultLanguage; + } + + function getStoredLanguage({ storageKey = DEFAULT_STORAGE_KEY } = {}) { + try { + return window.localStorage.getItem(storageKey); + } catch (error) { + return null; + } + } + + function storeLanguage(language, { storageKey = DEFAULT_STORAGE_KEY } = {}) { + try { + window.localStorage.setItem(storageKey, language); + } catch (error) { + return; + } + } + + function detectBrowserLanguage(options = {}) { + return normalizeLanguage(navigator.language || navigator.userLanguage || options.defaultLanguage, options); + } + + function getPreferredLanguage( + { + supportedLanguages = DEFAULT_SUPPORTED_LANGUAGES, + defaultLanguage = DEFAULT_LANGUAGE, + storageKey = DEFAULT_STORAGE_KEY, + fallbackLanguage = defaultLanguage + } = {} + ) { + return normalizeLanguage( + getStoredLanguage({ storageKey }) || detectBrowserLanguage({ supportedLanguages, defaultLanguage }) || fallbackLanguage, + { + supportedLanguages, + defaultLanguage: fallbackLanguage + } + ); + } + + function getNextLanguage(language, supportedLanguages = DEFAULT_SUPPORTED_LANGUAGES) { + const normalizedSupportedLanguages = resolveSupportedLanguages(supportedLanguages); + const currentLanguage = normalizeLanguage(language, { + supportedLanguages: normalizedSupportedLanguages, + defaultLanguage: normalizedSupportedLanguages[0] + }); + const currentIndex = normalizedSupportedLanguages.indexOf(currentLanguage); + + if (currentIndex === -1 || normalizedSupportedLanguages.length < 2) { + return currentLanguage; + } + + return normalizedSupportedLanguages[(currentIndex + 1) % normalizedSupportedLanguages.length]; + } + + function setDocumentLanguage(language) { + if (document.documentElement) { + document.documentElement.lang = language; + } + + if (document.body) { + document.body.dataset.language = language; + } + } + + function applyTextTranslations({ + translate, + selector = "[data-i18n]", + allowHtml = () => false + } = {}) { + if (typeof translate !== "function") { + return; + } + + document.querySelectorAll(selector).forEach((element) => { + const key = element.dataset.i18n; + const translatedText = translate(key, element); + + if (translatedText === undefined || translatedText === null) { + return; + } + + if (allowHtml(element, translatedText, key)) { + element.innerHTML = String(translatedText); + return; + } + + element.textContent = String(translatedText); + }); + } + + function applyAttributeTranslations({ + translate, + mappings = [] + } = {}) { + if (typeof translate !== "function") { + return; + } + + mappings.forEach(({ selector, datasetKey, attribute }) => { + document.querySelectorAll(selector).forEach((element) => { + const key = element.dataset[datasetKey]; + const translatedText = translate(key, element, attribute); + + if (translatedText === undefined || translatedText === null) { + return; + } + + element.setAttribute(attribute, String(translatedText)); + }); + }); + } + + function applyTranslations({ + translate, + selector = "[data-i18n]", + allowHtml, + attributeMappings = [] + } = {}) { + applyTextTranslations({ translate, selector, allowHtml }); + applyAttributeTranslations({ translate, mappings: attributeMappings }); + } + + function updateLanguageSwitch( + button, + { + language, + label = String(language || "").toUpperCase(), + ariaLabel, + title + } = {} + ) { + if (!button) { + return; + } + + button.textContent = label; + + if (ariaLabel !== undefined) { + button.setAttribute("aria-label", ariaLabel); + } + + if (title !== undefined) { + button.title = title; + } + } + + function createController({ + button = document.getElementById("langSwitch"), + defaultLanguage = DEFAULT_LANGUAGE, + supportedLanguages = DEFAULT_SUPPORTED_LANGUAGES, + storageKey = DEFAULT_STORAGE_KEY, + onApply = () => { } + } = {}) { + const options = { + defaultLanguage, + supportedLanguages, + storageKey + }; + + let currentLanguage = normalizeLanguage(defaultLanguage, options); + + function apply(language, source = "apply") { + currentLanguage = normalizeLanguage(language, options); + setDocumentLanguage(currentLanguage); + + onApply(currentLanguage, { + source, + button, + applyTranslations, + updateLanguageSwitch: (switchOptions = {}) => updateLanguageSwitch(button, { + language: currentLanguage, + ...switchOptions + }), + getNextLanguage: () => getNextLanguage(currentLanguage, supportedLanguages), + normalizeLanguage: (value) => normalizeLanguage(value, options) + }); + + storeLanguage(currentLanguage, { storageKey }); + return currentLanguage; + } + + function toggle() { + return apply(getNextLanguage(currentLanguage, supportedLanguages), "toggle"); + } + + function init(initialLanguage = getPreferredLanguage({ + supportedLanguages, + defaultLanguage, + storageKey, + fallbackLanguage: defaultLanguage + })) { + if (button) { + button.addEventListener("click", toggle); + } + + return apply(initialLanguage, "init"); + } + + return { + apply, + getCurrentLanguage: () => currentLanguage, + getPreferredLanguage: (fallbackLanguage = defaultLanguage) => getPreferredLanguage({ + supportedLanguages, + defaultLanguage, + storageKey, + fallbackLanguage + }), + init, + toggle + }; + } + + window.AppLanguage = { + DEFAULT_LANGUAGE, + DEFAULT_STORAGE_KEY, + DEFAULT_SUPPORTED_LANGUAGES: DEFAULT_SUPPORTED_LANGUAGES.slice(), + applyAttributeTranslations, + applyTextTranslations, + applyTranslations, + createController, + detectBrowserLanguage, + getNextLanguage, + getPreferredLanguage, + getStoredLanguage, + normalizeLanguage, + setDocumentLanguage, + storeLanguage, + updateLanguageSwitch + }; +})(window, document); diff --git a/app/static/js/legal.js b/app/static/js/legal.js index d8869fd..93e1a20 100644 --- a/app/static/js/legal.js +++ b/app/static/js/legal.js @@ -1,9 +1,9 @@ -const SUPPORTED_LANGUAGES = ["de", "en"]; -const DEFAULT_LANGUAGE = "de"; -const LANGUAGE_STORAGE_KEY = "preferredLanguage"; +//translation for English and German +const AppLanguage = window.AppLanguage; const langSwitch = document.getElementById("langSwitch"); const legalPage = document.body.dataset.legalPage; +let currentLanguage = AppLanguage.DEFAULT_LANGUAGE; const translations = { de: { @@ -27,10 +27,10 @@ const translations = { contactEmailLabel: "E-Mail:", projectResponsibilityTitle: "Projektverantwortung", projectResponsibilityText: "Dieses Webprojekt wurde im Rahmen eines Schulprojekts zum Acht-Damen- und Acht-Türme-Problem erstellt. Inhaltlich verantwortlich ist die jeweils oben genannte verantwortliche Stelle.", - contentLiabilityTitle: "Haftung fuer Inhalte", - contentLiabilityText: "Die Inhalte dieser Website wurden mit Sorgfalt erstellt. Für die Richtigkeit, Vollstaendigkeit und Aktualitaet der Inhalte kann jedoch keine Gewaehr übernommen werden. Bei Bekanntwerden von Rechtsverletzungen werden entsprechende Inhalte umgehend überarbeitet oder entfernt.", - linksLiabilityTitle: "Haftung fuer Links", - linksLiabilityText: "Diese Website kann Links zu externen Angeboten enthalten. Fuer deren Inhalte sind ausschliesslich die jeweiligen Betreiber verantwortlich. Eine permanente inhaltliche Kontrolle verlinkter Seiten ist ohne konkrete Anhaltspunkte einer Rechtsverletzung nicht zumutbar.", + contentLiabilityTitle: "Haftung für Inhalte", + contentLiabilityText: "Die Inhalte dieser Website wurden mit Sorgfalt erstellt. Für die Richtigkeit, Vollständigkeit und Aktualität der Inhalte kann jedoch keine Gewähr übernommen werden. Bei Bekanntwerden von Rechtsverletzungen werden entsprechende Inhalte umgehend überarbeitet oder entfernt.", + linksLiabilityTitle: "Haftung für Links", + linksLiabilityText: "Diese Website kann Links zu externen Angeboten enthalten. Für deren Inhalte sind ausschliesslich die jeweiligen Betreiber verantwortlich. Eine permanente inhaltliche Kontrolle verlinkter Seiten ist ohne konkrete Anhaltspunkte einer Rechtsverletzung nicht zumutbar.", versionTitle: "Stand", versionText: "Stand dieser Vorlage: April 2026" }, @@ -52,7 +52,7 @@ const translations = { processingPurposeItem4: "Speichern und Laden von Spielständen", processingPurposeItem5: "technischer Betrieb, Fehleranalyse und Missbrauchsschutz", legalBasisTitle: "4. Rechtsgrundlage", - legalBasisText: "Soweit personenbezogene Daten verarbeitet werden, erfolgt dies in der Regel zur Bereitstellung der Anwendung und ihrer Funktionen sowie aufgrund des berechtigten Interesses an einem sicheren und funktionsfaehigen Webangebot. Falls eine ausdrückliche Einwilligung erforderlich ist, wird diese gesondert eingeholt.", + legalBasisText: "Soweit personenbezogene Daten verarbeitet werden, erfolgt dies in der Regel zur Bereitstellung der Anwendung und ihrer Funktionen sowie aufgrund des berechtigten Interesses an einem sicheren und funktionsfähigen Webangebot. Falls eine ausdrückliche Einwilligung erforderlich ist, wird diese gesondert eingeholt.", storageTitle: "5. Speicherung und Löschung", storageText: "Personenbezogene Daten werden nur so lange gespeichert, wie sie für den jeweiligen Zweck benötigt werden. Benutzerkonten und zugehörige Spielstände bleiben grundsätzlich gespeichert, bis sie gelöscht oder für das Projekt nicht mehr benötigt werden.", thirdPartyTitle: "6. Weitergabe an Dritte", @@ -61,11 +61,11 @@ const translations = { rightsText: "Betroffene Personen haben im Rahmen des anwendbaren Datenschutzrechts insbesondere das Recht auf:", rightsItem1: "Auskunft über die verarbeiteten personenbezogenen Daten", rightsItem2: "Berichtigung unrichtiger Daten", - rightsItem3: "Löschung oder Einschraenkung der Verarbeitung", + rightsItem3: "Löschung oder Einschränkung der Verarbeitung", rightsItem4: "Widerspruch gegen die Verarbeitung", rightsItem5: "Herausgabe der bereitgestellten Daten, soweit anwendbar", securityTitle: "8. Datensicherheit", - securityText: "Es werden technische und organisatorische Massnahmen getroffen, um gespeicherte Daten vor Verlust, Missbrauch oder unberechtigtem Zugriff zu schuetzen. Dazu gehoeren insbesondere passwortgeschuetzte Konten und die Möglichkeit, Zwei-Faktor-Authentifizierung zu aktivieren.", + securityText: "Es werden technische und organisatorische Massnahmen getroffen, um gespeicherte Daten vor Verlust, Missbrauch oder unberechtigtem Zugriff zu schützen. Dazu gehoeren insbesondere passwortgeschützte Konten und die Möglichkeit, Zwei-Faktor-Authentifizierung zu aktivieren.", versionTitle: "9. Stand", versionText: "Stand dieser Vorlage: April 2026" } @@ -136,107 +136,39 @@ const translations = { } }; -function normalizeLanguage(language) { - const shortLanguage = String(language || "").trim().slice(0, 2).toLowerCase(); - return SUPPORTED_LANGUAGES.includes(shortLanguage) ? shortLanguage : DEFAULT_LANGUAGE; -} - -function getStoredLanguage() { - try { - return window.localStorage.getItem(LANGUAGE_STORAGE_KEY); - } catch (error) { - return null; - } -} - -function storeLanguage(language) { - try { - window.localStorage.setItem(LANGUAGE_STORAGE_KEY, language); - } catch (error) { - return; - } -} - -function detectBrowserLanguage() { - return normalizeLanguage(navigator.language || navigator.userLanguage || DEFAULT_LANGUAGE); -} - -function t(language, key) { - const commonTranslation = translations[language]?.common?.[key]; +function t(key) { + const commonTranslation = translations[currentLanguage]?.common?.[key]; if (commonTranslation) { return commonTranslation; } - return translations[language]?.[legalPage]?.[key] ?? null; + return translations[currentLanguage]?.[legalPage]?.[key] ?? null; } -function translateFooter(language) { - const footerText = document.querySelector(".page-footer-text"); - const footerNav = document.querySelector(".page-footer-links"); - const privacyLink = document.querySelector('.page-footer-links a[href$="/privacy-policy"]'); - const imprintLink = document.querySelector('.page-footer-links a[href$="/impressum"]'); - - if (footerText) { - footerText.textContent = t(language, "footerText") || footerText.textContent; - } - - if (footerNav) { - footerNav.setAttribute("aria-label", t(language, "footerNavAria") || footerNav.getAttribute("aria-label") || ""); - } - - if (privacyLink) { - privacyLink.textContent = t(language, "footerPrivacy") || privacyLink.textContent; - } - - if (imprintLink) { - imprintLink.textContent = t(language, "footerImprint") || imprintLink.textContent; - } -} - -function applyTranslations(language) { - document.documentElement.lang = language; - document.body.dataset.language = language; - - document.querySelectorAll("[data-i18n]").forEach((element) => { - const translatedText = t(language, element.dataset.i18n); - - if (!translatedText) { - return; - } - - element.textContent = translatedText; - }); - - document.querySelectorAll("[data-i18n-aria-label]").forEach((element) => { - const translatedText = t(language, element.dataset.i18nAriaLabel); - - if (!translatedText) { - return; - } - - element.setAttribute("aria-label", translatedText); - }); - - translateFooter(language); - - if (langSwitch) { - langSwitch.textContent = language.toUpperCase(); - langSwitch.setAttribute("aria-label", t(language, "languageSwitchAria") || "Sprache wechseln"); - langSwitch.title = language === "de" ? "Switch to English" : "Zu Deutsch wechseln"; - } - - storeLanguage(language); -} - -function setLanguage(language) { - applyTranslations(normalizeLanguage(language)); -} - -setLanguage(getStoredLanguage() || detectBrowserLanguage()); - -if (langSwitch) { - langSwitch.addEventListener("click", () => { - setLanguage(document.documentElement.lang === "de" ? "en" : "de"); +function applyTranslations() { + AppLanguage.applyTranslations({ + translate: (key) => t(key), + attributeMappings: [ + { + selector: "[data-i18n-aria-label]", + datasetKey: "i18nAriaLabel", + attribute: "aria-label" + } + ] }); } + +const languageController = AppLanguage.createController({ + button: langSwitch, + onApply(language, helpers) { + currentLanguage = language; + applyTranslations(); + helpers.updateLanguageSwitch({ + ariaLabel: t("languageSwitchAria") || "Sprache wechseln", + title: language === "de" ? "Switch to English" : "Zu Deutsch wechseln" + }); + } +}); + +languageController.init(); diff --git a/app/static/js/login.js b/app/static/js/login.js index 30b38a5..971697b 100644 --- a/app/static/js/login.js +++ b/app/static/js/login.js @@ -1,13 +1,14 @@ -const SUPPORTED_LANGUAGES = ["de", "en"]; -const DEFAULT_LANGUAGE = "de"; -const LANGUAGE_STORAGE_KEY = "preferredLanguage"; +//translation for English and German +const AppLanguage = window.AppLanguage; const langSwitch = document.getElementById("langSwitch"); const translations = { de: { pageTitle: "Login-Einstellungen", navBrand: "8-Damen- und 8-Türme Solver", + navLogin: "Anmelden", + navRegister: "Account erstellen", navDashboard: "Dashboard", navSettings: "Login-Einstellungen", navLogout: "Logout", @@ -51,7 +52,9 @@ const translations = { }, en: { pageTitle: "Login Settings", - navBrand: "8-Damen- und 8-Türme Solver", + navBrand: "8 Queens and 8 Rooks Solver", + navLogin: "Login", + navRegister: "Create account", navDashboard: "Dashboard", navSettings: "Login settings", navLogout: "Logout", @@ -112,32 +115,7 @@ const serverMessageTranslations = { en: {} }; -let currentLanguage = DEFAULT_LANGUAGE; - -function normalizeLanguage(language) { - const shortLanguage = String(language || "").trim().slice(0, 2).toLowerCase(); - return SUPPORTED_LANGUAGES.includes(shortLanguage) ? shortLanguage : DEFAULT_LANGUAGE; -} - -function getStoredLanguage() { - try { - return window.localStorage.getItem(LANGUAGE_STORAGE_KEY); - } catch (error) { - return null; - } -} - -function storeLanguage(language) { - try { - window.localStorage.setItem(LANGUAGE_STORAGE_KEY, language); - } catch (error) { - return; - } -} - -function detectBrowserLanguage() { - return normalizeLanguage(navigator.language || navigator.userLanguage || DEFAULT_LANGUAGE); -} +let currentLanguage = AppLanguage.DEFAULT_LANGUAGE; function t(key) { return translations[currentLanguage]?.[key] ?? translations.en[key] ?? key; @@ -172,29 +150,25 @@ function translateServerMessage(message) { } function applyTextTranslations() { - document.documentElement.lang = currentLanguage; - - document.querySelectorAll("[data-i18n]").forEach((element) => { - const translatedText = t(element.dataset.i18n); - - if (element.tagName === "TITLE") { - element.textContent = translatedText; - return; - } - - element.textContent = translatedText; - }); - - document.querySelectorAll("[data-i18n-aria-label]").forEach((element) => { - element.setAttribute("aria-label", t(element.dataset.i18nAriaLabel)); + AppLanguage.applyTranslations({ + translate: (key) => t(key), + attributeMappings: [ + { + selector: "[data-i18n-aria-label]", + datasetKey: "i18nAriaLabel", + attribute: "aria-label" + }, + { + selector: "[data-i18n-loading-label]", + datasetKey: "i18nLoadingLabel", + attribute: "data-loading-label" + } + ] }); document.querySelectorAll("[data-i18n-loading-label]").forEach((element) => { - const translatedLoadingLabel = t(element.dataset.i18nLoadingLabel); - element.dataset.loadingLabel = translatedLoadingLabel; - if (element.dataset.loadingLabelApplied === "true") { - element.textContent = translatedLoadingLabel; + element.textContent = element.dataset.loadingLabel; } }); @@ -205,30 +179,27 @@ function applyTextTranslations() { element.textContent = translateServerMessage(element.dataset.originalText); }); +} - if (langSwitch) { - langSwitch.textContent = currentLanguage.toUpperCase(); - langSwitch.setAttribute("aria-label", t("languageSwitchAria")); - langSwitch.title = currentLanguage === "de" ? "Switch to English" : "Zu Deutsch wechseln"; +const languageController = AppLanguage.createController({ + button: langSwitch, + onApply(language, helpers) { + currentLanguage = language; + applyTextTranslations(); + helpers.updateLanguageSwitch({ + ariaLabel: t("languageSwitchAria"), + title: currentLanguage === "de" ? "Switch to English" : "Zu Deutsch wechseln" + }); } -} +}); -function setLanguage(language) { - currentLanguage = normalizeLanguage(language); - applyTextTranslations(); - storeLanguage(currentLanguage); -} +languageController.init(); -setLanguage(getStoredLanguage() || detectBrowserLanguage()); - -if (langSwitch) { - langSwitch.addEventListener("click", () => { - setLanguage(currentLanguage === "de" ? "en" : "de"); - }); -} +// ----------------------------------------------------------------------------------------------- const magneticButtons = document.querySelectorAll(".magnetic"); +// magnetic button effect for desktop user magneticButtons.forEach((button) => { button.addEventListener("mousemove", (event) => { const rect = button.getBoundingClientRect(); diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 45379c1..fe6a61a 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -42,7 +42,7 @@
@@ -65,14 +65,14 @@

- +
@@ -82,13 +82,14 @@

Damen-Modus

Damen-Spielbrett

- Platziere Damen so, dass keine zwei dieselbe Spalte oder Diagonale teilen. + Platziere Damen frei auf dem Brett. Konflikte entstehen in derselben Reihe, Spalte oder + Diagonale.

+ aria-label="Spielregeln und Hilfe anzeigen">Spielanleitung

Aktuelle Brettgrösse im @@ -118,69 +119,31 @@

-
+
+
+
+
+ + + +

Lade eine Animation und steuere sie + am Brett oder spiele sie automatisch ab.

+
-
- - -
-
- -
-
-
-

Solver

-

Lösungswerkzeuge

-

Bereite einen Ablauf vor und spiele ihn Schritt - für Schritt ab.

-
-

Schritt 0 / 0

-
- -
-
-

Animations-Solver

-

- Bereite den Lösungsweg vor, gehe ihn manüll Schritt für Schritt durch oder spiele die - Animation automatisch ab. -

- -
- - - -
- -
- - - 320 ms -
-
- -
-

Sofort-Lösung

-

- Lade direkt eine gültige Lösung, ohne Zwischenschritte oder Animation. -

- -
- -
-
-
@@ -234,25 +197,68 @@