vimja

learn pi

Dec 12th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. // Copyright (c) 2013 by Vimja
  2. // This code is released under the terms
  3. // of the WTFPL (What the Fuck Pulic License) version 2.0
  4. // http://sam.zoy.org/wtfpl/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <curses.h>
  9. #include <string.h>
  10.  
  11. int main()
  12. {
  13.   // Huge array to hold 1K of pi numbers
  14.   char pi[] = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989";
  15.  
  16.   // count the numbers
  17.   int i;
  18.   // read in the character
  19.   int c;
  20.   // wanna retry?
  21.   int retry;
  22.   // cursor position
  23.   int cursorX = 5;
  24.   int cursorY = 5;
  25.  
  26.   // initialize ncurses stuff
  27.   WINDOW *mywindow;
  28.   mywindow = initscr();
  29.   nodelay(mywindow, FALSE);
  30.  
  31.   do {
  32.     i = 0;
  33.     retry = 'a';
  34.  
  35.     // prepare ncurses window
  36.     erase();
  37.     printw("pi:\n");
  38.     refresh();
  39.     flushinp();
  40.  
  41.     // read the input and compare
  42.     do {
  43.       c = getch();
  44.       i++;
  45.     } while ((int)pi[i-1] == c && i-1 < strlen(pi));
  46.  
  47.     // retrieve cursor position:
  48.     cursorY = getcury(mywindow);
  49.     cursorX = getcurx(mywindow);
  50.     // delete the wrong character
  51.     move(cursorY, cursorX-1);
  52.     delch();
  53.  
  54.     // now redraw the last (wrong) character, but underline it
  55.     attron(A_UNDERLINE);
  56.     printw("%c", (char)c);
  57.     refresh();
  58.     attroff(A_UNDERLINE);
  59.  
  60.     // print information about the users score
  61.     printw("\nYou got %d digits of pi right \n", i-1);
  62.  
  63.     // Teach the user some more numbers
  64.     if(i-1 < (int)strlen(pi)-5)
  65.       printw("Here are the next 5 chars of pi: %c%c%c%c%c \n", pi[i-1], pi[i], pi[i+1], pi[i+2], pi[i+3]);
  66.     else if (i-1 < (int)strlen(pi))
  67.       printw("Here are all the remaining numbers known to this application: %s \n", &pi[i-1]);
  68.     else
  69.       printw("You beat the shit out of this application which has totally ran out of numbers! \n");
  70.  
  71.     // Ask the user if they want to retry
  72.     do {
  73.       printw("Hit 'r' to retry or 'q' to abort\n");
  74.       refresh();
  75.       flushinp();
  76.       retry = getch();
  77.     } while (retry != 'r' && retry != 'q');
  78.  
  79.   } while (retry == 'r');
  80.  
  81.   // kill the ncurses window
  82.   endwin();
  83.  
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment