Advertisement
TawratNibir

Typing game

Feb 9th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.86 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8.  
  9.     // keypad(stdscr, TRUE);
  10.     char buffer[100];
  11.     int i = 0;
  12.     char a[800][20];
  13.     int len = 0;
  14.     char c[1000];
  15.     while (scanf(" %[^\n]%*c", buffer) != EOF)
  16.     {
  17.         strcpy(a[i], buffer);
  18.         i++;
  19.         len++;
  20.     }
  21.     char rep = 'r';
  22.     while (rep == 'r')
  23.     {
  24.         initscr();
  25.         cbreak();
  26.         // noecho();
  27.         refresh();
  28.         freopen("/dev/tty", "r", stdin);
  29.  
  30.         start_color();
  31.         init_pair(1, COLOR_GREEN, COLOR_BLACK);
  32.         init_pair(2, COLOR_RED, COLOR_BLACK);
  33.         init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  34.         init_pair(4, COLOR_BLUE, COLOR_BLACK);
  35.  
  36.         attron(COLOR_PAIR(3));
  37.         time_t start_time, end_time;
  38.         int typed = 0, mistakes = 0, correct = 1;
  39.  
  40.         int x;
  41.         int low = 0, high = 0, word = 0;
  42.         // printw("%d\n", len);
  43.         echo();
  44.         // printw("Enter \n1 for Easy\n2 for Medium\n3 for Hard\n");
  45.         refresh();
  46.         flushinp();
  47.         // noecho();
  48.         // while (1)
  49.         // {
  50.         //     scanw(" %d", &x);
  51.         //     if (x < 1 || x > 3)
  52.         //     {
  53.         //         printw("Invalid input!");
  54.         //         flushinp();
  55.         //     }
  56.         //     else
  57.         //     {
  58.         //         break;
  59.         //     }
  60.         // }
  61.  
  62.         clear();
  63.         x = atoi(argv[1]);
  64.         if (x == 1)
  65.         {
  66.             low = 1;
  67.             high = 6;
  68.             word = 100;
  69.         }
  70.         else if (x == 2)
  71.         {
  72.             low = 6;
  73.             high = 9;
  74.             word = 75;
  75.         }
  76.         else
  77.         {
  78.             low = 8;
  79.             high = 20;
  80.             word = 50;
  81.         }
  82.  
  83.         int k = 0, l = 0;
  84.         srand(time(NULL));
  85.         while (l < word)
  86.         {
  87.             i = rand() % len;
  88.             if (strlen(a[i]) - 1 >= low && strlen(a[i]) - 1 <= high)
  89.             {
  90.                 for (int j = 0; j < strlen(a[i]) - 1; j++)
  91.                 {
  92.                     if (a[i][j] == ' ')
  93.                     {
  94.                         break;
  95.                     }
  96.                     c[k] = a[i][j];
  97.                     k++;
  98.                 }
  99.                 c[k] = ' ';
  100.                 k++;
  101.                 l++;
  102.                 // k = 0;
  103.             }
  104.             else
  105.             {
  106.                 continue;
  107.             }
  108.         }
  109.         c[k] = '\0';
  110.         printw("%s\n\n", c);
  111.         attroff(COLOR_PAIR(3));
  112.         attron(COLOR_PAIR(4));
  113.         if(x == 1) {
  114.             printw("Easy\n");
  115.         } else if(x == 2) {
  116.             printw("Medium\n");
  117.         } else {
  118.             printw("Hard\n");
  119.         }
  120.         attroff(COLOR_PAIR(4));
  121.         i = 0;
  122.         int j = 0;
  123.         char dd[1000];
  124.         memset(dd, 0, sizeof(dd));
  125.         time(&start_time);
  126.         printw("\n");
  127.  
  128.         while (c[i] != '\0')
  129.         {
  130.             time(&end_time);
  131.             move(10, 0);
  132.             clrtoeol();
  133.             clrtoeol();
  134.             printw("%.2f s remaining\tWPM: %.2f\tTyped: %d\tMistakes: %d\tCPS: %.2f\n", 30.0 - difftime(end_time, start_time), (60.0 * (((float)typed - mistakes) / (4.0 * x))) / (difftime(end_time, start_time)), typed, mistakes, (60.0 * ((((float)typed - mistakes)) / difftime(end_time, start_time))));
  135.             refresh();
  136.             char m[100];
  137.             // int j = 0;
  138.             if (difftime(end_time, start_time) >= 30.0)
  139.             {
  140.                 attron(COLOR_PAIR(3));
  141.                 printw("\nTime is up!\n");
  142.                 break;
  143.                 attroff(COLOR_PAIR(3));
  144.             }
  145.             noecho();
  146.             char x = getch();
  147.             refresh();
  148.             if (x == 27)
  149.             {
  150.                 break;
  151.             }
  152.             if (x == c[i] && correct == 1)
  153.             {
  154.                 dd[i] = x;
  155.                 attron(COLOR_PAIR(1));
  156.                 clrtoeol();
  157.                 printw("%s", dd);
  158.                 refresh();
  159.                 attroff(COLOR_PAIR(1));
  160.                 i++;
  161.                 typed++;
  162.                 correct = 1;
  163.                 j = 0;
  164.                 strcpy(m, "");
  165.             }
  166.             else if (x == KEY_BACKSPACE || x == KEY_DC || x == 127 && correct == 0)
  167.             {
  168.                 attron(COLOR_PAIR(1));
  169.                 clrtoeol();
  170.                 m[j - 1] = '\0';
  171.                 j--;
  172.                 printw("%s", dd);
  173.                 refresh();
  174.                 // printw("\b ");
  175.                 // refresh();
  176.                 attroff(COLOR_PAIR(1));
  177.                 attron(COLOR_PAIR(2));
  178.                 printw("%s", m);
  179.                 refresh();
  180.                 attroff(COLOR_PAIR(2));
  181.                 if (j == 0)
  182.                 {
  183.                     correct = 1;
  184.                 }
  185.             }
  186.             else if (x == KEY_BACKSPACE || x == KEY_DC || x == 127 && correct == 1)
  187.             {
  188.                 attron(COLOR_PAIR(1));
  189.                 clrtoeol();
  190.                 dd[i-1] = '\0';
  191.                 printw("%s", dd);
  192.                 // printw("\b ");
  193.                 refresh();
  194.                 attroff(COLOR_PAIR(1));
  195.                 i--;
  196.                 mistakes++;
  197.             }
  198.             else
  199.             {
  200.  
  201.                 attron(COLOR_PAIR(1));
  202.                 clrtoeol();
  203.                 printw("%s", dd);
  204.                 attroff(COLOR_PAIR(1));
  205.                 refresh();
  206.                 attron(COLOR_PAIR(2));
  207.                 m[j] = x;
  208.                 printw("%s", m);
  209.                 j++;
  210.                 mistakes++;
  211.                 correct = 0;
  212.                 attroff(COLOR_PAIR(2));
  213.             }
  214.             printw("\nType '%c'", c[i]);
  215.             clrtoeol();
  216.         }
  217.  
  218.         // printw("\n%s\n", c);
  219.         refresh();
  220.         attron(COLOR_PAIR(4));
  221.         printw("\nTyped: %d\nMistakes: %d\n", typed, mistakes);
  222.         float timeTaken = difftime(end_time, start_time);
  223.         printw("Time taken: %.2f s\n", timeTaken);
  224.         printw("WPM: %.2f\n", (60.0 * (((float)typed - mistakes) / (4.0 * x))) / timeTaken);
  225.         printw("CPS: %.2f\n", (60.0 * ((((float)typed - mistakes)) / timeTaken)));
  226.         attroff(COLOR_PAIR(4));
  227.         attron(COLOR_PAIR(2));
  228.         printw("Accuracy: %.2f%%\n", 100.0 - ((float)mistakes / typed) * 100);
  229.         attroff(COLOR_PAIR(2));
  230.         refresh();
  231.         printw("Enter ESC or 'q to quit and 'r' to play again.\n");
  232.         // char loop;
  233.         rep = getch();
  234.         while (1)
  235.         {
  236.             if (rep == 'r' || rep == 'q' || rep == 27)
  237.             {
  238.                 break;
  239.             }
  240.             else
  241.             {
  242.                 printw("Invalid input!\n");
  243.                 rep = getch();
  244.             }
  245.         }
  246.         clear();
  247.     }
  248.     endwin();
  249.     return 0;
  250. }
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement