Main Page | Namespace List | Class Hierarchy | Data Structures | File List | Data Fields | Globals

D:/Archiv/Projekte/OpenGL/quaxbomber03/GameArea.cpp

Go to the documentation of this file.
00001 #include "GameArea.h"
00002 #include "GameTile.h"
00003 #include "CoveredGameTileModel.h"
00004 #include "UncoveredGameTileModel.h"
00005 
00006 #include <math.h>
00007 #include <time.h>
00008 #include <iostream>
00009 #include <stdexcept>
00010 #include <vector>
00011 
00012 using namespace std;
00013 
00014 
00027 GameArea::GameArea(Level level) {
00028   // set the current Level
00029   this->level = level;
00030   // set no° of GameTiles
00031   switch(level) {
00032     case EASY:
00033       this->dim = EASYSIZE;
00034       break;
00035     case MEDIUM:
00036       this->dim = MEDIUMSIZE;
00037       break;
00038     case HARD:
00039       this->dim = HARDSIZE;
00040       break;
00041     default:
00042       exit(0);
00043   }
00044   // calculate the no° of tiles tp draw
00045   this->tiles = dim*dim;
00046 
00047   gameTileArray = new GameTile[tiles];
00048   coveredGT = new CoveredGameTileModel();
00049   uncoveredGT = new UncoveredGameTileModel();
00050 
00051   int h = 0;
00052   int v = 0;
00053   int s = dim - 1;
00054     
00055   for(int index = 0; index < tiles; index++) {
00056     // coloumn
00057     h = index % dim; 
00058     // row
00059     v = index / dim;
00060     
00061     gameTileArray[index].addModel((Model*) coveredGT);
00062     gameTileArray[index].addModel((Model*) uncoveredGT);
00063     
00064     // upper left corner
00065     if ((h == 0)&&(v == 0)){
00066       //cout << "UL" << endl;
00067       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00068       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);   // down
00069       gameTileArray[index].addNeighbor(&gameTileArray[index+dim+1]); // down right
00070     }
00071     // upper right corner
00072     else if((h == s)&&(v == 0))  {
00073       //cout << "UR" << endl;
00074       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00075       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);    // down
00076       gameTileArray[index].addNeighbor(&gameTileArray[index+dim-1]);  // down left    
00077     }
00078     // first line, not the beginning and not the end
00079     else if ((h > 0)&&(h < s)&&(v == 0)) {
00080       //cout << "FIRST LINE" << endl;
00081       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00082       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00083       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);    // down
00084       gameTileArray[index].addNeighbor(&gameTileArray[index+dim+1]);    // down right
00085       gameTileArray[index].addNeighbor(&gameTileArray[index+dim-1]);  // down left    
00086     }
00087     // center of the gameTileArray
00088     else if((h > 0)&&(h < s)&&(v < s)&&(v > 0)) {
00089       //cout << "CENTER " << endl;
00090       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00091       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);    // down
00092       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00093       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00094       gameTileArray[index].addNeighbor(&gameTileArray[index-dim-1]);  // up left
00095       gameTileArray[index].addNeighbor(&gameTileArray[index-dim+1]);  // up right
00096       gameTileArray[index].addNeighbor(&gameTileArray[index+dim+1]);  // down right
00097       gameTileArray[index].addNeighbor(&gameTileArray[index+dim-1]);  // down left
00098     }
00099     // first coloumn, not the last line, not the first line
00100     else if((h == 0)&&(v < s)&&(v > 0)) {
00101       //cout << "FIRST COLUMN" << endl;
00102       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00103       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);    // down
00104       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00105       gameTileArray[index].addNeighbor(&gameTileArray[index-dim+1]);  // up right
00106       gameTileArray[index].addNeighbor(&gameTileArray[index+dim+1]);  // down right       
00107     }
00108     // last column, not the last line, not the first line
00109     else if ((h == s)&&(v < s)&&(v > 0)) {
00110       //cout << "LAST COLUMN" << endl;
00111       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00112       gameTileArray[index].addNeighbor(&gameTileArray[index+dim]);    // down
00113       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00114       gameTileArray[index].addNeighbor(&gameTileArray[index-dim-1]);    // up left
00115       gameTileArray[index].addNeighbor(&gameTileArray[index+dim-1]);  // down left
00116     } 
00117     // lower left corner
00118     else if((h == 0)&&(v == s)) {
00119       //cout << "LL" << endl;
00120       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00121       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00122       gameTileArray[index].addNeighbor(&gameTileArray[index-dim+1]);  // up right
00123     }
00124     // last line, not the beginning and not the end
00125     else if((h > 0)&&(h < s)&&(v == s)) {
00126       //cout << "LAST LINE" << endl ;
00127       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00128       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00129       gameTileArray[index].addNeighbor(&gameTileArray[index+1]);      // right
00130       gameTileArray[index].addNeighbor(&gameTileArray[index-dim-1]);  // up left
00131       gameTileArray[index].addNeighbor(&gameTileArray[index-dim+1]);  // up right   
00132     }
00133     // lower right corner
00134     else if ((h == s)&&(v == s)) {
00135       //cout << "LR" << endl;
00136       gameTileArray[index].addNeighbor(&gameTileArray[index-dim]);    // up
00137       gameTileArray[index].addNeighbor(&gameTileArray[index-1]);      // left
00138       gameTileArray[index].addNeighbor(&gameTileArray[index-dim-1]);  // up left
00139     }   
00140   }
00141   createItems(level);
00142 }
00143 
00144 
00170 bool GameArea::spreadItems(GameTile::InvisibleState it, int count) {
00171   int index, x, y;
00172   vector<GameTile*>::iterator iter;
00173   
00174   long s;
00175   srand(time(&s));
00176   
00177   while(count > 0) {
00178     // recalculate the field position
00179     index = (rand()) % (tiles);
00180     if(gameTileArray[index].currentIState == GameTile::CLEAN) {
00181       iter = gameTileArray[index].neighborList.begin();
00182       x = index%dim;
00183       y = index/dim;
00184       gameTileArray[index].setInvisibleState(it);
00185       if (it == GameTile::BOMB) {
00186         //cout << endl << "BombValue: " << it << "  x,y: " << x << "," << y << "  Case: ";    
00187         // update neighbors
00188         for(;iter < gameTileArray[index].neighborList.end(); iter++) {
00189           ((GameTile*)*iter)->bombCounter += 1;
00190         }
00191         //debugArrays(true, true, false);
00192       }
00193       // otherwise, if a life or timefreezer was spreaded increment the goodieCounters
00194       else if ((it == GameTile::LIFE)||(it == GameTile::TIMEFREEZER)) {
00195         //cout << endl << "LifeValue: " << it << "  x,y: " << x << "," << y << "  Case: ";
00196         for(;iter < gameTileArray[index].neighborList.end(); iter++) {
00197           //cout << ((GameTile*)*iter)->bombCounter << endl;          
00198           ((GameTile*)*iter)->goodieCounter += 1;
00199         }
00200         //debugArrays(true, false, true);
00201       }     
00202       count--;
00203     }
00204   }
00205   return true;
00206 }
00207 
00208 
00216 bool GameArea::createItems(Level l) {
00217   int y = 0;
00218   switch(l) {
00219     case EASY:      
00220       //cout << "create easy game area " << endl;
00221       if (spreadItems(GameTile::BOMB, EASYBOMBS) && 
00222           spreadItems(GameTile::LIFE, EASYLIFES)&&
00223           spreadItems(GameTile::TIMEFREEZER, EASYTIMEF)) 
00224       {
00225         return true;
00226       }
00227       else return false;
00228 
00229     case MEDIUM:
00230       //cout << "create medium game area" << endl;
00231       if (spreadItems(GameTile::BOMB, MEDIUMBOMBS) && 
00232           spreadItems(GameTile::LIFE, MEDIUMLIFES) &&
00233           spreadItems(GameTile::TIMEFREEZER, MEDIUMTIMEF)) 
00234       {
00235         return true;
00236       }
00237       else return false;
00238     case HARD:
00239       //cout << "create hard game area" << endl;
00240       if (spreadItems(GameTile::BOMB, HARDBOMBS) && 
00241           spreadItems(GameTile::LIFE, HARDLIFES) &&
00242           spreadItems(GameTile::TIMEFREEZER, HARDTIMEF)) 
00243       {
00244         return true;
00245       }
00246       else return false;
00247     default:
00248       //cout << "error in: GameArea::createItemsArray(Level l)" << endl;
00249       return false;
00250   }
00251 }
00252 
00253 
00258 void GameArea::renderGameArea() {
00259   glPushMatrix();
00260     GLfloat hOffset = 0.0f;
00261     GLfloat vOffset = 0.0f; 
00262     for(int v = 0; v < dim; v++) {
00263       vOffset = v * 2.0f;
00264       for(int h = 0; h < dim; h++) {
00265         glPushMatrix();
00266           hOffset = h * 2.0f;
00267           glTranslatef(hOffset, 0.0f, vOffset);
00268           gameTileArray[v*dim+h].renderModel();
00269           //cout << "currentState" << gameTileArray[v*dim+h].currentVState << endl;
00270         glPopMatrix();
00271       }
00272     }   
00273   glPopMatrix();
00274 }
00275 
00276 
00287 void GameArea::uncoverTile(int v, int h) {
00288   int offset = v*dim+h;
00289   // if calculated offset is in the array bounds
00290   if((offset < tiles)&&(offset >= 0)) {    
00291     gameTileArray[offset].uncover();  
00292   }
00293 }
00294 
00295 
00301 void GameArea::debugArrays(bool items = true, bool bombs = true, bool goodies = true) {
00302  int row = 0;
00303  cout << endl;
00304  if(items) {
00305    cout << "-------------------------------------------------------------" << endl;
00306    cout << "Field Values/ Invisible States " << endl;
00307    for(int x = 0; x < tiles; x++) {
00308       if(row < dim-1) {
00309         cout << gameTileArray[x].currentIState << " ";
00310         row++;
00311       }
00312       else {
00313         cout << gameTileArray[x].currentIState << " " << endl;
00314         row = 0;
00315       }     
00316     }
00317     row = 0;
00318   }
00319   if (bombs) {
00320     cout << "-------------------------------------------------------------" << endl;
00321     cout << "Bomb Counters" << endl;
00322     for(int x = 0; x < tiles; x++) {
00323       if(row < dim-1) {
00324         cout << gameTileArray[x].bombCounter << " ";
00325         row++;
00326       }
00327       else {
00328         cout << gameTileArray[x].bombCounter << " " << endl;
00329         row = 0;
00330       }     
00331     }
00332     row = 0;
00333   }
00334   if (goodies) {
00335     cout << "-------------------------------------------------------------" << endl;
00336     cout << "Goodie Counters" << endl;
00337     for(int x = 0; x < tiles; x++) {
00338       if(row < dim-1) {
00339         cout << gameTileArray[x].goodieCounter << " ";
00340         row++;
00341       }
00342       else {
00343         cout << gameTileArray[x].goodieCounter << " " << endl;
00344         row = 0;
00345       }     
00346     }
00347     cout << "-------------------------------------------------------------" << endl;
00348     row = 0;
00349   }
00350 }
00351 
00352 
00353 

Generated on Fri Apr 8 00:11:46 2005 for Quaxbomber by  doxygen 1.4.1