SudokuFormat::SudokuFormat(std::shared_ptr<SudokuType> type, std::string format) : type_(std::move(type)), template_(std::move(format)), labels_(choose_labels(template_)) { unsigned cells = 0; for (char& c : template_) { if (is_cell(c)) { c = '.'; ++cells; } } if (cells != type_->size()) { throw std::invalid_argument(""); } }
std::string SudokuFormat::to_string(const std::vector<unsigned>& values) const { if (values.size() != type_->size()) { throw std::invalid_argument("to_string(): wrong number of values (" + std::to_string(values.size()) + " vs. " + std::to_string(type_->size()) + ')'); } std::string result = template_; unsigned j = 0; for (unsigned i = 0; i < result.size(); ++i) { if (is_cell(result[i])) { if (j >= values.size()) { throw std::logic_error(""); } result[i] = label(values[j]); ++j; } } if (j != values.size()) { throw std::logic_error(""); } return result; }
/** Build the maze by removing walls according to Prim's algorithm. * * @param maze a maze with all walls present. */ static void build_prim(maze_t* maze) { // MST edges and cells. If (a, b) in mst_edges, then (b, a) not in // mst_edges. (a, b) in mst_edges iff a, b both in mst_cells. list356_t* mst_edges = make_list() ; list356_t* mst_cells = make_list() ; // The frontier. This is the collection of edges between cells in the MST // and cells not in the MST. If (a, b) in frontier, then a in mst_cells // and b not in mst_cells. list356_t* frontier = make_list() ; // Choose two adjacent cells at random to put into the MST, then // populate the frontier accordinately. For simplicitly, choose a // cell in the interior of the maze, then randomly choose a direction // for the other cell. cell_t* start = get_cell(maze, random_limit(1, maze->nrows-1), random_limit(1, maze->ncols-1)); unsigned char direction = 1 << random_limit(0, 4) ; cell_t* next = get_neighbor(maze, start, direction) ; /* debug("Removing (%d, %d) - (%d, %d).\n", start->r, start->c, next->r, next->c) ; */ remove_wall(maze, start, direction) ; edge_t init_edge = (edge_t){start, next} ; lst_add(mst_edges, &init_edge) ; lst_add(mst_cells, start) ; lst_add(mst_cells, next) ; for (int d=0; d<4; ++d) { if (directions[d] != direction && is_cell(maze, start, directions[d])) { cell_t* c = get_neighbor(maze, start, directions[d]) ; edge_t* e = malloc(sizeof(edge_t)) ; e->a = start ; e->b = c ; lst_add(frontier, e) ; } } for (int d=0; d<4; ++d) { if (directions[d] != opposite(direction) && is_cell(maze, next, directions[d])) { cell_t* c = get_neighbor(maze, next, directions[d]) ; edge_t* e = malloc(sizeof(edge_t)) ; e->a = next ; e->b = c ; lst_add(frontier, e) ; } } // As long as we don't have all the cells in the MST, choose an // edge in the frontier at random. Put the edge in the MST // and compute the new edges to add to the frontier. while (lst_size(mst_cells) < (maze->nrows)*(maze->ncols)) { int p = random_limit(0, lst_size(frontier)) ; edge_t* edge = lst_get(frontier, p) ; cell_t* old_cell = edge->a ; cell_t* new_cell = edge->b ; /* debug("Removing (%d, %d) - (%d, %d).\n", old_cell->r, old_cell->c, new_cell->r, new_cell->c) ; */ remove_wall(maze, old_cell, get_direction(old_cell, new_cell)) ; lst_add(mst_edges, edge) ; lst_add(mst_cells, new_cell) ; for (int d=0; d<4; ++d) { unsigned char dir = directions[d] ; if (is_cell(maze, new_cell, dir)) { cell_t* adj_cell = get_neighbor(maze, new_cell, dir) ; edge_t* edge2 = malloc(sizeof(edge_t)) ; edge2->a = new_cell ; edge2->b = adj_cell ; if (lst_contains(mst_cells, adj_cell, cell_cmp)) { lst_remove(frontier, edge2, edge_cmp) ; if (adj_cell != old_cell) free(edge2) ; } else { lst_add(frontier, edge2) ; } } } } }