Advertisement
the_alator

k2

May 15th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.03 KB | None | 0 0
  1. // ConsoleApplication6.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <windows.h>
  8. #include <fcntl.h>
  9. #include <io.h>
  10. #include <string>
  11. #include <ctime>
  12. #include "helpfulFunctions.h"
  13.  
  14. const int lengthInCageX = 25;
  15. const int lengthInCageY = 25;
  16. const int lengthOfCageX = 3;
  17. const int lengthOfCageY = 1;
  18.  
  19. using namespace std;
  20.  
  21. struct field {
  22. int horisontal = -1;
  23. int vertical = -1;
  24. char sym = -1;
  25. };
  26. struct word {
  27. int XStart, YStart, length = -1, direction;
  28. };
  29. void drawTable();
  30. void fillBaseMap();
  31. void drawArray();
  32. void writeRusText(string text);
  33. int findWord(int length, int Ystart, int Xstart, int direction);
  34. void putWordIntoTable(char str[], int length, int Ystart, int Xstart, int direction);
  35. int randWord();
  36. int randWord2(int Sdirection, int Slength, int SXstart, int SYstart, int shift, int Sborder, int Eborder);
  37. void replaceArrays();
  38.  
  39. HANDLE cons;
  40. COORD cursPos;
  41.  
  42. field baseMap[lengthInCageY][lengthInCageX];
  43. string words[100];
  44. string text[100];
  45.  
  46. word *wordsInTable = new word[100];
  47. word *tempWordsInTable = new word[100];
  48. int WITAddPoint = 0;
  49. int tempWITAddPoint = 0;
  50.  
  51.  
  52. FILE *wordsF;
  53. int main()
  54. {
  55. srand(time(0));
  56. fopen_s(&wordsF, "D:/Файлы/Временные/Новая папка (5)/CrossWord.txt", "r");
  57. if (wordsF == NULL) {
  58. cout << "file with words missing!" << endl;
  59. exit(1);
  60. }
  61.  
  62. SetConsoleOutputCP(1251);
  63.  
  64. _getch();
  65.  
  66. cons = GetStdHandle(STD_OUTPUT_HANDLE);
  67.  
  68. PCOORD nc = 0;
  69. SetConsoleDisplayMode(cons, CONSOLE_FULLSCREEN_MODE, nc);
  70. system("mode con lines=54");
  71.  
  72. SetConsoleTitle(L"Crosswords by Oleg Rozdaybeda");
  73.  
  74. //COORD LConSize = GetLargestConsoleWindowSize(cons);
  75. //wcout << LConSize.X << " --- Y: " << LConSize.Y << endl;
  76.  
  77. //wcout << "Hello world!" << endl;
  78.  
  79.  
  80. //_getch();
  81.  
  82.  
  83. fillBaseMap();
  84. drawTable();
  85. //createArray();
  86. drawArray();
  87.  
  88.  
  89.  
  90. printDebugArray();
  91. return 0;
  92. }
  93. const int VERTICAL = 0;
  94. const int HORIZONTAL = 1;
  95. void replaceArrays() {
  96. delete wordsInTable;
  97. wordsInTable = tempWordsInTable;
  98. WITAddPoint = tempWITAddPoint;
  99. tempWordsInTable = new word[100];
  100. tempWITAddPoint = 0;
  101. }
  102. void fillBaseMap() {
  103. randWord();
  104. drawArray();
  105. replaceArrays();
  106.  
  107. int a = 0;
  108. while (a++ < 8) {
  109. for (int b = 0; b < WITAddPoint; b++) {
  110. word current = *(wordsInTable + b);
  111. int newWords = 1 + current.length / (4 + (rand() % 3 - 1));
  112. for (int c = 0; c < newWords;) {
  113. c += genRandWord(current.direction,current.length,current.XStart, current.YStart);
  114.  
  115. }
  116. }
  117. replaceArrays();
  118. }
  119. /*
  120. for (int a = 0; a < 1; ) {
  121. a += randWord();
  122. //drawArray();
  123. //_getch();
  124. }*/
  125. }
  126.  
  127. const int RETURN_DIAPASON_IS_TOO_BIG = 1;
  128. const int RETURN_NO_SUCH_WORD = 2;
  129. const int RETURN_BAD_COORDS = 3;
  130. int genRandWord(int Sdirection, int Slength, int SXstart, int SYstart) {
  131. cout << "Sdirection = " << Sdirection << " Slength = " << Slength << " SXstart = " << SXstart << " SYstart = " << SYstart << endl;
  132. int length = 3 + rand() % 7;
  133. int dir = (Sdirection == HORIZONTAL) ? VERTICAL : HORIZONTAL;
  134. int Xst, Yst;
  135. if (Sdirection == HORIZONTAL) {
  136. Xst = SXstart + rand() % Slength;
  137. Yst = (SYstart - length + 1) + rand() % length;
  138. }
  139. else {
  140. Yst = SYstart + rand() % Slength;
  141. Xst = (SXstart - length + 1) + rand() % length;
  142. }
  143. cout << "direction = " << dir << " length = " << length << " Xst = " << Xst << " Yst = " << Yst << endl;
  144. randWord2(dir, length, Xst, Yst, 0);
  145. }
  146. int randWord2(int dir, int length, int Xst, int Yst, int shift,int Sborder,int Eborder) {
  147.  
  148.  
  149. //TODO проверка на выход за границы таблицы
  150.  
  151. int factorX = (dir == HORIZONTAL) ? 1 : 0;
  152. int factorY = (dir == VERTICAL) ? 1 : 0;
  153. bool flag = false;
  154.  
  155. if (baseMap[Yst + factorY*-1][Xst + factorX*-1].sym != -1)
  156. flag = true;
  157. //return 0; //TODO fault
  158. if (baseMap[Yst + factorY*length][Xst + factorX*length].sym != -1)
  159. flag = true;
  160. //return 0; //TODO fault
  161. cout << "222" << endl;
  162. for (int a = 0; a < length; a++) {
  163. bool isCenter = false, isSide1 = false, isSide2 = false;
  164. if (baseMap[Yst + factorY*a][Xst + factorX*a].sym != -1)
  165. isCenter = true;
  166.  
  167. if (baseMap[Yst + factorY*a + factorX * 1][Xst + factorX*a + factorY * 1].sym != -1)
  168. isSide1 = true;
  169.  
  170. if (baseMap[Yst + factorY*a + factorX * -1][Xst + factorX*a + factorY * -1].sym != -1)
  171. isSide2 = true;
  172. if (!isCenter && (isSide1 || isSide2)) {
  173. cout << "isCenter = "<<isCenter<<" isSide1 = "<< isSide1<< " isSide2 = "<< isSide2<< " a = " << a << endl;
  174. //return 0; //TODO fault
  175. flag = true;
  176. }
  177. }
  178. if (flag) {
  179. int newShift;
  180. if (shift == 0)
  181. newShift = (rand() % 2 == 0) ? 1 : -1;
  182. else
  183. newShift = shift;
  184. randWord2(dir,length,Xst + factorY*newShift,Yst + factorX*newShift,newShift);
  185. //проверка на последнюю клетку в сдвиге и полный выход
  186. }
  187. switch (findWord(length, Yst, Xst, dir)) {
  188. case RETURN_BAD_COORDS:
  189. cout << "333" << endl;
  190. return 0;
  191. break;
  192. case 0:
  193. word *current = tempWordsInTable + tempWITAddPoint++;
  194. current->length = length;
  195. current->direction = dir;
  196. current->XStart = Xst;
  197. current->YStart = Yst;
  198. return 1;
  199. break;
  200. }
  201. }
  202. int randWord() {
  203. int length = 3 + rand() % 7;
  204. int dir = rand() % 2;
  205. int Yst = (dir == VERTICAL) ? rand() % (lengthInCageY - length) : rand() % lengthInCageY;
  206. int Xst = (dir == HORIZONTAL) ? rand() % (lengthInCageX - length) : rand() % lengthInCageX;
  207.  
  208. //cout << "length " << length << " Yst " << Yst << " Xst " << Xst << " dir " << dir << endl;
  209. //cout<< "new word is "<< findWord(length, Yst, Xst, dir)<<endl;
  210. switch (findWord(length, Yst, Xst, dir)) {
  211. case RETURN_BAD_COORDS:
  212. return 0;
  213. case 0:
  214. word *current = tempWordsInTable + tempWITAddPoint++;
  215. current->length = length;
  216. current->direction = dir;
  217. current->XStart = Xst;
  218. current->YStart = Yst;
  219. return 1;
  220. }
  221. }
  222.  
  223. const int basicDiapasone = 1000;
  224.  
  225. int findWord(int length, int Ystart, int Xstart, int direction) {
  226. struct forSym {
  227. char sym;
  228. int location = -1;
  229. };
  230. //////
  231. char *matches = new char[length];
  232.  
  233. for (int a = 0; a < length; matches[a++] = -1);
  234. field tempS;
  235. int diapasoneReduce = 1;
  236.  
  237. for (int a = 0; a < length; a++) {
  238. if (direction == HORIZONTAL)
  239. tempS = baseMap[Ystart][Xstart + a];
  240. else
  241. tempS = baseMap[Ystart + a][Xstart];
  242. if (tempS.sym != -1) {
  243. diapasoneReduce *= 2;
  244. matches[a] = tempS.sym;
  245. //cout << "testPoint1, a = "<< a << " tempS.sym = "<< tempS.sym<< endl;
  246. }
  247. }//for: поиск символов на пути слова
  248.  
  249. if (diapasoneReduce > 8)
  250. return RETURN_DIAPASON_IS_TOO_BIG;
  251.  
  252. int random = rand()%(basicDiapasone/diapasoneReduce);
  253.  
  254. char str[100];
  255. int times = 0;
  256. //string st;
  257. bool flag = true;
  258. for (int a = 0; a < random;) {
  259. if (times++ > 300000) {
  260. delete[]matches;
  261. return RETURN_NO_SUCH_WORD;
  262. }
  263. //cout << "testFor2" << endl;
  264. int fsc = fscanf_s(wordsF, "%s", str,20);
  265. if (fsc == -1)
  266. rewind(wordsF);
  267.  
  268. if (strlen(str) == length) {
  269. for (int b = 0; b < length; b++)
  270. if (matches[b] == -1) {
  271. continue;
  272. //cout << "length is equals" << endl;
  273. }
  274. else
  275. {
  276. if (str[b] != matches[b]) {
  277. flag = false;
  278. //return RETURN_BAD_COORDS;
  279. break;
  280. }
  281. }
  282. if (flag) {
  283. a++;
  284. //cout << "a = " << a << endl;
  285. }
  286. else
  287. flag = true;
  288. if (a == random && flag) {
  289. putWordIntoTable(str,length, Ystart, Xstart, direction);
  290. delete[]matches;
  291. return 0;
  292. }
  293. }
  294. //cout << "a = " << a << endl;
  295. }
  296.  
  297.  
  298. }
  299. void putWordIntoTable(char str[], int length, int Ystart, int Xstart, int direction ) {
  300. for (int a = 0; a < length; a++) {
  301. if (direction == HORIZONTAL) {
  302. baseMap[Ystart][Xstart + a].sym = str[a];
  303. baseMap[Ystart][Xstart + a].horisontal = 0;
  304. }
  305. else {
  306. baseMap[Ystart + a][Xstart].sym = str[a];
  307. baseMap[Ystart][Xstart + a].vertical = 0;
  308. }
  309. }
  310. }
  311. void drawArray() {
  312. COORD xy;
  313. for (int a = 0; a < lengthInCageY; a++) {
  314. for (int b = 0; b < lengthInCageX; b++) {
  315.  
  316. xy.X = (b*4)+2;
  317. xy.Y = (a*2)+1;
  318. SetConsoleCursorPosition(cons, xy);
  319. if (baseMap[a][b].sym != -1)
  320. cout << baseMap[a][b].sym;
  321. }
  322. }
  323.  
  324. }
  325.  
  326. void writeRusText(string text) {
  327. SetConsoleOutputCP(1251);
  328. cout << text << endl;
  329. SetConsoleOutputCP(866);
  330. }
  331. void drawTable() {
  332. system("cls");
  333. SetConsoleOutputCP(866);
  334. for (int i = 0; i < lengthInCageY*(lengthOfCageY + 1) + 1; i++) {
  335. for (int j = 0; j < lengthInCageX*(lengthOfCageX + 1) + 1; j++) {
  336. if (i == 0 && j == 0)
  337. wcout << (char)201;
  338. else
  339. if (i == 0 && j == lengthInCageX*(lengthOfCageX + 1))
  340. wcout << (char)187;
  341. else
  342. if (i == lengthInCageY*(lengthOfCageY + 1) && j == 0)
  343. wcout << (char)200;
  344. else
  345. if (i == lengthInCageY*(lengthOfCageY + 1) && j == lengthInCageX*(lengthOfCageX + 1))
  346. wcout << (char)188;
  347. else
  348. if (i == 0)
  349. if (j % (lengthOfCageX + 1) == 0)
  350. wcout << (char)203;
  351. else
  352. wcout << (char)205;
  353. else
  354. if (i == lengthInCageY*(lengthOfCageY + 1))
  355. if (j % (lengthOfCageX + 1) == 0)
  356. wcout << (char)202;
  357. else
  358. wcout << (char)205;
  359. else
  360. if (j == 0)
  361. if (i % (lengthOfCageY + 1) == 0)
  362. wcout << (char)204;
  363. else
  364. wcout << (char)186;
  365. else
  366. if (j == lengthInCageX*(lengthOfCageX + 1))
  367. if (i % (lengthOfCageY + 1) == 0)
  368. wcout << (char)185;
  369. else
  370. wcout << (char)186;
  371. else
  372. if(i % (lengthOfCageY + 1) == 0 && j % (lengthOfCageX + 1) == 0)
  373. wcout << (char)206;
  374. else
  375. if (i % (lengthOfCageY + 1) == 0)
  376. wcout << (char)205;
  377. else
  378. if (j % (lengthOfCageX + 1) == 0)
  379. wcout << (char)186;
  380. else
  381. wcout << " ";
  382. }
  383.  
  384.  
  385. wcout << endl;
  386.  
  387. }
  388. SetConsoleOutputCP(1251);
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement