//Name: David Yang, Sean McMillan, Dominic Golab
//File: Dictioanry.cpp
//For: CSE 30331: Scrabble Gmae
//12/6/09
/*Description: This file contains the functionality of the dictionary class. It holds a vector of words from the dictionary.txt file. It uses binary searching to find the word and returns a bool.*/


#include "dictionary.h"
#include <string>
#include <fstream>
#include <ctype.h>

dictionary::dictionary(void)//constructor
{
	std::string word;
	infile.open("dictionary.txt");//opens dictionary file
	while(!infile.eof())//adds all the words in it to the vector
	{
		infile>>word;
		dict.push_back(word);
	}
	infile.close();//closes the file
}

bool dictionary::search(std::string &s)//determines if a string is in the dictionary
{
	//convert word to lower case
	for(int i=0; i<(int)s.size(); i++)
	{
		s[i]=tolower(s[i]);
	}

	int first, last, mid;//used for binary search
	first=0; 
	last=dict.size()-1;

	while(first<last)//while the bounds of the search are still proper
	{
		mid=((first+last)/2);//the middle is halfway between first and last
		if (s.compare(dict[mid])==0)//if found the word
			return true;//return true
		else if(s.compare(dict[mid])<0)//if the word we are looking for is smaller than the word in the dictionary
			last=mid;//look at the earlier half of the boundaries
		else
			first=mid+1;//look at the later half of the boundaries
	}
	return false;//if doesnt find it then return false
}


