-
puzzle help
i want help in this. i am new to this Object oriented programming.Please help. i want to know if i have to create public class, and should i create header file, driver.cpp and .h file and all. i have to do the program using this prototypes.
Instructor said, "This is a 3x3 slide tile puzzle. When writing this program, you should adhere to the object-oriented programming paradigm."
int SlidingPuzzle::theBoard[3][3];
SlidingPuzzle::SlidingPuzzle();
void SlidingPuzzle::InitializeBoard();
bool SlidingPuzzle::isBoardSolved();
bool SlidingPuzzle::slideTile(int);
void SlidingPuzzle::scrambleBoard();
void SlidingPuzzle::PrintBoard();
Code:
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <time.h>
#include <iomanip>
using namespace std;
// functions that perform elementary operations on the board
void initializeBoard(int board[3][3]);
void printBoard(int board[3][3]);
bool isBoardSolved(int[3][3]);
void slideTile(int board[3][3],int move);
void scrambleBoard(int[3][3]);
int main()
{
int puzzleBoard[3][3];
char input;
bool invalid = false;
initializeBoard(puzzleBoard);
printBoard(puzzleBoard);
cout << boolalpha;
cout<<"isBoardSolved(): "<<isBoardSolved(puzzleBoard)<<endl;
cout<<"Press enter to begin"<<endl;
cin.get();
cout<<"Scrambling the game tiles..."<<endl;
scrambleBoard(puzzleBoard);
cout<<"Scrambling complete, press enter to begin solving. "<<endl;
cout<<" Good Luck! "<<endl;
cin.get();
system("CLS"); // clear the screen to began solving.
printBoard(puzzleBoard);
cout<<endl<<endl;
cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl;
cout<<"Input: ";
// loop till the game is solved
while(!isBoardSolved(puzzleBoard))
{
input = getch();
system("CLS");
// selecting the game controls
switch(toupper(input))
{
case 'W':
slideTile(puzzleBoard,2);
break;
case 'A':
slideTile(puzzleBoard,0);
break;
case 'S':
slideTile(puzzleBoard,3);
break;
case 'D':
slideTile(puzzleBoard,1);
break;
default:
invalid = true;
}
printBoard(puzzleBoard);
cout<<endl<<endl;
cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl; // setting controls for user to understand
if(invalid)
{
cout<<"Invalid Input - ["<<input<<"]"; // output invalid if user enters other letters
invalid = false;
}
else
cout<<"Last Input: "<<input;
}
cout<<endl;
cout<<"Congratulations: BOARD SOLVED"<<endl;
system("PAUSE");
return 0;
}
void printBoard(int board[3][3])
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
if(board[row][column] == 0)
{
// setting colors
SetConsoleTextAttribute(hConsole, 6);
cout<<" *";
}
else
{
// if the number is in the right position
if(board[row][column] == ((row*3)+(column+1)))
SetConsoleTextAttribute(hConsole, 9);
else
// if the number is not in the right position
SetConsoleTextAttribute(hConsole, 13);
cout<<" "<<board[row][column];
}
}
cout<<endl;
}
SetConsoleTextAttribute(hConsole, 12); // set text color
}
// board setup. initializing
void initializeBoard(int board[3][3])
{
int i = 1;
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
board[row][column] = i;
i++;
}
board[2][2] = 0;
}
}
// movements
void slideTile(int board[3][3],int move)
{
int emptyRow;
int emptyColum;
int emptySpace[2];
bool legalMoves[4] = {1,1,1,1};
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
if(board[row][column] == 0)
{
emptyRow = row;
emptyColum = column;
}
}
}
if(emptyRow + 1 > 2)
legalMoves[2] = false;
else if(emptyRow - 1 < 0)
legalMoves[3] = false;
if(emptyColum - 1 < 0)
legalMoves[1] = false;
else if(emptyColum + 1 > 2)
legalMoves[0] = false;
switch(move)
{
case 0:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow][emptyColum + 1];
board[emptyRow][emptyColum + 1] = 0;
emptyColum = emptyColum+1;
}
break;
case 1:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow][emptyColum - 1];
board[emptyRow][emptyColum- 1] = 0;
emptyColum = emptyColum-1;
}
break;
case 2:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow+1][emptyColum];
board[emptyRow+1][emptyColum] = 0;
emptyRow = emptyRow+1;
}
break;
case 3:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow-1][emptyColum];
board[emptyRow-1][emptyColum] = 0;
emptyRow = emptyRow-1;
}
break;
}
}
void scrambleBoard(int board[3][3])
{
srand ( time(NULL) );
int move;
while(isBoardSolved(board))
{
for(int i = 0; i < 9000; i++)
{
move = rand() % 5;
slideTile(board,move);
}
}
}
// is the board solved
bool isBoardSolved(int board[3][3])
{
int solvedBoard[3][3] = {{1,2,3},{4,5,6},{7,8,0}};
bool boardSolved = true;
int row = 0;
int col = 0;
while(boardSolved && row<=2)
{
if(solvedBoard[row][col] == board[row][col])
{
col++;
if(col >= 3)
{
row++;
col = 0;
}
}
else
boardSolved = false;
}
return boardSolved;
}
-
So, why you are not happy with your code? It would be very kind of you if you help us helping you :)
-
If you have to use object-oriented programming than you have to create class. Header file is not necessary but would be nice if you have to use this class from another file.
So, you create class. According to prototypes:
Quote:
Originally Posted by
name12
Code:
int SlidingPuzzle::theBoard[3][3];
SlidingPuzzle::SlidingPuzzle();
void SlidingPuzzle::InitializeBoard();
bool SlidingPuzzle::isBoardSolved();
bool SlidingPuzzle::slideTile(int);
void SlidingPuzzle::scrambleBoard();
void SlidingPuzzle::PrintBoard();
class name should be SlidingPuzzle. Than you have to put in it the board, costructor, destructor (I suppose) and functions. Notice that slideTile function must return bool value (maybe if slide is successful) and all (exept slideTile) have no arguments. So class would look like this:
Code:
class SlidingPuzzle
{
int theBoard[3][3];
SlidingPuzzle()
{
}
~SlidingPuzzle()
{
}
void InitializeBoard()
{
// your code here
}
bool slideTile(int move)
{
// your code here
}
// other functions here
};
Now decide what members can be private (accessible only from class member), ex. you can initialize board in constructor and make function InitializeBoard private.
When you have class you have to create instance of it in main() and then use functions from this instance:
Code:
SlidingPuzzle puzzle = new SlidingPuzzle();
// other code
puzzle->PrintBoard();
And remember to delete "puzzle" in the end.