
#include <iostream>
using namespace std;
enum move {NONE, UP, DOWN, LEFT,RIGHT};
class EightTile
{ 
public:
	EightTile();  //constructor
	void setMove(enum move);  //select direction
	void display(void);	//draw puzzle
	void swap(void); //swap blank with selected tile
	bool okLeft(void); //can I move left
	bool okRight(void);//can I move right
	bool okUp(void);//can I move up
	bool okDown(void);//can I move down


private:
	enum move myMove;
	int tiles[3][3];  //puzzle now
	int solution [3][3];//puzzle at start (solved).
	int x;  //blank x position
	int y;  //blank y position

};



EightTile::EightTile()
{
	myMove = NONE;  //initialize member attributes
	x=0;  //initial blank position   - make  sure this agrees with solved puzzle!
	y=0;
	tiles[0][0]=0; tiles[1][0]=1; tiles[2][0]=2; //set up puzzle tiles
	tiles[0][1]=3; tiles[1][1]=4; tiles[2][1]=5;
	tiles[0][2]=6; tiles[1][2]=7; tiles[2][2]=8;
	solution[0][0]=0; solution[1][0]=1; solution[2][0]=2; //set up solved puzzle tile positions
	solution[0][1]=3; solution[1][1]=4; solution[2][1]=5;
	solution[0][2]=6; solution[1][2]=7; solution[2][2]=8;
}

void EightTile::display(void)
	//draw puzzle as it is now
{
	cout<<tiles[0][0]<<" "<< tiles[1][0]<<" "<<tiles[2][0]<<endl;
	cout<<tiles[0][1]<<" "<< tiles[1][1]<<" "<<tiles[2][1]<<endl;
	cout<<tiles[0][2]<<" "<< tiles[1][2]<<" "<<tiles[2][2]<<endl;
	cout<<endl;	
}
void EightTile::setMove(enum move m)
	//sets which way you want to move
{
	myMove=m;
}
void EightTile::swap(void)
	//attempts to move blank in direction set - up, down, left or right by doing a tile/blank swap
	//can't move beyond edges of puzzle
{
	int temp;
	if (okLeft() & (myMove==LEFT)) {
		temp=tiles[x-1][y];
		tiles[x-1][y]=tiles[x][y];
	    tiles[x][y]=temp;
		x=x-1; 

	}
	if (okRight() & (myMove==RIGHT)){
		temp=tiles[x+1][y];
		tiles[x+1][y]=tiles[x][y];
	    tiles[x][y]=temp;
		x=x+1;

	}

		if (okUp() & (myMove==UP)){
		temp=tiles[x][y-1];
		tiles[x][y-1]=tiles[x][y];
	    tiles[x][y]=temp;
		y=y-1;

	}
		if (okDown() & (myMove==DOWN)){
		temp=tiles[x][y+1];
		tiles[x][y+1]=tiles[x][y];
	    tiles[x][y]=temp;
		y=y+1;

	}
}
bool EightTile::okLeft(void)
	//can I move left?
{

		if (x > 0)
		return true; else return false;
}
bool EightTile::okRight(void)
	//can I move right?
{
		if (x < 2)
		return true; else return false;
}
bool EightTile::okUp(void)
		//can I move up?
{
		if (y > 0)
		return true; else return false;
}
bool EightTile::okDown(void)
		//can I move down?
{
		if (y < 2)
		return true; else return false;
}

int main(void)
	//simple program to exercise puzzle by moving blank around
	//and drawing tiles
	EightTile myPuzzle;//create class and init.
	char d; //keyboard interface valid values a,s,w,d	
		
	while (true) //go forever
	{

		cin>>d;
		if (d=='a') myPuzzle.setMove(LEFT);
		if (d=='d') myPuzzle.setMove(RIGHT);
		if (d=='w') myPuzzle.setMove(UP);
		if (d=='s') myPuzzle.setMove(DOWN);
		myPuzzle.swap(); //move blank
		myPuzzle.display(); //draw puzzle
	}
	return 0;
}//the end