Annotation of DewFind/GameBoard.cpp, revision 1.1.1.1
1.1 nick 1: #include "StdAfx.h"
2: #include "resource.h"
3: #include "gameboard.h"
4:
5: GameBoard::GameBoard()
6: {
7: ClearWords();
8: ClearBoard();
9: }
10:
11: GameBoard::~GameBoard()
12: {
13: }
14:
15: int GameBoard::GetWordsFound()
16: {
17: int found=0;
18: int x=0;
19: for(x=0;x<wordcount_;x++)
20: {
21: if (foundword_[x] == true)
22: {
23: found++;
24: }
25: }
26:
27: return(found);
28: }
29:
30: bool GameBoard::IsGameOver()
31: {
32: bool ans=false;
33:
34: if (GetWordsFound() == wordcount_)
35: {
36: ans=true;
37: ClearSelected();
38: }
39:
40: return(ans);
41: }
42:
43: void GameBoard::ClearWords()
44: {
45: int z=0;
46: wordcount_ = 0;
47: for (z=0;z<MAXWORDS;z++)
48: {
49: words_[z] = "";
50: foundword_[z] = false;
51: }
52: }
53:
54: void GameBoard::ClearBoard()
55: {
56: int x=0;int y=0;int z=0;
57: selects_ = 0;
58:
59: for(x=0;x<MAXROW;x++)
60: {
61: for(y=0;y<MAXCOL;y++)
62: {
63: board_[x][y].Clear();
64: }
65: }
66:
67: for (z=0;z<MAXWORDS;z++)
68: {
69: foundword_[z] = false;
70: }
71: }
72:
73:
74: void GameBoard::SetUpBoard()
75: {
76: //will place words where we want em
77: //HandleDown(0);
78: //HandleDown(1);
79: //HandleDown(2);
80: //HandleDown(3);
81: //CreateDiagonally(4);
82: //CreateDiagonally(5);
83: int i=0;
84: int ran = 0;
85: for (i=0;i<wordcount_;i++)
86: {
87: ran = rand() % 3;
88: if (ran == 1)
89: {
90: HandleDown(i);
91: }
92: else if (ran == 2)
93: {
94: CreateDiagonally(i);
95: }
96: else
97: {
98: HandleAcross(i);
99: }
100: }
101: FillRandom();
102: }
103:
104:
105:
106: void GameBoard::DrawBoard(CDC *xDC)
107: {
108: Word Red,Green,Black;
109: Black.SetColor(0);
110: Red.SetColor(1);
111: Green.SetColor(2);
112:
113: int x=0,y=0,z=0,a=0;
114: z = 50;
115: a = 50;
116: for (x=0;x<MAXROW;x++)
117: {
118: for(y=0;y<MAXCOL;y++)
119: {
120: if (board_[x][y].IsSelected() == true)
121: {
122: Red.Write(board_[x][y].GetLetter(),a,z,xDC);
123: }
124: else if (board_[x][y].IsFound() == true)
125: {
126: Green.Write(board_[x][y].GetLetter(),a,z,xDC);
127: }
128: else
129: {
130: Black.Write(board_[x][y].GetLetter(),a,z,xDC);
131: }
132: a = a + 19;
133: }
134: a = 50;
135: z = z + 30;
136: }
137: }
138:
139: void GameBoard::DrawWords(CDC *xDC)
140: {
141: Word wd;
142: wd.SetColor(0);
143:
144: int words=0;
145: int line=50;
146: for (words=0;words<wordcount_;words++)
147: {
148: if (foundword_[words] == false)
149: {
150: wd.SetColor(0);
151: wd.Write(words_[words],390,line,xDC);
152: line = line + 30;
153: }
154: else
155: {
156: wd.SetColor(2);
157: wd.Write(words_[words],390,line,xDC);
158: line = line + 30;
159: }
160: }
161: }
162: int GameBoard::GetWordCount()
163: {
164: return(wordcount_);
165: }
166: void GameBoard::LoadWordsFromFile(CString FN)
167: {
168: //Load words from File
169: //AddWord("NICK");
170: //AddWord("BRENDAN");
171: //AddWord("CHRIS");
172: //AddWord("BRENT");
173: //AddWord("JUSTIN");
174: //AddWord("MATT");
175: //ifstream inFile;
176: CString loadword("");
177: CStdioFile file;
178: if(file.Open(FN,CFile::modeRead))
179: {
180: while (file.ReadString(loadword))
181: {
182: if(!loadword.IsEmpty())
183: {
184: loadword.MakeUpper();
185: AddWord(loadword);
186: }
187: }
188: file.Close();
189: }
190: else
191: {
192: AddWord("WORD");
193: AddWord("FILE");
194: AddWord("IS");
195: AddWord("MISSING");
196: }
197: }
198:
199: bool GameBoard::SelectSquareMouse(int x,int y)
200: {
201: bool ans=false;
202: //Looks up selected
203: int square=-1;
204: int col=0,row=0;
205: int current=0;
206:
207: int a=0,b=0,c=0,d=0;
208:
209: a = 50;
210: b = 69;
211: c = 50;
212: d = 80;
213:
214: current = 0;
215:
216: for (row=0;row<MAXROW;row++)
217: {
218: for (col=0;col<MAXCOL;col++)
219: {
220: if (square == -1)
221: {
222: square = CheckLoc(x,y,a,b,c,d,current);
223: };
224: a = a + 19;
225: b = b + 19;
226: current++;
227: }
228: a = 50;
229: b = 69;
230: c = c + 30;
231: d = d + 30;
232: }
233: //square = CheckLoc(x,y,a,b,c,d,0);
234: //if (square == -1)
235: //{
236: // square = CheckLoc(x,y,50,69,80,110,15);
237: //};
238:
239: ans = HandleSelection(square);
240: return(ans);
241: }
242:
243: void GameBoard::ClearSelected()
244: {
245: int x=0,y=0;
246: for(x=0;x<MAXROW;x++)
247: {
248: for(y=0;y<MAXCOL;y++)
249: {
250: board_[x][y].SetSelect(false);
251: }
252: }
253: selects_ = 0;
254: }
255:
256: //--------------------------PRIVATE FUNCTIONS-------------------------------------------------------------
257:
258: bool GameBoard::HandleSelection(int square)
259: {
260: bool ans=false;
261: int x=0,y=0;
262: if (square > -1)
263: {
264: x = square/15;
265: y = square - (x*15);
266: SelectSquare(x,y);
267: ans = true;
268: }
269:
270: return(ans);
271: }
272:
273: int GameBoard::CheckLoc(int selx,int sely,int X,int X2,int Y,int Y2,int RTN)
274: {
275: int locx=0;
276: int locy=0;
277:
278: for (locx = X;locx < X2; locx++)
279: {
280: for (locy = Y; locy < Y2; locy++)
281: {
282: if ((locx == selx)&&(locy == sely))
283: return(RTN);
284: }
285: }
286: return(-1);
287: }
288:
289: void GameBoard::SelectSquare(int x,int y)
290: {
291: board_[x][y].SetSelect(true);
292: //Check if word is found
293: CheckWords();
294: //On your 10th select clear it
295: selects_++;
296: if (selects_ > 9)
297: {
298: ClearSelected();
299: }
300: }
301:
302: void GameBoard::CheckWords()
303: {
304: bool found=false;
305:
306: int x=0,y=0,wordnum=0,wordlen=0,sel=0;
307:
308: for(wordnum=0;wordnum<wordcount_;wordnum++)
309: {
310: wordlen = words_[wordnum].GetLength();
311: sel = 0;
312: if (foundword_[wordnum] == false)
313: {
314: for(x=0;x<MAXROW;x++)
315: {
316: for(y=0;y<MAXCOL;y++)
317: {
318: if ((board_[x][y].IsSelected() == true) && (board_[x][y].GetNumber() == wordnum))
319: {
320: sel++;
321: }
322: }
323: }
324: if (wordlen == sel)
325: {
326: WordFound(wordnum);
327: foundword_[wordnum] = true;
328: selects_ = 0;
329: found = true;
330: }
331: }
332: }
333: }
334:
335: void GameBoard::WordFound(int wordnumber)
336: {
337: int x=0,y=0;
338: for(x=0;x<MAXROW;x++)
339: {
340: for(y=0;y<MAXCOL;y++)
341: {
342: if (board_[x][y].GetNumber() == wordnumber)
343: {
344: board_[x][y].SetFound();
345: }
346: }
347: }
348: }
349:
350: void GameBoard::AddWord(CString word)
351: {
352: if (wordcount_ < MAXWORDS)
353: {
354: words_[wordcount_] = word;
355: wordcount_++;
356: };
357: }
358:
359:
360: void GameBoard::CreateDiagonally(int wordnumber)
361: {
362: CString WordToFind = HandleBackward(wordnumber);
363:
364: int len=0;
365: int open=0;
366: int placex=-1;
367: int placey=-1;
368: len = WordToFind.GetLength();
369: int found = 0;
370: int x=0;int y=0;
371: int a=0;int b=0;
372:
373: //Find Spot on gameboard
374: while ((found == 0)||(x > MAXROW))
375: {
376: if ((board_[a][b].GetLetter() == '1')&&(found == 0))
377: {
378: open = open + 1;
379: a++;
380: b++;
381: if (open == len)
382: {
383: placex = x;
384: placey = y;
385: found = 1;
386: }
387: if (b > MAXCOL-1)
388: {
389: x++;
390: y=0;
391: a = x;
392: b = y;
393: }
394:
395: }
396: else
397: {
398: open = 0;
399: y++;
400: if (y > MAXCOL)
401: {
402: x++;
403: y=0;
404: }
405: a = x;
406: b = y;
407: }
408: }
409: //Put word on gameboard
410: if ((placex > -1)&&(placey > -1))
411: {
412: char word[MAXLETTERS];
413: //WordToFind.copy(word,len,0);
414: strcpy(word,WordToFind);
415: int z=0;
416: int position=len;
417:
418: while (position > 0)
419: {
420: board_[placex][placey].SetData(word[z],wordnumber);
421: position--;
422: z++;
423: placex++;
424: placey++;
425: }
426: }
427:
428:
429: }
430: bool GameBoard::HandleDown(int wordnumber)
431: {
432: bool success=true;
433: if (RandomDown(wordnumber) == false)
434: {
435: CreateDown(wordnumber);
436: };
437: return(success);
438: }
439:
440: bool GameBoard::HandleAcross(int wordnumber)
441: {
442: bool success=true;
443: if (RandomAcross(wordnumber) == false)
444: {
445: CreateAcross(wordnumber);
446: };
447: return(success);
448: }
449:
450: bool GameBoard::RandomAcross(int wordnumber)
451: {
452: bool ans=false;
453: CString WordToFind = HandleBackward(wordnumber);
454:
455: int len=0,open=0, placey=-1;
456: len = WordToFind.GetLength();
457: int found = 0;
458:
459: int y=0,boardx=0,boardy=0;
460:
461: boardx=rand() % 15;
462: boardy=rand() % (15 - len);
463:
464: for (y=boardy;y<MAXCOL;y++)
465: {
466: if ((board_[boardx][y].GetLetter() == '1')&&(found == 0))
467: {
468: open = open + 1;
469: if (open == len)
470: {
471: placey = (y - len)+1;
472: found = 1;
473: }
474: }
475: else
476: {
477: open = 0;
478: }
479: }
480:
481: ans = PlaceAcross(boardx,placey,WordToFind,wordnumber);
482: return(ans);
483: }
484:
485: bool GameBoard::RandomDown(int wordnumber)
486: {
487: bool ans = false;
488: CString WordToFind = HandleBackward(wordnumber);
489:
490: int len=0,open=0, placex=-1, placey=-1;
491: len = WordToFind.GetLength();
492: int found = 0;
493: int y=0;
494:
495: int boardx=0,boardy=0;
496:
497: boardx=rand() % (15 - len);
498: boardy=rand() % 15;
499:
500: //boardx=3;
501: //boardy=3;
502: for (y=boardx;y<MAXROW;y++)
503: {
504: if ((board_[y][boardy].GetLetter() == '1')&&(found == 0))
505: {
506: open = open + 1;
507: if (open == len)
508: {
509: placex = boardy;
510: placey = (y - len)+1;
511: found = 1;
512: }
513: }
514: else
515: {
516: open = 0;
517: }
518:
519: }
520:
521: ans = PlaceDown(placex,placey,WordToFind,wordnumber);
522: return(ans);
523: }
524:
525:
526: void GameBoard::CreateDown(int wordnumber)
527: {
528: CString WordToFind = HandleBackward(wordnumber);
529:
530: //Find an open down spot
531: int len=0,open=0, placex=-1, placey=-1;
532: len = WordToFind.GetLength();
533: int found = 0;
534: int x=0;int y=0;
535:
536: for (x=0;x<MAXCOL;x++)
537: {
538: for (y=0;y<MAXROW;y++)
539: {
540: if ((board_[y][x].GetLetter() == '1')&&(found == 0))
541: {
542: open = open + 1;
543: if (open == len)
544: {
545: placex = x;
546: placey = (y - len)+1;
547: found = 1;
548: }
549: }
550: else
551: {
552: open = 0;
553: }
554:
555: }
556: open = 0;
557: }
558:
559: PlaceDown(placex,placey,WordToFind,wordnumber);
560: }
561:
562: bool GameBoard::PlaceDown(int boardx,int boardy, CString word,int wordnumber)
563: {
564: bool placed=true;
565: char wordtmp[MAXLETTERS];
566: int z=0, y=0;
567: int len = word.GetLength();
568:
569: if ((boardx > -1)&&(boardy > -1))
570: {
571: //word.copy(wordtmp,len,0);
572: strcpy(wordtmp,word);
573: for(y=boardy;y<boardy+len;y++)
574: {
575: board_[y][boardx].SetData(wordtmp[z],wordnumber);
576: z++;
577: }
578: }
579: else
580: {
581: placed=false;
582: }
583:
584: return(placed);
585: }
586:
587: bool GameBoard::PlaceAcross(int boardx,int boardy,CString word,int wordnumber)
588: {
589: bool placed=true;
590: char wordtmp[MAXLETTERS];
591: int z=0, y=0;
592: int len = word.GetLength();
593:
594: if ((boardx > -1)&&(boardy > -1))
595: {
596: strcpy(wordtmp,word);
597: //word.copy(wordtmp,len,0);
598: for(y=boardy;y<boardy+len;y++)
599: {
600: board_[boardx][y].SetData(wordtmp[z],wordnumber);
601: z++;
602: }
603: }
604: else
605: {
606: placed=false;
607: }
608:
609: return(placed);
610: }
611:
612: CString GameBoard::HandleBackward(int wordnumber)
613: {
614: //Will set word to backwards 50% of the time
615: CString word ("");
616: word = words_[wordnumber];
617: int ran = rand() % 2;
618: if (ran == 1)
619: {
620: word = ChangeToBackward(wordnumber);
621: }
622:
623: return(word);
624: }
625:
626: void GameBoard::CreateAcross(int wordnumber)
627: {
628: CString WordToFind = HandleBackward(wordnumber);
629:
630: int len=0,open=0, placex=-1, placey=-1;
631: len = WordToFind.GetLength();
632: int found = 0;
633:
634: int x=0;int y=0;
635:
636: for (x=0;x<MAXROW;x++)
637: {
638: for (y=0;y<MAXCOL;y++)
639: {
640: if ((board_[x][y].GetLetter() == '1')&&(found == 0))
641: {
642: open = open + 1;
643: if (open == len)
644: {
645: placex = x;
646: placey = (y - len)+1;
647: found = 1;
648: }
649: }
650: else
651: {
652: open = 0;
653: }
654: }
655: open = 0;
656: }
657:
658: PlaceAcross(placex,placey,WordToFind,wordnumber);
659: }
660:
661:
662: CString GameBoard::ChangeToBackward(int wordnumber)
663: {
664: CString backwards ("");
665:
666: int len=0,tmpy=0;
667: char tChar;
668:
669: len = words_[wordnumber].GetLength();
670: tmpy = len-1;
671: for(tmpy;tmpy>=0;tmpy--)
672: {
673: tChar = words_[wordnumber].GetAt(tmpy);
674: backwards = backwards + tChar;
675: };
676:
677: return(backwards);
678: }
679:
680: char GameBoard::RandomLetter()
681: {
682: int n = rand() % 26;
683: char c = (char)(n+65);
684: return(c);
685: }
686:
687: void GameBoard::FillRandom()
688: {
689: //change all 1's to a character
690: int x=0;int y=0;
691: for(x=0;x<MAXROW;x++)
692: {
693: for(y=0;y<MAXCOL;y++)
694: {
695: if (board_[x][y].GetLetter() == '1')
696: {
697: board_[x][y].SetData(RandomLetter(),-1);
698: }
699: }
700: }
701: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>