SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
invaderGrids.cpp
Go to the documentation of this file.
1
11#include<iostream>
12#include "invadersGrid.h"
13
15 return getOutterInvader()==size();
16}
17
19 unsigned i=size();
20 while(i>0){
21 --i;
22 if(at(i)!=InvaderType::NONE)return i;
23 }
24 return size();
25}
26
27
28// these are used to invoke rand() as less as possible
29
31
32
33 unsigned validTotal = 0;
34 for(InvaderType ite : *this){
35 if(ite!=InvaderType::NONE)++validTotal;
36 }
37
38 unsigned rd = rand() % validTotal;
39
40 unsigned validIndex = 0;
41 for(unsigned i=0;i<size();++i){
42 if(at(i)!=InvaderType::NONE){
43 if(validIndex==rd)return i;
44 ++validIndex;
45 }
46 }
47 throw runtime_error("SHOULD NOT HAPPEN : see randomValidInv()");
48}
49
51 unsigned validTotal = 0;
52 for(const InvadersColumn& i : *this){
53 if(!i.hasNoValid())++validTotal;
54 }
55 return validTotal;
56}
57
59
60
61
62 unsigned rd = rand() % validColsNumber();
63
64 unsigned validIndex = 0;
65 for(unsigned i=0;i<size();++i){
66 if(!at(i).hasNoValid()){
67 if(validIndex==rd)return i;
68 ++validIndex;
69 }
70 }
71 throw runtime_error("SHOULD NOT HAPPEN : see randomValidCol()");
72}
Column of invader.
Definition: invadersGrid.h:33
unsigned randomValidInv() const
Returns a random valid invader in the column (reminder, the column can contains NONE invaders)
unsigned getOutterInvader() const
gives the index of the last valid (type different than NONE) invader
bool hasNoValid() const
tells if the column contains no non type NONE invader
unsigned randomValidCol() const
Returns a random valid column in the grid.
unsigned validColsNumber() const
Helper function for randomValidCol() that returns the number of valid column (at least one invader in...
InvaderType
List of all invader type.
Definition: invadersGrid.h:22