From fb3a3fb19a3fc2595f0a3001445614d821ede71e Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Thu, 23 Jul 2015 21:21:57 +0300 Subject: [PATCH] Added getRandInterval funstion. Full support of dictionaries with multiple definitions of one word. --- src/crossgen.cpp | 2 +- src/crossgen.hpp | 5 +++++ wxCrossGen/fmain.cpp | 10 +++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/crossgen.cpp b/src/crossgen.cpp index 8e94325..c46b3e8 100644 --- a/src/crossgen.cpp +++ b/src/crossgen.cpp @@ -1,5 +1,5 @@ #include "crossgen.hpp" - +//TODO: fix crash when genereates crossword in debug mode with floating-point error const wxChar CELL_CLEAR = wxT('+'); const wxChar CELL_BORDER = wxT('-'); diff --git a/src/crossgen.hpp b/src/crossgen.hpp index eef99e3..a3b47f7 100644 --- a/src/crossgen.hpp +++ b/src/crossgen.hpp @@ -51,4 +51,9 @@ bool procCross( void generateCross(const GridType &grid, const AllWordsType &words, const CharsTransType &trans_type, std::vector &words_out); +template +InputIterator getRandInterval(const InputIterator first, const InputIterator last){ + auto d = std::distance(first, last); + return next(first, rand() % d); +} #endif // CROSSGEN_HPP diff --git a/wxCrossGen/fmain.cpp b/wxCrossGen/fmain.cpp index 7fa7ae8..a733073 100644 --- a/wxCrossGen/fmain.cpp +++ b/wxCrossGen/fmain.cpp @@ -140,9 +140,15 @@ void MainFrame::onGenerateClick(wxCommandEvent &event) { std::vector winfos; generateWordInfo(_grid, winfos); + if ( winfos.size() == 0 ) + throw 42; + _ques.clear(); for (size_t i = 0; i < words_out.size(); ++i) - _ques.push_back(_dict.find(words_out.at(i))->second); + _ques.push_back(getRandInterval( + _dict.lower_bound(words_out.at(i)), + _dict.upper_bound(words_out.at(i)) + )->second); tOutput->Clear(); @@ -154,8 +160,6 @@ void MainFrame::onGenerateClick(wxCommandEvent &event) { tOutput->AppendText(getQuesString(t_cross)); - if ( winfos.size() == 0 ) - throw 42; SetGridImage(_grid); } catch ( ... ){