#include <iostream>
#include <vector>

using namespace std;


	char img[30][15];
	char x = 'X';
	int dotLoc[24][2];
	int dotNum = 0;
	int ret[4];
	int curDie = 0;
	int dieNum = 0;
	int der = 0;

	vector<int>* readDice(vector<string>&);
	int dieDots(int, int);
	void findDots();
	bool adjX(int,int);
	bool traverse(int,int,int,int);


vector<int>* readDice(vector<string>& anImage) {
  // TODO: implement method
	


	vector<int> result;
	//blank initialization
	for (int poop = 0; poop < 4 ; poop ++){
		ret[poop] = -1;
	}
	//blank initialization
	for(int i = 0 ; i < 24 ; i++){
		for(int j = 0; j < 2 ; j++){
			dotLoc[i][j] = -1;
		}
	}
	//blank initialization
	for(int i = 0 ; i < 30 ; i++){
		for(int j = 0; j < 15 ; j++){
			img[i][j] = '.';
		}
	}
	//blank initialization
	dotNum = 0;
	curDie = 0;
	dieNum = 0;
	der = 0;


	// original to char array
	for(int i = 0 ; i < anImage.size() ; i++){
		for(int j = 0; j < anImage[i].size() ; j++){
			img[i][j] = anImage.at(i)[j];
		}
	}

	// initialize dotLoc to -1
	for(int j = 0; j < 24 ; j++){
		dotLoc[j][0] = -1;
		dotLoc[j][1] = -1;
	}
	findDots();

	for(int p = 0; p < 4 ; p++){
		for(int j = 0; j < 24 ; j++){
			if(!(dotLoc[j][0] == -1)){
				der = j;
				ret[p] = dieDots(dotLoc[j][0],dotLoc[j][1]);
			}
		}
	}

	for (int poop = 0; poop < 4 ; poop ++){
		if (!(ret[poop] == -1)){
			result.push_back(ret[poop]);
		}
	}
	
	
	return &result;
}

int dieDots(int x, int y){
	curDie = 0;
	for(int j = 0; j < 24 ; j++){
		
			if(!(dotLoc[j][0] == -1)){
				if(traverse(x,y,dotLoc[j][0],dotLoc[j][1])){
					curDie++;
					dotLoc[j][0] = -1;
					dotLoc[j][1] = -1;
				}
			}
		
	}
	dotLoc[der][0] = -1;
	dotLoc[der][1] = -1;

	return curDie;
}

void findDots(){
	for(int i = 0; i <30 ; i++){
		for(int j = 0; j <15 ; j++){
			if(img[i][j] == x){
				if (!adjX(i,j)){
					dotLoc[dotNum][0] = i;
					dotLoc[dotNum][1] = j;
					dotNum++;
				}
			}
		}
	}
}

bool adjX(int a, int b){	
	if ( (img[a-1][b]) == x || (img[a][b-1]) == x ){
		return true;
	}
	return false;
}

// True if same die
bool traverse(int x1, int y1, int x2, int y2){
	if((x1 == x2) && (y1 == y2)){
		return true;
	}
	if(img[x2][y2] == '.'){
		return false;
	}
	
	int dX = x1-x2;
	int dY = y1-y2;
	bool lol;
	if(dX > dY){
		lol = traverse(x1, y1, x1+dX-1, y1+dY);
		if (lol){
			return true;
		}
	}
	else{
		lol = traverse(x1, y1, x1+dX, y1+dY-1);
		if (lol){
			return true;
		}
	}
	return false;
}

int main (int argc, char * const argv[]) {
    cout << "testing C++ readDice method..." << endl;
	
	vector<string> input;
	vector<int>    expected;
	for (int i = 0; i < 10; i++) {
		input   .clear();
		expected.clear();
		switch (i) {
			case 0:
				input   .push_back( "***...***.***.." );
				input   .push_back( "*X*..*X*...*X*." );
				input   .push_back( "***.***.....***" );
				expected.push_back( 1 );
				expected.push_back( 1 );
				expected.push_back( 1 );
				break;
			case 1:
				input   .push_back( "****..****" );
				input   .push_back( "*X**..XX**" );
				input   .push_back( "**X*..**XX" );
				input   .push_back( "****..****" );
				expected.push_back( 2 );
				expected.push_back( 2 );
				break;
			case 2:
				input   .push_back( ".***..X**..X*X." );
				input   .push_back( ".*X*..*X*..*X*." );
				input   .push_back( ".***..**X..X*X." );
				expected.push_back( 1 );
				expected.push_back( 3 );
				expected.push_back( 5 );
				break;
			case 3:
				input   .push_back( "*X*X*.*****......" );
				input   .push_back( "*****.*X*X*.**X**" );
				input   .push_back( "*X*X*.**X**.*X*X*" );
				input   .push_back( "*****.*X*X*.**X**" );
				input   .push_back( "*X*X*.*****......" );
				expected.push_back( 4 );
				expected.push_back( 5 );
				expected.push_back( 6 );
				break;
			case 4:
				input   .push_back( ".............................." );
				input   .push_back( ".............................." );
				input   .push_back( "...............*.............." );
				input   .push_back( "...*****......****............" );
				input   .push_back( "...*X***.....**X***..........." );
				input   .push_back( "...*****....***X**............" );
				input   .push_back( "...***X*.....****............." );
				input   .push_back( "...*****.......*.............." );
				input   .push_back( ".............................." );
				input   .push_back( "........***........******....." );
				input   .push_back( ".......**X****.....*X**X*....." );
				input   .push_back( "......*******......******....." );
				input   .push_back( ".....****X**.......*X**X*....." );
				input   .push_back( "........***........******....." );
				input   .push_back( ".............................." );
				expected.push_back( 1 );
				expected.push_back( 2 );
				expected.push_back( 2 );
				expected.push_back( 4 );
				break;
			case 5:
				input   .push_back( ".................******..." );
				input   .push_back( "....*.............**X***.." );
				input   .push_back( "...*X*.............***X**." );
				input   .push_back( ".******.............******" );
				input   .push_back( "*X*XX*X*.................." );
				input   .push_back( ".******.*****.......******" );
				input   .push_back( "..*X*...***X*......**XX**." );
				input   .push_back( "...*....**X**.....**XX**.." );
				input   .push_back( "........*X***....******..." );
				input   .push_back( "........*****............." );
				expected.push_back( 1 );
				expected.push_back( 2 );
				expected.push_back( 3 );
				expected.push_back( 5 );
				break;
			case 6:
				input   .push_back( "......*............." );
				input   .push_back( ".....*X**..........." );
				input   .push_back( "....****X*.........." );
				input   .push_back( "...*X****.....******" );
				input   .push_back( "....**X*.....**X***." );
				input   .push_back( "......*.....***X**.." );
				input   .push_back( "...........******..." );
				input   .push_back( "........***........." );
				input   .push_back( ".......****X**......" );
				input   .push_back( "......**XX***......." );
				input   .push_back( ".....*X*****........" );
				input   .push_back( "........***........." );
				expected.push_back( 1 );
				expected.push_back( 3 );
				expected.push_back( 4 );
				break;
			case 7:
				input   .push_back( "............................" );
				input   .push_back( ".......**..................." );
				input   .push_back( "......**X**.........**......" );
				input   .push_back( ".....*X***X*......*X***....." );
				input   .push_back( "......**X**......***X***...." );
				input   .push_back( "........**...**...***X*....." );
				input   .push_back( "...**.......*X*X*...**......" );
				input   .push_back( ".**X**.....***X***.........." );
				input   .push_back( "***X***.....*X*X*..........." );
				input   .push_back( ".**X**........**............" );
				input   .push_back( "..**........................" );
				input   .push_back( "............................" );
				expected.push_back( 1 );
				expected.push_back( 3 );
				expected.push_back( 4 );
				expected.push_back( 5 );
				break;
			case 8:
				input   .push_back( "......*............." );
				input   .push_back( ".....*X**..........." );
				input   .push_back( "....*X**X*.........." );
				input   .push_back( "...*X**X*.....******" );
				input   .push_back( "....**X*.....X*X*X*." );
				input   .push_back( "......*.....X*X*X*.." );
				input   .push_back( "...........******..." );
				input   .push_back( "....***............." );
				input   .push_back( "...**X*X**.**X*X**.." );
				input   .push_back( "..**X*X**...**X*X**." );
				input   .push_back( ".*X**X**.....**X**X*" );
				input   .push_back( "....***............." );
				expected.push_back( 6 );
				expected.push_back( 6 );
				expected.push_back( 6 );
				expected.push_back( 6 );
				break;
			case 9:
				input   .push_back( "..........*****" );
				input   .push_back( "..**......**X**" );
				input   .push_back( ".*XX*.....*****" );
				input   .push_back( "*****.........." );
				input   .push_back( "*XX*...X**....." );
				input   .push_back( ".**...**X**...." );
				input   .push_back( ".....XX**XX*..." );
				input   .push_back( "......*X***...." );
				input   .push_back( ".......*X*....." );
				expected.push_back( 1 );
				expected.push_back( 2 );
				expected.push_back( 6 );
				break;
			default:
				cout << "should not reach here!" << endl;
				break;
		}
		vector<int>* actual = readDice( input );
		bool         ok     = expected == *actual;
		cout << "test " << i << ": " << (ok ? "ok" : "failed") << endl;
		
		delete actual;
	}
    cout << "done!\n";
    return 0;
}

It gives me a runtime error of "Unhandled exception at 0x5b9fad4a (msvcp100d.dll) in painis.exe: 0xC0000005: Access violation reading location 0xccccccd0."

Also, it creates another file called xutility and stops at this point

for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
			*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
			(*_Pnext)->_Myproxy = 0;
		_Myproxy->_Myfirstiter = 0;
		}

Any advice on what to do next or where the error is occurring would be nice. Thanks.

Compile for debug mode then single step through the program so that you can see where the problem is. How to do that will depend on the compiler you are using.

>>vector<int>* readDice(vector<string>& anImage) {
That function is attempting to return a pointer to local variable. And, like all local variables, that is destroyed immediately when the function returns, so the receiving function is getting an invalid pointer.

A solution to that problem is to add another parameter which is a reference to a vector that is created by the calling function, e.g. void readDice(vector<int>& result, vector<string>& anImage) { you really need to learn how to use your IDE's debugger because there are lots of other errors in that program.

Edited 4 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.