I'm having trouble getting the implementation file to work correctly.
interface file
implementation fileCode:#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
cpp source fileCode://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; } ////////////////////////////////////////////////////////////
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; }




Reply With Quote