-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvedSpellList.c
110 lines (88 loc) · 3.16 KB
/
solvedSpellList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdlib.h>
#include <string.h>
#include "globals.h"
void initSolvedList(solvedSpellList** ppList)
{
int i;
solvedSpellList* list = malloc(sizeof(solvedSpellList)); //create list with DEFAULT_SOLVED_LEN constant size
list->entries = malloc(sizeof(solvedSpellListEntry*)*DEFAULT_SOLVED_LEN);
for (i=0;i<DEFAULT_SOLVED_LEN;++i)
list->entries[i] = NULL; //set everything to null so that we can add and remove at will
list->num_entries = 0;
*ppList = list;
}
int findSolvedWord(solvedSpellList** list, const char* word)
{
int i;
if ((*list)->num_entries == 0)
return 0;
for (i=0;i<DEFAULT_SOLVED_LEN;++i) //random access array. Do not use list->num_entries
//to iterate through the list and find valid entries
{
if ((*list)->entries[i] == NULL) //bypass empty entries
continue;
if (strcmp(word, (*list)->entries[i]->word) == 0) //check if the word is there
return 1;
}
return 0;
}
int getSolvedWordSuccess(solvedSpellList** list, const char* word)
{
int i;
if ((*list)->num_entries == 0)
return 0;
for (i=0;i<DEFAULT_SOLVED_LEN;++i) //random access array. Do not use list->num_entries
//to iterate through the list and find valid entries
{
if ((*list)->entries[i] == NULL) //bypass empty entries
continue;
if (strcmp(word, (*list)->entries[i]->word) == 0) //check if the word is there
return (*list)->entries[i]->correct;
}
return -1;
}
//remove a word from the list after we've processed it
void removeSolvedWord(solvedSpellList** list, const char* word)
{
int i;
if ((*list)->num_entries == 0)
return;
for (i=0;i<DEFAULT_SOLVED_LEN;++i) //random access array. Do not use list->num_entries
//to iterate through the list and find valid entries
{
if ((*list)->entries[i] == NULL) //bypass empty entries
continue;
if (strcmp(word, (*list)->entries[i]->word) == 0) //check if the word is there
{
free((*list)->entries[i]); //free memory and set back to null so that this space can be reused
(*list)->entries[i] = NULL;
--((*list)->num_entries); //decrement number of entries
break;
}
}
}
//Error we had: 'correct' was 2485, because we didn't add a break after finding an empty spot to
//add to the solved list; therefore, it added the same word DEFAULT_SOLVED_LEN times (1000)
void addSolvedWord(solvedSpellList** list, const char* word, int correct)
{
int i;
for (i=0;i<DEFAULT_SOLVED_LEN;++i)
{
if ((*list)->entries[i] == NULL) //find an empty spot
{
(*list)->entries[i] = malloc(sizeof(solvedSpellList));
strcpy((*list)->entries[i]->word,word);
(*list)->entries[i]->correct = correct;
++((*list)->num_entries);
break;
}
}
}
void destroySolvedList(solvedSpellList** list)
{
int i;
for (i=0;i<DEFAULT_SOLVED_LEN;++i)
if ((*list)->entries[i] != NULL)
free((*list)->entries[i]);
free((*list));
}