Files
nd-wfc/demos/console_renderer.h
2025-08-26 14:00:00 +09:00

131 lines
3.8 KiB
C++

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <chrono>
#include <thread>
/**
* Base Console Renderer
*
* Provides utilities for console-based rendering with cursor control,
* allowing for real-time updates without adding new lines.
*
* Key features:
* - Allocate screen space before rendering
* - Move cursor to update specific areas
* - Clear and redraw content in place
* - Cross-platform cursor control
*/
class ConsoleRenderer {
public:
ConsoleRenderer() = default;
virtual ~ConsoleRenderer() = default;
// Core rendering interface
virtual void allocateSpace() = 0;
virtual void render() = 0;
virtual void clear() = 0;
// Cursor control utilities
static void moveCursorUp(int lines);
static void moveCursorDown(int lines);
static void moveCursorToColumn(int col);
static void moveCursorToPosition(int row, int col);
static void clearLine();
static void clearScreen();
static void hideCursor();
static void showCursor();
// Helper functions
static void sleep(int milliseconds);
static std::string repeatChar(char c, int count);
static std::string centerText(const std::string& text, int width);
protected:
int allocated_lines_ = 0;
bool space_allocated_ = false;
};
/**
* Sudoku Console Renderer
*
* Renders Sudoku puzzles with proper grid formatting and supports
* real-time updates for solving animations.
*
* Usage:
* SudokuRenderer renderer(sudoku);
* renderer.allocateSpace(); // Reserve console space
* renderer.render(); // Initial render
*
* // Update sudoku state and re-render
* sudoku.set(0, 0, 5);
* renderer.render(); // Updates in place
*/
class SudokuRenderer : public ConsoleRenderer {
public:
explicit SudokuRenderer(const class Sudoku& sudoku);
void allocateSpace() override;
void render() override;
void clear() override;
// Sudoku-specific methods
void renderWithHighlight(int highlight_row = -1, int highlight_col = -1);
void showSolvingProgress(const std::string& status = "");
// Animation support
void animateCell(int row, int col, uint8_t value, int delay_ms = 100);
private:
const class Sudoku& sudoku_;
// Rendering helpers
std::string formatSudokuLine(int row) const;
std::string getSeparatorLine() const;
char getCellChar(uint8_t value) const;
// Constants for formatting
static constexpr int GRID_WIDTH = 21; // "1 2 3 | 4 5 6 | 7 8 9"
static constexpr int GRID_HEIGHT = 11; // 9 rows + 2 separators
static constexpr int TOTAL_HEIGHT = 15; // Grid + title + status
};
/**
* Nonogram Console Renderer
*
* Renders Nonogram puzzles with hints and solution grid.
* Supports real-time updates for solving animations.
*/
class NonogramRenderer : public ConsoleRenderer {
public:
explicit NonogramRenderer(const class Nonogram& nonogram);
void allocateSpace() override;
void render() override;
void clear() override;
// Nonogram-specific methods
void renderWithState(const std::vector<std::vector<int>>& state);
void showSolvingProgress(const std::string& status = "");
private:
const class Nonogram& nonogram_;
// Rendering helpers
std::string formatNonogramLine(int row, const std::vector<std::vector<int>>* state = nullptr) const;
std::string formatColumnHints() const;
int calculateWidth() const;
int calculateHeight() const;
};
// Utility class for demo animations
class DemoAnimator {
public:
static void typewriterEffect(const std::string& text, int delay_ms = 50);
static void fadeIn(const std::vector<std::string>& lines, int delay_ms = 100);
static void progressBar(const std::string& label, int current, int total, int width = 30);
};