Advertisement
Alexqq11

lol

May 2nd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.20 KB | None | 0 0
  1. // Win32 builds require Windows.h for Sleep(milliseconds).
  2. // They also require curses.h for the PDCurses library.
  3. #ifdef _WIN32
  4. #   include <Windows.h>
  5. #   include <curses.h>
  6. #   define SLEEP(delay) Sleep(delay/1000)
  7. // Unix builds require ncurses.h for the Ncurses library.
  8. // Unix also requires unistd.h for usleep(microseconds).
  9. // usleep/1000 = Sleep
  10. #else
  11. #   include <ncurses.h>
  12. #   include <unistd.h>
  13. #   define SLEEP(delay) usleep(delay)
  14. #endif
  15. // Thanks to /u/aftli_work for the file consolidation suggestion!
  16.  
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <string.h>
  20. #define OFFSET_LEFT 0
  21. #define OFFSET_RIGHT 20
  22.  
  23. //#define _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  24. ///
  25. #include <iostream>
  26. using namespace std;
  27. int kbhit(){
  28.     /* Get the current char. */
  29.     int ch = getch();
  30.  
  31.     /* Returns true if a key has been hit. False if it hasn't. */
  32.     if (ch != ERR) {
  33.         return 1;
  34.     } else {
  35.         return 0;
  36.     }
  37. }
  38. void pass();
  39.  
  40. void slowPrint(char arr[], int size, int line){
  41.     int i = 0;
  42.    
  43.     for(i=0; i<size; i++){  
  44.         /* Print the current character in the current position. */
  45.         mvprintw(line,i,"%c",arr[i]);
  46.         /* Move the cursor to the next position */
  47.         move(line, i+1);
  48.         refresh();
  49.         /* If any keyboard input was recieved, go directly to pass(), otherwise continue */
  50.         if(kbhit()){
  51.             pass();
  52.         }
  53.         SLEEP(20000);
  54.     }
  55. }
  56. int main();
  57. void slowType(char arr[], int size, int line){
  58.     int i = 0;
  59.     for(i=0; i<size; i++){  
  60.         mvprintw(line,i+1,"%c",arr[i]);
  61.         move(line, i+2);
  62.         refresh();
  63.         if(kbhit()){
  64.             pass();
  65.         }
  66.         SLEEP(70000);
  67.     }
  68. }
  69.  
  70. void passPrint(char arr[], int size, int line){
  71.     int i;
  72.     for(i=0; i<size; i++){  
  73.         mvprintw(line,i,"%c",arr[i]);
  74.         move(line, i+1);
  75.         refresh();
  76.         SLEEP(20000);
  77.     }
  78. }
  79.  
  80.  
  81. void printChoices(int hex, char arr[], int line, int offset){
  82.     mvprintw(line,offset,"0x%X", hex);
  83.     int i = 0;
  84.     for(i=0; i<12; i++)
  85.         mvprintw(line,7+offset+i,"%c",arr[i]);
  86.     move(line, 20+offset);
  87.     refresh();
  88.     SLEEP(30000);
  89. }
  90.  
  91. void file_write_module(char *arr[],int len){
  92.    FILE *fr;  
  93.    fr = fopen ("passwords5.txt", "w");
  94.    int i = 0;
  95.    for (i = 0; i < len; i++){
  96.       fputs(arr[i], fr);
  97.       fputs("\n",fr);
  98.    }
  99.  
  100. }
  101.  
  102. int getCharLoc(int y, int x){
  103.     /* Left side */
  104.     if(x<19)
  105.         return 12*(y-5)+(x-7);
  106.     /* Right side */
  107.     else
  108.         return 12*(y-5)+(x-27+204);
  109. }
  110.  
  111.  
  112.  
  113. /// Plays the introduction sequence.
  114. ///
  115. void intro(){
  116.     clear();
  117.     SLEEP(250000);
  118.     char arr[] = "WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK";
  119.    
  120.     slowPrint(arr,sizeof(arr), 0);
  121.  
  122.     move(1, 0);
  123.     refresh();
  124.     SLEEP(30000);
  125.     mvprintw(2,0,"%c", '>');
  126.     move(2,1);
  127.     refresh();
  128.     SLEEP(1500000);
  129.  
  130.     char arr2[] = "SET TERMINAL/INQUIRE";
  131.     slowType(arr2,sizeof(arr2), 2);
  132.    
  133.  
  134.     char arr3[] = "RIT-V300";
  135.     slowPrint(arr3,sizeof(arr3), 4);
  136.  
  137.     mvprintw(6,0,"%c", '>');
  138.     refresh();
  139.     SLEEP(1500000);
  140.     char arr4[] = "SET FILE/PROTECTION=OWNER:RWED ACCOUNTS.F";
  141.     slowType(arr4,sizeof(arr4),6);
  142.    
  143.     mvprintw(7,0,"%c", '>');
  144.     refresh();
  145.     SLEEP(1500000);
  146.     char arr5[] = "SET HALT RESTART/MAINT";
  147.     slowType(arr5,sizeof(arr5),7);
  148.    
  149.     char arr6[] = "Initializing Robco Industries(TM) Boot Agent v2.3.0";
  150.     slowPrint(arr6,sizeof(arr6),9);
  151.  
  152.     char arr7[] = "RBIOS-4.02.08.00 53EE5.E7.E8";
  153.     slowPrint(arr7,sizeof(arr7),10);
  154.  
  155.     char arr8[] = "Copyright 2201-22-3 Robco Ind.";
  156.     slowPrint(arr8,sizeof(arr8),11);
  157.  
  158.     char arr9[] = "Uppermem: 64 KB";
  159.     slowPrint(arr9,sizeof(arr9),12);
  160.  
  161.     char arr10[] = "Root (5A8)";
  162.     slowPrint(arr10,sizeof(arr10),13);
  163.  
  164.     char arr11[] = "Maintenance Mode";
  165.     slowPrint(arr11,sizeof(arr11),14);
  166.  
  167.     mvprintw(16,0,"%c",'>');
  168.     refresh();
  169.     SLEEP(1500000);
  170.     char arr12[] = "RUN DEBUG/ACCOUNTS.F";
  171.     slowType(arr12,sizeof(arr12),16);
  172.     move(16,0);
  173.     refresh();
  174.     SLEEP(50000);
  175. }
  176.  
  177.  
  178. int currentCharContains(char arr[],char c){
  179.     int i;
  180.     for(i=0; i<12; i++)
  181.         if(arr[i]==c)
  182.             return 1;
  183.     return 0;
  184. }
  185.  
  186. long LinesAmount(char * buffer , long length){
  187.         long linesAmount = 0;
  188.         long i;
  189.         for (i = 0; i < length; i++) {
  190.             if (buffer[i] == '\n') {
  191.                 linesAmount++;
  192.             }
  193.         }
  194.         return linesAmount;
  195.     }
  196.  
  197. char **  ReadPasswordsFromFile(char * filename, int * len){
  198.     FILE* f;
  199.     f = fopen(filename, "r");   /*if (!f){  printf("failed to open file"); exit(42); }*/
  200.     fseek(f, 0, SEEK_END);// length of file
  201.     int length = ftell(f);
  202.     fseek(f, 0, SEEK_SET);
  203.     char* buffer = 0;
  204.     buffer = (char *)malloc(length);
  205.     if (buffer)
  206.         fread(buffer,sizeof(char), length, f);  /*if (!buffer) { printf("read nothing"); exit(42); }*/
  207.     fclose(f);
  208.     int linesAmount = LinesAmount(buffer,length);
  209.     char** lines = 0;
  210.     lines = (char **)malloc(linesAmount * sizeof(char*));
  211.     int currentLine = 0;
  212.     int previos = 0;
  213.     int i;
  214.     for (i = 0; i < length; i++) {
  215.         if (buffer[i] == '\n') {//  (|| i == length - 1) to read all but we add \n to end off data
  216.             lines[currentLine] = (char *)malloc(i - previos + 1);
  217.             memcpy(lines[currentLine], &buffer[previos], i - previos);
  218.             lines[currentLine][i - previos] = '\0'; // Последний символ в строке 0
  219.             previos = i+1; // +1 для windows (там где \r\n)
  220.             currentLine++;
  221.         }
  222.     }
  223.     //*wordSize = sizeof(lines[0]);
  224.     *len = currentLine;
  225.     free(buffer);
  226.     return lines;
  227.     }
  228.  
  229. void printPasswordScreenIntro(){
  230.     char prompt[] = "ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL";
  231.     passPrint(prompt,sizeof(prompt),0);
  232.     char prompt2[] = "ENTER PASSWORD NOW";
  233.     passPrint(prompt2, sizeof(prompt2), 1);
  234.     char prompt3[] = "4 ATTEMPT(S) LEFT: * * * *";
  235.     passPrint(prompt3, sizeof(prompt3), 3);
  236.     }
  237.  
  238. void StartNewScreenNodelayFalse(){
  239.     erase();
  240.     endwin();
  241.     initscr();
  242.     noecho();
  243.     refresh();
  244.     attron(A_BOLD);
  245.     nodelay(stdscr, 0);
  246.     if(has_colors() == 1){
  247.         // Colors
  248.         start_color();
  249.         init_pair(1,COLOR_GREEN,COLOR_BLACK);
  250.         attron(COLOR_PAIR(1));
  251.     }
  252. }
  253. char * GenBinTrash(int BIGSTRING_SIZE){ // rename it GenBinTrash
  254.     char * bigString = (char *)malloc(BIGSTRING_SIZE * sizeof(char) + 1); // keep attention with +1;
  255.     char randValues[] = "!@#$^*()_-=+\\|/[]{}?\"\':;,.<>";
  256.     int i;
  257.     for(i=0; i<BIGSTRING_SIZE; i++){
  258.         /* Fill bigString with random values */
  259.         bigString[i] = randValues[rand()%29];
  260.     }
  261.     bigString[BIGSTRING_SIZE] = '\0';
  262.     return bigString;
  263. }
  264. void GenPasswods(char ** passwordList, char * correctWord, char * bigString, int BIGSTRING_SIZE, int WORD_SIZE, int WORD_POOL_SIZE, int WORDS_CHOSEN){
  265.     int place;                      /* Current place for checking and word insertion*/
  266.     int takenWords[WORDS_CHOSEN];   /* Words already placed in bigString */
  267.     int valid;                      /* 1 if selected word is not already used and
  268.                                        does not conflict with other words, 0 otherwise */
  269.     int pickedWord = 0;             /* Indicate whether or not we've chosen the correct word */
  270.     int left = WORDS_CHOSEN;        /* # of words that still need to be chosen */
  271.     int i;
  272.     while(left>0){ // pointer to: passwordList , correctWord, bigString ; BIGSTRSIZE,WRDSIZE,WORD_POOL SIZE, WORD_CHOSEN,  
  273.         valid = 1;
  274.        
  275.         /* Choose a random place in bigString */
  276.         place = rand()%(BIGSTRING_SIZE-WORD_SIZE);
  277.        
  278.         /* Check of any characters there or around it are A-Z */
  279.         for(i=place-1; i<place+WORD_SIZE+1; i++){
  280.             if(bigString[i] > 64 && bigString[i] < 91){
  281.                 valid = 0;
  282.             }
  283.         }
  284.  
  285.  
  286.         if(valid){
  287.             int wordLoc = rand()%WORD_POOL_SIZE;
  288.            
  289.             /* Check if the word selected has already been selected */
  290.             for(i=0;i<=WORDS_CHOSEN-left;i++)
  291.             {
  292.                 if(wordLoc == takenWords[i])
  293.                     valid = 0;
  294.             }
  295.  
  296.             if(valid){
  297.  
  298.                 /* Add the word to bigString */
  299.                 for(i=place; i<place+WORD_SIZE; i++){
  300.                     bigString[i] = passwordList[wordLoc][i-place];
  301.                     /* If this is the first word chosen, it is the correct word. */
  302.                     if(!pickedWord)
  303.                         correctWord[i-place] = passwordList[wordLoc][i-place];
  304.                 }
  305.                 pickedWord = 1;
  306.                 left--;
  307.             }
  308.         }
  309.     }
  310. }
  311. void PrintGeneratedScreen(char * bigString){
  312.         /* Generate the hex values on the left sides */
  313.     int arbHex;
  314.     arbHex = (rand() % 200) + 63744;
  315.  
  316.         /* Print the hex and the filled bigString */
  317.     char temp[12];  
  318.     int current = 0;
  319.     int j = 0;
  320.     int i = 0;
  321.     for(i=5; i<22; i++){
  322.         /* Print left side */
  323.         for(j=0; j<12; j++){
  324.             temp[j] = bigString[j+current];
  325.         }
  326.         printChoices(arbHex,temp,i, OFFSET_LEFT);
  327.         current = current + 12;
  328.         arbHex = arbHex + 12;
  329.     }
  330.     j = 0;
  331.    
  332.     for(i=5; i<22; i++){
  333.         /* Print right side */
  334.         for(j=0; j<12; j++){
  335.             temp[j] = bigString[j+current];
  336.         }
  337.         printChoices(arbHex,temp,i, OFFSET_RIGHT);
  338.         current = current + 12;
  339.         arbHex = arbHex + 12;
  340.     }
  341.  
  342. }
  343. void CheckAttempts( int attemptsAmount){
  344.     char a0,a1,a2,a3;
  345.  
  346.     mvprintw(1,0,"                                 ");
  347.     mvprintw(3,0,"                              "); // NEED TOO ANDERSTUD WHY WE NEED THIS SPACES
  348.     switch(attemptsAmount){
  349.         case 1:
  350.             mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
  351.             attron(A_BLINK);
  352.             mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
  353.             attroff(A_BLINK);
  354.             attron(A_BOLD);
  355.             break;
  356.         case 2:
  357.             mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
  358.             mvprintw(1,0,"ENTER PASSWORD NOW");
  359.             break;
  360.         case 3:
  361.             mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
  362.             mvprintw(1,0,"ENTER PASSWORD NOW");
  363.             break;
  364.         case 4:
  365.             mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
  366.             mvprintw(1,0,"ENTER PASSWORD NOW");
  367.             break;
  368.         case 0:
  369.             clear();
  370.             /*mvprintw(getmaxx(stdscr)/2,getmaxy(stdscr)/2,"TERMINAL LOCKED");
  371.             mvprintw(getmaxx(stdscr)/2 + 2,getmaxy(stdscr)/2 + 2,"PLEASE CONTACT AN ADMINISTRATOR");*/
  372.             mvprintw(10,20,"TERMINAL LOCKED");
  373.             mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
  374.             refresh();
  375.             while(true){
  376.                 a0=getch();
  377.                 a1=getch();
  378.                 a2=getch();
  379.                 a3=getch();
  380.                 if ((a0 == '8') && (a1 == '8') && a2 == '4' && a3 == '1' ){
  381.                    /* endwin();
  382.                     srand ( (unsigned)time(NULL) );
  383.                     initscr();
  384.                     noecho();
  385.                     refresh();
  386.                     attron(A_BOLD);
  387.                     nodelay(stdscr, 1);
  388.                     if(has_colors() == 1){
  389.                    
  390.                         start_color();
  391.                         init_pair(1,COLOR_GREEN,COLOR_BLACK);
  392.                         attron(COLOR_PAIR(1));
  393.                     }
  394.                     intro();
  395.                     pass();
  396.                     */
  397.                     clear();
  398.                     mvprintw(12,12,"POWER OFF");
  399.                     refresh();
  400.                     getch();
  401.                     main();
  402.  
  403.                 }
  404.                 else if (a0 == '5' && a1 == '3' && a2 == '4' && a3 == '2'){ //AUTHOR ERR  PATH AT THE NEXT LINE
  405.                 endwin(); //
  406.                 exit(0);
  407.             }
  408.         }
  409.     }
  410.     refresh();
  411. }
  412.  
  413. int  checkSelectedPassword(char * currentChar,char * correctWord, char * output, int WORD_SIZE , int attemptsAmount){
  414.     int i = 0;
  415.     int rightLetters = WORD_SIZE;
  416.                 for(i=0;i<WORD_SIZE; i++){
  417.                     if(currentChar[i]!=correctWord[i])
  418.                         rightLetters--;
  419.                 }
  420.                 if(rightLetters==WORD_SIZE){
  421.                     mvprintw(15,40,">");
  422.                     for(i=0;i<12;i++){
  423.                         mvprintw(15,41+i,"%c",currentChar[i]);
  424.                     }
  425.                     sprintf(output,"Exact match!");
  426.                     mvprintw(16,40,">");
  427.                     for(i=0;i<12;i++){
  428.                         mvprintw(16,41+i,"%c",output[i]);
  429.                     }
  430.                     sprintf(output,"Please wait ");
  431.                     mvprintw(17,40,">");
  432.                     for(i=0;i<12;i++){
  433.                         mvprintw(17,41+i,"%c",output[i]);
  434.                     }
  435.                     sprintf(output,"while system");
  436.                     mvprintw(18,40,">");
  437.                     for(i=0;i<12;i++){
  438.                         mvprintw(18,41+i,"%c",output[i]);
  439.                     }
  440.                     sprintf(output,"is accessed.");
  441.                     mvprintw(19,40,">");
  442.                     for(i=0;i<12;i++){
  443.                         mvprintw(19,41+i,"%c",output[i]);
  444.                     }
  445.                     refresh();
  446.                     SLEEP(3000000);
  447.                     clear();
  448.                     /*mvprintw(getmaxx(stdscr)/2,getmaxy(stdscr)/2,"TERMINAL LOCKED");
  449.                      mvprintw(getmaxx(stdscr)/2 + 2,getmaxy(stdscr)/2 + 2,"PLEASE CONTACT AN ADMINISTRATOR");*/
  450.                     mvprintw(12,12,"FLAG IS: RUCTF_HAVE_A_FUN");
  451.                     refresh();
  452.                     getch();
  453.                     clear();
  454.                     mvprintw(12,12,"POWER OFF");
  455.                     refresh();
  456.                     getch();
  457.                     main();
  458.                     endwin();
  459.                     exit(0);
  460.                    
  461.                 }
  462.                 else{
  463.                     mvprintw(17,40,">");
  464.                     for(i=0;i<12;i++){
  465.                         mvprintw(17,41+i,"%c",currentChar[i]);
  466.                     }
  467.                     sprintf(output,"Entry denied");
  468.                     mvprintw(18,40,">");
  469.                     for(i=0;i<12;i++){
  470.                         mvprintw(18,41+i,"%c",output[i]);
  471.                     }
  472.                     sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
  473.                     mvprintw(19,40,">");
  474.                     for(i=0;i<12;i++){
  475.                         mvprintw(19,41+i,"%c",output[i]);
  476.                     }
  477.                     attemptsAmount--;
  478.                 }
  479.     return attemptsAmount;
  480. }
  481.  
  482. void pass(){
  483.     static const int BIGSTRING_SIZE = 408;
  484.     int WORD_POOL_SIZE = 0;
  485.     int WORD_SIZE = 5;
  486.     static const int WORDS_CHOSEN = 15;
  487.     /* Start a new screen where nodelay is false */
  488.     StartNewScreenNodelayFalse();
  489.    
  490.     /* Intro text */
  491.     printPasswordScreenIntro();
  492.    
  493.     /* Generate the string to hold the bracket tricks and words */
  494.     char * bigString  = GenBinTrash(BIGSTRING_SIZE);
  495.  
  496.     /* Words from http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt */
  497.     char filename[] = "passwords5.txt";
  498.  
  499.     char** passwordList = ReadPasswordsFromFile(filename, &WORD_POOL_SIZE) ;
  500.  
  501.  
  502.  
  503.     char correctWord[WORD_SIZE];    /* the correct word */
  504.     GenPasswods(passwordList, correctWord, bigString, BIGSTRING_SIZE, WORD_SIZE, WORD_POOL_SIZE, WORDS_CHOSEN);
  505.  
  506.     /* Create and fill an array to keep track of which brackets were used */
  507.     int i;
  508.     int usedBrackets[BIGSTRING_SIZE];
  509.     for(i=0; i<BIGSTRING_SIZE; i++){
  510.         usedBrackets[i] = 1;
  511.     }
  512.    
  513.     PrintGeneratedScreen(bigString);
  514.  
  515.     // attention//
  516.     mvprintw(21,40,"%c",'>');
  517.     move(5,7);
  518.     char currentChar[12]; /* Max length chrrentChar could be (total possible length of a bracket trick) */
  519.     currentChar[0] = mvinch(5,7);
  520.    
  521.     // TODO Clear any key presses that may have occurred during this loading sequence
  522.  
  523.     int y,x,origy,origx,starty,startx;  /* values that keep track of current yx locations, and original ones */
  524.     int wordLength;                     /* How long a word is */
  525.     int charStart;                      /* where character counting starts for brackets */
  526.     char keyPress;                      /* key pressed by user */
  527.     int charCounter;                    /* counts currentChar - used for incrementing currentChar to print or change it */
  528.     int bracketLength;                  /* length of a bracket trick */
  529.     char endBracket;                    /* the end bracket that corresponds to currentChar[0]; */
  530.     int bracketTricks=0;                /* Total number of bracket tricks used */
  531.     int needsClearing = 0;              /* Whether or not highlights need to be purged */
  532.     int needsClearingMultiLine = 0;     /* Whether or not a multi line highlight needs to be purged */
  533.     char output[12];                    /* Used for side terminal output */
  534.     int attemptsAmount = 4;
  535.    
  536.     while(1){
  537.         getyx(stdscr,y,x);
  538.        
  539.         /* Get attemptsAmount left */
  540.         CheckAttempts(attemptsAmount);
  541.         move(y,x);
  542.         /* Check if highlights need to be purged */
  543.         if(needsClearing){
  544.             charCounter = 0;
  545.             while(charCounter!=bracketLength+1){
  546.                 currentChar[charCounter] = mvinch(origy,charStart+charCounter);
  547.                 mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
  548.                 charCounter++;
  549.             }
  550.             mvprintw(21,41,"            ",currentChar[0]);
  551.             needsClearing = 0;
  552.             move(y,origx);
  553.         }
  554.         //CleanHighLights(&needsClearing , &charCounter, &bracketLength, currentChar, &origx, &y, &x ,&origy, &charStart);
  555.         if(needsClearingMultiLine){
  556.             charCounter = 0;
  557.             //int timesDown = 0;
  558.             while(charCounter!=wordLength){
  559.                 currentChar[charCounter] = mvinch(starty,startx);
  560.                 mvprintw(starty,startx,"%c",currentChar[charCounter]);
  561.                 charCounter++;
  562.                 startx++;
  563.                 if(startx==19 || startx==39){
  564.                     startx-=12;
  565.                     starty++;
  566.                 }
  567.             }
  568.             mvprintw(21,41,"            ",currentChar[0]);
  569.             needsClearingMultiLine = 0;
  570.             move(y,x);
  571.         }
  572.         /* Clear the char array */
  573.         for(i=0;i<12;i++)
  574.             currentChar[i]=' ';
  575.         currentChar[0] = mvinch(y,x);
  576.         /* Set the new y and x to origy and origx */
  577.         origy = y;
  578.         origx = x;
  579.  
  580.  
  581.  
  582.         /* Check for bracket tricks */
  583.         if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
  584.             charStart = x;
  585.             bracketLength=0;
  586.             while(x!=18 && x!=38){
  587.                 x++;
  588.                 endBracket = mvinch(y,x);
  589.                 bracketLength++;
  590.             if((endBracket == ')' && currentChar[0]=='(') ||
  591.                 (endBracket == '>' && currentChar[0]=='<') ||
  592.                 (endBracket == ']' && currentChar[0]=='[') ||
  593.                 (endBracket == '}' && currentChar[0]=='{')){
  594.                     /* Reprint the bracket trick with highlight */
  595.                     attron(A_STANDOUT);
  596.                     charCounter = 0;
  597.                     while(1){
  598.                         currentChar[charCounter] = mvinch(y,charStart+charCounter);
  599.                         mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
  600.                         if(currentChar[charCounter] == endBracket)
  601.                             break;
  602.                         charCounter++;
  603.                     }
  604.                     attroff(A_STANDOUT);
  605.                     /* Print the bracket trick to output */
  606.                     attron(A_BOLD);
  607.                     for(i=0;i<=charCounter;i++)
  608.                         mvprintw(21,41+i,"%c",(int)currentChar[i]);
  609.                     /* Notify that highlighting will need to be cleared next move */
  610.                     needsClearing = 1;
  611.                 }
  612.             }
  613.             if(!((endBracket == ')' && currentChar[0]=='(') ||
  614.                 (endBracket == '>' && currentChar[0]=='<') ||
  615.                 (endBracket == ']' && currentChar[0]=='[') ||
  616.                 (endBracket == '}' && currentChar[0]=='{'))){
  617.                 mvprintw(21,41,"%c",currentChar[0]);
  618.             }
  619.         }
  620.  
  621.  
  622.         /* Check for letters */
  623.         else if(currentChar[0]>64 && currentChar[0]<91){
  624.             /* Check for letter behind the current location */
  625.             int tempx = x;
  626.             int tempy = y;
  627.             while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  628.                 currentChar[0] = bigString[getCharLoc(tempy,tempx)];
  629.                 tempx--;
  630.                 if(tempx==6 || tempx==26){
  631.                     tempx+=12;
  632.                     tempy--;
  633.                 }
  634.             }
  635.             startx = tempx;
  636.             starty = tempy; /* We'll need the location of the first char for cleaning */
  637.             /* And start there */
  638.             charCounter = 0;
  639.             while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  640.                 currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  641.                 charCounter++;
  642.                 tempx++;
  643.                 if(tempx==19 || tempx==39){
  644.                     tempx-=12;
  645.                     tempy++;
  646.                 }
  647.             }
  648.             /* Now currentChar is the String, and charCounter+1 is the length */
  649.             wordLength = charCounter+1;
  650.             /* Reprint the word with highlight */
  651.             tempx = startx;
  652.             tempy = starty;
  653.             attron(A_STANDOUT);
  654.             charCounter = 0;
  655.             while(charCounter!=wordLength){
  656.                 currentChar[charCounter] = mvinch(tempy,tempx);
  657.                 mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
  658.                 charCounter++;
  659.                 tempx++;
  660.                 if(tempx==19 || tempx==39){
  661.                     tempx-=12;
  662.                     tempy++;
  663.                 }
  664.             }
  665.                     attroff(A_STANDOUT);
  666.                     /* Print the word to output */
  667.                     attron(A_BOLD);
  668.                     for(i=0;i<charCounter;i++)
  669.                         mvprintw(21,41+i,"%c",(int)currentChar[i]);
  670.                     /* Notify that highlighting will need to be cleared next move */
  671.                     needsClearingMultiLine = 1;
  672.         }
  673.         /* Nothing was found, print current char */
  674.         else
  675.             mvprintw(21,41,"%c",currentChar[0]);
  676.        
  677.         move(origy,origx);
  678.         refresh();
  679.        
  680.         keyPress = getch();
  681.         getyx(stdscr,y,x);
  682.         if (keyPress == '\033') { //красота кода? не-не, не слышал
  683.             getch(); // skip the [
  684.             switch(getch()) { // the real value
  685.                 case 'A':
  686.                     if(y>5)
  687.                         move(y-1,x);
  688.                     break;
  689.                 case 'B':
  690.                     if(y<21)
  691.                         move(y+1,x);
  692.                     break;
  693.                 case 'C':
  694.                     if(x<38){
  695.                         if(x==18)
  696.                             move(y,27);
  697.                         else
  698.                             move(y,x+1);
  699.                     }
  700.                     break;
  701.                 case 'D':
  702.                     if(x>7){
  703.                         if(x==27)
  704.                             move(y,18);
  705.                         else
  706.                             move(y,x-1);
  707.                     }
  708.                    break;
  709.                 }
  710.             }
  711.  
  712.         if(keyPress=='w'){
  713.             if(y>5)
  714.                 move(y-1,x);
  715.         }
  716.         if(keyPress=='s'){
  717.             if(y<21)
  718.                 move(y+1,x);
  719.         }
  720.         if(keyPress=='a'){
  721.             if(x>7){
  722.                 if(x==27)
  723.                     move(y,18);
  724.                 else
  725.                     move(y,x-1);
  726.             }
  727.                
  728.         }
  729.         if(keyPress=='d'){
  730.             if(x<38){
  731.                 if(x==18)
  732.                     move(y,27);
  733.                 else
  734.                     move(y,x+1);
  735.             }
  736.         }
  737.         if(keyPress==3)     /* Ctrl-C */
  738.             exit(0);
  739.         if(keyPress=='\n'){ /* Enter */
  740.             mvprintw(17,40,"              ");
  741.             mvprintw(18,40,"              ");
  742.             mvprintw(19,40,"              ");
  743.             /* If the char is a left bracket */
  744.             if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
  745.                (currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
  746.                (currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
  747.                (currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
  748.                 /* Set the selected bracket as used */
  749.                 usedBrackets[getCharLoc(y,x)] = 0;
  750.                 /* Increment total bracket tricks used */
  751.                 bracketTricks++;
  752.                 if(rand()%2==0){ // somethink  interesting it was 5
  753.                     /* 20% chance of allowance replenish */
  754.                     sprintf(output,"Allowance   ");
  755.                     mvprintw(18,40,">");
  756.                     for(i=0;i<12;i++){
  757.                         mvprintw(18,41+i,"%c",output[i]);
  758.                     }
  759.                     sprintf(output,"replenished.");
  760.                     mvprintw(19,40,">");
  761.                     for(i=0;i<12;i++){
  762.                         mvprintw(19,41+i,"%c",output[i]);
  763.                     }
  764.                     attemptsAmount = 4;
  765.                 }
  766.                 else{
  767.                     /* Remove a dud */
  768.                     int tempx,tempy;
  769.                     pickagain:do{
  770.                         if(rand()%2==0)
  771.                         tempx = (rand()%12)+7;
  772.                     else
  773.                         tempx = (rand()%12)+27;
  774.                     tempy = (rand()%17)+5;
  775.                     } while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
  776.                     while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  777.                         tempx--;
  778.                         if(tempx==6 || tempx==26){
  779.                             tempx+=12;
  780.                             tempy--;
  781.                         }
  782.                     }
  783.  
  784.                     startx = tempx;
  785.                     starty = tempy;
  786.                    
  787.                     charCounter = 0;
  788.                     while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  789.                         currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  790.                         charCounter++;
  791.                         tempx++;
  792.                         if(tempx==19 || tempx==39){
  793.                             tempx-=12;
  794.                             tempy++;
  795.                         }
  796.                     }
  797.                     /* Check if currentChar = correctWord */
  798.                     int allCorrect=1;
  799.                     for(i=0;i<WORD_SIZE;i++){
  800.                         if(currentChar[i]!=correctWord[i])
  801.                             allCorrect = 0;
  802.                     }
  803.                     if(allCorrect)
  804.                         goto pickagain;
  805.                    
  806.                     tempx = startx;
  807.                     tempy = starty;
  808.                     while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  809.                         mvprintw(tempy,tempx,"%c",'.');
  810.                         bigString[getCharLoc(tempy,tempx)] = '.';
  811.                         tempx++;
  812.                         if(tempx==19 || tempx==39){
  813.                             tempx-=12;
  814.                             tempy++;
  815.                         }
  816.                     }
  817.                    
  818.                     sprintf(output,"Dud removed.");
  819.                     mvprintw(19,40,">");
  820.                     for(i=0;i<12;i++){
  821.                         mvprintw(19,41+i,"%c",output[i]);
  822.                     }
  823.            
  824.                 }
  825.             }
  826.             /* Else compare it to the correct word */
  827.             else{
  828.                attemptsAmount = checkSelectedPassword( currentChar, correctWord, output, WORD_SIZE , attemptsAmount);
  829.  
  830.             }
  831.             move(y,x);
  832.         }  
  833.         refresh();
  834.        
  835.     }
  836.  
  837.     endwin();
  838.     exit(0);
  839. }
  840.  
  841.  
  842.  
  843.  
  844. int main(){
  845.  
  846.     // TODO  get input from args here. Pass it to pass
  847.  
  848.     srand ( (unsigned)time(NULL) );
  849.     initscr();
  850.     noecho();
  851.     refresh();
  852.     attron(A_BOLD);
  853.     nodelay(stdscr, 1);
  854.     if(has_colors() == 1){
  855.         /* Colors */
  856.         start_color();
  857.         init_pair(1,COLOR_GREEN,COLOR_BLACK);
  858.         attron(COLOR_PAIR(1));
  859.     }
  860.  
  861.     intro();
  862.     pass();
  863.  
  864.    
  865.     return 0;
  866. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement