Dynamically Bind dang you! :)

This is a discussion on Dynamically Bind dang you! :) within the C++ Programming forums, part of the General Programming Boards category; I am working on a chess program. I have a base class called Piece and then other classes like Pawn, ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Dynamically Bind dang you! :)

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    This has been answered elsewhere.

    *thread closed*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 12:58 PM
  2. File I/O problem for dynamically allocated struct array
    By veecee in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2006, 09:28 PM
  3. What does bind() do, exactly?
    By Hunter2 in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-07-2005, 12:12 PM
  4. Bind();
    By SirCrono6 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-26-2005, 07:01 PM
  5. I need a dynamically allocated linked list
    By Flex in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 01:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21