Iman Anwarzai 1 year ago
parent
commit
13a3ab65f3
9 changed files with 276873 additions and 90 deletions
  1. 7 2
      Makefile
  2. 47 14
      board.c
  3. BIN
      boggle
  4. 12 3
      boggle.h
  5. 34 28
      dictionaryChecker.c
  6. 66 0
      scoreboard.c
  7. 51 40
      user.c
  8. 13 3
      users.txt
  9. 276643 0
      wordlist.txt

+ 7 - 2
Makefile

@@ -1,2 +1,7 @@
-boggle: board.c
-	gcc -Wall -std=c99 -o boggle board.c
+CC = gcc
+CFLAGS = -Wall -std=c99
+all: boggle
+boggle:
+	$(CC) $(CFLAGS) board.c boggle.h user.c scoreboard.c dictionaryChecker.c -o boggle
+clean:
+	rm boggle

+ 47 - 14
board.c

@@ -4,6 +4,7 @@
 #include <time.h>
 #include <ctype.h>
 #include "boggle.h"
+
 //extern exists;
 char dice[16][6]; 
 
@@ -91,7 +92,7 @@ int isaword(char ** arr, char * goal){
                 for(int j = 0; j < 4; j++)
                         isaword_helper(arr, temp, i, j, string, goal);
         freeall(temp);
-        free(temp);
+        //free(temp);
         free(string);
         return exists;
 
@@ -124,25 +125,48 @@ int main () {
 
         int score = 0;
 	system("clear");
-//	int user = userLogin();
-//	User * head = createLL();
-//	User * head_s;	
+	int user = userLogin();
+	User * head = malloc(sizeof(User));
+	createLL(head);
+	//char * ttt = malloc(9);
+	User_s * head_s = malloc(sizeof(User_s));
+	head_s->score = 0;
+	char name[9];
+	FILE * f = fopen("users.txt", "r");
+	for(int i = 0; i < user; i++)
+		fscanf(f, "%s ", name);
+	strcpy(head_s->name, name);
+	head_s->next = NULL;
+	//addUser(head_s, user, score);	
 	while(1){
                 display(arr);
                 printf("enter a word from the board, or n for the next board, or u to logout, or q to quit: ");
                 char * word = malloc(sizeof(char) * 16);
                 scanf("%s", word);
-		if(strcmp(word, "q") == 0)
-                       break;
+		if(strcmp(word, "q") == 0){
+		  	//addUser(head_s, user, score); 
+			userUpdate(head, user, score);
+			displayScoreboard(head_s);
+		        break;
+		}
 		else if(strcmp(word, "n") == 0){
                   	system("clear");
 			printf("your current score: %d \n", score);
-                        createboard(arr);
+                        displayScoreboard(head_s);
+			createboard(arr);
 		}
 		else if(strcmp(word, "u") == 0){
-	//		userUpdate(head, u, score);
-//			user = userLogin();
-//			addUser(head_s, user);
+			userUpdate(head, user, score);
+			//User * p = head;
+			//while(p->next)
+			//	p = p->next;
+			//p->next = malloc
+			//addUser(head_s, user, score); if next is a null
+			displayScoreboard(head_s);
+			user = userLogin();
+			addUser(head_s, user, 0);
+			printf("added blank user");
+			createLL(head);
 		}
 		else if(strlen(word) > 2){
 			int val = 0;
@@ -158,15 +182,23 @@ int main () {
 						break;
 					}
 				}
-                        if(val != 0 ){
+                        if(val != 0 && isInDictionary(word)){
                                 system("clear");
 				score += wordval(word);
                                 printf("word value: %d\n", wordval(word));
                                 printf("total score: %d\n", score);
+				User_s * p = head_s;
+				while(p->next)
+					p = p->next;
+				p->score = score;
                         }
+			else if(!isInDictionary(word)){
+				system("clear");
+				printf("that word is not in the dictionary\n");
+			}
 			else{
 				system("clear");
-				printf("that word doesn't exist\n");
+				printf("that word doesn't exist in the board\n");
 			}
 		}
 		else{
@@ -175,8 +207,9 @@ int main () {
 		}
 	free(word);
 	}
-//	displayScoreboard(user_s);
-//	freeLL(head);
+	//displayScoreboard(head_s);
+	//freeLL(head);
+//	freeLL_s(head_s);
 	printf("game over, thank you\n");
 	for(int i = 0; i < 4; i++)
 		free(arr[i]);

BIN
boggle


+ 12 - 3
boggle.h

@@ -13,8 +13,15 @@ typedef struct user {
         struct user * next;
 }User;
 
+typedef struct user_s {
+        char * name;
+        int score;
+        struct user_s* next;
+}User_s;
+
+void addUser(User_s * head, int user, int score);
 int validUser(char * str);
-User * createLL();
+void createLL( User * head);
 int userExists(char * str);
 int userLogin();
 void userUpdate(User * head, int un, int s);
@@ -22,6 +29,8 @@ void freeLL(User * head);
 void capitalize(char* s);
 int wordval(char * s);
 int isaword(char ** arr, char * goal);
-void addUser(User * head, int n);
-void displayScoreBoard(User * head);
+void createLL_s(User_s* head);
+void freeLL_s(User_s * head);
+void displayScoreboard(User_s * head);
+int isInDictionary(char * word);
 #endif

+ 34 - 28
dictionaryChecker.c

@@ -2,10 +2,27 @@
 #include <stdlib.h>
 #include <string.h>
 #include "boggle.h"
+#include <ctype.h>
+#define MAX_LINE 100
+#define BIG_HASH_SIZE 20000
+#define SMALL_HASH_SIZE 100
+
 typedef struct d_node {
 	char* key;
     struct d_node *next;
 }DNode;
+
+char *copystr(const char *s) { /* make a duplicate of s */
+        char *p;
+        int len = strlen(s);
+
+        p = (char *) malloc(len+1); /* +1 for í\0í */
+        if (p != NULL)
+                strncpy(p, s, len);
+        p[len] = '\0';
+
+        return p;
+}
 //form hash value for string s
 ////this produces a starting value in the dictionary array
 unsigned hash(const char *s) {
@@ -63,33 +80,22 @@ void free_dictionary (DNode ** dictionary, int hash_size) {
 		}
 	}
 }
-char *copystr(const char *s) { /* make a duplicate of s */
-	char *p;
-	int len = strlen(s);
 
-	p = (char *) malloc(len+1); /* +1 for í\0í */
-	if (p != NULL)
-		strncpy(p, s, len);
-	p[len] = '\0';
-
-	return p;
-}
-
-int isInDictionary(int argc, char ** argv) {
-	int i;
+int isInDictionary(char * word) {
+	//int i;
 	FILE *input_FP;
 	char line [MAX_LINE];
 	char * file_name;
 	DNode* result;
 	static DNode* big_dictionary [BIG_HASH_SIZE];
-	static DNode* small_dictionary[SMALL_HASH_SIZE];
+//	static DNode* small_dictionary[SMALL_HASH_SIZE];
 	
-
-	if (argc < 2) {
-		fprintf (stderr, "test_dictionary <dictionary file name>\n");
-		return 1;
-	}
-	file_name = argv [1];
+//
+//	if (argc < 2) {
+//		fprintf (stderr, "test_dictionary <dictionary file name>\n");
+//		return 1;
+//	}
+	file_name = "wordlist.txt";
 
 	if(!(input_FP = fopen ( file_name , "r" )))    {
         fprintf(stderr,"Could not open file \"%s\" for reading dictionary words\n", file_name);
@@ -101,21 +107,21 @@ int isInDictionary(int argc, char ** argv) {
 	}	
 	fclose (input_FP);
 
-	result = lookup (big_dictionary, BIG_HASH_SIZE, "APE");
+	result = lookup (big_dictionary, BIG_HASH_SIZE, word);
 	if (result != NULL) 
-		printf ("<APE> is in the dictionary\n");
+		return 1;
 	else
-		printf ("<APE> not found\n");
+		return 0;
 	
-	result = lookup (big_dictionary, BIG_HASH_SIZE, "MACAC");
+	/*result = lookup (big_dictionary, BIG_HASH_SIZE, "MACAC");
 	if (result != NULL) 
 		printf ("<MACAC> is in the dictionary\n");
 	else
 		printf ("<MACAC> not found\n");
-
+*/
 	free_dictionary(big_dictionary, BIG_HASH_SIZE);
 
-	printf ("\n new game session: 1\n");
+/*	printf ("\n new game session: 1\n");
 	//now testing small dictionary - twice - with cleanup
 	char key[] = "arc";
 	capitalize(key);
@@ -144,6 +150,6 @@ int isInDictionary(int argc, char ** argv) {
 	}
 		
 	free_dictionary(small_dictionary, SMALL_HASH_SIZE);
-
-	return 0;
+*/
+	return -10000;
 }

+ 66 - 0
scoreboard.c

@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "boggle.h"
+void displayScoreboard(User_s * head){
+        printf("Scoreboard\n----------\n");
+        User_s * p = head; //->next;
+        while(p){
+		int s = p->score;
+		char * name = malloc(9);
+		strcpy(name, p->name);
+                printf("%s \t %d\n", name, s);
+               // printf("one");
+		p = p->next;
+	//	printf("two");
+        }
+	//printf("done displaying\n");
+}
+/*void createLL_s(User_s* head){
+        FILE * userList_c;
+        //userList_c = fopen("users.txt", "r");
+        char name[9];
+        //fscanf(userList_c, "%s ", name);
+        //User_s * head;
+        head->name = malloc(strlen(name) + 1);
+        strcpy(head->name, name);
+        head->score = 0;
+        fclose(userList_c);
+        //return head;
+}*/
+
+void freeLL_s(User_s * head){
+        User_s * curr = head;
+        while(curr != NULL){
+                free(curr->name);
+                curr = curr->next;
+        }
+}
+void addUser(User_s * head, int user, int score){
+        FILE* userList_r;
+	userList_r = fopen("users.txt", "r");
+	char name[9];
+	int a, b, c;
+	for(int i = 0; i < user; i++){
+		fscanf(userList_r, "%s %d %d %d", name, &a, &b, &c);
+	}
+	//if(head->next == NULL){
+	//	printf("first user abt to be added");
+//		strcpy(head->name, name);
+//		head->score = score;
+	//	printf("first user added\n");
+	//	return;
+	//}
+	User_s * temp = head;
+        while(temp->next){
+                temp = temp->next;
+        }
+	//temp->score = score;
+        User_s * new_user = malloc(sizeof(User_s ));
+        new_user->name = malloc(9);
+	strcpy(new_user->name, name);
+	new_user->score = score;
+        temp->next = new_user;
+	//printf("added new user\n");
+	fclose(userList_r);
+}

+ 51 - 40
user.c

@@ -2,74 +2,82 @@
 #include "boggle.h"
 #include <string.h>
 
-void addUser(User * head, int n){
-	
-	if(head == NULL)
-		head = 
-}
-void displayScoreBoard(User * head){
-}
 int validUser(char * str){
         if(strlen(str) > 8)
                 return 0;
 	for(int i = 0; i < strlen(str); i++)
-		if((str[i] < 'a' | str[i] > 'z') && (str[i] != '-' | str[i] != '.'))
+		if(((str[i] < 'a') | (str[i] > 'z')) && ((str[i] != '-') | (str[i] != '.')) && (str[i] < '0') | (str[i] > '9'))
 			return 0;
 	return 1; 
 }
-User * createLL(){
+void createLL(User * head){
 	FILE * userList_c;
 	userList_c = fopen("users.txt", "r");
 	char name[9];
 	int hs, g, ts;
-	fscanf(userList_c, "%s %d %d %d \n", name, hs, g, ts);
-	if(name == ' ')
-		return NULL;
-	User * head = {name, hs, g, ts};
+	fscanf(userList_c, "%s %d %d %d \n", name, &hs, &g, &ts);
+	//User * head;
+	head->name = malloc(strlen(name) + 1);
+        strcpy(head->name, name);
+        head->highscore = hs;
+        head->total_games = g;
+        head->total_score = ts;
 	User * curr = head;
-	while(fscanf(userList_c, "%s %d %d %d \n", name, hs, g, ts) == 4){
-		User * temp = {name, hs, g, ts};
+	while(fscanf(userList_c, "%s %d %d %d \n", name, &hs, &g, &ts) == 4){
+		User * temp = malloc(sizeof(User));
+		temp->name = malloc(strlen(name) + 1);
+		strcpy(temp->name, name);
+		temp->highscore = hs;
+		temp->total_games = g;
+		temp->total_score = ts;
 		curr->next = temp;
 		curr = curr->next;
 	}
-	fclose(userList_c);
-	return head;	
+	fclose(userList_c);	
 }
-int userExists(char * str){
-	FILE * userList_r;
-	userList_r = fopen("users.txt", "r");
-	char name[9];
+int userNumberInFile(char * str){
+        FILE * userList_r;
+        userList_r = fopen("users.txt", "r");
+        char name[9];
         int hs, g, ts;
-	int c = 0;
-        while(fscanf(userList_r, "%s %d %d %d \n", name, hs, g, ts) == 4){
-		c ++;
-		if(strcmp(name, str) == 0)
-			return c;
-	}
-	return c;
+        int c = 0;
+        while(fscanf(userList_r, "%s %d %d %d \n", name, &hs, &g, &ts) == 4){
+                c ++;
+                if(strcmp(name, str) == 0)
+                        return c;
+        }
+	fclose(userList_r);
+        return -1;
+}
+int userExists(char * str){
+	if(userNumberInFile(str) > 0)
+		return 1;
+	return 0;
 }
 int userLogin(){
         FILE * userList_a;
 	userList_a = fopen("users.txt", "a");
 	char * user = malloc(sizeof(char) * 9);
-        scanf("Hello! Username? %s \n", user);
-        while(!validUser(user))
-	scanf("That is not a valid username.  Please enter another: %s \n", user);	
+        printf("Hello! Username? \n");
+	scanf("%s", user);
+        while(!validUser(user)){
+		printf("That is not a valid username.  Please enter another: \n");
+		scanf("%s", user);
+	}	
 	if(userExists(user)){
 		printf("Welcome back %s!\n", user);
-		return userExists(user);
 	}
 	else{
 		printf("Welcome new user!\n");
-		fprintf(userList_a, "%s %d %d\n", user, 0, 0);
+		fprintf(userList_a, "%s %d %d %d\n", user, 0, 0, 0);
 	}
-	fclose(userList_a); 
-	return userExists(user);
-			
+	fclose(userList_a);
+	int u = userNumberInFile(user);
+	return u;
 }
 void userUpdate(User * head, int un, int s){
 	FILE * userList_w;
-	userList_w = fopen(userList_w);
+	userList_w = fopen("users.txt", "w");
 	User * curr = head;
 	int c = 0;
 	while(curr != NULL){
@@ -78,10 +86,13 @@ void userUpdate(User * head, int un, int s){
 			if(curr->highscore < s)
 				curr->highscore = s;
 			curr->total_games = curr->total_games + 1;
-			curr->total_score = curr->total_score + score;
+			curr->total_score = curr->total_score + s;
 		}
-		fprintf("%s %d %d %d\n", curr->name, curr->highscore, curr->total_games, curr->total_score);
-	}	
+		char * str = curr->name;
+		fprintf(userList_w, "%s %d %d %d\n", str, curr->highscore, curr->total_games, curr->total_score);
+		curr = curr->next;
+	}
+	fclose(userList_w);	
 }
 void freeLL(User * head){
 	User * curr = head;

+ 13 - 3
users.txt

@@ -1,3 +1,13 @@
-iman 12 4 30
-ale 24 2 55
-alena 4 1 4
+iman 1 28 3
+alena 4 5 6
+ella 7 12 9
+ale 0 0 0
+aya 0 1 0
+cayleigh 0 0 0
+athena 0 0 0
+iman1 0 0 0
+imann2 0 0 0
+newuser 0 0 0
+anu 0 3 0
+ismael 0 1 0
+anushka 0 0 0

File diff suppressed because it is too large
+ 276643 - 0
wordlist.txt