1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
bool board::validateMove(const char& m,const string& l,const int& n, const int& fd, const int& md, int& mtype, bool& scoreMove) const{
if(!cell::validateLocation(l))//confirm existence of location
return false;
else{
cell* cp = cells.at(l);//initialize to leading cell
const char marble = cp->getMarble();
if(marble!=m)//trying to move a space or a marble that is not yours
return false;
for(int i=1;i<n;i++){
cp=cp->getAdjacent(fd);
if(cp==nullptr || marble!=cp->getMarble())//confirm availability and type of claimed resources
return false;
}
if(fd==getAdjacentIndex(md)){//in-line move
cp = cells.at(l);//initialize to leading cell
mtype=0;//how many opponent marbles are in front of the leading marble i.e in the move direction
for(int i=0;i<n;i++){
cp=cp->getAdjacent(md);
if(cp!=nullptr){//confirm availability of required resources
if(marble==cp->getMarble())//met own marble instead of space
return false;
if('+'!=cp->getMarble())//if it belongs to opponent
mtype++;//count opponent marbles
else
break;
}
else{
if(mtype>0)//opponent's marble will be pushed off the board
scoreMove=true;
else//if trying to push your own marble off the board
return false;
break;
}
}
if(mtype>=n)//should only push fewer than n of the opponent's marbles
return false;
}
else{//broad-side move
mtype=-1;
if(n<=1)//broad-side move must use at least 2 marbles
return false;
cp = cells.at(l);
for(int i=0;i<n;i++){
cell* dp=cp->getAdjacent(md);//destination pointer
if(dp!=nullptr && '+' == dp->getMarble())//is there an empty space to move to?
cp=cp->getAdjacent(fd);
else
return false;
}
}
return true;
}
}
bool board::executeMove(const char& m,const string& l,const int& n, const int& fd, const int& md){
int moveType = 0;//default to in-line with no following marbles
bool scoreMove = false;
bool valid = validateMove(m,l,n,fd,md,moveType,scoreMove);
if(valid){
if(moveType==-1){//broad-side
if(cells.find(l)==cells.end()) return false;
cell* cp = cells.at(l);
for(int i=0;i<n;i++){
if(cp==nullptr) return false;
cell* dp=cp->getAdjacent(md);//destination pointer
if(dp==nullptr)
return false;
dp->setMarble(cp->getMarble());
cp->setMarble('+');
cp=cp->getAdjacent(fd);
}
}
else{//in-line
cell* cp = cells.at(l), *rearp=cp;
const char marble = cp->getMarble();
cell* frontp=cp->getAdjacent(md);
for(int i=1;i<n;i++){
rearp=rearp->getAdjacent(fd);
}
if(scoreMove){
frontp->setMarble(rearp->getMarble());
rearp->setMarble('+');
if(marble=='O')
boc++;
if(marble=='@')
woc++;
}
else{
cell* tipp = frontp;
for(int i=0;i<moveType;i++){
tipp=tipp->getAdjacent(md);//looking for empty space
}
if(moveType!=0)//tipp is already equal to frontp
tipp->setMarble(frontp->getMarble());
frontp->setMarble(rearp->getMarble());
rearp->setMarble('+');
}
}
}
return valid;
}
|