f
Dependencies: mbed 4DGL-uLCD-SE MMA8452
keyboard.cpp@6:453dc852ac0f, 2022-04-12 (annotated)
- Committer:
- dfrausto3
- Date:
- Tue Apr 12 01:39:20 2022 +0000
- Revision:
- 6:453dc852ac0f
- Parent:
- 0:8e3b9bb1084a
f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lballard9 | 0:8e3b9bb1084a | 1 | #include "keyboard.h" |
lballard9 | 0:8e3b9bb1084a | 2 | #include "dictionary.h" |
lballard9 | 0:8e3b9bb1084a | 3 | #include <string> |
lballard9 | 0:8e3b9bb1084a | 4 | |
dfrausto3 | 6:453dc852ac0f | 5 | DLinkedList* keyboard; |
dfrausto3 | 6:453dc852ac0f | 6 | LLNode* current; |
dfrausto3 | 6:453dc852ac0f | 7 | Word goalWord; |
dfrausto3 | 6:453dc852ac0f | 8 | char guess[5]; |
dfrausto3 | 6:453dc852ac0f | 9 | char correctGuess[5]; |
dfrausto3 | 6:453dc852ac0f | 10 | int guessLetter = 0; |
dfrausto3 | 6:453dc852ac0f | 11 | int j = 0; int ccount = 0; |
dfrausto3 | 6:453dc852ac0f | 12 | char* curr; |
lballard9 | 0:8e3b9bb1084a | 13 | |
lballard9 | 0:8e3b9bb1084a | 14 | int compareChar(void* input1, void* input2){ |
lballard9 | 0:8e3b9bb1084a | 15 | if(*(char*)input1 == *(char*)input2){ |
lballard9 | 0:8e3b9bb1084a | 16 | return 1; |
lballard9 | 0:8e3b9bb1084a | 17 | } |
lballard9 | 0:8e3b9bb1084a | 18 | else{ |
lballard9 | 0:8e3b9bb1084a | 19 | return 0; |
lballard9 | 0:8e3b9bb1084a | 20 | } |
lballard9 | 0:8e3b9bb1084a | 21 | } |
lballard9 | 0:8e3b9bb1084a | 22 | |
dfrausto3 | 6:453dc852ac0f | 23 | void lost_game() |
dfrausto3 | 6:453dc852ac0f | 24 | { |
dfrausto3 | 6:453dc852ac0f | 25 | uLCD.cls(); |
dfrausto3 | 6:453dc852ac0f | 26 | while(1) |
dfrausto3 | 6:453dc852ac0f | 27 | { |
dfrausto3 | 6:453dc852ac0f | 28 | uLCD.locate(1, 3); |
dfrausto3 | 6:453dc852ac0f | 29 | uLCD.printf("Sorry You Lost"); |
dfrausto3 | 6:453dc852ac0f | 30 | uLCD.locate(3, 5); |
dfrausto3 | 6:453dc852ac0f | 31 | uLCD.printf("Please Reset"); |
dfrausto3 | 6:453dc852ac0f | 32 | } |
dfrausto3 | 6:453dc852ac0f | 33 | } |
dfrausto3 | 6:453dc852ac0f | 34 | |
dfrausto3 | 6:453dc852ac0f | 35 | void won_game() |
dfrausto3 | 6:453dc852ac0f | 36 | { |
dfrausto3 | 6:453dc852ac0f | 37 | uLCD.cls(); |
dfrausto3 | 6:453dc852ac0f | 38 | while(1) |
dfrausto3 | 6:453dc852ac0f | 39 | { |
dfrausto3 | 6:453dc852ac0f | 40 | uLCD.locate(1, 3); |
dfrausto3 | 6:453dc852ac0f | 41 | uLCD.printf("Congrats you WON!"); |
dfrausto3 | 6:453dc852ac0f | 42 | uLCD.locate(3, 5); |
dfrausto3 | 6:453dc852ac0f | 43 | uLCD.printf("Please Reset"); |
dfrausto3 | 6:453dc852ac0f | 44 | } |
dfrausto3 | 6:453dc852ac0f | 45 | } |
dfrausto3 | 6:453dc852ac0f | 46 | |
lballard9 | 0:8e3b9bb1084a | 47 | |
lballard9 | 0:8e3b9bb1084a | 48 | void init_keyboard() |
lballard9 | 0:8e3b9bb1084a | 49 | { |
dfrausto3 | 6:453dc852ac0f | 50 | int x = rand() % 11; |
dfrausto3 | 6:453dc852ac0f | 51 | goalWord = dictionary[x]; |
dfrausto3 | 6:453dc852ac0f | 52 | keyboard = create_dlinkedlist(compareChar); |
dfrausto3 | 6:453dc852ac0f | 53 | for (int i = 0; i < 26; i++) { |
dfrausto3 | 6:453dc852ac0f | 54 | char* temp = (char*) malloc(sizeof(char)); |
dfrausto3 | 6:453dc852ac0f | 55 | *temp = 'a' + i; |
dfrausto3 | 6:453dc852ac0f | 56 | insertTail(keyboard, temp); |
dfrausto3 | 6:453dc852ac0f | 57 | } |
dfrausto3 | 6:453dc852ac0f | 58 | current = keyboard->head; |
dfrausto3 | 6:453dc852ac0f | 59 | char* test = (char*)(keyboard->head->data); |
dfrausto3 | 6:453dc852ac0f | 60 | uLCD.locate(9, 13); |
dfrausto3 | 6:453dc852ac0f | 61 | uLCD.printf("%c", *test); |
dfrausto3 | 6:453dc852ac0f | 62 | uLCD.locate(1, 2); |
dfrausto3 | 6:453dc852ac0f | 63 | uLCD.printf("Correct Letters"); |
lballard9 | 0:8e3b9bb1084a | 64 | } |
lballard9 | 0:8e3b9bb1084a | 65 | |
lballard9 | 0:8e3b9bb1084a | 66 | |
lballard9 | 0:8e3b9bb1084a | 67 | void moveleft() |
lballard9 | 0:8e3b9bb1084a | 68 | { |
dfrausto3 | 6:453dc852ac0f | 69 | if(current->prev) |
dfrausto3 | 6:453dc852ac0f | 70 | { |
dfrausto3 | 6:453dc852ac0f | 71 | current = current->prev; |
dfrausto3 | 6:453dc852ac0f | 72 | uLCD.locate(9, 13); |
dfrausto3 | 6:453dc852ac0f | 73 | uLCD.printf("%c", *((char*)current->data)); |
dfrausto3 | 6:453dc852ac0f | 74 | wait_ms(200); |
dfrausto3 | 6:453dc852ac0f | 75 | } |
dfrausto3 | 6:453dc852ac0f | 76 | |
lballard9 | 0:8e3b9bb1084a | 77 | } |
lballard9 | 0:8e3b9bb1084a | 78 | |
lballard9 | 0:8e3b9bb1084a | 79 | |
lballard9 | 0:8e3b9bb1084a | 80 | void moveright() |
lballard9 | 0:8e3b9bb1084a | 81 | { |
dfrausto3 | 6:453dc852ac0f | 82 | if(current->next) |
dfrausto3 | 6:453dc852ac0f | 83 | { |
dfrausto3 | 6:453dc852ac0f | 84 | current = current->next; |
dfrausto3 | 6:453dc852ac0f | 85 | uLCD.locate(9, 13); |
dfrausto3 | 6:453dc852ac0f | 86 | uLCD.printf("%c", *((char*)current->data)); |
dfrausto3 | 6:453dc852ac0f | 87 | wait_ms(200); |
dfrausto3 | 6:453dc852ac0f | 88 | } |
lballard9 | 0:8e3b9bb1084a | 89 | } |
lballard9 | 0:8e3b9bb1084a | 90 | |
lballard9 | 0:8e3b9bb1084a | 91 | |
lballard9 | 0:8e3b9bb1084a | 92 | void select_letter() |
lballard9 | 0:8e3b9bb1084a | 93 | { |
dfrausto3 | 6:453dc852ac0f | 94 | if (guessLetter < 5) { |
dfrausto3 | 6:453dc852ac0f | 95 | curr = (char*)(current->data); |
dfrausto3 | 6:453dc852ac0f | 96 | guess[guessLetter] = *(curr); |
dfrausto3 | 6:453dc852ac0f | 97 | |
dfrausto3 | 6:453dc852ac0f | 98 | uLCD.locate(5 + guessLetter, 4 + j); |
dfrausto3 | 6:453dc852ac0f | 99 | uLCD.printf("%c", guess[guessLetter]); |
dfrausto3 | 6:453dc852ac0f | 100 | |
dfrausto3 | 6:453dc852ac0f | 101 | guessLetter++; |
dfrausto3 | 6:453dc852ac0f | 102 | if (guessLetter >= 5) { |
dfrausto3 | 6:453dc852ac0f | 103 | guessLetter = 0; |
dfrausto3 | 6:453dc852ac0f | 104 | j++; |
dfrausto3 | 6:453dc852ac0f | 105 | wait_ms(200); |
dfrausto3 | 6:453dc852ac0f | 106 | check_word(); |
dfrausto3 | 6:453dc852ac0f | 107 | } |
dfrausto3 | 6:453dc852ac0f | 108 | |
dfrausto3 | 6:453dc852ac0f | 109 | } |
dfrausto3 | 6:453dc852ac0f | 110 | wait_ms(200); |
lballard9 | 0:8e3b9bb1084a | 111 | } |
lballard9 | 0:8e3b9bb1084a | 112 | |
lballard9 | 0:8e3b9bb1084a | 113 | |
lballard9 | 0:8e3b9bb1084a | 114 | void remove_letter() |
lballard9 | 0:8e3b9bb1084a | 115 | { |
dfrausto3 | 6:453dc852ac0f | 116 | if (guessLetter > 0) { |
dfrausto3 | 6:453dc852ac0f | 117 | guessLetter--; |
dfrausto3 | 6:453dc852ac0f | 118 | uLCD.locate(5 + guessLetter, 4 + j); |
dfrausto3 | 6:453dc852ac0f | 119 | guess[guessLetter] = NULL; |
dfrausto3 | 6:453dc852ac0f | 120 | uLCD.printf(" "); |
dfrausto3 | 6:453dc852ac0f | 121 | } |
dfrausto3 | 6:453dc852ac0f | 122 | wait_ms(200); |
lballard9 | 0:8e3b9bb1084a | 123 | } |
lballard9 | 0:8e3b9bb1084a | 124 | |
lballard9 | 0:8e3b9bb1084a | 125 | void check_word() |
lballard9 | 0:8e3b9bb1084a | 126 | { |
dfrausto3 | 6:453dc852ac0f | 127 | int x; int nword; int count = 0; |
dfrausto3 | 6:453dc852ac0f | 128 | char* a = goalWord.letters; |
dfrausto3 | 6:453dc852ac0f | 129 | for (x = 0; x < 5; x++) { |
dfrausto3 | 6:453dc852ac0f | 130 | if(guess[x] == *(a+x)) { |
dfrausto3 | 6:453dc852ac0f | 131 | count++; |
dfrausto3 | 6:453dc852ac0f | 132 | } else { |
dfrausto3 | 6:453dc852ac0f | 133 | count = 0; |
dfrausto3 | 6:453dc852ac0f | 134 | int y; |
dfrausto3 | 6:453dc852ac0f | 135 | for (y = 0; y < 5; y++) { |
dfrausto3 | 6:453dc852ac0f | 136 | nword = 1; |
dfrausto3 | 6:453dc852ac0f | 137 | if(guess[x] == *(a+y)) { |
dfrausto3 | 6:453dc852ac0f | 138 | pc.printf("%c\n", guess[x]); |
dfrausto3 | 6:453dc852ac0f | 139 | uLCD.color(WHITE); |
dfrausto3 | 6:453dc852ac0f | 140 | uLCD.locate(5+x, 3+j); |
dfrausto3 | 6:453dc852ac0f | 141 | uLCD.printf("%c", guess[x]); |
dfrausto3 | 6:453dc852ac0f | 142 | uLCD.color(GREEN); |
dfrausto3 | 6:453dc852ac0f | 143 | nword = 0; |
dfrausto3 | 6:453dc852ac0f | 144 | break; |
dfrausto3 | 6:453dc852ac0f | 145 | } |
dfrausto3 | 6:453dc852ac0f | 146 | } |
dfrausto3 | 6:453dc852ac0f | 147 | if (nword) { |
dfrausto3 | 6:453dc852ac0f | 148 | pc.printf("nword is 1!!!\n", guess[x]); |
dfrausto3 | 6:453dc852ac0f | 149 | uLCD.locate(5+x, 3+j); |
dfrausto3 | 6:453dc852ac0f | 150 | uLCD.printf(" "); |
dfrausto3 | 6:453dc852ac0f | 151 | //char* test = (char*)(keyboard->head->data); |
dfrausto3 | 6:453dc852ac0f | 152 | deleteNode(keyboard, findNode(keyboard, (char*)(guess+x))); |
dfrausto3 | 6:453dc852ac0f | 153 | //uLCD.locate(9, 13); |
dfrausto3 | 6:453dc852ac0f | 154 | //uLCD.printf("%c", *test); |
dfrausto3 | 6:453dc852ac0f | 155 | } else { |
dfrausto3 | 6:453dc852ac0f | 156 | correctGuess[x] = guess[x]; |
dfrausto3 | 6:453dc852ac0f | 157 | int g; |
dfrausto3 | 6:453dc852ac0f | 158 | for(g = 0; g < 5; g++) { |
dfrausto3 | 6:453dc852ac0f | 159 | if (correctGuess[g] == guess[x]) { |
dfrausto3 | 6:453dc852ac0f | 160 | break; |
dfrausto3 | 6:453dc852ac0f | 161 | } else { |
dfrausto3 | 6:453dc852ac0f | 162 | uLCD.locate(5 + ccount++, 3); |
dfrausto3 | 6:453dc852ac0f | 163 | uLCD.printf("%c", guess[x]); |
dfrausto3 | 6:453dc852ac0f | 164 | break; |
dfrausto3 | 6:453dc852ac0f | 165 | } |
dfrausto3 | 6:453dc852ac0f | 166 | } |
dfrausto3 | 6:453dc852ac0f | 167 | } |
dfrausto3 | 6:453dc852ac0f | 168 | } |
dfrausto3 | 6:453dc852ac0f | 169 | } |
dfrausto3 | 6:453dc852ac0f | 170 | if (count == 5) { |
dfrausto3 | 6:453dc852ac0f | 171 | won_game(); |
dfrausto3 | 6:453dc852ac0f | 172 | } else if (j==6) { |
dfrausto3 | 6:453dc852ac0f | 173 | lost_game(); |
dfrausto3 | 6:453dc852ac0f | 174 | } |
dfrausto3 | 6:453dc852ac0f | 175 | } |