Puzzle.hpp

Tools for all puzzle-analysis techniques.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2024 Michigan State University MIT Software license; see doc/LICENSE.md

Defines

INCLUDE_EMP_GAMES_PUZZLE_HPP_GUARD
struct PuzzleMove
#include <Puzzle.hpp>

Public Types

enum MoveType

Values:

enumerator BLOCK_STATE
enumerator SET_STATE

Public Functions

inline String TypeString() const
inline String ToString(size_t num_cols) const
inline std::ostream &Print(size_t row_size, std::ostream &os = std::cout) const

Public Members

MoveType type
size_t pos_id
uint8_t state
struct PuzzleSolveFun
#include <Puzzle.hpp>

Public Types

using move_set_t = vector<PuzzleMove>
using solve_fun_t = move_set_t()

Public Members

String name
double difficulty
std::function<solve_fun_t> fun
struct PuzzleProfile
#include <Puzzle.hpp>

Public Types

enum FinalState

Values:

enumerator SOLVED
enumerator UNSOLVED
enumerator UNSOLVABLE

Public Functions

inline size_t size() const
inline bool IsSolved() const
inline bool IsUnsolved() const
inline bool IsUnsolvable() const
inline void AddMoves(size_t level, size_t count)
inline void SetSolved()
inline void SetUnsolved()
inline void SetUnsolvable()
inline void Clear()
inline size_t CountTypes() const
inline size_t CountMoves(size_t id) const
inline double CalcScore() const
inline String ToString() const
inline void Print(std::ostream &out = std::cout) const

Public Members

std::vector<Slice> slices
FinalState final_state = UNSOLVED
struct Slice
#include <Puzzle.hpp>

Public Functions

inline String ToString() const

Public Members

size_t level
size_t count
template<size_t NUM_ROWS, size_t NUM_COLS, size_t NUM_STATES>
struct GridPuzzleAnalyzer
#include <Puzzle.hpp>

A generic analyzer for puzzles that have a set of positions, each in a set of values.

Public Types

using values_t = array<uint8_t, NUM_CELLS>
using grid_bits_t = BitSet<NUM_CELLS>
using move_set_t = vector<PuzzleMove>

Public Functions

inline GridPuzzleAnalyzer()
virtual ~GridPuzzleAnalyzer() = default
inline uint8_t GetValue(size_t cell) const
inline const values_t &GetValues() const
inline bool HasValue(size_t cell) const
inline size_t GetNumSolveFuns() const
inline void SetSymbols(String in_symbols)
inline uint8_t SymbolToState(char symbol) const
inline values_t LoadToArray(std::istream &is, CharSet empty = "-.")
inline values_t LoadToArray(String filename, CharSet empty = "-.")
inline bool Load(std::span<size_t> board)
inline bool LoadArray(const values_t &vals)
inline bool Load(std::istream &is, CharSet empty = "-.")
inline bool Load(const String &filename, CharSet empty = "-")
inline String CellToCoords(size_t id)

Convert a cell ID to its coordinates.

inline bool HasOption(size_t cell, uint8_t state) const

Test if a cell is allowed to be a particular state.

inline uint8_t FindOption(size_t cell)

Return a currently valid option for provided cell; may not be correct solution.

inline bool IsSet(size_t cell) const
inline bool IsSolved() const
inline void Clear()

Clear out the old solution info when starting a new solve attempt.

template<typename FUN_T>
inline void AddSolveFunction(String name, double difficulty, FUN_T fun)

Add a function solving method to this puzzle type.

Parameters:
  • name – Unique name for this solving technique

  • difficulty – Difficulty for a human to use this technique

  • fun – Function to run to perform this technique and return moves

inline virtual bool Set(size_t cell, uint8_t state)

Set the value of an individual cell Return true/false based on whether progress was made toward solving the puzzle.

inline void Block(size_t cell, uint8_t state)

Remove a symbol option from a particular cell.

inline bool MoveProgress(const PuzzleMove &move) const

Identify if a move will make progress in the puzzle.

inline bool MoveProgress(const vector<PuzzleMove> &moves) const

Do any of the provided moves progress the puzzle?

inline bool Move(const PuzzleMove &move)

Operate on a “move” object; return false if move is invalid.

inline bool Move(const vector<PuzzleMove> &moves)
inline void PrintMoves(const vector<PuzzleMove> &moves, std::ostream &os = std::cout) const
inline String Symbol(uint8_t id) const
inline String ColorSymbol(uint8_t id, bool reverse = false) const
inline virtual void Print(bool = true, std::ostream &out = std::cout)
inline bool IsUnsolvable()
inline PuzzleProfile CalcProfile()

Public Members

array<char, NUM_STATES + 1> symbols
array<uint8_t, 128> symbol_map
values_t values
array<grid_bits_t, NUM_STATES + 1> bit_options
grid_bits_t is_set
vector<PuzzleSolveFun> solve_funs

Public Static Attributes

static constexpr size_t NUM_CELLS = NUM_ROWS * NUM_COLS
static constexpr uint8_t UNSET_STATE = 0
static constexpr uint8_t UNKNOWN_STATE = NUM_STATES + 1