ELEC2645 (2018/19) / Mbed 2 deprecated el17ntkv

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Grid.cpp Source File

Grid.cpp

00001 #include "Grid.h"
00002 
00003 //Default grid object constructor
00004 Grid::Grid(){
00005 }
00006 
00007 //Default gid object destructor
00008 Grid::~Grid(){
00009 }
00010 
00011 void Grid::init(ScreenArray &screen,int ghostnumber){
00012    
00013     
00014     for(int b = 0 ; b < 48; b++) {
00015         screen.set(0,b,1);
00016     }
00017 
00018     for(int a = 0; a < 84; a++) {
00019         screen.set(a,0,1);
00020     }
00021 
00022     for(int b = 0 ; b <48; b++) {
00023         screen.set(83,b,1);
00024     }
00025 
00026     for(int a = 0; a < 84; a++) {
00027         screen.set(a,47,1);
00028     }
00029 
00030     screen.setRange(7,7,15,7,1);
00031     screen.setRange(16,13,6,12,1);
00032     screen.setRange(7,19,3,6,1);
00033     screen.setRange(0,32,9,8,1);
00034     screen.setRange(17,32,19,4,1);
00035     screen.setRange(30,36,6,4,1);
00036     screen.setRange(15,42,6,7,1);
00037     screen.setRange(28,0,7,11,1);
00038     screen.setRange(28,17,6,9,1);
00039     screen.setRange(44,12,5,6,1);
00040     screen.setRange(50,24,4,3,1);
00041     screen.setRange(42,27,12,4,1);
00042     screen.setRange(43,38,19,3,1);
00043     screen.setRange(51,41,3,7,1);
00044     screen.setRange(44,0,6,5,1);
00045     screen.setRange(60,7,13,4,1);
00046     screen.setRange(60,11,6,12,1);
00047     screen.setRange(60,30,9,2,1);
00048     screen.setRange(73,20,10,4,1);
00049     screen.setRange(69,38,10,3,1);
00050     screen.setRange(79,31,5,17,1);
00051 
00052     generateFruits(screen);
00053     generateGhosts(screen,ghostnumber);
00054 
00055 }
00056 
00057 //Mutator method that takes an its input parameter a screen array referece object
00058 //generating the fruit characters randomly within empty spaces of the screen array
00059 //denoted by the region consisting of a series of zeros
00060 void Grid::generateFruits(ScreenArray &screen){
00061     srand (time(NULL));
00062 
00063     int count = 0;
00064     while(count != 9) {
00065         bool addSprite = false;
00066         Charactersprites character = Charactersprites();
00067         while(addSprite == false) {
00068 
00069             int x = rand()%83 + 1;
00070 
00071             int y = rand()%47 + 1;
00072 
00073             int type = rand()%2 + 1;
00074 
00075             character.init(x,y,type);
00076 
00077             int overlapcount = 0;
00078             for(int a = character.getTopl().x - 2; a < character.getTopl().x + 5; a++) {
00079                 for(int b = character.getTopl().y - 2  ; b < character.getTopl().y + 5; b++) {
00080 
00081                     if(screen.get(a,b)!= 0) {
00082                         overlapcount++;
00083                     }
00084                 }
00085             }
00086             if(overlapcount == 0) {
00087                 addSprite = true;
00088             }
00089 
00090             if (!(character.getTopl().x < 83 && character.getTopl().y < 47   && character.getBottl().x < 83 && character.getBottl().y < 47 && character.getTopr().x < 83 && character.getTopr().y < 47  && character.getBottr().x < 83 && character.getBottr().y < 47  ) || !(character.getTopl().x >0 && character.getTopl().y >0   && character.getBottl().x >0 && character.getBottl().y >0 && character.getTopr().x >0 && character.getTopr().y >0  && character.getBottr().x >0 && character.getBottr().y >0  )) {
00091                 addSprite = false;
00092             }
00093             if(screen.get(character.getTopl().x,character.getTopl().y  ) !=0 &&screen.get(character.getBottl().x,character.getBottl().y ) != 0 && screen.get(character.getTopr().x,character.getTopr().y ) != 0 && screen.get(character.getBottr().x,character.getBottr().y ) != 0  ) {
00094                 addSprite = false;
00095             }
00096         }
00097         if(addSprite == true) {
00098             fruits.push_back(character);
00099             screen.setRange(character.getTopl().x,character.getTopl().y,3,3,character.getValue());
00100             count++;
00101         }
00102     }
00103 }
00104 
00105 
00106 //Mutator method that takes an its input parameter a screen array reference object
00107 //generating the ghost characters randomly within empty spaces of the screen array
00108 //denoted by the region consisting of a series of zeros
00109 void Grid::generateGhosts(ScreenArray &screen,int ghostnumber) {
00110     while(ghostnumber != 0) {
00111         bool addGhost = false;
00112         int x ;
00113         int y;
00114         Ghost ghost =  Ghost();
00115         while(addGhost == false) {
00116 
00117             x = rand()%72 + 10;
00118             y = rand()%36 + 10;
00119 
00120             ghost.init(2,x,y);
00121 
00122             int overlapcount =0;
00123             for(int a = ghost.getPos().x - 3; a < ghost.getPos().x +3; a++) {
00124                 for(int b = ghost.getPos().y - 3  ; b < ghost.getPos().y + 3; b++) {
00125 
00126                     if(screen.get(a,b)!= 0) {
00127                         overlapcount++;
00128                     }
00129                 }
00130             }
00131             if(overlapcount == 0) {
00132                 addGhost = true;
00133             }
00134         }
00135         if(addGhost == true) {
00136             ghosts.push_back(ghost);
00137             ghostnumber--;
00138         }
00139     }
00140 }
00141 
00142 
00143 void Grid::updateFruit(int x,int y,ScreenArray &screen) {
00144     int index = 99;
00145     for(std::vector<Charactersprites>::size_type i = 0; i != fruits.size(); i++) {
00146 
00147         if(x >= fruits.at(i).getTopl().x && x <= fruits.at(i).getTopr().x && y >= fruits.at(i).getTopl().y && y <= fruits.at(i).getBottl().y) {
00148             index = i;
00149         }
00150     }
00151     if(index != 99) {
00152         screen.setRange(fruits.at(index).getTopl().x,fruits.at(index).getTopl().y,3,3,0);
00153         fruits.erase(fruits.begin() + index);
00154     }
00155 }
00156 
00157 //Muatator method that takes as input a LCD screen reference object and an integer
00158 //representing the number of ghosts in the grid at that particular moment
00159 //displaying the grid,fruit characters and ghosts characters depending on their
00160 //particular positions within the grid
00161 void Grid::drawGrid(N5110 &lcd,int ghostnumber) {
00162     lcd.drawLine(0,0,0,47,1);
00163     lcd.drawLine(0,0,83,0,1);
00164     lcd.drawLine(83,0,83,48,1);
00165     lcd.drawLine(0,47,83,47,1);
00166     lcd.drawRect(7,7,15,7,FILL_BLACK);
00167     lcd.drawRect(16,13,6,12,FILL_BLACK);
00168     lcd.drawRect(7,19,3,6,FILL_BLACK);
00169     lcd.drawRect(0,32,9,8,FILL_BLACK);
00170     lcd.drawRect(17,32,19,4,FILL_BLACK);
00171     lcd.drawRect(30,36,6,4,FILL_BLACK);
00172     lcd.drawRect(15,42,6,7,FILL_BLACK);
00173     lcd.drawRect(28,0,7,11,FILL_BLACK);
00174     lcd.drawRect(28,17,6,9,FILL_BLACK);
00175     lcd.drawRect(44,12,5,6,FILL_BLACK);
00176     lcd.drawRect(50,24,4,3,FILL_BLACK);
00177     lcd.drawRect(42,27,12,4,FILL_BLACK);
00178     lcd.drawRect(43,38,19,3,FILL_BLACK);
00179     lcd.drawRect(51,41,3,7,FILL_BLACK);
00180     lcd.drawRect(44,0,6,5,FILL_BLACK);
00181     lcd.drawRect(60,7,13,4,FILL_BLACK);
00182     lcd.drawRect(60,11,6,12,FILL_BLACK);
00183     lcd.drawRect(60,30,9,2,FILL_BLACK);
00184     lcd.drawRect(73,20,10,4,FILL_BLACK);
00185     lcd.drawRect(69,38,10,3,FILL_BLACK);
00186     lcd.drawRect(79,31,5,17,FILL_BLACK);
00187 
00188     for(std::vector<Charactersprites>::size_type i = 0; i != fruits.size(); i++) {
00189         fruits.at(i).displaySprite(lcd);
00190     }
00191     for(std::vector<Ghost>::size_type i = 0; i != ghostnumber ; i++) {
00192         ghosts.at(i).draw(lcd);
00193     }
00194 
00195 }
00196 
00197 //Accessor method that takes as input a reference to the screen array object used
00198 //for collision detection within our program,the number of ghosts available within
00199 //the maze at this point in time within the game and the centre coordinates of
00200 //badman character and its radius for badman-ghost collision detection-
00201 //returning true if any badman-ghost collisions have taken place and false if none have taken
00202 //place
00203 bool Grid::update(ScreenArray &screen,int ghostdisplay,int x,int y,int r) {
00204     bool value = false;
00205     for(std::vector<Ghost>::size_type i = 0; i != ghostdisplay ; i++) {
00206         ghosts.at(i).update(screen);
00207 
00208         if(ghosts.at(i).collisiondetection( x, y, r) == true) {
00209             value = true;
00210         }
00211     }
00212     return value;
00213 }
00214 
00215 //Accessor method returns the number of fruits remaining in the vector that holds the number
00216 //of fruits remaining in the game
00217 int Grid::getFruitNumber() {
00218     return fruits.size();
00219 }
00220 
00221 
00222 
00223