Skip to content

Commit

Permalink
Merge pull request #141 from jpd236/ordered-cluelist
Browse files Browse the repository at this point in the history
Respect clue list ordering from file and expand initial clue selection.
  • Loading branch information
mrichards42 authored Jun 6, 2021
2 parents 79fa394 + e310d2b commit 5d60782
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
22 changes: 20 additions & 2 deletions puz/Clue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,27 @@ bool Clues::HasWords() const
ClueList & Clues::operator[](const string_t & direction)
{
iterator it = find(direction);
if (it == end())
return insert(std::make_pair(direction, ClueList(direction))).first->second;
if (it == end()) {
push_back(std::make_pair(direction, ClueList(direction)));
return back().second;
}
return it->second;
}

std::vector<std::pair<string_t, ClueList> >::iterator Clues::find(const string_t& direction) {
std::vector<std::pair<string_t, ClueList> >::iterator it = begin();
while (it != end() && it->first != direction) {
it++;
}
return it;
}

std::vector<std::pair<string_t, ClueList> >::const_iterator Clues::find(const string_t& direction) const {
std::vector<std::pair<string_t, ClueList> >::const_iterator it = begin();
while (it != end() && it->first != direction) {
it++;
}
return it;
}

} // namespace puz
5 changes: 4 additions & 1 deletion puz/Clue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class PUZ_API ClueList : public std::vector<Clue>


// This class holds all of the clues
class PUZ_API Clues : public std::map<string_t, ClueList>
class PUZ_API Clues : public std::vector<std::pair<string_t, ClueList> >
{
public:
Clues() {}
Expand All @@ -153,6 +153,9 @@ class PUZ_API Clues : public std::map<string_t, ClueList>
ret.SetTitle(direction);
return ret;
}

std::vector<std::pair<string_t, ClueList> >::iterator find(const string_t& direction);
std::vector<std::pair<string_t, ClueList> >::const_iterator find(const string_t& direction) const;
};

} // namespace puz
Expand Down
7 changes: 5 additions & 2 deletions puz/Puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ void Puzzle::SetAllClues(const std::vector<string_t> & clues)
{
NumberGrid();

ClueList & across = SetClueList(puzT("Across"), ClueList());
ClueList & down = SetClueList(puzT("Down"), ClueList());
ClueList across = ClueList();
ClueList down = ClueList();

std::vector<string_t>::const_iterator clue_it = clues.begin();
std::vector<string_t>::const_iterator clue_end = clues.end();
Expand Down Expand Up @@ -220,6 +220,9 @@ void Puzzle::SetAllClues(const std::vector<string_t> & clues)
++clueNumber;
}

SetClueList(puzT("Across"), across);
SetClueList(puzT("Down"), down);

if (clue_it != clue_end)
throw InvalidClues();
GenerateWords();
Expand Down
11 changes: 6 additions & 5 deletions src/XGridCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ XGridCtrl::SetPuzzle(puz::Puzzle * puz)
puz::Clues* clues = &puz->GetClues();
puz::ClueList* firstClueList = NULL;
if (!m_grid->IsDiagramless()) {
if (clues->size() == 1) {
// If there's only one list, use that.
firstClueList = &clues->begin()->second;
}
else if (clues->find(puzT("Across")) != clues->end()) {
if (clues->find(puzT("Across")) != clues->end()) {
// If there's an "Across" list, use that.
firstClueList = &clues->GetAcross();
}
else if (!clues->empty()) {
// Select the first clue list - assumes that the file is likely in the intended
// clue list order (and that this order is retained when reading the file).
firstClueList = &clues->begin()->second;
}
}

m_focusedWord = NULL;
Expand Down

0 comments on commit 5d60782

Please sign in to comment.