NEED HELP WITH PROJECT

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 {

private:

TrieNode* root;
int count;


public:
Trie() {
root = new TrieNode();
count = 0;
}


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;

public:

void ReadFile(){
fin.open("/Users/Tommy/Desktop/englishWords.txt");

while (!fin.eof()) { // read whole file
getline(fin,aLine);
T.insert(aLine);
}
fin.close();
}

/* --------------------------------------------------------------
Function : ReadFile()

Purpose : Opens text file of words then reads every single line.
--------------------------------------------------------------- */


void instructions(){

ifstream readFile;
string line;

readFile.open("/Users/Tommy/Desktop/instructions.txt");

while(getline(readFile,line)) {
cout << endl;
cout << line << endl;
}
cin.get();
readFile.close();
}

/* --------------------------------------------------------------
Function : instructions()

Purpose : opens the text files of instructions on how to play the game.
--------------------------------------------------------------- */

void StartGame()
{
cout << " 👻 GHOST GAME 👻 " << endl;

cin.get();

do{
cout << "[1] START GAME" << endl;
cout << "[2] INSTRUCTIONS" << endl;
cout << "[3] EXIT GAME" << endl;
cin >> choice;

switch (choice) {
case 1:
playGame();
break;
case 2:
instructions();
cin.get();
break;
case 3:
cout << "GOOD BYE" << endl;
default:
break;}
}while(choice != 3);
}

/* --------------------------------------------------------------
Function : StartGame()

Purpose : To show the menu interface of the game
--------------------------------------------------------------- */


void playGame(){


ReadFile();
while(T.search(word)==false){
cout << "[" << word << "]" << endl;
cout<< "Player 1 Insert a letter"<<endl;
cin>> P1;
word+=P1;
if(T.search(word)==true )
{ cout << "Completed word: [ " << word << " ]" << endl;
cout<< "Player 2 Wins!"<<endl;
exit(EXIT_FAILURE);
}

cout << "[" << word << "]" << endl;
cout<< "Player 2 Insert a letter"<<endl;
cin>>P2;
word+=P2;

if(T.search(word)==true)
{ cout << "Completed word: [ " << word << " ]" << endl;
cout<< "Player 1 Wins!"<<endl;
exit(EXIT_FAILURE);
} } }

/* --------------------------------------------------------------
Function : playGame()

Purpose : the actual gameplay between player 1 and 2
--------------------------------------------------------------- */

};

int main()
{

GhostGame G1;
G1.StartGame();


}
Topic archived. No new replies allowed.