Sapper
/
main.c
1516 строк · 66.9 Кб
1/* С версия */
2#include <locale.h>3#include <stdarg.h>4#include <stdbool.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <time.h>9
10bool victory = false;11
12struct result13{
14char* name;15unsigned short games;16unsigned short victories;17double percent;18unsigned short izzy;/*успешно пройденный просто*/19unsigned short hard;/*успешно пройденный не просто*/20unsigned short doom_guy;/*успешно пройденный ХАРДКОР*/21};22int scanf_rng(const char* const _Format, ...)23{
24int _Result;25va_list _ArgList;26va_start(_ArgList, _Format);27_Result = vfscanf(stdin, _Format, _ArgList);28va_end(_ArgList);29scanf("%*[^\n]");30fflush(stdin);31return _Result;32}
33int compare_games(const void* a, const void* b)34{
35const struct result* k = (const struct result*)a;36const struct result* l = (const struct result*)b;37return ((k->games) - (l->games));38}
39int compare_izzy(const void* a, const void* b)40{
41const struct result* k = (const struct result*)a;42const struct result* l = (const struct result*)b;43return ((k->izzy) - (l->izzy));44}
45int compare_hard(const void* a, const void* b)46{
47const struct result* k = (const struct result*)a;48const struct result* l = (const struct result*)b;49return ((k->hard) - (l->hard));50}
51int compare_doom(const void* a, const void* b)52{
53const struct result* k = (const struct result*)a;54const struct result* l = (const struct result*)b;55return ((k->doom_guy) - (l->doom_guy));56}
57int compare_percent(const void* a, const void* b)58{
59const struct result* k = (const struct result*)a;60const struct result* l = (const struct result*)b;61return ((k->percent) - (l->percent));62}
63void save(struct result* save, const unsigned short f)64{
65FILE* fout = fopen("results.bin", "wbe");66for (unsigned short q = 0; q < f; q++)67{68const unsigned short sz = strlen(save[q].name);69fwrite(&sz, sizeof(unsigned short), 1, fout);70fwrite(save[q].name, sizeof(char), sz, fout);71fwrite(&save[q].games, sizeof(unsigned short), 1, fout);72fwrite(&save[q].victories, sizeof(unsigned short), 1, fout);73fwrite(&save[q].percent, sizeof(double), 1, fout);74fwrite(&save[q].izzy, sizeof(unsigned short), 1, fout);75fwrite(&save[q].hard, sizeof(unsigned short), 1, fout);76fwrite(&save[q].doom_guy, sizeof(unsigned short), 1, fout);77}78fclose(fout);79}
80void printdesk(char** deskgr, const unsigned short x, const unsigned short y)81{
82printf(" ");83for (unsigned short i = 1; i < y + 1; i++)84printf(" %d", i % 10);85printf("\n");86for (unsigned short i = 0; i < x; i++)87{88printf(" ");89for (unsigned short q = 1; q < y + 1; q++)90printf(" _");91printf("\n");92printf("%d ", (i + 1) % 10);93for (unsigned short q = 0; q < y; q++)94printf("|%c", deskgr[i][q]);95printf("|\n");96}97printf(" ");98for (unsigned short q = 1; q < y + 1; q++)99printf(" _");100printf("\n");101}
102char opencl(const unsigned short num)103{
104switch (num)105{106case 0:107return 48;108break;109case 1:110return 49;111break;112case 2:113return 50;114break;115case 3:116return 51;117break;118case 4:119return 52;120break;121case 5:122return 53;123break;124case 6:125return 54;126break;127case 7:128return 55;129break;130default:131return 56;132break;133}134}
135short to_hu(char* XM)136{
137unsigned short xm = 0;138bool correct = true;139if (XM[1] == 49 || XM[1] == 50 || XM[1] == 51 || XM[1] == 52 || XM[1] == 53 || XM[1] == 54 || XM[1] == 55 || XM[1] == 56 || XM[1] == 57 || XM[1] == 58)140{141switch (XM[0])142{143case 49:144xm = 10;145break;146case 50:147xm = 20;148break;149case 51:150xm = 30;151break;152default:153printf("Ввод не корректен.\n");154correct = false;155break;156}157if (correct)158{159switch (XM[1])160{161case 48:162break;163case 49:164xm++;165break;166case 50:167xm += 2;168break;169case 51:170xm += 3;171break;172case 52:173xm += 4;174break;175case 53:176xm += 5;177break;178case 54:179xm += 6;180break;181case 55:182xm += 7;183break;184case 56:185xm += 8;186break;187case 57:188xm += 9;189break;190default:191printf("Ввод не корректен.\n");192correct = false;193break;194}195}196if (correct && XM[2] != 0)197{198printf("Ввод не корректен.\n");199correct = false;200}201}202else if (XM[1] == 0)203{204switch (*XM)205{206case 48:207break;208case 49:209xm++;210break;211case 50:212xm += 2;213break;214case 51:215xm += 3;216break;217case 52:218xm += 4;219break;220case 53:221xm += 5;222break;223case 54:224xm += 6;225break;226case 55:227xm += 7;228break;229case 56:230xm += 8;231break;232case 57:233xm += 9;234break;235default:236printf("Ввод не корректен.\n");237correct = false;238break;239}240}241else242{243printf("Ввод не корректен.\n");244correct = false;245}246return correct ? xm : -1;247}
248void game(struct result* p, struct result player, const unsigned short type)249{
250unsigned short x = 0, y = 0;/*custom*/251unsigned short mines = 0;252char** deskgr = 0;253unsigned short** bnum = 0;254unsigned short count = 0;/*кол-во мин*/255unsigned short* position = 0;256if (type != 1)257{258deskgr = (char**)malloc(type * sizeof(char*));259for (unsigned short i = 0; i < type; i++)260deskgr[i] = (char*)malloc(type * sizeof(char));261for (unsigned short i = 0; i < type; i++)262for (unsigned short q = 0; q < type; q++)263deskgr[i][q] = 32;264/*то, что отображается*/265bnum = (unsigned short**)malloc(type * sizeof(unsigned short*));266for (unsigned short i = 0; i < type; i++)267bnum[i] = (unsigned short*)malloc(type * sizeof(unsigned short));268for (unsigned short i = 0; i < type; i++)269for (unsigned short q = 0; q < type; q++)270bnum[i][q] = 0;271/*номера(рядом с минами)*/272switch (type)273{274case 8:275printdesk(deskgr, 8, 8);276break;277case 16:278printdesk(deskgr, 16, 16);279break;280default:281printdesk(deskgr, 32, 32);282break;283}284srand(time(NULL));285count = 0;286position = (unsigned short*)malloc(type * 2 * sizeof(unsigned short));/*номера мин*/287while (count != type * 2)288{289bool test = false;290unsigned short place = rand() % ((type * type - 1) - 0 + 1) + 0; /*позиция мины*/291for (unsigned short i = 0; i < count; i++)292{293if (place == position[i])294{295test = true;296break;297}298}299if (!test)300{301count++;302position[count - 1] = place;303}304}/*создали мины*/305unsigned short xc = 0, yc = 0;306for (unsigned short q = 0; q < count; q++)307{308xc = position[q] / type;/*строка с миной*/309yc = position[q] - (type * xc);/*столбец с миной*/310/*определили положение мины*/311if (xc > 0)312bnum[xc - 1][yc]++;313if (xc < type - 1)314bnum[xc + 1][yc]++;315if (yc > 0)316bnum[xc][yc - 1]++;317if (yc < type - 1)318bnum[xc][yc + 1]++;319if (xc > 0 && yc > 0)320bnum[xc - 1][yc - 1]++;321if (xc < type - 1 && yc > 0)322bnum[xc + 1][yc - 1]++;323if (xc > 0 && yc < type - 1)324bnum[xc - 1][yc + 1]++;325if (xc < type - 1 && yc < type - 1)326bnum[xc + 1][yc + 1]++;327}/*Заполнили номерами для мин*/328}329else330{331char* X = (char*)malloc(0);332char* Y = (char*)malloc(0);333printf("Задайте размер доски.\n");334do335{336printf("Кол-во строк(не больше 50, минимум 3): ");337scanf_rng("%3s", X);338x = atoi(X);339if (x < 3 || x > 50)340printf("Ввод не корректен\n");341}342while (x < 3 || x > 50);343do344{345printf("Кол-во столбцов(не больше 50, минимум 3): ");346scanf_rng("%3s", Y);347y = atoi(Y);348if (y < 3 || y > 50)349printf("Ввод не корректен\n");350}351while (y < 3 || y > 50);352deskgr = (char**)malloc(x * sizeof(char*));353for (unsigned short i = 0; i < x; i++)354deskgr[i] = (char*)malloc(y * sizeof(char));355for (unsigned short i = 0; i < x; i++)356for (unsigned short q = 0; q < y; q++)357deskgr[i][q] = 32;358/*то, что отображается*/359bnum = (unsigned short**)malloc(x * sizeof(unsigned short*));360for (unsigned short i = 0; i < x; i++)361bnum[i] = (unsigned short*)malloc(y * sizeof(unsigned short));362for (unsigned short i = 0; i < x; i++)363for (unsigned short q = 0; q < y; q++)364bnum[i][q] = 0;365/*номера(рядом с минами)*/366printdesk(deskgr, x, y);367do368{369mines = 0;370char* MINES = (char*)malloc(0);371printf("Задайте кол-во мин (минимум 5%% от площади доски и максимум 95%%): ");372scanf_rng("%4s", MINES);373mines = atoi(MINES);374if (mines < (x * y) / 100.0 * 5 || mines >(x * y) / 100.0 * 95)375printf("Ввод не корректен\n");376}377while (mines < (x * y) / 100.0 * 5 || mines >(x * y) / 100.0 * 95);378srand(time(NULL));379count = 0;380position = (unsigned short*)malloc(mines * sizeof(unsigned short));/*номера мин*/381while (count != mines)382{383bool test = false;384unsigned short place = rand() % ((x * y - 1) - 0 + 1) + 0; /*позиция мины*/385for (unsigned short i = 0; i < count; i++)386{387if (place == position[i])388{389test = true;390break;391}392}393if (!test)394{395count++;396position[count - 1] = place;397}398} /*создали мины*/399unsigned short xc = 0, yc = 0;400for (unsigned short q = 0; q < count; q++)401{402xc = position[q] / y;/*строка с миной*/403yc = position[q] - y * xc;/*столбец с миной*/404/*определили положение мины*/405if (xc > 0)406bnum[xc - 1][yc]++;407if (xc < x - 1)408bnum[xc + 1][yc]++;409if (yc > 0)410bnum[xc][yc - 1]++;411if (yc < y - 1)412bnum[xc][yc + 1]++;413if (xc > 0 && yc > 0)414bnum[xc - 1][yc - 1]++;415if (xc < x - 1 && yc > 0)416bnum[xc + 1][yc - 1]++;417if (xc > 0 && yc < y - 1)418bnum[xc - 1][yc + 1]++;419if (xc < x - 1 && yc < y - 1)420bnum[xc + 1][yc + 1]++;421}/*Заполнили номерами для мин*/422}423unsigned short xh = 0, yh = 0, mch = 0, mv = 0;424bool lose = false;425unsigned short space = type != 1 ? type * type - type * 2 : x * y - mines;/*не мины*/;426char* MV = (char*)malloc(0);427char* YESf = (char*)malloc(0);428char* YESd = (char*)malloc(0);429char* YESm = (char*)malloc(0);430char* XM = (char*)malloc(0);431char* YM = (char*)malloc(0);432char* XD = (char*)malloc(0);433char* YD = (char*)malloc(0);434char* XH = (char*)malloc(0);435char* YH = (char*)malloc(0);436time_t start = time(NULL);437do438{439do440{441printf("1. Поставить метку\n2. Убрать метку\n3. Сделать ход\n");442printf("Выберите ЦИФРУ: ");443scanf_rng("%2s", MV);444if (*MV != 49 && *MV != 50 && *MV != 51 || MV[1] != 0)445printf("Ввод не коректен\n");446}447while (*MV != 49 && *MV != 50 && *MV != 51 || MV[1] != 0);448switch (*MV)449{450case 49:451mv = 1;452break;453case 50:454mv = 2;455break;456default:457mv = 3;458break;459}460switch (mv)461{462case 1:463do464{465printf("Если у вас есть предположение о месте мин, то можете поставить + на это место. Если хотите это сделать, то введите YES, иначе что-то другое: ");466scanf_rng("%4s", YESf);467if (strcmp(YESf, "YES") == 0)468{469unsigned short xm = 0, ym = 0;/*координаты мин*/470short xt = 0, yt = 0;/*тест*/471printf("Введите координаты мины.\n");472do473{474xm = 0;475printf("Введите номер строки: ");476scanf_rng("%3s", XM);477xt = to_hu(XM);478}479while (xt == -1);480xm = xt;481do482{483ym = 0;484printf("Введите номер столбца: ");485scanf_rng("%3s", YM);486yt = to_hu(YM);487}488while (yt == -1);489ym = yt;490if (type != 1)491{492if ((xm > type || xm < 1) && (ym > type || ym < 1))493{494printf("Вы вышли за рамки доски\n");495break;496}497}498else if ((xm > x || xm < 1) && (ym > y || ym < 1))499{500printf("Вы вышли за рамки доски\n");501break;502}503if (deskgr[xm - 1][ym - 1] != 32)504{505printf("Сомневаюсь, что вы хотите поставить сюда метку...\n");506break;507}508deskgr[xm - 1][ym - 1] = 43;509switch (type)510{511case 8:512printdesk(deskgr, 8, 8);513break;514case 16:515printdesk(deskgr, 16, 16);516break;517case 32:518printdesk(deskgr, 32, 32);519break;520default:521printdesk(deskgr, x, y);522break;523}524}525}526while (strcmp(YESf, "YES") == 0);527break;528case 2:529do530{531printf("Вы можете убрать метку на мину. Если хотите это сделать, то введите YES, иначе что-то другое: ");532scanf_rng("%4s", YESd);533if (strcmp(YESd, "YES") == 0)534{535unsigned short xm = 0, ym = 0;/*координаты мин*/536short xt = 0, yt = 0;/*тест*/537printf("Введите координаты мины.\n");538do539{540xm = 0;541printf("Введите номер строки: ");542scanf_rng("%3s", XD);543xt = to_hu(XD);544}545while (xt == -1);546xm = xt;547do548{549ym = 0;550printf("Введите номер столбца: ");551scanf_rng("%3s", YD);552yt = to_hu(YD);553}554while (yt == -1);555ym = yt;556if (type != 1)557{558if ((xm > type || xm < 1) && (ym > type || ym < 1))559{560printf("Вы вышли за рамки доски\n");561break;562}563}564else if ((xm > x || xm < 1) && (ym > y || ym < 1))565{566printf("Вы вышли за рамки доски\n");567break;568}569if (deskgr[xm - 1][ym - 1] != 43)570{571printf("Здесь нет метки...\n");572break;573}574deskgr[xm - 1][ym - 1] = 32;575switch (type)576{577case 8:578printdesk(deskgr, 8, 8);579break;580case 16:581printdesk(deskgr, 16, 16);582break;583case 32:584printdesk(deskgr, 32, 32);585break;586default:587printdesk(deskgr, x, y);588break;589}590}591}592while (strcmp(YESd, "YES") == 0);593break;594default:595printf("Вы уверены, что хотите совершить ход? Если да, то введите YES, иначе что-то другое: ");596scanf_rng("%4s", YESm);597if (strcmp(YESm, "YES") != 0)598break;599short xt = 0, yt = 0;/*тест*/600bool beyond = false;601do602{603printf("Введите координаты мины\n");604do605{606xh = 0;607printf("Введите номер строки: ");608scanf_rng("%3s", XH);609xt = to_hu(XH);610}611while (xt == -1);612xh = xt;613do614{615yh = 0;616printf("Введите номер столбца: ");617scanf_rng("%3s", YH);618yt = to_hu(YH);619}620while (yt == -1);621yh = yt;622beyond = false;623if (type != 1)624{625if ((xh > type || xh < 1) && (yh > type || yh < 1))626{627printf("Вы вышли за рамки доски\n");628beyond = true;629}630}631else if ((xh > x || xh < 1) && (yh > y || yh < 1))632{633printf("Вы вышли за рамки доски\n");634beyond = true;635}636}637while (beyond);638for (unsigned short c = 0; c < count; c++)639{640if (type != 1)641{642if ((xh - 1) * type + yh - 1 == position[c]) /*просрали*/643{644for (unsigned short o = 0; o < count; o++)645deskgr[position[o] / type][position[o] - (position[o] / type) * type] = 120;646switch (type)647{648case 8:649printdesk(deskgr, 8, 8);650break;651case 16:652printdesk(deskgr, 16, 16);653break;654default:655printdesk(deskgr, 32, 32);656break;657}658printf("Вы взорвались :(\n");659lose = true;660break;661}662}663else if ((xh - 1) * y + yh - 1 == position[c]) /*просрали*/664{665for (unsigned short o = 0; o < count; o++)666deskgr[position[o] / y][position[o] - (position[o] / y) * y] = 120;667printdesk(deskgr, x, y);668printf("Вы взорвались :(\n");669lose = true;670break;671}672}673if (!lose && deskgr[xh - 1][yh - 1] != 32)674printf("Эта клетка уже открыта\n");675else if (!lose && deskgr[xh - 1][yh - 1] == 32) /*не просрали*/676{677unsigned short i = 0;678unsigned short amount = 0;679if (bnum[xh - 1][yh - 1] == 0) /*открытие клеток*/680{681unsigned short* eternal_x = (unsigned short*)malloc(1 * sizeof(unsigned short));/*строки 0*/682*eternal_x = xh - 1;683unsigned short* eternal_y = (unsigned short*)malloc(1 * sizeof(unsigned short));/*столбцы 0*/684*eternal_y = yh - 1;685unsigned short eternal_size = 1;686for (i = 0; i < eternal_size; i++)687{688deskgr[eternal_x[i]][eternal_y[i]] = 48;689amount++;690if (eternal_x[i] > 0)691{692if (bnum[eternal_x[i] - 1][eternal_y[i]] == 0)693{694bool potato = false;695for (unsigned short c = 0; c < eternal_size; c++)696{697if (eternal_x[c] == eternal_x[i] - 1 && eternal_y[c] == eternal_y[i])698{699potato = true;700break;701}702}703if (!potato)704{705eternal_size++;706eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));707eternal_x[eternal_size - 1] = eternal_x[i] - 1;708eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));709eternal_y[eternal_size - 1] = eternal_y[i];710}711}712else if (deskgr[eternal_x[i] - 1][eternal_y[i]] == 32)713{714deskgr[eternal_x[i] - 1][eternal_y[i]] = opencl(bnum[eternal_x[i] - 1][eternal_y[i]]);715amount++;716}717}718if (type != 1)719{720if (eternal_x[i] < type - 1)721{722if (bnum[eternal_x[i] + 1][eternal_y[i]] == 0)723{724bool potato = false;725for (unsigned short c = 0; c < eternal_size; c++)726{727if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i])728{729potato = true;730break;731}732}733if (!potato)734{735eternal_size++;736eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));737eternal_x[eternal_size - 1] = eternal_x[i] + 1;738eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));739eternal_y[eternal_size - 1] = eternal_y[i];740}741}742else if (deskgr[eternal_x[i] + 1][eternal_y[i]] == 32)743{744deskgr[eternal_x[i] + 1][eternal_y[i]] = opencl(bnum[eternal_x[i] + 1][eternal_y[i]]);745amount++;746}747}748}749else750{751if (eternal_x[i] < x - 1)752{753if (bnum[eternal_x[i] + 1][eternal_y[i]] == 0)754{755bool potato = false;756for (unsigned short c = 0; c < eternal_size; c++)757{758if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i])759{760potato = true;761break;762}763}764if (!potato)765{766eternal_size++;767eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));768eternal_x[eternal_size - 1] = eternal_x[i] + 1;769eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));770eternal_y[eternal_size - 1] = eternal_y[i];771}772}773else if (deskgr[eternal_x[i] + 1][eternal_y[i]] == 32)774{775deskgr[eternal_x[i] + 1][eternal_y[i]] = opencl(bnum[eternal_x[i] + 1][eternal_y[i]]);776amount++;777}778}779}780if (eternal_y[i] > 0)781{782if (bnum[eternal_x[i]][eternal_y[i] - 1] == 0)783{784bool potato = false;785for (unsigned short c = 0; c < eternal_size; c++)786{787if (eternal_x[c] == eternal_x[i] && eternal_y[c] == eternal_y[i] - 1)788{789potato = true;790break;791}792}793if (!potato)794{795eternal_size++;796eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));797eternal_x[eternal_size - 1] = eternal_x[i];798eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));799eternal_y[eternal_size - 1] = eternal_y[i] - 1;800}801}802else if (deskgr[eternal_x[i]][eternal_y[i] - 1] == 32)803{804deskgr[eternal_x[i]][eternal_y[i] - 1] = opencl(bnum[eternal_x[i]][eternal_y[i] - 1]);805amount++;806}807}808if (type != 1)809{810if (eternal_y[i] < type - 1)811{812if (bnum[eternal_x[i]][eternal_y[i] + 1] == 0)813{814bool potato = false;815for (unsigned short c = 0; c < eternal_size; c++)816{817if (eternal_x[c] == eternal_x[i] && eternal_y[c] == eternal_y[i] + 1)818{819potato = true;820break;821}822}823if (!potato)824{825eternal_size++;826eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));827eternal_x[eternal_size - 1] = eternal_x[i];828eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));829eternal_y[eternal_size - 1] = eternal_y[i] + 1;830}831}832else if (deskgr[eternal_x[i]][eternal_y[i] + 1] == 32)833{834deskgr[eternal_x[i]][eternal_y[i] + 1] = opencl(bnum[eternal_x[i]][eternal_y[i] + 1]);835amount++;836}837}838}839else840{841if (eternal_y[i] < y - 1)842{843if (bnum[eternal_x[i]][eternal_y[i] + 1] == 0)844{845bool potato = false;846for (unsigned short c = 0; c < eternal_size; c++)847{848if (eternal_x[c] == eternal_x[i] && eternal_y[c] == eternal_y[i] + 1)849{850potato = true;851break;852}853}854if (!potato)855{856eternal_size++;857eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));858eternal_x[eternal_size - 1] = eternal_x[i];859eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));860eternal_y[eternal_size - 1] = eternal_y[i] + 1;861}862}863else if (deskgr[eternal_x[i]][eternal_y[i] + 1] == 32)864{865deskgr[eternal_x[i]][eternal_y[i] + 1] = opencl(bnum[eternal_x[i]][eternal_y[i] + 1]);866amount++;867}868}869}870if (eternal_x[i] > 0 && eternal_y[i] > 0)871{872if (bnum[eternal_x[i] - 1][eternal_y[i] - 1] == 0)873{874bool potato = false;875for (unsigned short c = 0; c < eternal_size; c++)876{877if (eternal_x[c] == eternal_x[i] - 1 && eternal_y[c] == eternal_y[i] - 1)878{879potato = true;880break;881}882}883if (!potato)884{885eternal_size++;886eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));887eternal_x[eternal_size - 1] = eternal_x[i] - 1;888eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));889eternal_y[eternal_size - 1] = eternal_y[i] - 1;890}891}892else if (deskgr[eternal_x[i] - 1][eternal_y[i] - 1] == 32)893{894deskgr[eternal_x[i] - 1][eternal_y[i] - 1] = opencl(bnum[eternal_x[i] - 1][eternal_y[i] - 1]);895amount++;896}897}898if (type != 1)899{900if (eternal_x[i] < type - 1 && eternal_y[i] > 0)901{902if (bnum[eternal_x[i] + 1][eternal_y[i] - 1] == 0)903{904bool potato = false;905for (unsigned short c = 0; c < eternal_size; c++)906{907if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i] - 1)908{909potato = true;910break;911}912}913
914if (!potato)915{916eternal_size++;917eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));918eternal_x[eternal_size - 1] = eternal_x[i] + 1;919eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));920eternal_y[eternal_size - 1] = eternal_y[i] - 1;921}922}923else if (deskgr[eternal_x[i] + 1][eternal_y[i] - 1] == 32)924{925deskgr[eternal_x[i] + 1][eternal_y[i] - 1] = opencl(bnum[eternal_x[i] + 1][eternal_y[i] - 1]);926amount++;927}928}929}930else931{932if (eternal_x[i] < x - 1 && eternal_y[i] > 0)933{934if (bnum[eternal_x[i] + 1][eternal_y[i] - 1] == 0)935{936bool potato = false;937for (unsigned short c = 0; c < eternal_size; c++)938{939if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i] - 1)940{941potato = true;942break;943}944}945if (!potato)946{947eternal_size++;948eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));949eternal_x[eternal_size - 1] = eternal_x[i] + 1;950eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));951eternal_y[eternal_size - 1] = eternal_y[i] - 1;952}953}954else if (deskgr[eternal_x[i] + 1][eternal_y[i] - 1] == 32)955{956deskgr[eternal_x[i] + 1][eternal_y[i] - 1] = opencl(bnum[eternal_x[i] + 1][eternal_y[i] - 1]);957amount++;958}959}960}961if (type != 1)962{963if (eternal_x[i] > 0 && eternal_y[i] < type - 1)964{965if (bnum[eternal_x[i] - 1][eternal_y[i] + 1] == 0)966{967bool potato = false;968for (unsigned short c = 0; c < eternal_size; c++)969{970if (eternal_x[c] == eternal_x[i] - 1 && eternal_y[c] == eternal_y[i] + 1)971{972potato = true;973break;974}975}976if (!potato)977{978eternal_size++;979eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));980eternal_x[eternal_size - 1] = eternal_x[i] - 1;981eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));982eternal_y[eternal_size - 1] = eternal_y[i] + 1;983}984}985else if (deskgr[eternal_x[i] - 1][eternal_y[i] + 1] == 32)986{987deskgr[eternal_x[i] - 1][eternal_y[i] + 1] = opencl(bnum[eternal_x[i] - 1][eternal_y[i] + 1]);988amount++;989}990}991}992else993{994if (eternal_x[i] > 0 && eternal_y[i] < y - 1)995{996if (bnum[eternal_x[i] - 1][eternal_y[i] + 1] == 0)997{998bool potato = false;999for (unsigned short c = 0; c < eternal_size; c++)1000{1001if (eternal_x[c] == eternal_x[i] - 1 && eternal_y[c] == eternal_y[i] + 1)1002{1003potato = true;1004break;1005}1006}1007if (!potato)1008{1009eternal_size++;1010eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));1011eternal_x[eternal_size - 1] = eternal_x[i] - 1;1012eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));1013eternal_y[eternal_size - 1] = eternal_y[i] + 1;1014}1015}1016else if (deskgr[eternal_x[i] - 1][eternal_y[i] + 1] == 32)1017{1018deskgr[eternal_x[i] - 1][eternal_y[i] + 1] = opencl(bnum[eternal_x[i] - 1][eternal_y[i] + 1]);1019amount++;1020}1021}1022}1023if (type != 1)1024{1025if (eternal_x[i] < type - 1 && eternal_y[i] < type - 1)1026{1027if (bnum[eternal_x[i] + 1][eternal_y[i] + 1] == 0)1028{1029bool potato = false;1030for (unsigned short c = 0; c < eternal_size; c++)1031{1032if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i] + 1)1033{1034potato = true;1035break;1036}1037}1038if (!potato)1039{1040eternal_size++;1041eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));1042eternal_x[eternal_size - 1] = eternal_x[i] + 1;1043eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));1044eternal_y[eternal_size - 1] = eternal_y[i] + 1;1045}1046}1047else if (deskgr[eternal_x[i] + 1][eternal_y[i] + 1] == 32)1048{1049deskgr[eternal_x[i] + 1][eternal_y[i] + 1] = opencl(bnum[eternal_x[i] + 1][eternal_y[i] + 1]);1050amount++;1051}1052}1053}1054else1055{1056if (eternal_x[i] < x - 1 && eternal_y[i] < y - 1)1057{1058if (bnum[eternal_x[i] + 1][eternal_y[i] + 1] == 0)1059{1060bool potato = false;1061for (unsigned short c = 0; c < eternal_size; c++)1062{1063if (eternal_x[c] == eternal_x[i] + 1 && eternal_y[c] == eternal_y[i] + 1)1064{1065potato = true;1066break;1067}1068}1069if (!potato)1070{1071eternal_size++;1072eternal_x = (unsigned short*)realloc(eternal_x, eternal_size * sizeof(unsigned short));1073eternal_x[eternal_size - 1] = eternal_x[i] + 1;1074eternal_y = (unsigned short*)realloc(eternal_y, eternal_size * sizeof(unsigned short));1075eternal_y[eternal_size - 1] = eternal_y[i] + 1;1076}1077}1078else if (deskgr[eternal_x[i] + 1][eternal_y[i] + 1] == 32)1079{1080deskgr[eternal_x[i] + 1][eternal_y[i] + 1] = opencl(bnum[eternal_x[i] + 1][eternal_y[i] + 1]);1081amount++;1082}1083}1084}1085}1086free(eternal_x);1087free(eternal_y);1088}1089else1090{1091deskgr[xh - 1][yh - 1] = opencl(bnum[xh - 1][yh - 1]);1092amount++;1093}1094switch (type)1095{1096case 8:1097printdesk(deskgr, 8, 8);1098break;1099case 16:1100printdesk(deskgr, 16, 16);1101break;1102case 32:1103printdesk(deskgr, 32, 32);1104break;1105default:1106printdesk(deskgr, x, y);1107break;1108}1109space -= amount;1110}1111break;1112}1113}1114while (!lose && space != 0);1115if (!lose)1116{1117printf("ВЫ ВЫЖИЛИ!\n");1118victory = true;1119}1120time_t finish = time(NULL);1121printf("Ваше время: %.0lf секунд\n", difftime(finish, start));1122free(position);1123for (unsigned short i = 0; i < type; i++)1124free(deskgr[i]);1125free(deskgr);1126for (unsigned short i = 0; i < type; i++)1127free(bnum[i]);1128free(bnum);1129}
1130int main()1131{
1132setlocale(LC_ALL, "Russian");1133unsigned short list_size = 0;1134struct result* list = (struct result*)malloc(list_size * sizeof(struct result));1135FILE* fin = fopen("results.bin", "rbe");1136if (fin == NULL)1137{1138FILE* create = fopen("results.bin", "wbe");1139fclose(create);1140}1141else1142{1143while (!feof(fin))1144{1145struct result guy;1146unsigned short name_sz = 0, games = 0, vic = 0, iz = 0, hrd = 0, slayer = 0;1147double pr = 0.0;1148char x = 0;1149fread(&name_sz, sizeof(unsigned short), 1, fin);1150if (name_sz != 0)1151{1152char* namez = (char*)malloc((name_sz + 1) * sizeof(char));1153for (unsigned short i = 0; i < name_sz; i++)1154{1155fread(&x, sizeof(char), 1, fin);1156namez[i] = x;1157}1158namez[name_sz] = '\0';1159fread(&games, sizeof(unsigned short), 1, fin);1160fread(&vic, sizeof(unsigned short), 1, fin);1161fread(&pr, sizeof(double), 1, fin);1162fread(&iz, sizeof(unsigned short), 1, fin);1163fread(&hrd, sizeof(unsigned short), 1, fin);1164fread(&slayer, sizeof(unsigned short), 1, fin);1165guy.name = namez;1166guy.games = games;1167guy.victories = vic;1168guy.percent = pr;1169guy.izzy = iz;1170guy.hard = hrd;1171guy.doom_guy = slayer; /*ОСТОРОЖНО, СПОЙЛЕР К DOOM ETERNAL :)*/1172list_size++;1173list = (struct result*)realloc(list, list_size * sizeof(struct result));1174list[list_size - 1] = guy;1175}1176else1177break;1178}1179fclose(fin);1180}1181unsigned short choice = 0, gm_ch = 0, res_ch = 0;1182char* CHOICE = (char*)malloc(0);1183char* GAME_CH = (char*)malloc(0);1184char* RES_CH = (char*)malloc(0);1185char* away = (char*)malloc(0);1186char* resaw = (char*)malloc(0);1187bool g8sch = false;1188bool g16sch = false;1189bool g32sch = false;1190do1191{1192g8sch = false;1193g16sch = false;1194g32sch = false;1195do1196{1197printf("ххххх_САПЁР_ххххх\n");1198printf("1.Играть\n2.Рекорды\n3.Выход\n");1199printf("Введите ЦИФРУ, чтобы выполнить действие: ");1200scanf_rng("%2s", CHOICE);1201if (*CHOICE != 49 && *CHOICE != 50 && *CHOICE != 51 || CHOICE[1] != 0)1202printf("Ввод не корректен\n");1203}1204while (*CHOICE != 49 && *CHOICE != 50 && *CHOICE != 51 || CHOICE[1] != 0);1205switch (*CHOICE)1206{1207case 49:1208choice = 1;1209break;1210case 50:1211choice = 2;1212break;1213default:1214choice = 3;1215break;1216}1217struct result player;1218switch (choice)1219{1220case 1:1221printf("Введите ваше имя (максимум 15 символов): ");1222char* name = (char*)malloc(15 * sizeof(char));1223scanf_rng("%15s", name);1224player.name = name;1225do1226{1227printf("Выберите уровень сложности: 1.Просто(доска 8x8, 16 мин); 2.Не так просто(доска 16x16, 32 мины); 3.ХАРДКОР(доска 32x32, 64 мины); 4.Своя игра: ");1228scanf_rng("%2s", GAME_CH);1229if (*GAME_CH != 49 && *GAME_CH != 50 && *GAME_CH != 51 && *GAME_CH != 52 || GAME_CH[1] != 0)1230printf("Ввод не корректен\n");1231}1232while (*GAME_CH != 49 && *GAME_CH != 50 && *GAME_CH != 51 && *GAME_CH != 52 || GAME_CH[1] != 0);1233switch (*GAME_CH)1234{1235case 49:1236gm_ch = 1;1237break;1238case 50:1239gm_ch = 2;1240break;1241case 51:1242gm_ch = 3;1243break;1244default:1245gm_ch = 4;1246break;1247}1248switch (gm_ch)1249{1250case 1:1251for (unsigned short e = 0; e < list_size; e++)1252{1253if (strcmp(list[e].name, player.name) == 0)1254{1255g8sch = true;1256game(list, list[e], 8);1257if (victory)1258{1259list[e].victories++;1260list[e].izzy++;1261victory = false;1262}1263list[e].games++;1264list[e].percent = (list[e].victories / (list[e].games / 100.0));1265break;1266}1267}1268if (!g8sch)1269{1270player.victories = 0;1271player.izzy = 0;1272player.hard = 0;1273player.doom_guy = 0;1274game(list, player, 8);1275if (victory)1276{1277player.victories = 1;1278player.izzy = 1;1279victory = false;1280}1281player.games = 1;1282player.percent = (player.victories / (player.games / 100.0));1283list_size++;1284list = (struct result*)realloc(list, list_size * sizeof(struct result));1285list[list_size - 1] = player;1286}1287save(list, list_size);1288break;1289case 2:1290for (unsigned short e = 0; e < list_size; e++)1291{1292if (strcmp(list[e].name, player.name) == 0)1293{1294g16sch = true;1295game(list, list[e], 16);1296if (victory)1297{1298list[e].victories++;1299list[e].hard++;1300victory = false;1301}1302list[e].games++;1303list[e].percent = (list[e].victories / (list[e].games / 100.0));1304break;1305}1306}1307if (!g16sch)1308{1309player.victories = 0;1310player.izzy = 0;1311player.hard = 0;1312player.doom_guy = 0;1313game(list, player, 16);1314if (victory)1315{1316player.victories = 1;1317player.hard = 1;1318victory = false;1319}1320player.games = 1;1321player.percent = (player.victories / (player.games / 100.0));1322list_size++;1323list = (struct result*)realloc(list, list_size * sizeof(struct result));1324list[list_size - 1] = player;1325}1326save(list, list_size);1327break;1328case 3:1329for (unsigned short e = 0; e < list_size; e++)1330{1331if (strcmp(list[e].name, player.name) == 0)1332{1333g32sch = true;1334game(list, list[e], 32);1335if (victory)1336{1337list[e].victories++;1338list[e].doom_guy++;1339victory = false;1340}1341list[e].games++;1342list[e].percent = (list[e].victories / (list[e].games / 100.0));1343break;1344}1345}1346if (!g32sch)1347{1348player.victories = 0;1349player.izzy = 0;1350player.hard = 0;1351player.doom_guy = 0;1352game(list, player, 32);1353if (victory)1354{1355player.victories++;1356player.doom_guy++;1357victory = false;1358}1359player.games = 1;1360player.percent = (player.victories / (player.games / 100.0));1361list_size++;1362list = (struct result*)realloc(list, list_size * sizeof(struct result));1363list[list_size - 1] = player;1364}1365save(list, list_size);1366break;1367default:1368for (unsigned short e = 0; e < list_size; e++)1369{1370if (strcmp(list[e].name, player.name) == 0)1371{1372g8sch = true;1373game(list, list[e], 1);1374if (victory)1375{1376list[e].victories++;1377victory = false;1378}1379list[e].games++;1380list[e].percent = (list[e].victories / (list[e].games / 100.0));1381break;1382}1383}1384if (!g8sch)1385{1386player.victories = 0;1387player.izzy = 0;1388player.hard = 0;1389player.doom_guy = 0;1390game(list, player, 1);1391if (victory)1392{1393player.victories = 1;1394victory = false;1395}1396player.games = 1;1397player.percent = (player.victories / (player.games / 100.0));1398list_size++;1399list = (struct result*)realloc(list, list_size * sizeof(struct result));1400list[list_size - 1] = player;1401}1402save(list, list_size);1403break;1404}1405break;1406case 2:1407do1408{1409printf("Результаты\n");1410do1411{1412printf(1413"1.Сыграно игр\n2.Побед на сложности 'легко'\n3.Побед на сложности 'не так легко'\n4.Побед на сложности 'ХАРДКОР'\n5.Процент побед.\n6.Выход\n");1414printf("Выберите ЦИФРУ действия: ");1415scanf_rng("%2s", RES_CH);1416if (*RES_CH != 49 && *RES_CH != 50 && *RES_CH != 51 && *RES_CH != 52 && *RES_CH != 53 && *RES_CH !=54 || RES_CH[1] != 0)1417printf("Ввод не корректен\n");1418}1419while (*RES_CH != 49 && *RES_CH != 50 && *RES_CH != 51 && *RES_CH != 52 && *RES_CH != 53 && *RES_CH != 54 || RES_CH[1] != 0);1420switch (*RES_CH)1421{1422case 49:1423res_ch = 1;1424break;1425case 50:1426res_ch = 2;1427break;1428case 51:1429res_ch = 3;1430break;1431case 52:1432res_ch = 4;1433break;1434case 53:1435res_ch = 5;1436break;1437default:1438res_ch = 6;1439break;1440}1441switch (res_ch)1442{1443case 1:1444if (list_size == 0)1445printf("Ещё никто не играл...\n");1446else1447{1448qsort(list, list_size, sizeof(*list), compare_games);1449for (unsigned short i = list_size; i > 0; i--)1450printf("%s : %hu\n", list[i - 1].name, list[i - 1].games);1451}1452break;1453case 2:1454if (list_size == 0)1455printf("Ещё никто не играл...\n");1456else1457{1458qsort(list, list_size, sizeof(*list), compare_izzy);1459for (unsigned short i = list_size; i > 0; i--)1460printf("%s : %hu\n", list[i - 1].name, list[i - 1].izzy);1461}1462break;1463case 3:1464if (list_size == 0)1465printf("Ещё никто не играл...\n");1466else1467{1468qsort(list, list_size, sizeof(*list), compare_hard);1469for (unsigned short i = list_size; i > 0; i--)1470printf("%s : %hu\n", list[i - 1].name, list[i - 1].hard);1471}1472break;1473case 4:1474if (list_size == 0)1475printf("Ещё никто не играл...\n");1476else1477{1478qsort(list, list_size, sizeof(*list), compare_doom);1479for (unsigned short i = list_size; i > 0; i--)1480printf("%s : %hu\n", list[i - 1].name, list[i - 1].doom_guy);1481}1482break;1483case 5:1484if (list_size == 0)1485printf("Ещё никто не играл...\n");1486else1487{1488qsort(list, list_size, sizeof(*list), compare_percent);1489for (unsigned short i = list_size; i > 0; i--)1490{1491int perc = list[i - 1].percent;1492printf("%s : %d%%\n", list[i - 1].name, perc);1493}1494}1495break;1496default:1497printf("Вы точно хотите выйти? Введите YES, если да или что-то другое, если нет.\n");1498scanf_rng("%4s", resaw);1499if (strcmp(resaw, "YES") != 0)1500res_ch = 0;1501break;1502}1503}1504while (res_ch != 6);1505break;1506default:1507printf("Вы точно хотите выйти? Введите YES, если да или что-то другое, если нет.\n");1508scanf_rng("%4s", away);1509if (strcmp(away, "YES") != 0)1510choice = 0;1511break;1512}1513}1514while (choice != 3);1515return 0;1516}