Advertisement
the_alator

Untitled

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