f

Dependencies:   mbed 4DGL-uLCD-SE MMA8452

Revision:
6:453dc852ac0f
Parent:
0:8e3b9bb1084a
--- a/keyboard.cpp	Mon Mar 14 23:38:03 2022 +0000
+++ b/keyboard.cpp	Tue Apr 12 01:39:20 2022 +0000
@@ -2,8 +2,14 @@
 #include "dictionary.h"
 #include <string>
 
-
-
+DLinkedList* keyboard;
+LLNode* current;
+Word goalWord;
+char guess[5];
+char correctGuess[5];
+int guessLetter = 0;
+int j = 0; int ccount = 0;
+char* curr;
 
 int compareChar(void* input1, void* input2){
     if(*(char*)input1 == *(char*)input2){
@@ -14,37 +20,156 @@
     }
 }
 
+void lost_game() 
+{
+    uLCD.cls();
+    while(1)
+    {
+        uLCD.locate(1, 3);
+        uLCD.printf("Sorry You Lost");
+        uLCD.locate(3, 5);
+        uLCD.printf("Please Reset");   
+    }
+}
+
+void won_game()
+{
+    uLCD.cls();
+    while(1)
+    {
+        uLCD.locate(1, 3);
+        uLCD.printf("Congrats you WON!");
+        uLCD.locate(3, 5);
+        uLCD.printf("Please Reset");
+    }
+}
+
 
 void init_keyboard()
 {
-
+    int x = rand() % 11;
+    goalWord = dictionary[x];
+    keyboard = create_dlinkedlist(compareChar);
+    for (int i = 0; i < 26; i++) {
+        char* temp = (char*) malloc(sizeof(char));
+        *temp = 'a' + i;
+        insertTail(keyboard, temp);
+    }
+    current = keyboard->head;
+    char* test = (char*)(keyboard->head->data);
+    uLCD.locate(9, 13);
+    uLCD.printf("%c", *test);
+    uLCD.locate(1, 2);
+    uLCD.printf("Correct Letters");
 }
 
 
 void moveleft()
 {
-
+    if(current->prev)
+    {
+        current = current->prev;
+        uLCD.locate(9, 13);
+        uLCD.printf("%c", *((char*)current->data));
+        wait_ms(200);
+    }
+    
 }
 
 
 void moveright()
 {
-    
+    if(current->next)
+    {
+        current = current->next;
+        uLCD.locate(9, 13);
+        uLCD.printf("%c", *((char*)current->data));
+        wait_ms(200);
+    }
 }
 
 
 void select_letter()
 {   
-
+    if (guessLetter < 5) {
+        curr = (char*)(current->data);
+        guess[guessLetter] = *(curr);
+        
+        uLCD.locate(5 + guessLetter, 4 + j);
+        uLCD.printf("%c", guess[guessLetter]);
+        
+        guessLetter++;
+        if (guessLetter >= 5) {
+            guessLetter = 0;
+            j++;
+            wait_ms(200);
+            check_word();
+        }
+            
+    }
+    wait_ms(200);
 }
 
 
 void remove_letter()
 {   
-
+    if (guessLetter > 0) {
+        guessLetter--;
+        uLCD.locate(5 + guessLetter, 4 + j);
+        guess[guessLetter] = NULL;
+        uLCD.printf(" ");
+    }
+    wait_ms(200);
 }
 
 void check_word()
 {
-       
-}
+    int x; int nword; int count = 0;
+    char* a = goalWord.letters;
+    for (x = 0; x < 5; x++) {
+        if(guess[x] == *(a+x)) {
+            count++;                        
+        } else { 
+            count = 0;
+            int y;
+            for (y = 0; y < 5; y++) {
+                nword = 1;
+                if(guess[x] == *(a+y)) {
+                    pc.printf("%c\n", guess[x]);
+                    uLCD.color(WHITE);
+                    uLCD.locate(5+x, 3+j);
+                    uLCD.printf("%c", guess[x]); 
+                    uLCD.color(GREEN);
+                    nword = 0;   
+                    break;
+                }
+            }
+            if (nword) {
+                pc.printf("nword is 1!!!\n", guess[x]);
+                uLCD.locate(5+x, 3+j);
+                uLCD.printf(" ");
+                //char* test = (char*)(keyboard->head->data);
+                deleteNode(keyboard, findNode(keyboard, (char*)(guess+x)));
+                //uLCD.locate(9, 13);
+                //uLCD.printf("%c", *test);
+            } else {
+                correctGuess[x] = guess[x];
+                int g;
+                for(g = 0; g < 5; g++) {
+                    if (correctGuess[g] == guess[x]) {
+                        break;                        
+                    } else {
+                        uLCD.locate(5 + ccount++, 3);
+                        uLCD.printf("%c", guess[x]);
+                        break;
+                    }
+                }
+            }
+        }
+    }
+    if (count == 5) {
+        won_game();
+    } else if (j==6) {
+        lost_game();
+    }    
+}
\ No newline at end of file