For my final project I am suppose to create the game " Ghost word game " and I used a Trie data structure, the problem is I don't have any validation for example it reads the file of words and if the player enters in a letter that doesn't match a letter in the word it will not stop and keep continue. Also I need to limit the minimal number of letters in a word spelled out to win is 5, how do I do so?
// Tommy Rachmam
#include <iostream>
#include <fstream> // LIBRARY of file input and output
#include <string> // LIBRARY of string data type
using namespace std;
// Created class of the trie node for the trie tree.
class TrieNode {
public:
int value; // Value or data held in the node
TrieNode* children[26]; // pointer array of 26 because alphabet hold 26 characters.
TrieNode()
{
value = 0;
for (int i=0;i<26;i++)
{children[i] = NULL;}
}
/* --------------------------------------------------------------
Function : TrieNode()
Purpose : Default constructor that intializes the default values to null.
--------------------------------------------------------------- */};
// Created a class that consists of a trie tree.
class Trie {
void insert(string s) {
TrieNode *temp = root;
long int len = s.size();
for (int i=0;i<len;i++){
int index = s[i] - 'a';
if (!temp->children[index]){
temp->children[index] = new TrieNode();
}
temp = temp->children[index];
}
count++;
temp->value = count;
}
/* --------------------------------------------------------------
Function : insert()
Purpose : Inserts a word into the trie.
--------------------------------------------------------------- */
bool search(string key) {
TrieNode *temp = root;
long int len = key.size();
for (int i=4;i<len;i++){
int idx = key[i] - 'a' ;
if (temp->children[idx]){
temp = temp->children[idx];
}
else{
return false;
}
}
if (temp->value > 0){
return true;
}
else{
return false;
}
}
/* --------------------------------------------------------------
Function : search()
Purpose : Returns if the word is in the trie.
--------------------------------------------------------------- */
};
class GhostGame{
private:
string aLine;
ifstream fin;
string word;
char P1,P2;
Trie T;
int choice = 0;