Advertisement
the_alator

k

May 15th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 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.  
  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++ < 10) {
  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 += randWord2(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.  
  131. int randWord2(int Sdirection,int Slength,int SXstart,int SYstart) {
  132. cout << "Sdirection = " << Sdirection << " Slength = " << Slength << " SXstart = " << SXstart << " SYstart = " << SYstart << endl;
  133. int length = 3 + rand() % 7;
  134. int dir = (Sdirection == HORIZONTAL)?VERTICAL:HORIZONTAL;
  135. int Xst, Yst;
  136. if (Sdirection == HORIZONTAL) {
  137. Xst = SXstart + rand() % Slength;
  138. Yst = (SYstart - length + 1) + rand() % length;
  139. }
  140. else {
  141. Yst = SYstart + rand() % Slength;
  142. Xst = (SXstart - length + 1) + rand() % length;
  143. }
  144. cout << "direction = " << dir << " length = " << length << " Xst = " << Xst << " Yst = " << Yst << endl;
  145. //TODO проверка на выход за границы таблицы
  146.  
  147. int factorX = (dir == HORIZONTAL) ? 1 : 0;
  148. int factorY = (dir == VERTICAL) ? 1 : 0;
  149.  
  150. if (baseMap[Yst + factorY*-1][Xst + factorX*-1].sym != -1)
  151. return 0; //TODO fault
  152. if (baseMap[Yst + factorY*length][Xst + factorX*length].sym != -1)
  153. return 0; //TODO fault
  154. cout << "222" << endl;
  155. for (int a = 0; a < length; a++) {
  156. bool isCenter = false, isSide1 = false, isSide2 = false;
  157. if (baseMap[Yst + factorY*a][Xst + factorX*a].sym != -1)
  158. isCenter = true;
  159.  
  160. if (baseMap[Yst + factorY*a + factorX * 1][Xst + factorX*a + factorY * 1].sym != -1)
  161. isSide1 = true;
  162.  
  163. if (baseMap[Yst + factorY*a + factorX * -1][Xst + factorX*a + factorY * -1].sym != -1)
  164. isSide2 = true;
  165. if (!isCenter && (isSide1 || isSide2)) {
  166. cout << "isCenter = "<<isCenter<<" isSide1 = "<< isSide1<< " isSide2 = "<< isSide2<< " a = " << a << endl;
  167. return 0; //TODO fault
  168. }
  169. }
  170.  
  171. switch (findWord(length, Yst, Xst, dir)) {
  172. case RETURN_BAD_COORDS:
  173. cout << "333" << endl;
  174. return 0;
  175. break;
  176. case 0:
  177. word *current = tempWordsInTable + tempWITAddPoint++;
  178. current->length = length;
  179. current->direction = dir;
  180. current->XStart = Xst;
  181. current->YStart = Yst;
  182. return 1;
  183. break;
  184. }
  185. }
  186. int randWord() {
  187. int length = 3 + rand() % 7;
  188. int dir = rand() % 2;
  189. int Yst = (dir == VERTICAL) ? rand() % (lengthInCageY - length) : rand() % lengthInCageY;
  190. int Xst = (dir == HORIZONTAL) ? rand() % (lengthInCageX - length) : rand() % lengthInCageX;
  191.  
  192. //cout << "length " << length << " Yst " << Yst << " Xst " << Xst << " dir " << dir << endl;
  193. //cout<< "new word is "<< findWord(length, Yst, Xst, dir)<<endl;
  194. switch (findWord(length, Yst, Xst, dir)) {
  195. case RETURN_BAD_COORDS:
  196. return 0;
  197. case 0:
  198. word *current = tempWordsInTable + tempWITAddPoint++;
  199. current->length = length;
  200. current->direction = dir;
  201. current->XStart = Xst;
  202. current->YStart = Yst;
  203. return 1;
  204. }
  205. }
  206.  
  207. const int basicDiapasone = 1000;
  208.  
  209. int findWord(int length, int Ystart, int Xstart, int direction) {
  210. struct forSym {
  211. char sym;
  212. int location = -1;
  213. };
  214. //////
  215. char *matches = new char[length];
  216.  
  217. for (int a = 0; a < length; matches[a++] = -1);
  218. field tempS;
  219. int diapasoneReduce = 1;
  220.  
  221. for (int a = 0; a < length; a++) {
  222. if (direction == HORIZONTAL)
  223. tempS = baseMap[Ystart][Xstart + a];
  224. else
  225. tempS = baseMap[Ystart + a][Xstart];
  226. if (tempS.sym != -1) {
  227. diapasoneReduce *= 2;
  228. matches[a] = tempS.sym;
  229. //cout << "testPoint1, a = "<< a << " tempS.sym = "<< tempS.sym<< endl;
  230. }
  231. }//for: поиск символов на пути слова
  232.  
  233. if (diapasoneReduce > 8)
  234. return RETURN_DIAPASON_IS_TOO_BIG;
  235.  
  236. int random = rand()%(basicDiapasone/diapasoneReduce);
  237.  
  238. char str[100];
  239. int times = 0;
  240. //string st;
  241. bool flag = true;
  242. for (int a = 0; a < random;) {
  243. if (times++ > 300000) {
  244. delete[]matches;
  245. return RETURN_NO_SUCH_WORD;
  246. }
  247. //cout << "testFor2" << endl;
  248. int fsc = fscanf_s(wordsF, "%s", str,20);
  249. if (fsc == -1)
  250. rewind(wordsF);
  251.  
  252. if (strlen(str) == length) {
  253. for (int b = 0; b < length; b++)
  254. if (matches[b] == -1) {
  255. continue;
  256. //cout << "length is equals" << endl;
  257. }
  258. else
  259. {
  260. if (str[b] != matches[b]) {
  261. flag = false;
  262. //return RETURN_BAD_COORDS;
  263. break;
  264. }
  265. }
  266. if (flag) {
  267. a++;
  268. //cout << "a = " << a << endl;
  269. }
  270. else
  271. flag = true;
  272. if (a == random && flag) {
  273. putWordIntoTable(str,length, Ystart, Xstart, direction);
  274. delete[]matches;
  275. return 0;
  276. }
  277. }
  278. //cout << "a = " << a << endl;
  279. }
  280.  
  281.  
  282. }
  283. void putWordIntoTable(char str[], int length, int Ystart, int Xstart, int direction ) {
  284. for (int a = 0; a < length; a++) {
  285. if (direction == HORIZONTAL) {
  286. baseMap[Ystart][Xstart + a].sym = str[a];
  287. baseMap[Ystart][Xstart + a].horisontal = 0;
  288. }
  289. else {
  290. baseMap[Ystart + a][Xstart].sym = str[a];
  291. baseMap[Ystart][Xstart + a].vertical = 0;
  292. }
  293. }
  294. }
  295. void drawArray() {
  296. COORD xy;
  297. for (int a = 0; a < lengthInCageY; a++) {
  298. for (int b = 0; b < lengthInCageX; b++) {
  299.  
  300. xy.X = (b*4)+2;
  301. xy.Y = (a*2)+1;
  302. SetConsoleCursorPosition(cons, xy);
  303. if (baseMap[a][b].sym != -1)
  304. cout << baseMap[a][b].sym;
  305. }
  306. }
  307.  
  308. }
  309.  
  310. void writeRusText(string text) {
  311. SetConsoleOutputCP(1251);
  312. cout << text << endl;
  313. SetConsoleOutputCP(866);
  314. }
  315. void drawTable() {
  316. system("cls");
  317. SetConsoleOutputCP(866);
  318. for (int i = 0; i < lengthInCageY*(lengthOfCageY + 1) + 1; i++) {
  319. for (int j = 0; j < lengthInCageX*(lengthOfCageX + 1) + 1; j++) {
  320. if (i == 0 && j == 0)
  321. wcout << (char)201;
  322. else
  323. if (i == 0 && j == lengthInCageX*(lengthOfCageX + 1))
  324. wcout << (char)187;
  325. else
  326. if (i == lengthInCageY*(lengthOfCageY + 1) && j == 0)
  327. wcout << (char)200;
  328. else
  329. if (i == lengthInCageY*(lengthOfCageY + 1) && j == lengthInCageX*(lengthOfCageX + 1))
  330. wcout << (char)188;
  331. else
  332. if (i == 0)
  333. if (j % (lengthOfCageX + 1) == 0)
  334. wcout << (char)203;
  335. else
  336. wcout << (char)205;
  337. else
  338. if (i == lengthInCageY*(lengthOfCageY + 1))
  339. if (j % (lengthOfCageX + 1) == 0)
  340. wcout << (char)202;
  341. else
  342. wcout << (char)205;
  343. else
  344. if (j == 0)
  345. if (i % (lengthOfCageY + 1) == 0)
  346. wcout << (char)204;
  347. else
  348. wcout << (char)186;
  349. else
  350. if (j == lengthInCageX*(lengthOfCageX + 1))
  351. if (i % (lengthOfCageY + 1) == 0)
  352. wcout << (char)185;
  353. else
  354. wcout << (char)186;
  355. else
  356. if(i % (lengthOfCageY + 1) == 0 && j % (lengthOfCageX + 1) == 0)
  357. wcout << (char)206;
  358. else
  359. if (i % (lengthOfCageY + 1) == 0)
  360. wcout << (char)205;
  361. else
  362. if (j % (lengthOfCageX + 1) == 0)
  363. wcout << (char)186;
  364. else
  365. wcout << " ";
  366. }
  367.  
  368.  
  369. wcout << endl;
  370.  
  371. }
  372. SetConsoleOutputCP(1251);
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement