File:  [Local Repository] / DewFind / GameBoard.cpp
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed Mar 2 17:51:54 2011 UTC (13 years, 9 months ago) by nick
Branches: MAIN, INITIAL
CVS tags: r_0, HEAD
Initial import

#include "StdAfx.h"
#include "resource.h"
#include "gameboard.h"

GameBoard::GameBoard()
{
	ClearWords();
	ClearBoard();
}

GameBoard::~GameBoard()
{
}

int GameBoard::GetWordsFound()
{
	int found=0;
	int x=0;
	for(x=0;x<wordcount_;x++)
	{
		if (foundword_[x] == true)
		{
			found++;
		}
	}
	
	return(found);
}

bool GameBoard::IsGameOver()
{
	bool ans=false;

	if (GetWordsFound() == wordcount_)
	{
		ans=true;
		ClearSelected();
	}

	return(ans);
}

void GameBoard::ClearWords()
{
	int z=0;
	wordcount_ = 0;
	for (z=0;z<MAXWORDS;z++)
	{
		words_[z] = "";
		foundword_[z] = false;
	}
}

void GameBoard::ClearBoard()
{
	int x=0;int y=0;int z=0;
	selects_ = 0;

	for(x=0;x<MAXROW;x++)
	{
		for(y=0;y<MAXCOL;y++)
		{
			board_[x][y].Clear(); 
		}
	}

	for (z=0;z<MAXWORDS;z++)
	{
		foundword_[z] = false;
	}
}


void GameBoard::SetUpBoard()
{
	//will place words where we want em
	//HandleDown(0);
	//HandleDown(1);
	//HandleDown(2);
	//HandleDown(3);
	//CreateDiagonally(4);
	//CreateDiagonally(5);
	int i=0;
	int ran = 0;
	for (i=0;i<wordcount_;i++)
	{
		ran = rand() % 3;
		if (ran == 1)
		{
			HandleDown(i);
		}
		else if (ran == 2)
		{
			CreateDiagonally(i);
		}
		else
		{
			HandleAcross(i);
		}
	}
	FillRandom();
}



void GameBoard::DrawBoard(CDC *xDC)
{
	Word Red,Green,Black;		
	Black.SetColor(0);
	Red.SetColor(1);
	Green.SetColor(2);

	int x=0,y=0,z=0,a=0;
	z = 50;
	a = 50;
	for (x=0;x<MAXROW;x++)
	{
		for(y=0;y<MAXCOL;y++)
		{
			if (board_[x][y].IsSelected() == true)
			{
				Red.Write(board_[x][y].GetLetter(),a,z,xDC);
			}
			else if (board_[x][y].IsFound() == true)
			{
				Green.Write(board_[x][y].GetLetter(),a,z,xDC);
			}
			else
			{
				Black.Write(board_[x][y].GetLetter(),a,z,xDC);
			}
			a = a + 19;
		}
		a = 50;
		z = z + 30;
	}
}

void GameBoard::DrawWords(CDC *xDC)
{
	Word wd;
	wd.SetColor(0);

	int words=0;
	int line=50;
	for (words=0;words<wordcount_;words++)
	{
		if (foundword_[words] == false)
		{
			wd.SetColor(0);
			wd.Write(words_[words],390,line,xDC);
			line = line + 30;
		}
		else
		{
			wd.SetColor(2);
			wd.Write(words_[words],390,line,xDC);
			line = line + 30;
		}
	}
}
int GameBoard::GetWordCount()
{
	return(wordcount_);
}
void GameBoard::LoadWordsFromFile(CString FN)
{
	//Load words from File
	//AddWord("NICK");
	//AddWord("BRENDAN");
	//AddWord("CHRIS");
	//AddWord("BRENT");
	//AddWord("JUSTIN");
	//AddWord("MATT");
	//ifstream inFile;
	CString loadword("");
	CStdioFile file;
	if(file.Open(FN,CFile::modeRead))
	{
		while (file.ReadString(loadword))
		{
			if(!loadword.IsEmpty())
			{
				loadword.MakeUpper();
				AddWord(loadword);
			}
		}
		file.Close();
	}
	else
	{
		AddWord("WORD");
		AddWord("FILE");
		AddWord("IS");
		AddWord("MISSING");
	}
}

bool GameBoard::SelectSquareMouse(int x,int y)
{
	bool ans=false;
	//Looks up selected
	int square=-1;
	int col=0,row=0;
	int current=0;

	int a=0,b=0,c=0,d=0;

	a = 50;
	b = 69;
	c = 50;
	d = 80;
 
	current = 0;
	
	for (row=0;row<MAXROW;row++)
	{
		for (col=0;col<MAXCOL;col++)
		{
			if (square == -1)
			{
				square = CheckLoc(x,y,a,b,c,d,current);
			};
			a = a + 19;
			b = b + 19;
			current++;
		}
		a = 50;
		b = 69;
		c = c + 30;
		d = d + 30;
	}
	//square = CheckLoc(x,y,a,b,c,d,0);
	//if (square == -1)
	//{
	//	square = CheckLoc(x,y,50,69,80,110,15);
	//};

	ans = HandleSelection(square);
	return(ans);
}

void GameBoard::ClearSelected()
{
	int x=0,y=0;
	for(x=0;x<MAXROW;x++)
	{
		for(y=0;y<MAXCOL;y++)
		{
			board_[x][y].SetSelect(false);
		}
	}
	selects_ = 0;
}

//--------------------------PRIVATE FUNCTIONS-------------------------------------------------------------

bool GameBoard::HandleSelection(int square)
{
	bool ans=false;
	int x=0,y=0;
	if (square > -1)
	{	
		x = square/15;
		y = square - (x*15);
		SelectSquare(x,y);
		ans = true;
	}

	return(ans);
}

int GameBoard::CheckLoc(int selx,int sely,int X,int X2,int Y,int Y2,int RTN)
{
 int locx=0;
 int locy=0;

	for (locx = X;locx < X2; locx++)
	{
		for (locy = Y; locy < Y2; locy++)
		{
			if ((locx == selx)&&(locy == sely))
				return(RTN);
		}
	}
 return(-1);
}

void GameBoard::SelectSquare(int x,int y)
{
	board_[x][y].SetSelect(true);
	//Check if word is found
	CheckWords();
	//On your 10th select clear it
	selects_++;
	if (selects_ > 9)
	{
		ClearSelected();
	}
}

void GameBoard::CheckWords()
{
	bool found=false;

	int x=0,y=0,wordnum=0,wordlen=0,sel=0;

	for(wordnum=0;wordnum<wordcount_;wordnum++)
	{
		wordlen = words_[wordnum].GetLength();
		sel = 0;
		if (foundword_[wordnum] == false)
		{
			for(x=0;x<MAXROW;x++)
			{
				for(y=0;y<MAXCOL;y++)
				{
					if ((board_[x][y].IsSelected() == true) && (board_[x][y].GetNumber() == wordnum))
					{
						sel++;
					}
				}
			}
			if (wordlen == sel)
			{
				WordFound(wordnum);
				foundword_[wordnum] = true;
				selects_ = 0;
				found = true;
			}
		}
	}
}

void GameBoard::WordFound(int wordnumber)
{
	int x=0,y=0;
	for(x=0;x<MAXROW;x++)
	{
		for(y=0;y<MAXCOL;y++)
		{
			if (board_[x][y].GetNumber() == wordnumber)
			{
				board_[x][y].SetFound();
			}
		}
	}
}

void GameBoard::AddWord(CString word)
{
	if (wordcount_ < MAXWORDS)
	{
		words_[wordcount_] = word;
		wordcount_++;
	};
}


void GameBoard::CreateDiagonally(int wordnumber)
{
	CString WordToFind = HandleBackward(wordnumber);

	int len=0;
	int open=0;
	int placex=-1; 
	int placey=-1;
	len = WordToFind.GetLength();
	int found = 0;
	int x=0;int y=0;
	int a=0;int b=0;

	//Find Spot on gameboard
	while ((found == 0)||(x > MAXROW))
	{
		if ((board_[a][b].GetLetter() == '1')&&(found == 0))
		{
			open = open + 1;
			a++;
			b++;
			if (open == len)
			{
					placex = x;
					placey = y;
					found = 1;
			}
			if (b > MAXCOL-1)
			{
				x++;
				y=0;
				a = x;
				b = y;
			}
			
		}
		else 
		{
			open = 0;
			y++;
			if (y > MAXCOL)
			{
				x++;
				y=0;
			}
			a = x;
			b = y;
		}
	}
	//Put word on gameboard
	if ((placex > -1)&&(placey > -1))
	{
		char word[MAXLETTERS];
		//WordToFind.copy(word,len,0);
		strcpy(word,WordToFind);
		int z=0;
		int position=len;
		
		while (position > 0)
		{
			board_[placex][placey].SetData(word[z],wordnumber);
			position--;
			z++;
			placex++;
			placey++;
		}
	}


}
bool GameBoard::HandleDown(int wordnumber)
{
	bool success=true;
	if (RandomDown(wordnumber) == false)
	{
		CreateDown(wordnumber);
	};
	return(success);
}

bool GameBoard::HandleAcross(int wordnumber)
{
	bool success=true;
	if (RandomAcross(wordnumber) == false)
	{
		CreateAcross(wordnumber);
	};
	return(success);
}

bool GameBoard::RandomAcross(int wordnumber)
{
	bool ans=false;
	CString WordToFind = HandleBackward(wordnumber);
	
	int len=0,open=0, placey=-1;
	len = WordToFind.GetLength();
	int found = 0;

	int y=0,boardx=0,boardy=0;
	
	boardx=rand() % 15;
	boardy=rand() % (15 - len);

	for (y=boardy;y<MAXCOL;y++)
	{
		if ((board_[boardx][y].GetLetter() == '1')&&(found == 0))
		{
				open = open + 1;
				if (open == len)
				{
					placey = (y - len)+1;
					found = 1;
				}
		}
		else 
		{
			open = 0;
		}
	}

	ans = PlaceAcross(boardx,placey,WordToFind,wordnumber);
	return(ans);
}

bool GameBoard::RandomDown(int wordnumber)
{
	bool ans = false;
	CString WordToFind = HandleBackward(wordnumber);
	
	int len=0,open=0, placex=-1, placey=-1;
	len = WordToFind.GetLength();
	int found = 0;
	int y=0;

	int boardx=0,boardy=0;
	
	boardx=rand() % (15 - len);
	boardy=rand() % 15;

	//boardx=3;
	//boardy=3;
		for (y=boardx;y<MAXROW;y++)
		{
			if ((board_[y][boardy].GetLetter() == '1')&&(found == 0))
			{
				open = open + 1;
				if (open == len)
				{
					placex = boardy;
					placey = (y - len)+1;
					found = 1;
				}
			}
			else 
			{
				open = 0;
			}

		}

	ans = PlaceDown(placex,placey,WordToFind,wordnumber);
	return(ans);
}


void GameBoard::CreateDown(int wordnumber)
{
	CString WordToFind = HandleBackward(wordnumber);

	//Find an open down spot
	int len=0,open=0, placex=-1, placey=-1;
	len = WordToFind.GetLength(); 
	int found = 0;
	int x=0;int y=0;

	for (x=0;x<MAXCOL;x++)
	{
		for (y=0;y<MAXROW;y++)
		{
			if ((board_[y][x].GetLetter() == '1')&&(found == 0))
			{
				open = open + 1;
				if (open == len)
				{
					placex = x;
					placey = (y - len)+1;
					found = 1;
				}
			}
			else 
			{
				open = 0;
			}

		}
		open = 0;
	}

	PlaceDown(placex,placey,WordToFind,wordnumber);
}

bool GameBoard::PlaceDown(int boardx,int boardy, CString word,int wordnumber)
{
	bool placed=true;
	char wordtmp[MAXLETTERS];
	int z=0, y=0;
	int len = word.GetLength();
	
	if ((boardx > -1)&&(boardy > -1))
	{
		//word.copy(wordtmp,len,0);
		strcpy(wordtmp,word);
		for(y=boardy;y<boardy+len;y++)
		{
			board_[y][boardx].SetData(wordtmp[z],wordnumber);
			z++;
		}
	}
	else
	{
		placed=false;
	}

	return(placed);
}

bool GameBoard::PlaceAcross(int boardx,int boardy,CString word,int wordnumber)
{
	bool placed=true;
	char wordtmp[MAXLETTERS];
	int z=0, y=0;
	int len = word.GetLength();

	if ((boardx > -1)&&(boardy > -1))
	{
		strcpy(wordtmp,word);
		//word.copy(wordtmp,len,0);
		for(y=boardy;y<boardy+len;y++)
		{
			board_[boardx][y].SetData(wordtmp[z],wordnumber);
			z++;
		}
	}
	else
	{
		placed=false;
	}

	return(placed);
}

CString GameBoard::HandleBackward(int wordnumber)
{
	//Will set word to backwards 50% of the time
	CString word ("");
	word = words_[wordnumber];
	int ran = rand() % 2;
	if (ran == 1)
	{
		word = ChangeToBackward(wordnumber);
	}

	return(word);
}

void GameBoard::CreateAcross(int wordnumber)
{
	CString WordToFind = HandleBackward(wordnumber);
	
	int len=0,open=0, placex=-1, placey=-1;
	len = WordToFind.GetLength();
	int found = 0;

	int x=0;int y=0;

	for (x=0;x<MAXROW;x++)
	{
		for (y=0;y<MAXCOL;y++)
		{
			if ((board_[x][y].GetLetter() == '1')&&(found == 0))
			{
				open = open + 1;
				if (open == len)
				{
					placex = x;
					placey = (y - len)+1;
					found = 1;
				}
			}
			else 
			{
				open = 0;
			}
		}
		open = 0;
	}

	PlaceAcross(placex,placey,WordToFind,wordnumber);
}


CString GameBoard::ChangeToBackward(int wordnumber)
{
	CString backwards ("");
	
	int len=0,tmpy=0;	
	char tChar;

	len = words_[wordnumber].GetLength();
	tmpy = len-1;
	for(tmpy;tmpy>=0;tmpy--)
	{
		tChar = words_[wordnumber].GetAt(tmpy);
		backwards = backwards + tChar;
	};

	return(backwards); 
}

char GameBoard::RandomLetter()
{
	int n = rand() % 26;
	char c = (char)(n+65);
	return(c);
}

void GameBoard::FillRandom()
{
	//change all 1's to a character
	int x=0;int y=0;
	for(x=0;x<MAXROW;x++)
	{
		for(y=0;y<MAXCOL;y++)
		{
			if (board_[x][y].GetLetter() == '1')
			{
				board_[x][y].SetData(RandomLetter(),-1);
			}
		}
	}
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>