Dynamic array of objects in class

This is a discussion on Dynamic array of objects in class within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to write game Minesweeper in OOP way. I have classes: Square which inherits to mineSquare, emptySquare, digitSquare ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    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 (ImageShack Album - 2 images) when program try run method init().


    Can anyone help me?

    I use VC++ 2008

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    After a quick look:
    in Board() function you instance the memory for an array of Square, but the Square itself is not instanced.
    In init() the tbl in not initialized.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Less quick:
    Code:
    	Board(int xMax=10, int yMax=10, int amountOfMines=10){
    		gameOver = false;
    		this->xMax = xMax;
    		this->yMax = yMax;
    		this->amountOfMines = amountOfMines;
    		tbl = new Square**[xMax]; // Square ***tbl : declare a temporary pointer
    		for(int i=0; i<xMax; i++)
    		{
    			tbl[i] = new Square*[yMax];
    			for (int j=0 ; j < yMax; j++)
    				tbl[i][j]=new Square;
    		}
    	}

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    My init() function looks like that:

    Code:
    void Board::init(){
    	for(int i=0; i<xMax; i++){
    		for(int j=0; j<yMax; j++){
    			tbl[i][j] = new Square();
    		}
    	}
    }

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Ok. I've improved the code.

    Now i have another problem.

    Code:
    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)){ // it comes into this loop
    			delete tbl[xRandom][yRandom];
    			tbl[xRandom][yRandom] = new mineSquare();
    		}
    		else amountOfMines++;
    	}
    }
    Function placeMines() reals coordinates of the field where mine supposed to be. It should change type of cell from Square to mineSquare, but it doesn't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating instances of objects in an array?
    By stumon in forum C# Programming
    Replies: 5
    Last Post: 10-27-2009, 07:37 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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