1 Replies - 516 Views - Last Post: 13 November 2012 - 03:04 PM Rate Topic: -----

#1 name12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-November 12

C++ challenging question.

Posted 13 November 2012 - 02:02 PM

I am creating an object Oriented Sliding tile puzzle. I would like to know if this is the right way to go about it.
do i have to create three classes. like
a header file, and two source file.
should i create a Implementation, Specification, and a driver file.
what should i write in Private method, if i went the above way.

class Game
{
  int solvedBoard[10][10];

public:
  Game(); 


  void printBoard();
{
	HANDLE hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	for(int row = 0; row <= amt; row++)
	{
		for(int column = 0; column <= amt; column++)
		{
			if(board[row][column] == 0)
			{
				SetConsoleTextAttribute(hConsole, 7); //Default color
				cout << "\xB1\xB1 ";
			}
			else
			{
				if(board[row][column] == solvedBoard[row][column]) //If board location is equal to solvedBoard location then set to green
					SetConsoleTextAttribute(hConsole, 10);
				else
					SetConsoleTextAttribute(hConsole, 12); //else, set to red
				if (board[row][column]<10) // Print a 0 first if # < 10
					cout<<"0";
				cout<<board[row][column] << " ";
			}
		}
		cout<<endl;
	}
	SetConsoleTextAttribute(hConsole, 7);
}
  void initializeBoard();
{
	int i = 1;
	for(int row = 0; row <= amt; row++)
	{
		for(int column = 0; column <= amt; column++)
		{
			board[row][column] = i;
			solvedBoard[row][column] = i;
			i++;
		}
		board[amt][amt] = 0;
		solvedBoard[amt][amt] = 0;
	}
}
void slideTile(int move);
 int emptyRow;
	int emptyCol;
	bool legalMoves[4] = {1,1,1,1}; //array of legal moves, [0] = left, [1] = right, [2] = up, [3] = down. true (1)   indicates a legal move.
	for(int row = 0; row <= amt; row++)
	{
		for(int column = 0; column <= amt; column++)
		{
			if(board[row][column] == 0) //Find location of empty space
			{
				emptyRow = row;
				emptyCol = column;
			}
		}
	}
	if(emptyRow + 1 > amt) //Can i move up?
		legalMoves[2] = false; //If no, set move flag to false
	else if(emptyRow - 1 < 0) //Move down?
		legalMoves[3] = false;

	if(emptyCol - 1 < 0) //Move right?
		legalMoves[1] = false;
	else if(emptyCol + 1 > amt) //Move left?
		legalMoves[0] = false;

	switch(move) //Replace zero space with space to the left right up or down.
	{
	case 0:
		if(legalMoves[move])
		{
			board[emptyRow][emptyCol] = board[emptyRow][emptyCol + 1];
			board[emptyRow][emptyCol + 1] = 0;
			emptyCol = emptyCol+1;
		}
		break;
	case 1:
		if(legalMoves[move])
		{
			board[emptyRow][emptyCol] = board[emptyRow][emptyCol - 1];
			board[emptyRow][emptyCol- 1] = 0;
			emptyCol = emptyCol-1;
		}
		break;
	case 2:
		if(legalMoves[move])
		{
			board[emptyRow][emptyCol] = board[emptyRow+1][emptyCol];
			board[emptyRow+1][emptyCol] = 0;
			emptyRow = emptyRow+1;
		}
		break;
	case 3:
		if(legalMoves[move])
		{
			board[emptyRow][emptyCol] = board[emptyRow-1][emptyCol];
			board[emptyRow-1][emptyCol] = 0;
			emptyRow = emptyRow-1;
		}
		break;
	}

}

  bool isBoardSolved();

{
	bool boardSolved = true;
	int row = 0;
	int col = 0;
	while(boardSolved && row<=amt)
	{
		if(solvedBoard[row][col] == board[row][col]) //Compare each index of solved board with game board
		{
			col++;
			if(col == amt)
			{
				row++;
				col = 0;
			}
		}
		else //Once a discrepancy is found, set boardSolved to false to break the loop
			boardSolved = false;
	}
	return boardSolved;
}

  void scrambleBoard();
  time_t t;
    srand((unsigned) time(&t));
	int move;
	while(isBoardSolved(board,amt)) //If the board ends up being solved at the end of the scramble, scramble the board again
	{
		for(int i = 0; i < 100000;i++) //Series of random moves
		{
			move = rand() % 4;
			slideTile(board,move,amt);
		}
	}
}
}
// now you need to implement functions 
 Game::Game()
{
// ...
}

void Game::printBoard()
{
 // ...
}
// ... 



Is This A Good Question/Topic? 0
  • +

Replies To: C++ challenging question.

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: C++ challenging question.

Posted 13 November 2012 - 03:04 PM

Not so much a challenging question as a vague question. Generally, for each class you will want to make a header (.h) file and an implementation (.cpp) file. You link to the class by using "#include myClass.h" command. I think the data is simple enough that you only need one class. I wouldn't expect the need for much more than a 2d array to handle the data. Board and solvedBoard should be private members.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1