Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ncurses.h>
- #include <time.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- // keypad(stdscr, TRUE);
- char buffer[100];
- int i = 0;
- char a[800][20];
- int len = 0;
- char c[1000];
- while (scanf(" %[^\n]%*c", buffer) != EOF)
- {
- strcpy(a[i], buffer);
- i++;
- len++;
- }
- char rep = 'r';
- while (rep == 'r')
- {
- initscr();
- cbreak();
- // noecho();
- refresh();
- freopen("/dev/tty", "r", stdin);
- start_color();
- init_pair(1, COLOR_GREEN, COLOR_BLACK);
- init_pair(2, COLOR_RED, COLOR_BLACK);
- init_pair(3, COLOR_YELLOW, COLOR_BLACK);
- init_pair(4, COLOR_BLUE, COLOR_BLACK);
- attron(COLOR_PAIR(3));
- time_t start_time, end_time;
- int typed = 0, mistakes = 0, correct = 1;
- int x;
- int low = 0, high = 0, word = 0;
- // printw("%d\n", len);
- echo();
- // printw("Enter \n1 for Easy\n2 for Medium\n3 for Hard\n");
- refresh();
- flushinp();
- // noecho();
- // while (1)
- // {
- // scanw(" %d", &x);
- // if (x < 1 || x > 3)
- // {
- // printw("Invalid input!");
- // flushinp();
- // }
- // else
- // {
- // break;
- // }
- // }
- clear();
- x = atoi(argv[1]);
- if (x == 1)
- {
- low = 1;
- high = 6;
- word = 100;
- }
- else if (x == 2)
- {
- low = 6;
- high = 9;
- word = 75;
- }
- else
- {
- low = 8;
- high = 20;
- word = 50;
- }
- int k = 0, l = 0;
- srand(time(NULL));
- while (l < word)
- {
- i = rand() % len;
- if (strlen(a[i]) - 1 >= low && strlen(a[i]) - 1 <= high)
- {
- for (int j = 0; j < strlen(a[i]) - 1; j++)
- {
- if (a[i][j] == ' ')
- {
- break;
- }
- c[k] = a[i][j];
- k++;
- }
- c[k] = ' ';
- k++;
- l++;
- // k = 0;
- }
- else
- {
- continue;
- }
- }
- c[k] = '\0';
- printw("%s\n\n", c);
- attroff(COLOR_PAIR(3));
- attron(COLOR_PAIR(4));
- if(x == 1) {
- printw("Easy\n");
- } else if(x == 2) {
- printw("Medium\n");
- } else {
- printw("Hard\n");
- }
- attroff(COLOR_PAIR(4));
- i = 0;
- int j = 0;
- char dd[1000];
- memset(dd, 0, sizeof(dd));
- time(&start_time);
- printw("\n");
- while (c[i] != '\0')
- {
- time(&end_time);
- move(10, 0);
- clrtoeol();
- clrtoeol();
- 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))));
- refresh();
- char m[100];
- // int j = 0;
- if (difftime(end_time, start_time) >= 30.0)
- {
- attron(COLOR_PAIR(3));
- printw("\nTime is up!\n");
- break;
- attroff(COLOR_PAIR(3));
- }
- noecho();
- char x = getch();
- refresh();
- if (x == 27)
- {
- break;
- }
- if (x == c[i] && correct == 1)
- {
- dd[i] = x;
- attron(COLOR_PAIR(1));
- clrtoeol();
- printw("%s", dd);
- refresh();
- attroff(COLOR_PAIR(1));
- i++;
- typed++;
- correct = 1;
- j = 0;
- strcpy(m, "");
- }
- else if (x == KEY_BACKSPACE || x == KEY_DC || x == 127 && correct == 0)
- {
- attron(COLOR_PAIR(1));
- clrtoeol();
- m[j - 1] = '\0';
- j--;
- printw("%s", dd);
- refresh();
- // printw("\b ");
- // refresh();
- attroff(COLOR_PAIR(1));
- attron(COLOR_PAIR(2));
- printw("%s", m);
- refresh();
- attroff(COLOR_PAIR(2));
- if (j == 0)
- {
- correct = 1;
- }
- }
- else if (x == KEY_BACKSPACE || x == KEY_DC || x == 127 && correct == 1)
- {
- attron(COLOR_PAIR(1));
- clrtoeol();
- dd[i-1] = '\0';
- printw("%s", dd);
- // printw("\b ");
- refresh();
- attroff(COLOR_PAIR(1));
- i--;
- mistakes++;
- }
- else
- {
- attron(COLOR_PAIR(1));
- clrtoeol();
- printw("%s", dd);
- attroff(COLOR_PAIR(1));
- refresh();
- attron(COLOR_PAIR(2));
- m[j] = x;
- printw("%s", m);
- j++;
- mistakes++;
- correct = 0;
- attroff(COLOR_PAIR(2));
- }
- printw("\nType '%c'", c[i]);
- clrtoeol();
- }
- // printw("\n%s\n", c);
- refresh();
- attron(COLOR_PAIR(4));
- printw("\nTyped: %d\nMistakes: %d\n", typed, mistakes);
- float timeTaken = difftime(end_time, start_time);
- printw("Time taken: %.2f s\n", timeTaken);
- printw("WPM: %.2f\n", (60.0 * (((float)typed - mistakes) / (4.0 * x))) / timeTaken);
- printw("CPS: %.2f\n", (60.0 * ((((float)typed - mistakes)) / timeTaken)));
- attroff(COLOR_PAIR(4));
- attron(COLOR_PAIR(2));
- printw("Accuracy: %.2f%%\n", 100.0 - ((float)mistakes / typed) * 100);
- attroff(COLOR_PAIR(2));
- refresh();
- printw("Enter ESC or 'q to quit and 'r' to play again.\n");
- // char loop;
- rep = getch();
- while (1)
- {
- if (rep == 'r' || rep == 'q' || rep == 27)
- {
- break;
- }
- else
- {
- printw("Invalid input!\n");
- rep = getch();
- }
- }
- clear();
- }
- endwin();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement