Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication6.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- #include <fcntl.h>
- #include <io.h>
- #include <string>
- #include <ctime>
- #include "helpfulFunctions.h"
- const int lengthInCageX = 25;
- const int lengthInCageY = 25;
- const int lengthOfCageX = 3;
- const int lengthOfCageY = 1;
- using namespace std;
- struct field {
- int horisontal = -1;
- int vertical = -1;
- char sym = -1;
- };
- struct word {
- int XStart, YStart, length = -1, direction;
- };
- void drawTable();
- void fillBaseMap();
- void drawArray();
- void writeRusText(string text);
- int findWord(int length, int Ystart, int Xstart, int direction);
- void putWordIntoTable(char str[], int length, int Ystart, int Xstart, int direction);
- int randWord();
- int randWord2(int Sdirection, int Slength, int SXstart, int SYstart);
- void replaceArrays();
- HANDLE cons;
- COORD cursPos;
- field baseMap[lengthInCageY][lengthInCageX];
- string words[100];
- string text[100];
- word *wordsInTable = new word[100];
- word *tempWordsInTable = new word[100];
- int WITAddPoint = 0;
- int tempWITAddPoint = 0;
- FILE *wordsF;
- int main()
- {
- srand(time(0));
- fopen_s(&wordsF, "D:/Файлы/Временные/Новая папка (5)/CrossWord.txt", "r");
- if (wordsF == NULL) {
- cout << "file with words missing!" << endl;
- exit(1);
- }
- SetConsoleOutputCP(1251);
- _getch();
- cons = GetStdHandle(STD_OUTPUT_HANDLE);
- PCOORD nc = 0;
- SetConsoleDisplayMode(cons, CONSOLE_FULLSCREEN_MODE, nc);
- system("mode con lines=54");
- SetConsoleTitle(L"Crosswords by Oleg Rozdaybeda");
- //COORD LConSize = GetLargestConsoleWindowSize(cons);
- //wcout << LConSize.X << " --- Y: " << LConSize.Y << endl;
- //wcout << "Hello world!" << endl;
- //_getch();
- fillBaseMap();
- drawTable();
- //createArray();
- drawArray();
- printDebugArray();
- return 0;
- }
- const int VERTICAL = 0;
- const int HORIZONTAL = 1;
- void replaceArrays() {
- delete wordsInTable;
- wordsInTable = tempWordsInTable;
- WITAddPoint = tempWITAddPoint;
- tempWordsInTable = new word[100];
- tempWITAddPoint = 0;
- }
- void fillBaseMap() {
- randWord();
- drawArray();
- replaceArrays();
- int a = 0;
- while (a++ < 10) {
- for (int b = 0; b < WITAddPoint; b++) {
- word current = *(wordsInTable + b);
- int newWords = 1 + current.length / (4 + (rand() % 3 - 1));
- for (int c = 0; c < newWords;) {
- c += randWord2(current.direction,current.length,current.XStart, current.YStart);
- }
- }
- replaceArrays();
- }
- /*
- for (int a = 0; a < 1; ) {
- a += randWord();
- //drawArray();
- //_getch();
- }*/
- }
- const int RETURN_DIAPASON_IS_TOO_BIG = 1;
- const int RETURN_NO_SUCH_WORD = 2;
- const int RETURN_BAD_COORDS = 3;
- int randWord2(int Sdirection,int Slength,int SXstart,int SYstart) {
- cout << "Sdirection = " << Sdirection << " Slength = " << Slength << " SXstart = " << SXstart << " SYstart = " << SYstart << endl;
- int length = 3 + rand() % 7;
- int dir = (Sdirection == HORIZONTAL)?VERTICAL:HORIZONTAL;
- int Xst, Yst;
- if (Sdirection == HORIZONTAL) {
- Xst = SXstart + rand() % Slength;
- Yst = (SYstart - length + 1) + rand() % length;
- }
- else {
- Yst = SYstart + rand() % Slength;
- Xst = (SXstart - length + 1) + rand() % length;
- }
- cout << "direction = " << dir << " length = " << length << " Xst = " << Xst << " Yst = " << Yst << endl;
- //TODO проверка на выход за границы таблицы
- int factorX = (dir == HORIZONTAL) ? 1 : 0;
- int factorY = (dir == VERTICAL) ? 1 : 0;
- if (baseMap[Yst + factorY*-1][Xst + factorX*-1].sym != -1)
- return 0; //TODO fault
- if (baseMap[Yst + factorY*length][Xst + factorX*length].sym != -1)
- return 0; //TODO fault
- cout << "222" << endl;
- for (int a = 0; a < length; a++) {
- bool isCenter = false, isSide1 = false, isSide2 = false;
- if (baseMap[Yst + factorY*a][Xst + factorX*a].sym != -1)
- isCenter = true;
- if (baseMap[Yst + factorY*a + factorX * 1][Xst + factorX*a + factorY * 1].sym != -1)
- isSide1 = true;
- if (baseMap[Yst + factorY*a + factorX * -1][Xst + factorX*a + factorY * -1].sym != -1)
- isSide2 = true;
- if (!isCenter && (isSide1 || isSide2)) {
- cout << "isCenter = "<<isCenter<<" isSide1 = "<< isSide1<< " isSide2 = "<< isSide2<< " a = " << a << endl;
- return 0; //TODO fault
- }
- }
- switch (findWord(length, Yst, Xst, dir)) {
- case RETURN_BAD_COORDS:
- cout << "333" << endl;
- return 0;
- break;
- case 0:
- word *current = tempWordsInTable + tempWITAddPoint++;
- current->length = length;
- current->direction = dir;
- current->XStart = Xst;
- current->YStart = Yst;
- return 1;
- break;
- }
- }
- int randWord() {
- int length = 3 + rand() % 7;
- int dir = rand() % 2;
- int Yst = (dir == VERTICAL) ? rand() % (lengthInCageY - length) : rand() % lengthInCageY;
- int Xst = (dir == HORIZONTAL) ? rand() % (lengthInCageX - length) : rand() % lengthInCageX;
- //cout << "length " << length << " Yst " << Yst << " Xst " << Xst << " dir " << dir << endl;
- //cout<< "new word is "<< findWord(length, Yst, Xst, dir)<<endl;
- switch (findWord(length, Yst, Xst, dir)) {
- case RETURN_BAD_COORDS:
- return 0;
- case 0:
- word *current = tempWordsInTable + tempWITAddPoint++;
- current->length = length;
- current->direction = dir;
- current->XStart = Xst;
- current->YStart = Yst;
- return 1;
- }
- }
- const int basicDiapasone = 1000;
- int findWord(int length, int Ystart, int Xstart, int direction) {
- struct forSym {
- char sym;
- int location = -1;
- };
- //////
- char *matches = new char[length];
- for (int a = 0; a < length; matches[a++] = -1);
- field tempS;
- int diapasoneReduce = 1;
- for (int a = 0; a < length; a++) {
- if (direction == HORIZONTAL)
- tempS = baseMap[Ystart][Xstart + a];
- else
- tempS = baseMap[Ystart + a][Xstart];
- if (tempS.sym != -1) {
- diapasoneReduce *= 2;
- matches[a] = tempS.sym;
- //cout << "testPoint1, a = "<< a << " tempS.sym = "<< tempS.sym<< endl;
- }
- }//for: поиск символов на пути слова
- if (diapasoneReduce > 8)
- return RETURN_DIAPASON_IS_TOO_BIG;
- int random = rand()%(basicDiapasone/diapasoneReduce);
- char str[100];
- int times = 0;
- //string st;
- bool flag = true;
- for (int a = 0; a < random;) {
- if (times++ > 300000) {
- delete[]matches;
- return RETURN_NO_SUCH_WORD;
- }
- //cout << "testFor2" << endl;
- int fsc = fscanf_s(wordsF, "%s", str,20);
- if (fsc == -1)
- rewind(wordsF);
- if (strlen(str) == length) {
- for (int b = 0; b < length; b++)
- if (matches[b] == -1) {
- continue;
- //cout << "length is equals" << endl;
- }
- else
- {
- if (str[b] != matches[b]) {
- flag = false;
- //return RETURN_BAD_COORDS;
- break;
- }
- }
- if (flag) {
- a++;
- //cout << "a = " << a << endl;
- }
- else
- flag = true;
- if (a == random && flag) {
- putWordIntoTable(str,length, Ystart, Xstart, direction);
- delete[]matches;
- return 0;
- }
- }
- //cout << "a = " << a << endl;
- }
- }
- void putWordIntoTable(char str[], int length, int Ystart, int Xstart, int direction ) {
- for (int a = 0; a < length; a++) {
- if (direction == HORIZONTAL) {
- baseMap[Ystart][Xstart + a].sym = str[a];
- baseMap[Ystart][Xstart + a].horisontal = 0;
- }
- else {
- baseMap[Ystart + a][Xstart].sym = str[a];
- baseMap[Ystart][Xstart + a].vertical = 0;
- }
- }
- }
- void drawArray() {
- COORD xy;
- for (int a = 0; a < lengthInCageY; a++) {
- for (int b = 0; b < lengthInCageX; b++) {
- xy.X = (b*4)+2;
- xy.Y = (a*2)+1;
- SetConsoleCursorPosition(cons, xy);
- if (baseMap[a][b].sym != -1)
- cout << baseMap[a][b].sym;
- }
- }
- }
- void writeRusText(string text) {
- SetConsoleOutputCP(1251);
- cout << text << endl;
- SetConsoleOutputCP(866);
- }
- void drawTable() {
- system("cls");
- SetConsoleOutputCP(866);
- for (int i = 0; i < lengthInCageY*(lengthOfCageY + 1) + 1; i++) {
- for (int j = 0; j < lengthInCageX*(lengthOfCageX + 1) + 1; j++) {
- if (i == 0 && j == 0)
- wcout << (char)201;
- else
- if (i == 0 && j == lengthInCageX*(lengthOfCageX + 1))
- wcout << (char)187;
- else
- if (i == lengthInCageY*(lengthOfCageY + 1) && j == 0)
- wcout << (char)200;
- else
- if (i == lengthInCageY*(lengthOfCageY + 1) && j == lengthInCageX*(lengthOfCageX + 1))
- wcout << (char)188;
- else
- if (i == 0)
- if (j % (lengthOfCageX + 1) == 0)
- wcout << (char)203;
- else
- wcout << (char)205;
- else
- if (i == lengthInCageY*(lengthOfCageY + 1))
- if (j % (lengthOfCageX + 1) == 0)
- wcout << (char)202;
- else
- wcout << (char)205;
- else
- if (j == 0)
- if (i % (lengthOfCageY + 1) == 0)
- wcout << (char)204;
- else
- wcout << (char)186;
- else
- if (j == lengthInCageX*(lengthOfCageX + 1))
- if (i % (lengthOfCageY + 1) == 0)
- wcout << (char)185;
- else
- wcout << (char)186;
- else
- if(i % (lengthOfCageY + 1) == 0 && j % (lengthOfCageX + 1) == 0)
- wcout << (char)206;
- else
- if (i % (lengthOfCageY + 1) == 0)
- wcout << (char)205;
- else
- if (j % (lengthOfCageX + 1) == 0)
- wcout << (char)186;
- else
- wcout << " ";
- }
- wcout << endl;
- }
- SetConsoleOutputCP(1251);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement