I am working on a chess program. I have a base class called Piece and then other classes like Pawn, Rook, etc... Problem is that I can't get them to do dynamic binding. Here's the code that needs to do the dynamic binding:
Code:
vector<location> Board::possibleMoves(location loc)
{
Piece* temp = &board[loc.row][loc.col];
vector<location> listLocs;
listLocs = temp->getMoves(board);
return listLocs;
}
in the piece class it looks like:
Code:
virtual vector<location> getMoves(vector<vector<Piece> > board)
{ cout<<"pay attention: "<<endl;
vector<location> temp;
return temp;
}
Finally I want the pawn class to overload this particular function and it looks like:
Code:
vector<location> getMoves(vector<vector<Piece> > board)
{
cout<<"We made it"<<endl;
vector<location> temp;
if(isBlack)
{
location loc1;
if(row==1)
{
loc1.row=row;
loc1.col=col+1;
temp.push_back(loc1);
loc1.col=col+2;
temp.push_back(loc1);
}
else
{
loc1.row=row;
loc1.col=col+1;
if(loc1.col<=7)
temp.push_back(loc1);
}
if(row+1<=7 && col+1<=7 && board[row+1][col+1].getRow()!=-1)
{
loc1.row=row+1;
loc1.col=col+1;
temp.push_back(loc1);
}
if(row+1<=7 && col-1>=0 && board[row+1][col-1].getRow()!=-1)
{
loc1.row=row+1;
loc1.col=col-1;
temp.push_back(loc1);
}
}
else
{
location loc1;
if(row==6)
{
loc1.row=row;
loc1.col=col-1;
temp.push_back(loc1);
loc1.col=col-2;
temp.push_back(loc1);
}
else
{
loc1.row=row;
loc1.col=col-1;
if(loc1.col>=0)
temp.push_back(loc1);
}
if(row-1>=0 && col+1<=7 && board[row-1][col+1].getRow()!=-1)
{
loc1.row=row-1;
loc1.col=col+1;
temp.push_back(loc1);
}
if(row-1>=0 && col-1>=0 && board[row-1][col-1].getRow()!=-1)
{
loc1.row=row-1;
loc1.col=col-1;
temp.push_back(loc1);
}
}
return temp;
}
You can see my debug code. It's still in there. When the function is called, it prints "pay attention" instead of "we made it". Any thoughts on what needs to be changed to make this perform dynamic binding?