Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "platform_conio.h" // get this header file at http://nether.space/platform_conio
- const char * dictionary[] = { // statically allocated 2D array
- "type", "things", "stuff", "noun", "object",
- "bruce", "daniel", "harrison", "william", "ray"
- };
- // example create random word function (allocates memory)
- // TODO don't forget to free()
- char * randomWord(int size) {
- char * word = (char*)malloc(size+1);
- for(int i = 0; i < size; ++i){
- word[i] = 'a' + (rand() % 26);
- }
- word[size] = '\0'; // putting the null terminator at the end of the string
- return word;
- }
- // return true if it should keep looping, false if it should break
- // cursor is being passed "by reference"
- // animIndex is being passed "by pointer"
- // normally, function arguments are passed "by value" (copies are made)
- bool getOneLetterAtATime(const char * wordToType, int & cursor, const char * winMessage, bool * keepAskingWords,
- const char * animation, int * animIndex ) {
- //char letter = platform_getch(); // getch is a blocking call (waits till it processes)
- long letter = platform_getchar(); // getchar is a non-blocking call (returns -1 if it doesn't process)
- if(letter != -1) {
- if(letter == wordToType[cursor]) {
- cursor++;
- platform_setColor(PLATFORM_COLOR_GREEN | PLATFORM_COLOR_INTENSITY, -1); putchar(letter); platform_setColor(-1, -1);
- } else {
- platform_setColor(PLATFORM_COLOR_RED, -1); printf("%c", letter); platform_setColor(-1, -1);
- }
- if(letter == 27) {
- winMessage = "bye!";
- *keepAskingWords = false;
- return false;
- } // stop if escape is pressed
- } else {
- putchar(animation[*animIndex]);
- putchar('\b');
- (*animIndex) ++;
- if(*animIndex >= 4) (*animIndex) = 0;
- platform_sleep(100);
- }
- return true;
- }
- // count is passed by value
- // keepAskingWords is passed by pointer
- // animIndex is passed by reference
- void userTypesrandomWord(int count, bool * keepAskingWords, const char * animation, int & animIndex, char** dictionary) {
- const char * wordToType = dictionary[rand() % count]; // a pointer pointing at a word
- printf("you should type \"%s\": ", wordToType);
- const char * winMessage = "You did it!";
- fflush(stdout);
- int cursor = 0;
- int timeStart = platform_upTimeMS();
- while(wordToType[cursor] != 0)
- {
- if(getOneLetterAtATime(wordToType, cursor, winMessage, keepAskingWords, animation, &animIndex) == false){ break; }
- }
- putchar('\n');
- int timeEnd = platform_upTimeMS();
- int totalTime = timeEnd - timeStart;
- printf("that took %dms\n", totalTime);
- printf("\n%s", winMessage);
- }
- // TODO free this memory!
- char * loadEntireFile(const char * filename, int * out_count) {
- // first, we open the file, and see how many bytes there are
- FILE *ptr_file;
- ptr_file = fopen(filename,"r");
- char letter;
- if (!ptr_file)
- return NULL;
- (*out_count) = 0;
- while (!feof(ptr_file)) {
- letter = fgetc(ptr_file);
- // printf(" %d %c", count, letter);
- ++(*out_count);
- }
- fclose(ptr_file);
- printf("I need %d bytes\n", *out_count);
- // allocate that many bytes
- char* buffer = (char*)malloc((*out_count)+1); // the +1 is for a null terminator
- // open file again, this time filling the allocated buffer with letters
- ptr_file = fopen(filename,"r");
- int index = 0;
- while (!feof(ptr_file)) {
- buffer[index++] = fgetc(ptr_file);
- }
- buffer[*out_count] = '\0';
- fclose(ptr_file);
- return buffer;
- }
- // TODO free this memory!
- char ** turnFileIntoDictionary(char * fileData, int count, int * out_howManyWords) {
- *out_howManyWords = 1;
- int linefeeds = 0, carriagereturns = 0;
- // go through the entire file and replace all \n and \r with \0
- for(int i = 0; i < count; ++i) {
- // when a \n is found, increment out_howManyWords
- if(fileData[i] == '\n') { linefeeds++; }
- if(fileData[i] == '\r') { carriagereturns++; }
- if(fileData[i] == '\n' || fileData[i] == '\r') { fileData[i] = '\0'; }
- }
- *out_howManyWords = (linefeeds > carriagereturns ? linefeeds : carriagereturns)+1;
- //printf("found %d words\n", *out_howManyWords);
- // create a list of char pointers out_howManyWords elements big
- char ** dictionary = (char**)malloc(sizeof(char*)*(*out_howManyWords));
- // point each pointer from the list at a word in fileData(which has the null terminators separating the words)
- dictionary[0] = &fileData[0];
- bool readingNull = false;
- int word = 1;
- for(int i = 0; i < count; ++i) {
- char c = fileData[i];
- if(c == '\0'){
- readingNull = true;
- } else {
- if(readingNull) {
- //printf("word %d at %d\n", word, i);
- dictionary[word++] = &fileData[i];
- readingNull = false;
- }
- }
- }
- return dictionary;
- }
- int main() {
- int byteCount, wordCount;
- char * fileData = loadEntireFile("words.txt", &byteCount);
- char** bigDictionary = turnFileIntoDictionary(fileData, byteCount, &wordCount);
- for(int i = 0; i < wordCount; ++i){
- printf("[%d] \"%s\"\n", i, bigDictionary[i]);
- }
- // printf("%s\n", fileData);
- int sizeOfRandomDictionary = 10;
- char ** dynamicDict = (char**)malloc(sizeof(char*)*sizeOfRandomDictionary); // dynamically allocated 2D array
- for(int i = 0; i < sizeOfRandomDictionary; ++i){
- dynamicDict[i] = randomWord(8);
- }
- for(int i = 0; i < sizeOfRandomDictionary; ++i){
- printf("[%d]: \"%s\"\n", i, dynamicDict[i]);
- }
- printf("%f\n", (float)1 / 1000); // <-- this is how you can print decimal numbers with printf
- const char * animation = "/-\\|";
- int animIndex = 0;
- int count = sizeof(dictionary) / sizeof(dictionary[0]);
- srand(time(NULL));
- bool userWantsMoreWords = true;
- while(userWantsMoreWords) {
- userTypesrandomWord(wordCount, &userWantsMoreWords, animation, animIndex, bigDictionary);
- }
- // you assignment:
- // * calculate words per minute
- // wordsPerMinute = numberOfWords / numberOfMinutes
- // numerOfMinutes = (ms / 1000) / 60 ?
- // * calculate mistakes percentage (number of letters incorrect divided by total number of letters)
- return 0;
- }
Add Comment
Please, Sign In to add comment