pass.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. //
  2. // File: pass.c
  3. // pass.c plays the password guessing game
  4. // @author Josh Bicking <josh1147582>
  5. // // // // // // // // // // // // // // // // // // // // // // //
  6. #define _BSD_SOURCE // for unistd.h
  7. #ifdef _WIN32
  8. # include <Windows.h>
  9. # include <curses.h>
  10. # define SLEEP(delay) Sleep(delay/1000)
  11. #else
  12. # include <ncurses.h>
  13. # include <unistd.h>
  14. # define SLEEP(delay) usleep(delay)
  15. #endif
  16. #include <time.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "pass.h"
  20. #include "print.h"
  21. #include "wordParse.h"
  22. #include "intro.h"
  23. #define OFFSET_LEFT 0
  24. #define OFFSET_RIGHT 20
  25. #define BIGSTRING_SIZE 408
  26. static int currentCharContains(char arr[],char c){
  27. int i;
  28. for(i=0; i<12; i++)
  29. if(arr[i]==c)
  30. return 1;
  31. return 0;
  32. }
  33. static int getCharLoc(int y, int x){
  34. // Left side
  35. if(x<19)
  36. return 12*(y-5)+(x-7);
  37. // Right side
  38. else
  39. return 12*(y-5)+(x-27+204);
  40. }
  41. void pass(){
  42. // Note: Most of the strings in this function are NOT NUL terminated (they
  43. // do not end with a \0, and will not work with many of the string.h
  44. // functions). When I wrote the first revision of this program, I knew very
  45. // little about C, and therefore managed the length of my strings manually.
  46. // I've decided to keep it this way, as a majority of the work deals with
  47. // fixed-length substrings of bigString. It's easier to pull characters out
  48. // of that and campare them than it would be to try and make proper C
  49. // strings out of every operation.
  50. // Clear the screen
  51. erase();
  52. // Intro text
  53. passPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL",0);
  54. passPrint("ENTER PASSWORD NOW", 1);
  55. passPrint("4 ATTEMPT(S) LEFT: * * * *", 3);
  56. // Generate the hex values on the left sides
  57. int arbHex;
  58. arbHex = (rand() % 200) + 63744;
  59. // Generate the string to hold the bracket tricks and words
  60. char bigString [BIGSTRING_SIZE];
  61. char randValues[] = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>";
  62. int i;
  63. for(i=0; i<BIGSTRING_SIZE; i++){
  64. // Fill bigString with random values
  65. bigString[i] = randValues[rand()%29];
  66. }
  67. char ** wordArr = getWordArr();
  68. int WORD_POOL_SIZE = getNumWords();
  69. int WORD_SIZE = getWordLength();
  70. int WORDS_CHOSEN = getWordsToChoose();
  71. // Place a word in the string total times, making sure it doesn
  72. // Overwrite another word or get placed right next to it
  73. int place; // Current place for checking and word insertion
  74. int takenWords[WORDS_CHOSEN]; // Words already placed in bigString
  75. for(int i=0; i<WORDS_CHOSEN; i++)
  76. takenWords[i] = 0;
  77. int valid; // 1 if selected word is not already used a
  78. // does not conflict with other words, 0 otherwise */
  79. int pickedWord = 0; // Indicate whether or not we've chosen the correct word
  80. char correctWord[WORD_SIZE]; // the correct word
  81. // Place all the words into bigString
  82. for(int i=0; i<WORD_SIZE; i++) {
  83. // Find a WORD_SIZE length spot in bigString that isn't already
  84. // occupied by another word or part of another word.
  85. do {
  86. valid = 1;
  87. // Choose a random place in bigString
  88. place = rand()%(BIGSTRING_SIZE-WORD_SIZE);
  89. // Check of any characters there or around it are A-Z
  90. for(int j=place-1; j<place+WORD_SIZE+1; j++){
  91. if(bigString[j] > 64 && bigString[j] < 91){
  92. valid = 0;
  93. break;
  94. }
  95. }
  96. }while(!valid);
  97. // Find a word that hasn't already been inserted
  98. int wordLoc = 0;
  99. do {
  100. wordLoc = rand()%WORD_POOL_SIZE;
  101. }while(takenWords[wordLoc]);
  102. // Set it as taken
  103. takenWords[wordLoc] = 1;
  104. // Add the word to bigString
  105. for(int j=place; j<place+WORD_SIZE; j++){
  106. bigString[j] = *(*(wordArr+wordLoc)+(j-place));
  107. // If this is the first word chosen, it is the correct word.
  108. if(!pickedWord)
  109. correctWord[j-place] = *(*(wordArr+wordLoc)+(j-place));
  110. }
  111. pickedWord = 1;
  112. }
  113. // Create and fill an array to keep track of which brackets were used
  114. int usedBrackets[BIGSTRING_SIZE];
  115. for(i=0; i<BIGSTRING_SIZE; i++){
  116. usedBrackets[i] = 1;
  117. }
  118. // Print the hex and the filled bigString to the screen
  119. char temp[12];
  120. int current = 0;
  121. for(i=5; i<22; i++){
  122. // Print left side
  123. for(int j=0; j<12; j++){
  124. temp[j] = bigString[j+current];
  125. }
  126. printChoices(arbHex,temp,i, OFFSET_LEFT);
  127. current = current + 12;
  128. arbHex = arbHex + 12;
  129. }
  130. for(i=5; i<22; i++){
  131. // Print right side
  132. for(int j=0; j<12; j++){
  133. temp[j] = bigString[j+current];
  134. }
  135. printChoices(arbHex,temp,i, OFFSET_RIGHT);
  136. current = current + 12;
  137. arbHex = arbHex + 12;
  138. }
  139. // Print the cursor and move the selection to the top left
  140. mvprintw(21,40,"%c",'>');
  141. move(5,7);
  142. char currentChar[12]; // Max length currentChar could be (total possible length of a bracket trick)
  143. currentChar[0] = (char)mvinch(5,7);
  144. int y,x; // values that keep track of current yx locations
  145. int origy, origx; // yx values from the previous cycle. Used for clearing highlights
  146. int starty, startx; // yx values used for storing the start of a word
  147. int wordLength; // How long a word is
  148. int charStart; // where character counting starts for brackets
  149. int keyPress; // key pressed by user
  150. int charCounter; // counts currentChar - used for incrementing currentChar to print or change it
  151. int bracketLength; // length of a bracket trick
  152. char endBracket; // the end bracket that corresponds to currentChar[0];
  153. int bracketTricks=0; // Total number of bracket tricks used
  154. int needsClearing = 0; // Whether or not highlights need to be pur
  155. int needsClearingMultiLine = 0; // Whether or not a multi line highlight needs to be purged
  156. char output[13]; // Used for side terminal output
  157. int allowances = 4; // Number of guesses remaining
  158. // Get the key config
  159. int GO_LEFT, GO_RIGHT, GO_DOWN, GO_UP;
  160. switch(getKeyConfig()){
  161. case ARROWS:
  162. GO_LEFT = KEY_LEFT;
  163. GO_RIGHT = KEY_RIGHT;
  164. GO_UP = KEY_UP;
  165. GO_DOWN = KEY_DOWN;
  166. break;
  167. case WASD:
  168. GO_LEFT = 'a';
  169. GO_RIGHT = 'd';
  170. GO_UP = 'w';
  171. GO_DOWN = 's';
  172. break;
  173. case HJKL:
  174. GO_LEFT = 'h';
  175. GO_RIGHT = 'l';
  176. GO_UP = 'k';
  177. GO_DOWN = 'j';
  178. break;
  179. }
  180. // Get rid of all typed characters
  181. int ch = getch();
  182. while(ch != ERR)
  183. ch = getch();
  184. // Finally, set nodelay to false so we can wait for input
  185. nodelay(stdscr, 0);
  186. while(1){
  187. getyx(stdscr,y,x);
  188. // Get allowances left
  189. mvprintw(1,0," ");
  190. mvprintw(3,0," ");
  191. switch(allowances){
  192. case 1: mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
  193. attron(A_BLINK);
  194. mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
  195. attroff(A_BLINK);
  196. attron(A_BOLD);
  197. break;
  198. case 2: mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
  199. mvprintw(1,0,"ENTER PASSWORD NOW");
  200. break;
  201. case 3: mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
  202. mvprintw(1,0,"ENTER PASSWORD NOW");
  203. break;
  204. case 4: mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
  205. mvprintw(1,0,"ENTER PASSWORD NOW");
  206. break;
  207. case 0: clear();
  208. mvprintw(10,20,"TERMINAL LOCKED");
  209. mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
  210. refresh();
  211. SLEEP(3000000);
  212. endwin();
  213. if(strlen(getCompleteProg())> 2)
  214. system(getCompleteProg());
  215. freeAll();
  216. exit(EXIT_FAILURE);
  217. }
  218. refresh();
  219. // Move the cursor back to where it was
  220. move(y,x);
  221. // Check if highlights need to be purged
  222. if(needsClearing){
  223. // Grab each character printed, and reprint it without a highlight
  224. charCounter = 0;
  225. while(charCounter!=bracketLength+1){
  226. currentChar[charCounter] = (char)mvinch(origy,charStart+charCounter);
  227. mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
  228. charCounter++;
  229. }
  230. // Clear the > prompt, which previously contained the entire highlighted string
  231. mvprintw(21,41," ",currentChar[0]);
  232. needsClearing = 0;
  233. move(y,origx);
  234. }
  235. if(needsClearingMultiLine){
  236. charCounter = 0;
  237. // Same as above, but jumps between lines if necessary
  238. while(charCounter!=wordLength){
  239. currentChar[charCounter] = (char)mvinch(starty,startx);
  240. mvprintw(starty,startx,"%c",currentChar[charCounter]);
  241. charCounter++;
  242. startx++;
  243. if(startx==19 || startx==39){
  244. startx-=12;
  245. starty++;
  246. if(starty == 22) {
  247. starty = 5;
  248. startx+=20;
  249. }
  250. }
  251. }
  252. mvprintw(21,41," ",currentChar[0]);
  253. needsClearingMultiLine = 0;
  254. move(y,x);
  255. }
  256. // Clear the char array
  257. for(i=0;i<12;i++)
  258. currentChar[i]=' ';
  259. currentChar[0] = (char) (char)mvinch(y,x);
  260. // Set the new y and x to origy and origx
  261. origy = y;
  262. origx = x;
  263. // Check for bracket tricks
  264. if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
  265. charStart = x;
  266. bracketLength=0;
  267. // Check any chars to the right of the current char for a corresponding bracket
  268. while(x!=18 && x!=38){
  269. x++;
  270. endBracket = (char)mvinch(y,x);
  271. bracketLength++;
  272. if((endBracket == ')' && currentChar[0]=='(') ||
  273. (endBracket == '>' && currentChar[0]=='<') ||
  274. (endBracket == ']' && currentChar[0]=='[') ||
  275. (endBracket == '}' && currentChar[0]=='{')){
  276. // Reprint the brackets, and anything in between them, with a highlight
  277. attron(A_STANDOUT);
  278. charCounter = 0;
  279. while(1){
  280. currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
  281. mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
  282. if(currentChar[charCounter] == endBracket)
  283. break;
  284. charCounter++;
  285. }
  286. attroff(A_STANDOUT);
  287. // Print the bracket trick to output
  288. attron(A_BOLD);
  289. for(i=0;i<=charCounter;i++)
  290. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  291. // Notify that highlighting will need to be cleared next move
  292. needsClearing = 1;
  293. }
  294. }
  295. // If this bracket isn't part of a pair, just print the bracket in the > prompt
  296. if(!((endBracket == ')' && currentChar[0]=='(') ||
  297. (endBracket == '>' && currentChar[0]=='<') ||
  298. (endBracket == ']' && currentChar[0]=='[') ||
  299. (endBracket == '}' && currentChar[0]=='{'))){
  300. mvprintw(21,41,"%c",currentChar[0]);
  301. }
  302. }
  303. // Check for letters
  304. else if(currentChar[0]>64 && currentChar[0]<91){
  305. // Check for letter behind the current location
  306. int tempx = x;
  307. int tempy = y;
  308. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  309. currentChar[0] = bigString[getCharLoc(tempy,tempx)];
  310. tempx--;
  311. if(tempx==6 || tempx==26){
  312. tempx+=12;
  313. tempy--;
  314. if(tempy == 4){
  315. tempy = 21;
  316. tempx-=20;
  317. }
  318. }
  319. }
  320. startx = tempx;
  321. starty = tempy; // We'll need the location of the first char for clean
  322. // And start there
  323. charCounter = 0;
  324. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  325. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  326. charCounter++;
  327. tempx++;
  328. if(tempx==19 || tempx==39){
  329. tempx-=12;
  330. tempy++;
  331. if(tempy == 22) {
  332. tempy = 5;
  333. tempx+=20;
  334. }
  335. }
  336. }
  337. // Now currentChar is the String, and charCounter is the length
  338. wordLength = charCounter;
  339. // Reprint the word with highlight
  340. tempx = startx;
  341. tempy = starty;
  342. attron(A_STANDOUT);
  343. charCounter = 0;
  344. while(charCounter!=wordLength){
  345. currentChar[charCounter] = (char)mvinch(tempy,tempx);
  346. mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
  347. charCounter++;
  348. tempx++;
  349. if(tempx==19 || tempx==39){
  350. tempx-=12;
  351. tempy++;
  352. if(tempy == 22) {
  353. tempy = 5;
  354. tempx+=20;
  355. }
  356. }
  357. }
  358. attroff(A_STANDOUT);
  359. // Print the word to output
  360. attron(A_BOLD);
  361. for(i=0;i<charCounter;i++)
  362. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  363. // Notify that highlighting will need to be cleared next move
  364. needsClearingMultiLine = 1;
  365. }
  366. // Nothing was found, print current char
  367. else
  368. mvprintw(21,41,"%c",currentChar[0]);
  369. move(origy,origx);
  370. refresh();
  371. keyPress = getch();
  372. getyx(stdscr,y,x);
  373. if(keyPress==GO_UP){
  374. if(y>5)
  375. move(y-1,x);
  376. }
  377. if(keyPress==GO_DOWN){
  378. if(y<21)
  379. move(y+1,x);
  380. }
  381. if(keyPress==GO_LEFT){
  382. if(x>7){
  383. if(x==27)
  384. move(y,18);
  385. else
  386. move(y,x-1);
  387. }
  388. }
  389. if(keyPress==GO_RIGHT){
  390. if(x<38){
  391. if(x==18)
  392. move(y,27);
  393. else
  394. move(y,x+1);
  395. }
  396. }
  397. if(keyPress==3) // Ctrl-C
  398. exit(0);
  399. if(keyPress=='\n'){ // Enter
  400. // Get past answers and shift them up along the right.
  401. // This "log" handles 5 previous commands.
  402. mvprintw(5,41," ");
  403. mvprintw(6,41," ");
  404. mvprintw(7,41," ");
  405. char buf[15];
  406. for(int i=8; i<19; i+=3) {
  407. for(int j=0; j< 3; j++){
  408. mvinnstr(i+j, 40, buf, 14);
  409. mvprintw(i+j,40," ");
  410. mvprintw(i+j-3, 40, "%s", buf);
  411. }
  412. }
  413. // If the char is a left bracket
  414. if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
  415. (currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
  416. (currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
  417. (currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
  418. // Set the selected bracket as used
  419. usedBrackets[getCharLoc(y,x)] = 0;
  420. // Increment total bracket tricks used
  421. bracketTricks++;
  422. if(rand()%5==0){
  423. // 20% chance of allowance replenish
  424. mvinnstr(21,40, buf, 14);
  425. mvprintw(17,40, "%s", buf);
  426. sprintf(output,"Allowance ");
  427. mvprintw(18,40,">");
  428. for(i=0;i<12;i++){
  429. mvprintw(18,41+i,"%c",output[i]);
  430. }
  431. sprintf(output,"replenished.");
  432. mvprintw(19,40,">");
  433. for(i=0;i<12;i++){
  434. mvprintw(19,41+i,"%c",output[i]);
  435. }
  436. allowances = 4;
  437. }
  438. else{
  439. // 80% chance to remove a dud
  440. int tempx,tempy; // Mark the beginning of the selected string, and read chars into currentChar
  441. int allCorrect = 1; // Shows if all the chars in the string match the correct word
  442. // Pick a random A-Z character in bigString
  443. do{
  444. do{
  445. if(rand()%2==0)
  446. tempx = (rand()%12)+7;
  447. else
  448. tempx = (rand()%12)+27;
  449. tempy = (rand()%17)+5;
  450. } while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
  451. // Move tempx to the beginning of the word selected
  452. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  453. tempx--;
  454. if(tempx==6 || tempx==26){
  455. tempx+=12;
  456. tempy--;
  457. }
  458. }
  459. // Mark the start of the word
  460. startx = tempx;
  461. starty = tempy;
  462. // Read the word into currentChar
  463. charCounter = 0;
  464. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  465. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  466. charCounter++;
  467. tempx++;
  468. if(tempx==19 || tempx==39){
  469. tempx-=12;
  470. tempy++;
  471. }
  472. }
  473. // Check if currentChar = correctWord
  474. allCorrect=1;
  475. for(i=0;i<WORD_SIZE;i++){
  476. if(currentChar[i]!=correctWord[i])
  477. allCorrect = 0;
  478. }
  479. } while(allCorrect); // Pick again if the correct word was chosen
  480. tempx = startx;
  481. tempy = starty;
  482. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  483. mvprintw(tempy,tempx,"%c",'.');
  484. bigString[getCharLoc(tempy,tempx)] = '.';
  485. tempx++;
  486. if(tempx==19 || tempx==39){
  487. tempx-=12;
  488. tempy++;
  489. }
  490. }
  491. mvinnstr(21,40, buf, 14);
  492. mvprintw(17,40, "%s", buf);
  493. mvprintw(18,40,">Dud");
  494. mvprintw(19,40,">removed.");
  495. }
  496. }
  497. // Else compare it to the correct word
  498. else{
  499. // Get the number of letters that match up with the correct word
  500. int rightLetters = WORD_SIZE;
  501. for(i=0;i<WORD_SIZE; i++){
  502. if(currentChar[i]!=correctWord[i])
  503. rightLetters--;
  504. }
  505. // If all letters matched, it's the correct word
  506. if(rightLetters==WORD_SIZE){
  507. mvprintw(15,40,">");
  508. for(i=0;i<12;i++){
  509. mvprintw(15,41+i,"%c",currentChar[i]);
  510. }
  511. sprintf(output,"Exact match!");
  512. mvprintw(16,40,">");
  513. for(i=0;i<12;i++){
  514. mvprintw(16,41+i,"%c",output[i]);
  515. }
  516. sprintf(output,"Please wait ");
  517. mvprintw(17,40,">");
  518. for(i=0;i<12;i++){
  519. mvprintw(17,41+i,"%c",output[i]);
  520. }
  521. sprintf(output,"while system");
  522. mvprintw(18,40,">");
  523. for(i=0;i<12;i++){
  524. mvprintw(18,41+i,"%c",output[i]);
  525. }
  526. sprintf(output,"is accessed.");
  527. mvprintw(19,40,">");
  528. for(i=0;i<12;i++){
  529. mvprintw(19,41+i,"%c",output[i]);
  530. }
  531. refresh();
  532. SLEEP(3000000);
  533. endwin();
  534. if(strlen(getVictoryProg()) > 2)
  535. system(getVictoryProg());
  536. else if(strlen(getCompleteProg())> 2)
  537. system(getCompleteProg());
  538. freeAll();
  539. exit(EXIT_SUCCESS);
  540. }
  541. // Otherwise, print the number right , decrement allowances, and prompt again
  542. else{
  543. mvprintw(17,40,">");
  544. for(i=0;i<12;i++){
  545. mvprintw(17,41+i,"%c",currentChar[i]);
  546. }
  547. sprintf(output,"Entry denied");
  548. mvprintw(18,40,">");
  549. for(i=0;i<12;i++){
  550. mvprintw(18,41+i,"%c",output[i]);
  551. }
  552. sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
  553. mvprintw(19,40,">");
  554. for(i=0;i<12;i++){
  555. mvprintw(19,41+i,"%c",output[i]);
  556. }
  557. allowances--;
  558. }
  559. }
  560. move(y,x);
  561. }
  562. refresh();
  563. }
  564. }