mvaganov

20200619 c++ game class

Jun 26th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. #include "platform_conio.h" // get this header file at http://nether.space/platform_conio
  2.  
  3. const char * dictionary[] = { // statically allocated 2D array
  4.     "type", "things", "stuff", "noun", "object",
  5.     "bruce", "daniel", "harrison", "william", "ray"
  6. };
  7.  
  8. // example create random word function (allocates memory)
  9. // TODO don't forget to free()
  10. char * randomWord(int size) {
  11.     char * word = (char*)malloc(size+1);
  12.     for(int i = 0; i < size; ++i){
  13.         word[i] = 'a' + (rand() % 26);
  14.     }
  15.     word[size] = '\0'; // putting the null terminator at the end of the string
  16.     return word;
  17. }
  18.  
  19. // return true if it should keep looping, false if it should break
  20. // cursor is being passed "by reference"
  21. // animIndex is being passed "by pointer"
  22. // normally, function arguments are passed "by value" (copies are made)
  23. bool getOneLetterAtATime(const char * wordToType, int & cursor, const char * winMessage, bool * keepAskingWords,
  24. const char * animation, int * animIndex ) {
  25.     //char letter = platform_getch(); // getch is a blocking call (waits till it processes)
  26.     long letter = platform_getchar(); // getchar is a non-blocking call (returns -1 if it doesn't process)
  27.     if(letter != -1) {
  28.         if(letter == wordToType[cursor]) {
  29.             cursor++;
  30.             platform_setColor(PLATFORM_COLOR_GREEN | PLATFORM_COLOR_INTENSITY, -1); putchar(letter); platform_setColor(-1, -1);
  31.         } else {
  32.             platform_setColor(PLATFORM_COLOR_RED, -1); printf("%c", letter); platform_setColor(-1, -1);
  33.         }
  34.         if(letter == 27) {
  35.             winMessage = "bye!";
  36.             *keepAskingWords = false;
  37.             return false;
  38.         } // stop if escape is pressed
  39.     } else {
  40.         putchar(animation[*animIndex]);
  41.         putchar('\b');
  42.         (*animIndex) ++;
  43.         if(*animIndex >= 4) (*animIndex) = 0;
  44.         platform_sleep(100);
  45.     }
  46.     return true;
  47. }
  48.  
  49. // count is passed by value
  50. // keepAskingWords is passed by pointer
  51. // animIndex is passed by reference
  52. void userTypesrandomWord(int count, bool * keepAskingWords, const char * animation, int & animIndex, char** dictionary) {
  53.     const char * wordToType = dictionary[rand() % count];              // a pointer pointing at a word
  54.     printf("you should type \"%s\": ", wordToType);
  55.     const char * winMessage = "You did it!";
  56.     fflush(stdout);
  57.     int cursor = 0;
  58.     int timeStart = platform_upTimeMS();
  59.     while(wordToType[cursor] != 0)
  60.     {
  61.         if(getOneLetterAtATime(wordToType, cursor, winMessage, keepAskingWords, animation, &animIndex) == false){ break; }
  62.     }
  63.     putchar('\n');
  64.     int timeEnd = platform_upTimeMS();
  65.     int totalTime = timeEnd - timeStart;
  66.     printf("that took %dms\n", totalTime);
  67.     printf("\n%s", winMessage);
  68. }
  69.  
  70. // TODO free this memory!
  71. char * loadEntireFile(const char * filename, int * out_count) {
  72.     // first, we open the file, and see how many bytes there are
  73.     FILE *ptr_file;
  74.     ptr_file = fopen(filename,"r");
  75.     char letter;
  76.     if (!ptr_file)
  77.         return NULL;
  78.     (*out_count) = 0;
  79.     while (!feof(ptr_file)) {
  80.         letter = fgetc(ptr_file);
  81.         // printf(" %d %c", count, letter);
  82.         ++(*out_count);
  83.     }
  84.     fclose(ptr_file);
  85.     printf("I need %d bytes\n", *out_count);
  86.     // allocate that many bytes
  87.     char* buffer = (char*)malloc((*out_count)+1); // the +1 is for a null terminator
  88.     // open file again, this time filling the allocated buffer with letters
  89.     ptr_file = fopen(filename,"r");
  90.     int index = 0;
  91.     while (!feof(ptr_file)) {
  92.         buffer[index++] = fgetc(ptr_file);
  93.     }
  94.     buffer[*out_count] = '\0';
  95.     fclose(ptr_file);
  96.     return buffer;
  97. }
  98. // TODO free this memory!
  99. char ** turnFileIntoDictionary(char * fileData, int count, int * out_howManyWords) {
  100.     *out_howManyWords = 1;
  101.     int linefeeds = 0, carriagereturns = 0;
  102.     // go through the entire file and replace all \n and \r with \0
  103.     for(int i = 0; i < count; ++i) {
  104.         // when a \n is found, increment out_howManyWords
  105.         if(fileData[i] == '\n') { linefeeds++; }
  106.         if(fileData[i] == '\r') { carriagereturns++; }
  107.         if(fileData[i] == '\n' || fileData[i] == '\r') { fileData[i] = '\0'; }
  108.     }
  109.     *out_howManyWords = (linefeeds > carriagereturns ? linefeeds : carriagereturns)+1;
  110.     //printf("found %d words\n", *out_howManyWords);
  111.     // create a list of char pointers out_howManyWords elements big
  112.     char ** dictionary = (char**)malloc(sizeof(char*)*(*out_howManyWords));
  113.  
  114.     // point each pointer from the list at a word in fileData(which has the null terminators separating the words)
  115.     dictionary[0] = &fileData[0];
  116.     bool readingNull = false;
  117.     int word = 1;
  118.     for(int i = 0; i < count; ++i) {
  119.         char c = fileData[i];
  120.         if(c == '\0'){
  121.             readingNull = true;
  122.         } else {
  123.             if(readingNull) {
  124.                 //printf("word %d at %d\n", word, i);
  125.                 dictionary[word++] = &fileData[i];
  126.                 readingNull = false;
  127.             }
  128.         }
  129.     }
  130.     return dictionary;
  131. }
  132. int main() {
  133.     int byteCount, wordCount;
  134.     char * fileData = loadEntireFile("words.txt", &byteCount);
  135.     char** bigDictionary = turnFileIntoDictionary(fileData, byteCount, &wordCount);
  136.     for(int i = 0; i < wordCount; ++i){
  137.         printf("[%d] \"%s\"\n", i, bigDictionary[i]);
  138.     }
  139.     // printf("%s\n", fileData);
  140.     int sizeOfRandomDictionary = 10;
  141.     char ** dynamicDict = (char**)malloc(sizeof(char*)*sizeOfRandomDictionary); // dynamically allocated 2D array
  142.     for(int i = 0; i < sizeOfRandomDictionary; ++i){
  143.         dynamicDict[i] = randomWord(8);
  144.     }
  145.     for(int i = 0; i < sizeOfRandomDictionary; ++i){
  146.         printf("[%d]: \"%s\"\n", i, dynamicDict[i]);
  147.     }
  148.     printf("%f\n", (float)1 / 1000);  //  <-- this is how you can print decimal numbers with printf
  149.     const char * animation = "/-\\|";
  150.     int animIndex = 0;
  151.     int count = sizeof(dictionary) / sizeof(dictionary[0]);
  152.     srand(time(NULL));
  153.     bool userWantsMoreWords = true;
  154.     while(userWantsMoreWords) {
  155.         userTypesrandomWord(wordCount, &userWantsMoreWords, animation, animIndex, bigDictionary);
  156.     }
  157.     // you assignment:
  158.     // * calculate words per minute
  159.         // wordsPerMinute = numberOfWords / numberOfMinutes
  160.         // numerOfMinutes = (ms / 1000) / 60 ?
  161.     // * calculate mistakes percentage (number of letters incorrect divided by total number of letters)
  162.     return 0;
  163. }
Add Comment
Please, Sign In to add comment