From 294e5517dc2f8e9e8198eb9bf599e3caeca1207c Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Sun, 31 May 2015 17:46:43 +0300 Subject: [PATCH] Some structure changes --- .gitignore | 7 +- src/crossgen.hpp | 208 ++++++++++++++++++++++++++++++++++ wxCrossGen/main.cpp | 186 +----------------------------- wxCrossGen/wxCrossGen.mk | 2 +- wxCrossGen/wxCrossGen.project | 4 + wxCrossGen/wxgui.hpp | 21 +--- 6 files changed, 222 insertions(+), 206 deletions(-) create mode 100644 src/crossgen.hpp diff --git a/.gitignore b/.gitignore index de1cd40..69d3576 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,9 @@ #CodeLite .clang .codelite -.Debug +Debug *.cpp.o -*.cpp.o.d \ No newline at end of file +*.cpp.o.d + +#Release folder is NOT code! +Release \ No newline at end of file diff --git a/src/crossgen.hpp b/src/crossgen.hpp new file mode 100644 index 0000000..74466bd --- /dev/null +++ b/src/crossgen.hpp @@ -0,0 +1,208 @@ +#ifndef CROSSGEN_HPP +#define CROSSGEN_HPP + +#include +#include + +#include + +typedef std::map DictType; +typedef std::vector< std::vector > GridType; +typedef std::vector< std::vector > CurGridType; +// Первый индекс -- длина слова +typedef std::vector< std::vector > AllWordsType; +typedef std::set< wxString > UsedWords; + +struct WordInfo { + size_t x; + size_t y; + size_t len; + size_t ind; + //true for vertical and false for horisontal + bool direct; +}; + +const wxChar CELL_CLEAR = wxT('+'); + +void readDict(wxString path, DictType &dict){ + wxTextFile f; + f.Open(path); + for ( wxString str = f.GetFirstLine(); !f.Eof(); str = f.GetNextLine() ) { + wxString key,val; + int del_ind = str.Index('-'); + key = str.Left(del_ind-1); + val = str.Right(str.size() - del_ind - 2); + dict[key] = val; + } + wxLogDebug(wxString::Format(wxT("Прочитан словарь размером %d записей"), + static_cast(dict.size()))); + f.Close(); +}; + +void readGrid(wxString path, GridType &grid){ + wxTextFile f; + f.Open(path); + wxString str = f.GetFirstLine(); + + grid.resize(str.size()); + for (unsigned int i = 0; i < grid.size(); ++i) + grid.at(i).resize(f.GetLineCount()); + + wxLogDebug(wxT("Total lines: %d. First line is %s and size = %d"),f.GetLineCount(), str.c_str(),str.size()); + unsigned int i = 0; + for ( ; !f.Eof(); str = f.GetNextLine() ) { + wxLogDebug(str); + for (unsigned int j = 0; j < str.size(); ++j) + grid.at(j).at(i) = str.at(j); + i++; + } + wxLogDebug(wxT("Прочитана сетка размером %d x %d"), + static_cast(grid.size()), static_cast(grid.at(0).size())); + f.Close(); +} + +void generateWordInfo(GridType &grid, std::vector &winfos){ + wxLogDebug(wxT("Printing grid: ")); + for (size_t i = 0; i < grid.size(); ++i){ + wxString st; + for (size_t j = 0; j < grid.at(0).size(); ++j) + st+= grid.at(i).at(j); + wxLogDebug(st); + } + size_t cur_ind = 1; + bool exist = false; + for (size_t i = 0; i < grid.size(); ++i){ + for (size_t j = 0; j < grid.at(0).size(); ++j){ + if (grid.at(i).at(j) == CELL_CLEAR){ + if (((j ==0) || (grid.at(i).at(j - 1) != CELL_CLEAR)) && + (j != grid.at(0).size() - 1)) + if (grid.at(i).at(j+1) == CELL_CLEAR){ + size_t cur_len = 1; + bool cont = true; + while ((j + cur_len < grid.at(0).size()) && cont){ + cur_len++; + if (grid.at(i).at(j+cur_len-1) != CELL_CLEAR){ + cont = false; + cur_len--; + } + } + exist = true; + WordInfo t; + t.x = i; + t.y = j; + t.len = cur_len; + t.ind = cur_ind; + t.direct = false; + winfos.push_back(t); + } + + if (((i ==0) || (grid.at(i - 1).at(j) != CELL_CLEAR)) && + (i != grid.size() - 1)) + if (grid.at(i + 1).at(j) == CELL_CLEAR){ + size_t cur_len = 1; + bool cont = true; + while ((i + cur_len < grid.size()) && cont){ + cur_len++; + if (grid.at(i+cur_len-1).at(j) != CELL_CLEAR){ + cont = false; + cur_len--; + } + } + exist = true; + WordInfo t; + t.x = i; + t.y = j; + t.len = cur_len; + t.ind = cur_ind; + t.direct = true; + winfos.push_back(t); + } + if (exist){ + exist = false; + cur_ind++; + } + } + } + } +} + + +bool procCross(UsedWords used, AllWordsType &words, CurGridType grid, + std::vector &winfos, size_t cur_word_ind, std::vector &out){ + if (cur_word_ind == winfos.size()) + return true; + WordInfo cur_wi = winfos.at(cur_word_ind); + size_t rand_add = rand() % 100000; + size_t cur_len = cur_wi.len; + size_t cur_words_size = words.at(cur_len).size(); + for (size_t i = 0; i < cur_words_size; ++i){ + wxString cur_word = words.at(cur_len).at((i + rand_add) % cur_words_size); + if (used.find(cur_word) != used.end()) + continue; + // Показывает, можно ли записать это слово в сетку + bool can_write = true; + if (cur_wi.direct == false){ + for (size_t j = 0; j < cur_wi.len; ++j) + if ((grid.at(cur_wi.x).at(j + cur_wi.y) != CELL_CLEAR) && + (grid.at(cur_wi.x).at(j + cur_wi.y) != cur_word.at(j))) + can_write = false; + } + + if (cur_wi.direct == true){ + for (size_t j = 0; j < cur_wi.len; ++j) + if ((grid.at(cur_wi.x + j).at(cur_wi.y) != CELL_CLEAR) && + (grid.at(cur_wi.x + j).at(cur_wi.y) != cur_word.at(j))) + can_write = false; + } + + if (can_write) { + UsedWords t_used(used); + t_used.insert(cur_word); + + CurGridType t_grid(grid); + + if (cur_wi.direct == false){ + for (size_t j = 0; j < cur_wi.len; ++j) + t_grid.at(cur_wi.x).at(j + cur_wi.y) = cur_word.at(j); + } + + if (cur_wi.direct == true){ + for (size_t j = 0; j < cur_wi.len; ++j) + t_grid.at(cur_wi.x + j).at(cur_wi.y) = cur_word.at(j); + } + + if (procCross(t_used, words, t_grid, winfos, cur_word_ind + 1, out)){ + //wxLogDebug(wxT("Word at (%d,%d) with len = %d and index = %d and dir = %d and word is ") + cur_word, + // cur_wi.x,cur_wi.y,cur_wi.len, cur_wi.ind, int(cur_wi.direct)); + wxLogDebug(cur_word); + out.push_back(cur_word); + return true; + } + } + } + return false; +} + +void generateCross(GridType &grid, DictType &dict, std::vector &words_out){ + AllWordsType words; + for (DictType::iterator i = dict.begin(); i != dict.end(); ++i){ + if (words.size() <= i->first.size()) + words.resize(i->first.size() + 4); + words.at(i->first.size()).push_back(i->first); + } + for (size_t i = 2; i < words.size(); ++i){ + wxLogDebug(wxT("Number of words with length %d is %d"), i, words.at(i).size()); + } + std::vector winfos; + generateWordInfo(grid, winfos); + for (size_t i = 0; i < winfos.size(); ++i) + wxLogDebug(wxT("Word at (%d,%d) with len = %d and index = %d and dir = %d"), + winfos.at(i).x,winfos.at(i).y,winfos.at(i).len, winfos.at(i).ind, int(winfos.at(i).direct)); + + UsedWords t_used; + srand(time(NULL)); + procCross(t_used, words, grid, winfos, 0, words_out); + std::reverse(words_out.begin(), words_out.end()); +} + +#endif // CROSSGEN_HPP diff --git a/wxCrossGen/main.cpp b/wxCrossGen/main.cpp index ab9b084..8109592 100644 --- a/wxCrossGen/main.cpp +++ b/wxCrossGen/main.cpp @@ -9,11 +9,11 @@ #include #include "wxgui.hpp" +#include "crossgen.hpp" + #define wxID_PATH 1079 #define wxID_GENERATE 1080 -const wxChar CELL_CLEAR = wxT('+'); - MainFrame::MainFrame(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style): wxFrame(parent, id, title, pos, size, wxDEFAULT_FRAME_STYLE) { @@ -68,188 +68,6 @@ BEGIN_EVENT_TABLE(MainFrame, wxFrame) // end wxGlade END_EVENT_TABLE(); - - -void readDict(wxString path, DictType &dict){ - wxTextFile f; - f.Open(path); - for ( wxString str = f.GetFirstLine(); !f.Eof(); str = f.GetNextLine() ) { - wxString key,val; - int del_ind = str.Index('-'); - key = str.Left(del_ind-1); - val = str.Right(str.size() - del_ind - 2); - dict[key] = val; - } - wxLogDebug(wxString::Format(wxT("Прочитан словарь размером %d записей"), - static_cast(dict.size()))); - f.Close(); -}; - -void readGrid(wxString path, GridType &grid){ - wxTextFile f; - f.Open(path); - wxString str = f.GetFirstLine(); - - grid.resize(str.size()); - for (unsigned int i = 0; i < grid.size(); ++i) - grid.at(i).resize(f.GetLineCount()); - - wxLogDebug(wxT("Total lines: %d. First line is %s and size = %d"),f.GetLineCount(), str.c_str(),str.size()); - unsigned int i = 0; - for ( ; !f.Eof(); str = f.GetNextLine() ) { - wxLogDebug(str); - for (unsigned int j = 0; j < str.size(); ++j) - grid.at(j).at(i) = str.at(j); - i++; - } - wxLogDebug(wxT("Прочитана сетка размером %d x %d"), - static_cast(grid.size()), static_cast(grid.at(0).size())); - f.Close(); -} - -void generateWordInfo(GridType &grid, std::vector &winfos){ - wxLogDebug(wxT("Printing grid: ")); - for (size_t i = 0; i < grid.size(); ++i){ - wxString st; - for (size_t j = 0; j < grid.at(0).size(); ++j) - st+= grid.at(i).at(j); - wxLogDebug(st); - } - size_t cur_ind = 1; - bool exist = false; - for (size_t i = 0; i < grid.size(); ++i){ - for (size_t j = 0; j < grid.at(0).size(); ++j){ - if (grid.at(i).at(j) == CELL_CLEAR){ - if (((j ==0) || (grid.at(i).at(j - 1) != CELL_CLEAR)) && - (j != grid.at(0).size() - 1)) - if (grid.at(i).at(j+1) == CELL_CLEAR){ - size_t cur_len = 1; - bool cont = true; - while ((j + cur_len < grid.at(0).size()) && cont){ - cur_len++; - if (grid.at(i).at(j+cur_len-1) != CELL_CLEAR){ - cont = false; - cur_len--; - } - } - exist = true; - WordInfo t; - t.x = i; - t.y = j; - t.len = cur_len; - t.ind = cur_ind; - t.direct = false; - winfos.push_back(t); - } - - if (((i ==0) || (grid.at(i - 1).at(j) != CELL_CLEAR)) && - (i != grid.size() - 1)) - if (grid.at(i + 1).at(j) == CELL_CLEAR){ - size_t cur_len = 1; - bool cont = true; - while ((i + cur_len < grid.size()) && cont){ - cur_len++; - if (grid.at(i+cur_len-1).at(j) != CELL_CLEAR){ - cont = false; - cur_len--; - } - } - exist = true; - WordInfo t; - t.x = i; - t.y = j; - t.len = cur_len; - t.ind = cur_ind; - t.direct = true; - winfos.push_back(t); - } - if (exist){ - exist = false; - cur_ind++; - } - } - } - } -} - -bool procCross(UsedWords used, AllWordsType &words, CurGridType grid, - std::vector &winfos, size_t cur_word_ind, std::vector &out){ - if (cur_word_ind == winfos.size()) - return true; - WordInfo cur_wi = winfos.at(cur_word_ind); - size_t rand_add = rand() % 100000; - size_t cur_len = cur_wi.len; - size_t cur_words_size = words.at(cur_len).size(); - for (size_t i = 0; i < cur_words_size; ++i){ - wxString cur_word = words.at(cur_len).at((i + rand_add) % cur_words_size); - if (used.find(cur_word) != used.end()) - continue; - // Показывает, можно ли записать это слово в сетку - bool can_write = true; - if (cur_wi.direct == false){ - for (size_t j = 0; j < cur_wi.len; ++j) - if ((grid.at(cur_wi.x).at(j + cur_wi.y) != CELL_CLEAR) && - (grid.at(cur_wi.x).at(j + cur_wi.y) != cur_word.at(j))) - can_write = false; - } - - if (cur_wi.direct == true){ - for (size_t j = 0; j < cur_wi.len; ++j) - if ((grid.at(cur_wi.x + j).at(cur_wi.y) != CELL_CLEAR) && - (grid.at(cur_wi.x + j).at(cur_wi.y) != cur_word.at(j))) - can_write = false; - } - - if (can_write) { - UsedWords t_used(used); - t_used.insert(cur_word); - - CurGridType t_grid(grid); - - if (cur_wi.direct == false){ - for (size_t j = 0; j < cur_wi.len; ++j) - t_grid.at(cur_wi.x).at(j + cur_wi.y) = cur_word.at(j); - } - - if (cur_wi.direct == true){ - for (size_t j = 0; j < cur_wi.len; ++j) - t_grid.at(cur_wi.x + j).at(cur_wi.y) = cur_word.at(j); - } - - if (procCross(t_used, words, t_grid, winfos, cur_word_ind + 1, out)){ - //wxLogDebug(wxT("Word at (%d,%d) with len = %d and index = %d and dir = %d and word is ") + cur_word, - // cur_wi.x,cur_wi.y,cur_wi.len, cur_wi.ind, int(cur_wi.direct)); - wxLogDebug(cur_word); - out.push_back(cur_word); - return true; - } - } - } - return false; -} - -void generateCross(GridType &grid, DictType &dict, std::vector &words_out){ - AllWordsType words; - for (DictType::iterator i = dict.begin(); i != dict.end(); ++i){ - if (words.size() <= i->first.size()) - words.resize(i->first.size() + 4); - words.at(i->first.size()).push_back(i->first); - } - for (size_t i = 2; i < words.size(); ++i){ - wxLogDebug(wxT("Number of words with length %d is %d"), i, words.at(i).size()); - } - std::vector winfos; - generateWordInfo(grid, winfos); - for (size_t i = 0; i < winfos.size(); ++i) - wxLogDebug(wxT("Word at (%d,%d) with len = %d and index = %d and dir = %d"), - winfos.at(i).x,winfos.at(i).y,winfos.at(i).len, winfos.at(i).ind, int(winfos.at(i).direct)); - - UsedWords t_used; - srand(time(NULL)); - procCross(t_used, words, grid, winfos, 0, words_out); - std::reverse(words_out.begin(), words_out.end()); -} - void MainFrame::OnbtnPathClick(wxCommandEvent &event) { event.Skip(); wxFileDialog dlgOpen(this, wxT("Open XYZ file"), wxEmptyString, wxEmptyString, diff --git a/wxCrossGen/wxCrossGen.mk b/wxCrossGen/wxCrossGen.mk index d8829b4..8031bb6 100644 --- a/wxCrossGen/wxCrossGen.mk +++ b/wxCrossGen/wxCrossGen.mk @@ -36,7 +36,7 @@ ObjectsFileList :="wxCrossGen.txt" PCHCompileFlags := MakeDirCommand :=mkdir -p LinkOptions := -s $(shell wx-config --debug=no --libs --unicode=yes) -IncludePath := $(IncludeSwitch). +IncludePath := $(IncludeSwitch). $(IncludeSwitch)../src IncludePCH := RcIncludePath := Libs := diff --git a/wxCrossGen/wxCrossGen.project b/wxCrossGen/wxCrossGen.project index db3d332..ede6be2 100644 --- a/wxCrossGen/wxCrossGen.project +++ b/wxCrossGen/wxCrossGen.project @@ -29,10 +29,14 @@ + + + + diff --git a/wxCrossGen/wxgui.hpp b/wxCrossGen/wxgui.hpp index b455ec8..bf507e6 100644 --- a/wxCrossGen/wxgui.hpp +++ b/wxCrossGen/wxgui.hpp @@ -13,28 +13,11 @@ #define WXGUI_HPP #include -#include -#include - #include #include #include "wx/intl.h" -typedef std::map DictType; -typedef std::vector< std::vector > GridType; -typedef std::vector< std::vector > CurGridType; -// Первый индекс -- длина слова -typedef std::vector< std::vector > AllWordsType; -typedef std::set< wxString > UsedWords; - -struct WordInfo { - size_t x; - size_t y; - size_t len; - size_t ind; - //true for vertical and false for horisontal - bool direct; -}; +#include "crossgen.hpp" #ifndef APP_CATALOG #define APP_CATALOG "app" // replace with the appropriate catalog name @@ -81,4 +64,4 @@ public: }; // wxGlade: end class -#endif // WXGUI_H +#endif // WXGUI_HPP