pass.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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(i=place-1; i<place+WORD_SIZE+1; i++){
  91. if(bigString[i] > 64 && bigString[i] < 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(i=place; i<place+WORD_SIZE; i++){
  106. bigString[i] = *(*(wordArr+wordLoc)+(i-place));
  107. // If this is the first word chosen, it is the correct word.
  108. if(!pickedWord)
  109. correctWord[i-place] = *(*(wordArr+wordLoc)+(i-place));
  110. }
  111. pickedWord = 1;
  112. left--;
  113. }
  114. // Create and fill an array to keep track of which brackets were used
  115. int usedBrackets[BIGSTRING_SIZE];
  116. for(i=0; i<BIGSTRING_SIZE; i++){
  117. usedBrackets[i] = 1;
  118. }
  119. // Print the hex and the filled bigString to the screen
  120. char temp[12];
  121. int current = 0;
  122. for(i=5; i<22; i++){
  123. // Print left side
  124. for(int j=0; j<12; j++){
  125. temp[j] = bigString[j+current];
  126. }
  127. printChoices(arbHex,temp,i, OFFSET_LEFT);
  128. current = current + 12;
  129. arbHex = arbHex + 12;
  130. }
  131. for(i=5; i<22; i++){
  132. // Print right side
  133. for(int j=0; j<12; j++){
  134. temp[j] = bigString[j+current];
  135. }
  136. printChoices(arbHex,temp,i, OFFSET_RIGHT);
  137. current = current + 12;
  138. arbHex = arbHex + 12;
  139. }
  140. // Print the cursor and move the selection to the top left
  141. mvprintw(21,40,"%c",'>');
  142. move(5,7);
  143. char currentChar[12]; // Max length currentChar could be (total possible length of a bracket trick)
  144. currentChar[0] = (char)mvinch(5,7);
  145. int y,x; // values that keep track of current yx locations
  146. int origy, origx; // yx values from the previous cycle. Used for clearing highlights
  147. int starty, startx; // yx values used for storing the start of a word
  148. int wordLength; // How long a word is
  149. int charStart; // where character counting starts for brackets
  150. int keyPress; // key pressed by user
  151. int charCounter; // counts currentChar - used for incrementing currentChar to print or change it
  152. int bracketLength; // length of a bracket trick
  153. char endBracket; // the end bracket that corresponds to currentChar[0];
  154. int bracketTricks=0; // Total number of bracket tricks used
  155. int needsClearing = 0; // Whether or not highlights need to be pur
  156. int needsClearingMultiLine = 0; // Whether or not a multi line highlight needs to be purged
  157. char output[13]; // Used for side terminal output
  158. int allowances = 4; // Number of guesses remaining
  159. // Get the key config
  160. int GO_LEFT, GO_RIGHT, GO_DOWN, GO_UP;
  161. switch(getKeyConfig()){
  162. case ARROWS:
  163. GO_LEFT = KEY_LEFT;
  164. GO_RIGHT = KEY_RIGHT;
  165. GO_UP = KEY_UP;
  166. GO_DOWN = KEY_DOWN;
  167. break;
  168. case WASD:
  169. GO_LEFT = 'a';
  170. GO_RIGHT = 'd';
  171. GO_UP = 'w';
  172. GO_DOWN = 's';
  173. break;
  174. case HJKL:
  175. GO_LEFT = 'h';
  176. GO_RIGHT = 'l';
  177. GO_UP = 'k';
  178. GO_DOWN = 'j';
  179. break;
  180. }
  181. // Get rid of all typed characters
  182. int ch = getch();
  183. while(ch != ERR)
  184. ch = getch();
  185. // Finally, set nodelay to false so we can wait for input
  186. nodelay(stdscr, 0);
  187. while(1){
  188. getyx(stdscr,y,x);
  189. // Get allowances left
  190. mvprintw(1,0," ");
  191. mvprintw(3,0," ");
  192. switch(allowances){
  193. case 1: mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
  194. attron(A_BLINK);
  195. mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
  196. attroff(A_BLINK);
  197. attron(A_BOLD);
  198. break;
  199. case 2: mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
  200. mvprintw(1,0,"ENTER PASSWORD NOW");
  201. break;
  202. case 3: mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
  203. mvprintw(1,0,"ENTER PASSWORD NOW");
  204. break;
  205. case 4: mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
  206. mvprintw(1,0,"ENTER PASSWORD NOW");
  207. break;
  208. case 0: clear();
  209. mvprintw(10,20,"TERMINAL LOCKED");
  210. mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
  211. refresh();
  212. SLEEP(3000000);
  213. endwin();
  214. if(strlen(getCompleteProg())> 2)
  215. system(getCompleteProg());
  216. freeAll();
  217. exit(EXIT_FAILURE);
  218. }
  219. refresh();
  220. // Move the cursor back to where it was
  221. move(y,x);
  222. // Check if highlights need to be purged
  223. if(needsClearing){
  224. // Grab each character printed, and reprint it without a highlight
  225. charCounter = 0;
  226. while(charCounter!=bracketLength+1){
  227. currentChar[charCounter] = (char)mvinch(origy,charStart+charCounter);
  228. mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
  229. charCounter++;
  230. }
  231. // Clear the > prompt, which previously contained the entire highlighted string
  232. mvprintw(21,41," ",currentChar[0]);
  233. needsClearing = 0;
  234. move(y,origx);
  235. }
  236. if(needsClearingMultiLine){
  237. charCounter = 0;
  238. // Same as above, but jumps between lines if necessary
  239. while(charCounter!=wordLength){
  240. currentChar[charCounter] = (char)mvinch(starty,startx);
  241. mvprintw(starty,startx,"%c",currentChar[charCounter]);
  242. charCounter++;
  243. startx++;
  244. if(startx==19 || startx==39){
  245. startx-=12;
  246. starty++;
  247. if(starty == 22) {
  248. starty = 5;
  249. startx+=20;
  250. }
  251. }
  252. }
  253. mvprintw(21,41," ",currentChar[0]);
  254. needsClearingMultiLine = 0;
  255. move(y,x);
  256. }
  257. // Clear the char array
  258. for(i=0;i<12;i++)
  259. currentChar[i]=' ';
  260. currentChar[0] = (char) (char)mvinch(y,x);
  261. // Set the new y and x to origy and origx
  262. origy = y;
  263. origx = x;
  264. // Check for bracket tricks
  265. if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
  266. charStart = x;
  267. bracketLength=0;
  268. // Check any chars to the right of the current char for a corresponding bracket
  269. while(x!=18 && x!=38){
  270. x++;
  271. endBracket = (char)mvinch(y,x);
  272. bracketLength++;
  273. if((endBracket == ')' && currentChar[0]=='(') ||
  274. (endBracket == '>' && currentChar[0]=='<') ||
  275. (endBracket == ']' && currentChar[0]=='[') ||
  276. (endBracket == '}' && currentChar[0]=='{')){
  277. // Reprint the brackets, and anything in between them, with a highlight
  278. attron(A_STANDOUT);
  279. charCounter = 0;
  280. while(1){
  281. currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
  282. mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
  283. if(currentChar[charCounter] == endBracket)
  284. break;
  285. charCounter++;
  286. }
  287. attroff(A_STANDOUT);
  288. // Print the bracket trick to output
  289. attron(A_BOLD);
  290. for(i=0;i<=charCounter;i++)
  291. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  292. // Notify that highlighting will need to be cleared next move
  293. needsClearing = 1;
  294. }
  295. }
  296. // If this bracket isn't part of a pair, just print the bracket in the > prompt
  297. if(!((endBracket == ')' && currentChar[0]=='(') ||
  298. (endBracket == '>' && currentChar[0]=='<') ||
  299. (endBracket == ']' && currentChar[0]=='[') ||
  300. (endBracket == '}' && currentChar[0]=='{'))){
  301. mvprintw(21,41,"%c",currentChar[0]);
  302. }
  303. }
  304. // Check for letters
  305. else if(currentChar[0]>64 && currentChar[0]<91){
  306. // Check for letter behind the current location
  307. int tempx = x;
  308. int tempy = y;
  309. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  310. currentChar[0] = bigString[getCharLoc(tempy,tempx)];
  311. tempx--;
  312. if(tempx==6 || tempx==26){
  313. tempx+=12;
  314. tempy--;
  315. if(tempy == 4){
  316. tempy = 21;
  317. tempx-=20;
  318. }
  319. }
  320. }
  321. startx = tempx;
  322. starty = tempy; // We'll need the location of the first char for clean
  323. // And start there
  324. charCounter = 0;
  325. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  326. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  327. charCounter++;
  328. tempx++;
  329. if(tempx==19 || tempx==39){
  330. tempx-=12;
  331. tempy++;
  332. if(tempy == 22) {
  333. tempy = 5;
  334. tempx+=20;
  335. }
  336. }
  337. }
  338. // Now currentChar is the String, and charCounter+1 is the length
  339. wordLength = charCounter+1;
  340. // Reprint the word with highlight
  341. tempx = startx;
  342. tempy = starty;
  343. attron(A_STANDOUT);
  344. charCounter = 0;
  345. while(charCounter!=wordLength){
  346. currentChar[charCounter] = (char)mvinch(tempy,tempx);
  347. mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
  348. charCounter++;
  349. tempx++;
  350. if(tempx==19 || tempx==39){
  351. tempx-=12;
  352. tempy++;
  353. if(tempy == 22) {
  354. tempy = 5;
  355. tempx+=20;
  356. }
  357. }
  358. }
  359. attroff(A_STANDOUT);
  360. // Print the word to output
  361. attron(A_BOLD);
  362. for(i=0;i<charCounter;i++)
  363. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  364. // Notify that highlighting will need to be cleared next move
  365. needsClearingMultiLine = 1;
  366. }
  367. // Nothing was found, print current char
  368. else
  369. mvprintw(21,41,"%c",currentChar[0]);
  370. move(origy,origx);
  371. refresh();
  372. keyPress = getch();
  373. getyx(stdscr,y,x);
  374. if(keyPress==GO_UP){
  375. if(y>5)
  376. move(y-1,x);
  377. }
  378. if(keyPress==GO_DOWN){
  379. if(y<21)
  380. move(y+1,x);
  381. }
  382. if(keyPress==GO_LEFT){
  383. if(x>7){
  384. if(x==27)
  385. move(y,18);
  386. else
  387. move(y,x-1);
  388. }
  389. }
  390. if(keyPress==GO_RIGHT){
  391. if(x<38){
  392. if(x==18)
  393. move(y,27);
  394. else
  395. move(y,x+1);
  396. }
  397. }
  398. if(keyPress==3) // Ctrl-C
  399. exit(0);
  400. if(keyPress=='\n'){ // Enter
  401. // Get past answers and shift them up along the right.
  402. // This "log" handles 5 previous commands.
  403. mvprintw(5,41," ");
  404. mvprintw(6,41," ");
  405. mvprintw(7,41," ");
  406. char buf[15];
  407. for(int i=8; i<19; i+=3) {
  408. for(int j=0; j< 3; j++){
  409. mvinnstr(i+j, 40, buf, 14);
  410. mvprintw(i+j,40," ");
  411. mvprintw(i+j-3, 40, "%s", buf);
  412. }
  413. }
  414. // If the char is a left bracket
  415. if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
  416. (currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
  417. (currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
  418. (currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
  419. // Set the selected bracket as used
  420. usedBrackets[getCharLoc(y,x)] = 0;
  421. // Increment total bracket tricks used
  422. bracketTricks++;
  423. if(rand()%5==0){
  424. // 20% chance of allowance replenish
  425. mvinnstr(21,40, buf, 14);
  426. mvprintw(17,40, "%s", buf);
  427. sprintf(output,"Allowance ");
  428. mvprintw(18,40,">");
  429. for(i=0;i<12;i++){
  430. mvprintw(18,41+i,"%c",output[i]);
  431. }
  432. sprintf(output,"replenished.");
  433. mvprintw(19,40,">");
  434. for(i=0;i<12;i++){
  435. mvprintw(19,41+i,"%c",output[i]);
  436. }
  437. allowances = 4;
  438. }
  439. else{
  440. // 80% chance to remove a dud
  441. int tempx,tempy; // Mark the beginning of the selected string, and read chars into currentChar
  442. int allCorrect = 1; // Shows if all the chars in the string match the correct word
  443. // Pick a random A-Z character in bigString
  444. do{
  445. do{
  446. if(rand()%2==0)
  447. tempx = (rand()%12)+7;
  448. else
  449. tempx = (rand()%12)+27;
  450. tempy = (rand()%17)+5;
  451. } while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
  452. // Move tempx to the beginning of the word selected
  453. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  454. tempx--;
  455. if(tempx==6 || tempx==26){
  456. tempx+=12;
  457. tempy--;
  458. }
  459. }
  460. // Mark the start of the word
  461. startx = tempx;
  462. starty = tempy;
  463. // Read the word into currentChar
  464. charCounter = 0;
  465. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  466. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  467. charCounter++;
  468. tempx++;
  469. if(tempx==19 || tempx==39){
  470. tempx-=12;
  471. tempy++;
  472. }
  473. }
  474. // Check if currentChar = correctWord
  475. allCorrect=1;
  476. for(i=0;i<WORD_SIZE;i++){
  477. if(currentChar[i]!=correctWord[i])
  478. allCorrect = 0;
  479. }
  480. } while(allCorrect); // Pick again if the correct word was chosen
  481. tempx = startx;
  482. tempy = starty;
  483. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  484. mvprintw(tempy,tempx,"%c",'.');
  485. bigString[getCharLoc(tempy,tempx)] = '.';
  486. tempx++;
  487. if(tempx==19 || tempx==39){
  488. tempx-=12;
  489. tempy++;
  490. }
  491. }
  492. mvinnstr(21,40, buf, 14);
  493. mvprintw(17,40, "%s", buf);
  494. mvprintw(18,40,">Dud");
  495. mvprintw(19,40,">removed.");
  496. }
  497. }
  498. // Else compare it to the correct word
  499. else{
  500. // Get the number of letters that match up with the correct word
  501. int rightLetters = WORD_SIZE;
  502. for(i=0;i<WORD_SIZE; i++){
  503. if(currentChar[i]!=correctWord[i])
  504. rightLetters--;
  505. }
  506. // If all letters matched, it's the correct word
  507. if(rightLetters==WORD_SIZE){
  508. mvprintw(15,40,">");
  509. for(i=0;i<12;i++){
  510. mvprintw(15,41+i,"%c",currentChar[i]);
  511. }
  512. sprintf(output,"Exact match!");
  513. mvprintw(16,40,">");
  514. for(i=0;i<12;i++){
  515. mvprintw(16,41+i,"%c",output[i]);
  516. }
  517. sprintf(output,"Please wait ");
  518. mvprintw(17,40,">");
  519. for(i=0;i<12;i++){
  520. mvprintw(17,41+i,"%c",output[i]);
  521. }
  522. sprintf(output,"while system");
  523. mvprintw(18,40,">");
  524. for(i=0;i<12;i++){
  525. mvprintw(18,41+i,"%c",output[i]);
  526. }
  527. sprintf(output,"is accessed.");
  528. mvprintw(19,40,">");
  529. for(i=0;i<12;i++){
  530. mvprintw(19,41+i,"%c",output[i]);
  531. }
  532. refresh();
  533. SLEEP(3000000);
  534. endwin();
  535. if(strlen(getVictoryProg()) > 2)
  536. system(getVictoryProg());
  537. else if(strlen(getCompleteProg())> 2)
  538. system(getCompleteProg());
  539. freeAll();
  540. exit(EXIT_SUCCESS);
  541. }
  542. // Otherwise, print the number right , decrement allowances, and prompt again
  543. else{
  544. mvprintw(17,40,">");
  545. for(i=0;i<12;i++){
  546. mvprintw(17,41+i,"%c",currentChar[i]);
  547. }
  548. sprintf(output,"Entry denied");
  549. mvprintw(18,40,">");
  550. for(i=0;i<12;i++){
  551. mvprintw(18,41+i,"%c",output[i]);
  552. }
  553. sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
  554. mvprintw(19,40,">");
  555. for(i=0;i<12;i++){
  556. mvprintw(19,41+i,"%c",output[i]);
  557. }
  558. allowances--;
  559. }
  560. }
  561. move(y,x);
  562. }
  563. refresh();
  564. }
  565. }