CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    34

    Unhappy Having trouble with implentation file

    I'm having trouble getting the implementation file to work correctly.

    interface file
    Code:
    #ifndef GAMEBOARD_H
    #define GAMEBOARD_H
    
    #include <vector>
    #include <iostream>
    
    /*
    A class that encapsulates a game board. Each position
    on the game board can hold a single character,
    by default space. Initially, the gameboard is empty
    and the size is 0x0. If the user attempts to set
    a character to a position outside of the current
    coordiates, the size of the board will automatically
    expand. If the user attempts to get a character
    outside of the current size, it will return a space.
    */
    class GameBoard
    {
    public:
    	//construct an initially empty board
    	GameBoard();
    
    	//do-nothing destructor
    	~GameBoard();
    
    	//set the character at position x,y,
    	//expanding if necessary
    	void set(int x, int y, char val);
    
    	//get the character at position x,y
    	char get(int x, int y);
    
    	//set reference parameters x and y
    	//to the current maximum extents
    	//of the board
    	void getSize(int &x, int &y);
    
    	//write the board to the given
    	//output stream
    	void print(std::ostream &out);
    private:
    	//stores the data as a vector of vectors
    	std::vector<std::vector<char> > data;
    };
    
    #endif
    implementation file
    Code:
    //g++ -o gameboard.out gameboard.cpp mygame.cpp
    
    #include "gameboard.h"
    #include <iostream>
    
    using namespace std;
    ////////////////////////////////////////////////////////////
    GameBoard::~GameBoard()
    {
     // Just leave it blank
    }
    ////////////////////////////////////////////////////////////
    GameBoard::GameBoard()
    {
        int x = 0;
        int y = 0;
       
       
    }
    ////////////////////////////////////////////////////////////
    void GameBoard::set(int x, int y, char val)
    {
        while (x<0||y<0);
        {
            if (data.size()<=x)
            data.resize(x+1);
            if (data[x].size()<=y)
            data[x].resize(y+1,' ');
            data[x][y]= val;
               
               
        }
       
    }
    
    ////////////////////////////////////////////////////////////
    char GameBoard::get(int x, int y)
    {   
        char value;
        if(x<data[x].size() && y <data[x-1].size()) // use same as the get size
        {
            return data[x][y];
        }
            else
            {
                return ' ';
            }
    }
    
    ////////////////////////////////////////////////////////////
    void GameBoard::getSize(int &x, int &y)
    {
       
    // get vecotr of vectors largest hgeight for the y=value
    // the x-value is the vector of vectors
         x = data.size();
         y = data[x-1].size(); // minus 1
         //cout<<"4"<<endl;
    
    }
    
    ////////////////////////////////////////////////////////////
    void GameBoard::print(std::ostream &out)
    {
       
            for (int i=0; i<data.size(); i++)
            {
                for (int j=0; j<data[i].size();j++)
                {
                char theChar = data[i][j];
                out<<theChar;
                }
            out<<endl;
            }   
       
        //cout<<"5"<<endl;
    
    }
    ////////////////////////////////////////////////////////////
    cpp source file
    Code:
    #include "gameboard.h"
    #include <iostream>
    
    using namespace std;
    
    void setup(GameBoard &a)
    {
    	for (int i = 0; i < 30; i++)
    	{
    		a.set(i,0,'!');
    		a.set(i,10,'!');
    	}
    	for (int i = 1; i < 10; i++)
    	{
    		a.set(0,i,'#');
    		a.set(29,i,'#');
    	}
    }
    
    int main()
    {
    	GameBoard game;
    	setup(game);
    	game.set(3,8,'?');
    	cout << "Position at 56,70 is " << game.get(56,70) << endl;
    	cout << "Position at 0,0 is " << game.get(0,0) << endl;
    	game.print(cout);
    	return 0;
    }

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Having trouble with implentation file

    Quote Originally Posted by aurorablaze07 View Post
    I'm having trouble getting the implementation file to work correctly.
    Maybe you want to tell us about your trouble? Few people here are mind readers....
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Mar 2008
    Posts
    38

    Re: Having trouble with implentation file

    what is the error?

  4. #4
    Join Date
    Jun 2009
    Posts
    34

    Re: Having trouble with implentation file

    The two lines in the constructor serve no purpose at all. -commented from my teacher so i need to fix


    The loop you've defined in the set() is infinite, so call to set with invalid parameters will hang. Furthermore, if the parameters are valid, then the function does nothing.



    getSize will return the wrong information.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Having trouble with implentation file

    Quote Originally Posted by aurorablaze07 View Post
    The two lines in the constructor serve no purpose at all. -commented from my teacher so i need to fix
    You are declaring local variables x and y and setting them instead of the object variables. Don't declare variables in the constructor, just set them.
    Quote Originally Posted by aurorablaze07 View Post
    The loop you've defined in the set() is infinite, so call to set with invalid parameters will hang. Furthermore, if the parameters are valid, then the function does nothing.
    I don't understand as it's not clear to me what are valid and invalid parameters. If only positive values are allowed, then the parameters should be declared as unsigned int.
    Quote Originally Posted by aurorablaze07 View Post
    getSize will return the wrong information.
    Test with coordinates (0,5) and (3,0). What's the real size, what will your function return?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Having trouble with implentation file

    Quote Originally Posted by aurorablaze07 View Post

    The loop you've defined in the set() is infinite, so call to set with invalid parameters will hang. Furthermore, if the parameters are valid, then the function does nothing.
    Code:
    void GameBoard::set(int x, int y, char val)
    {
        while (x<0||y<0);
        {
            if (data.size()<=x)
            data.resize(x+1);
            if (data[x].size()<=y)
            data[x].resize(y+1,' ');
            data[x][y]= val;
               
               
        }
    Your while statement is terminated by the semi-colon you've included.

    Even without the semicolon, the statement runs only if x is negative or y is negative. Since size can never be negative data.size() can never be less than x when x is negative and it can never be less than y when y is negative. So it x and y are both less than 0, the loop runs indefinitely.

    Rather than just looping and resizing by 1 every time, why not just figure out how much you need to resize by and do it in one statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured