Dynamic array of objects in class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Dynamic array of objects in class

  1. #1
    Join Date
    Oct 2010
    Posts
    0

    Dynamic array of objects in class

    Hi,

    I have to write game Minesweeper in OOP way. I have classes:
    Square which inherits to mineSquare, emptySquare, digitSquare and class Board which is responsible for initialize and control the game.

    I have such code:

    Code:
    #include <iostream>
    #include <typeinfo>
    #include "time.h"
    
    using namespace std;
    
    
    class Square{
    public:
    	Square(){
    		this->isShown = false;
    		this->isFlagged = false;
    	}
    	bool getIsShown(){
    		return this->isShown;
    	}
    	bool getIsFlagged(){
    		return this->isFlagged;
    	}
    	void makeItShown(){
    		this->isShown = true;
    	}
    	void makeItFlagged(){
    		this->isFlagged = true;
    	}
    	void makeItUnflagged(){
    		this->isFlagged = false;
    	}
    private:
    	bool isShown;
    	bool isFlagged;
    };
    
    
    class digitSquare : Square{
    public:
    	digitSquare(){
    		value = 0;
    	}
    	int getValue(){
    		return value;
    	}
    	void setValue(int value){
    		this->value = value;
    	}
    private:
    	int value;
    };
    
    
    
    class emptySquare : Square{
    
    };
    
    class mineSquare : public Square{
    };
    
    mineSquare type_mineSquare;
    emptySquare type_emptySquare;
    digitSquare type_digitSquare;
    
    class Board{
    public:
    	Board(int xMax=10, int yMax=10, int amountOfMines=10){
    		gameOver = false;
    		this->xMax = xMax;
    		this->yMax = yMax;
    		this->amountOfMines = amountOfMines;
    		Square*** tbl = new Square**[xMax];
    		for(int i=0; i<xMax; i++){
    			tbl[i] = new Square*[yMax];
    		}
    	}
    	void init();
    	void placeMines();
    	bool isGameOver(){
    		if(gameOver == true){
    			unhideTable();
    		}
    		else{
    
    		}
    		return gameOver;
    	}
    private:
    	void unhideNeighboringEmptySquares(int x, int y){
    	} 
    	void unhideTable(){
    	}
    	Square ***tbl;
    	int xMax;
    	int yMax;
    	int amountOfMines;
    	bool isMine(int x, int y);
    	int countNeighboringMines(int x, int y);
    	void showSquare(int x, int y);
    	void flagSquare(int x, int y);
    	bool gameOver;
    	
    };
    
    void Board::init(){
    	for(int i=0; i<xMax; i++){
    		for(int j=0; j<yMax; j++){
    			tbl[i][j]->makeItFlagged();
    		}
    	}
    }
    
    void Board::placeMines(){
    	int amountOfMines = this->amountOfMines;
    	for(amountOfMines; amountOfMines>0; amountOfMines--){
    		srand((unsigned int)time(NULL));
    		int xRandom = rand()%xMax;
    		int yRandom = rand()%yMax;
    		if(!isMine(xRandom,yRandom)){ 
    			delete tbl[xRandom][yRandom];
    			tbl[xRandom][yRandom] = new mineSquare();
    		}
    		else amountOfMines++;
    	}
    }
    
    bool Board::isMine(int x, int y){
    	if(typeid(*tbl[x][y]) == typeid(type_mineSquare)) return true;
    	else false;
    }
    int Board::countNeighboringMines(int x, int y){
    
    	int amountOfNeighboringMines = 0;
    
    	if(x > 0){
    		if(isMine(x-1, y)) amountOfNeighboringMines++;
    		if(y > 0 && isMine(x-1, y-1)) amountOfNeighboringMines++;
    		if((y < this->yMax-1) && isMine(x-1, y+1)) amountOfNeighboringMines++;
    	}
    
    	if(x < this->xMax-1){
    		if(isMine(x+1, y)) amountOfNeighboringMines++;
    		if(y > 0 && isMine(x+1, y-1)) amountOfNeighboringMines++;
    		if((y < this->yMax-1) && isMine(x+1, y+1)) amountOfNeighboringMines++;
    	}
    
    	if(y > 0 && isMine(x, y-1)) amountOfNeighboringMines++;
    	if((y < this->yMax-1) && isMine(x, y+1)) amountOfNeighboringMines++;
    	return amountOfNeighboringMines;
    }
    
    void Board::showSquare(int x, int y){
    	if(tbl[x][y]->getIsShown() == false) tbl[x][y]->makeItShown();
    	if(isMine(x,y)) this->gameOver = true;
    }
    
    void Board::flagSquare(int x, int y){
    	if(tbl[x][y]->getIsShown() == false){
    		if(tbl[x][y]->getIsFlagged() == false){
    			tbl[x][y]->makeItFlagged();
    		}
    		else{
    			tbl[x][y]->makeItUnflagged();
    		}
    	}
    }
    
    
    
    int main(){
    	Board* Game = new Board();
    	Game->init(); // error
    	Game->placeMines(); // i suppose i would get error here also
    
    	system("PAUSE");
    	return 0;
    }
    It's compiling without any problems. I get error (http://img218.imageshack.us/gal.php?g=pppml.jpg) when program try run method init().


    Can anyone help me?

    I use VC++ 2008

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Arrow Re: Dynamic array of objects in class

    Your code is neither C++/CLI (which is the topic of the forum here) nor is it specific to VC++ and/or the Windows environment. So the best place to post your question would be the C++ (Non Visual C++ Issues) forum. It's unlikely that you'll get much help here from the C++/CLI people.

    Ah, and... Welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dynamic array of objects in class

    The answer is plain obvious: tlb, which is declared as:
    Square ***tbl;
    is not initialized. You did not allocate memory for it.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center