《 graphics - 간단 한 러시아 블록 실현 - 성적 미 추가 》

기본 적 인 기능 을 실현 하여 카드 문 제 를 해결 하 였 다.
///////////////////////////////////////////////////////
/* ////////////////////////////////////////////////////
★→☆→★→☆→★→☆→★→☆→★→☆→★→☆→★→☆
↑                                      ↓
☆ Copyright (c) 2011,                   ★
↑ All rights reserved.                             ↓
★     :  cubo.cpp//                      ☆
↑       :                                    ↓
☆              ★
↑      :  V 2.0                                ↓
★←☆←★←☆←★←☆←★←☆←★←☆←★←☆←★←☆
*//////////////////////////////////////////////////////
//    :Visual C++ 6.0 / 2010, Easyx 2011   
//   :    [email protected]
///////////////////////////////////////////////////////
#include "graphics.h"
#include "iostream"
#include <conio.h>
#include <time.h>
using namespace std;
////////////////////////////////////
//   
////////////////////////////////////
#define PIXEL 20//    
#define WIDTH 12//      
#define HEIGHT 22//      

////////////////////////////////////
//   
////////////////////////////////////
/////////////////////
//     
class Cubo
{
private: 
	int bx,by;
	int cx,cy;
	int setnum, setstyle, setstyle2;
	int cub[4][4], cubstore[4][4];
	int tmep_cub[4][4];
	/////////////////////////////////
	//      
	/////////////////////////////////
	//     
	void selectCuboNum(int selcet)
	{
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				cubstore[i][j]=0;
			switch(selcet)
				
			{
			case 0:
				//0
				//11
				//011
				cubstore[1][0] = 1;
				cubstore[1][1] = 1;
				cubstore[2][1] = 1;
				cubstore[2][2] = 1;
				break;	
			case 1:
				//0
				//010
				//111
				cubstore[2][0] = 1;
				cubstore[1][1] = 1;
				cubstore[2][1] = 1;
				cubstore[2][2] = 1;
				break;
			case 2:
				//010
				//010
				//010
				//010
				cubstore[0][1] = 1;
				cubstore[1][1] = 1;
				cubstore[2][1] = 1;
				cubstore[3][1] = 1;
				break;
			case 3:
				//0000
				//0110
				//0110
				//0000
				cubstore[1][2] = 1;
				cubstore[1][1] = 1;
				cubstore[2][1] = 1;
				cubstore[2][2] = 1;
				break;
			case 4:
				//0
				//01
				//0111
				cubstore[2][3] = 1;
				cubstore[1][1] = 1;
				cubstore[2][1] = 1;
				cubstore[2][2] = 1;
				break;
			case 5:
				//0
				//001
				//111
				cubstore[1][2] = 1;
				cubstore[2][0] = 1;
				cubstore[2][1] = 1;
				cubstore[2][2] = 1;
				break;
			case 6:
				//0
				//011
				//11
				cubstore[1][1] = 1;
				cubstore[1][2] = 1;
				cubstore[2][1] = 1;
				cubstore[2][0] = 1;
				break;
			case 7:
				cubstore[0][0] = 1;
			}
	}
	//        
	void setCuboStyle(int selcet)
	{
		switch(selcet)
		{
		case 0:
			setfillstyle(LIGHTMAGENTA, SOLID_FILL, NULL);
			break;
		case 1:
			setfillstyle(YELLOW, SOLID_FILL, NULL);
			break;
		case 2:
			setfillstyle(GREEN, SOLID_FILL, NULL);
			break;
		case 3:
			setfillstyle(RED, SOLID_FILL, NULL);
			break;
		case 4:
			setfillstyle(MAGENTA, SOLID_FILL, NULL);
			break;
		case 5:
			setfillstyle(LIGHTGREEN, SOLID_FILL, NULL);
			break;
		case 6:
			setfillstyle(LIGHTCYAN, SOLID_FILL, NULL);
			break;
		}
	}
	////////////////////////////////////////////////////////
public:
	Cubo()
	{
		srand (( unsigned ) time ( NULL ));

		setnum = rand()%7;
		setstyle = rand()%7;
		getCuboNum();
	}
/*	void intb()//    
	{
		for (int i = 0; i < 4; i++)
		{	
			for (int j = 0; j < 4; j++)
				cout<<	cub[i][j];
			cout<<endl;
		}
		for ( i = 0; i < 4; i++)
		{	
			for (int j = 0; j < 4; j++)
				cout<<cubstore[i][j];
			cout<<endl;
		}
	}*/
	void setcxy(int x,int y)
	{
		bx = cx;
		by = cy;
		this->cx = x*PIXEL;
		this->cy = y*PIXEL;
	}
	//      cub[y][x]  
	int get_Cubostore(int x,int y)
	{
		return cub[y][x];
	}
	int get_Cubocx(int x)
	{
		return cx/PIXEL+x-2;
	}
	int get_Cubocy(int y)
	{
		return cy/PIXEL+y-1;
	}
	//    
	int get_cx()
	{
		return cx;
	}
	int get_cy()
	{
		return cy;
	}
	//      
	void setCuboNum()
	{
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				cub[i][j] = cubstore[i][j];
	}
	
	void getCuboNum()
	{
		selectCuboNum(setnum);
	//	selectCuboNum(7);
	}
	void getCuboStyle(int x)
	{
		if (x)
			setCuboStyle(setstyle);
		else
			setCuboStyle(setstyle2);

	}
	////////////////////////////////////////////////
	//   (   )
	void barCubo()
	{
		int i, j;
		setfillstyle(BLACK, SOLID_FILL, NULL);//        
		for (i = 0; i < 4; i++)
			for (j = 0; j < 4; j++)
			{
				if (cub[i][j])
					bar(bx+j*PIXEL, by+i*PIXEL, bx+PIXEL-2+j*PIXEL, by+PIXEL-2+i*PIXEL);
			}
			getCuboStyle(0);
			for ( i = 0; i < 4; i++)
				for (j = 0; j < 4; j++)
				{
					if (cub[i][j])
						bar(cx+j*PIXEL, cy+i*PIXEL, cx+PIXEL-2+j*PIXEL, cy+PIXEL-2+i*PIXEL);
				}
	}
	void barCubostore()
	{
		setfillstyle(BLACK, SOLID_FILL, NULL);
		bar(430+3, 30+3, 550-3, 150-3);
		//setfillstyle(LIGHTRED, SOLID_FILL, NULL);
		getCuboStyle(1);
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
			{
				if (cubstore[i][j])
					bar(cx+16*PIXEL+j*PIXEL, cy+3*PIXEL+i*PIXEL, cx+16*PIXEL+PIXEL-2+j*PIXEL, cy+3*PIXEL+PIXEL-2+i*PIXEL);
			}
	}
	void setCubo()
	{
		setcxy(7, -1);
		setcxy(7, -1);
		setCuboNum();
		setnum = rand()%7;

		setstyle2 = setstyle;
		setstyle = rand()%7;
		
		getCuboNum();
		barCubostore();//     
		
	}
	////////////////////////////////////////////////
	//      
	void onLeft()
	{
		bx = cx;
		by = cy;
		cx = cx - PIXEL;
	//	barCubo();
	}
	void onRight()
	{
		bx = cx;
		by = cy;
		cx = cx + PIXEL;
	//	barCubo();
	}
	void onDown()
	{
		bx = cx;
		by = cy;
		cy = cy + PIXEL;
		barCubo();
	}
	void onUp()
	{
		int temp[4];
		for (int x = 0; x < 4; x++)
			for (int y = 0; y < 4; y++)
				tmep_cub[x][y] = cub[x][y];
			//        
			setfillstyle(BLACK, SOLID_FILL, NULL);
			for (int i = 0; i < 4; i++)
				for (int j = 0; j < 4; j++)
				{
					if (cub[i][j])
						bar(cx+j*PIXEL, cy+i*PIXEL, cx+PIXEL-2+j*PIXEL, cy+PIXEL-2+i*PIXEL);
				}
				
				for(int n=0;n<=2;n++)
					temp[n]=cub[0][n];
				
				temp[3]=cub[1][1];
				for(n = 0; n <= 2; n++)
					cub[0][n] = cub[3-n][0];
				for(n = 0; n <= 2; n++)
					cub[3-n][0] = cub[3][3-n];
				for(n = 0; n <= 2; n++)
					cub[3][3-n] = cub[n][3];
				for(n = 0; n <= 2; n++)
					cub[n][3] = temp[n];
				
				cub[1][1] = cub[2][1];
				cub[2][1] = cub[2][2];
				cub[2][2] = cub[1][2];
				cub[1][2] = temp[3];
		//			barCubo();//  				
	}
	void onUpBack()
	{
		for (int x = 0; x < 4; x++)
			for (int y = 0; y < 4; y++)
				cub[x][y] = tmep_cub[x][y];
	}
};
////////////////////////
//     
class InterFace
{
private:
	int Ix,Iy;
	int vessel[HEIGHT][WIDTH+2];
	//      
	void setInter(int x,int y)
	{
		Ix = x*PIXEL;
		Iy = y*PIXEL;
	}
public:
	InterFace()//    
	{
	//	inti();
	}
	//     
	void setVess(int x,int y)
	{
		vessel[y][x] = 1;
	}
	void setVess0(int x,int y)
	{
		vessel[y][x] = 0;
	}
	int get_Vessstore(int x,int y)
	{
		return vessel[y][x];
	}
	//     
	void getVess()
	{
		for (int i = 0; i < HEIGHT; i++)
			for (int j = 0; j < WIDTH+2; j++)
			{
				vessel[i][j] = 0;
			}
			
			for (i = 0; i < HEIGHT; i++)
			{
				vessel[i][13] = 2;
				vessel[i][0] = 2;
				if (i < 22)
				{	
				vessel[21][i] = 2;
				}
			}
	}
	//   
	void barVess()
	{
		setfillstyle(BLUE, SOLID_FILL, NULL);
		for (int i = 0; i < HEIGHT; i++)
			for (int j = 0; j < WIDTH+2; j++)
			{
				if (vessel[i][j] == 2)
				{
					bar(Ix+j*PIXEL, Iy+i*PIXEL, Ix+PIXEL-2+j*PIXEL, Iy+PIXEL-2 + i*PIXEL);
				}
			}
	}
	//   
	void barStock()
	{
		setcolor(LIGHTGREEN);
		setlinestyle(PS_SOLID, NULL, 3);
		rectangle(430, 30, 550, 150);
	}
	//    
	void outScore()
	{
		setfont(0,0,0,0,0,TRUE,FALSE,FALSE,"  "); //      
		outtextxy(430,180,"  :       0");
	}
	//       
	void inti()
	{
		initgraph(640, 480);//    
		
		cleardevice();//  
		
		setInter(2,1);
		getVess();
		barVess();
		barStock();
		outScore();
	}	
};
///////////////////////////////////
 	
//    
Cubo cu;//    
InterFace inter;//    
///////////////////////////////////

///////////////////////////////////
//    
///////////////////////////////////
void control();//      
int onBlank();//   
bool detectDown();//     
bool detectLeft();//     
bool detectRight();//     
void set_DeVess();//          
void clearCubo();//           
//
void jiance();
///////////////////////////////////
//////////////////////////////////   //////////////////////////////
///////////////////////////////////
void main()
{

	//      
   
	inter.inti();//     
//	cu.initCubo();
	while(true)
	{
	
		cu.setCubo();//    
//		cu.intb();
//		cu.barCubostore();

		control();//    
	

		clearCubo();//  
	
	}
	
//		jiance();
	
				getchar();
				//      
				closegraph();
}
/////////////////////////////////////     ////////////////////////////////////////////////
/////////////////////////////////////////
//    
/////////////////////////////////////////
//      
void control()
{
	bool pass = false;
	bool right, left;
	int cycle_time = 0;//    
	while(true)
	{
		
		//     ;
		if(kbhit()) 
		{
			switch(getch())
			{
			case 'a':
			case 'A':
				if (detectLeft())break;
				cu.onLeft();
				break;
			case 'd':
			case 'D':
				if (detectRight())break;
				cu.onRight();
				break;
			case 's':
			case 'S':
				if(detectDown())//  
				{
					set_DeVess();//  
					break;
				}
				cu.onDown();
				cu.barCubo();//   
				break;
			case 'w':
			case 'W':
				left = false;
				right = false;
				cu.onUp();
				while(detectLeft())
				{
					cu.onRight();
					left = true;
				}
				if (left)
					cu.onLeft();
				while(detectRight())
				{
					cu.onLeft();
					right = true;
				}
				if (right)
					cu.onRight();
			/*	if (right || left)
			    	inter.barVess();*/
				if (detectDown())
					cu.onUpBack();
				break;
			case ' ':
				pass = true;
				break;
			}		
		}
		cu.barCubo();//   
		if (cycle_time > 20 || pass)//  20      800ms    
		{
			if(detectDown())//  
			{
				set_DeVess();//  
				break;
			}

			cu.onDown();//    
			cycle_time = 0;	
		}
		
		if(!pass)
			Sleep(40);
		cycle_time = cycle_time + 1;
	}
}
//   
int onBlank()
{
	for (int j = 0; j < 20; j++)
	{
		cu.onDown();
	}
	return HEIGHT;
}
///////////////  ///////////////////
//    
bool detectDown()
{
	for (int i = 3; i >= 0; i--)
		for (int j = 0; j < 4; j++)
			if (cu.get_Cubostore(j, i))
			{
				if (inter.get_Vessstore(cu.get_Cubocx(j), cu.get_Cubocy(i)+1))	
					return true;
			}
			return false;
}
bool detectLeft()
{
	
	for (int i = 0; i < 4; i++)
		for (int j = 0; j < 4; j++)
			if (cu.get_Cubostore(j, i))
			{
				if (cu.get_Cubocx(j) < 2) 
					return true;
				if (inter.get_Vessstore(cu.get_Cubocx(j)-1, cu.get_Cubocy(i)))
					return true;	
			}
			return false;
}
bool detectRight()
{
	
	for (int i = 3; i >= 0; i--)
		for (int j = 0; j < 4; j++)
			if (cu.get_Cubostore(i, j))
			{
				if (cu.get_Cubocx(i) > 15) 
					return true;
				if (inter.get_Vessstore(cu.get_Cubocx(i)+1, cu.get_Cubocy(j)))
					return true;
			}
			return false;
}
// vess  
void set_DeVess()
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
			if(cu.get_Cubostore(j,i))
				inter.setVess(cu.get_Cubocx(j), cu.get_Cubocy(i)) ; 
	}
}
//           
void clearCubo()
{
	int i;
	bool pass = true;
	for (i = 20; i >= 0; i--)
	{		
		pass = true;
		for (int j = 1; j < WIDTH+1; j++)
		{
			if (!inter.get_Vessstore(j ,i))
			{
				pass = false;
				break;
			}
		}
		//      
		if (pass)
		{
		//	cout<<i<<endl;
			i++;
			//    
			setfillstyle(YELLOW, DIAGCROSS3_FILL, NULL);
			bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
			Sleep(100);
			setfillstyle(YELLOW, VERTICAL3_FILL, NULL);
			bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
			Sleep(100);
			setfillstyle(YELLOW, CROSS3_FILL, NULL);
			bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-2, 3*PIXEL, i*PIXEL);
			Sleep(100);
			setfillstyle(YELLOW, DOT3_FILL, NULL);
			bar((WIDTH+3)*PIXEL, (i+1)*PIXEL-1-2, 3*PIXEL, i*PIXEL);
			Sleep(100);
			
			//    
			IMAGE img;
			getimage(&img, 3*PIXEL, PIXEL, 15*PIXEL, (i-1)*PIXEL);
	        putimage(3*PIXEL, 2*PIXEL, &img);
			//     
//			for (int n = 1; n < 13; n++)
//				inter.vessel[i][n] = 0;
			for (int x = i-1; x >= 1; x--)
			{	
				for (int y = 1; y < 13; y++)
				{
					if (inter.get_Vessstore(y, x-1))
						inter.setVess(y, x);
					else 
						inter.setVess0(y ,x);
					//	inter.vessel[x][y] = inter.vessel[x-1][y];
				}
			}
		}//if(pass)
		
	}
}

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void jiance()
{
	int i,j;
	inter.getVess();
	cu.setCubo();//     
				 /*	for(i=0;i<4;i++)
				 {
				 for( j=0;j<4;j++)
				 cout<<cu.get_Cubostore(j,i);
				 cout<<endl;
		}*/
	
	

		while(true)
	{
			cu.setCubo();//     
		control();
		for(i=0;i<HEIGHT;i++)
		{
			for(int j=0;j<(WIDTH+2);j++)	
			{
				cout<<inter.get_Vessstore(j,i);
			}
			cout<<i;
			cout<<endl;
		}
		clearCubo();//  
	}

}
다운로드 소스 코드 클릭 (실행 환경: VS 6.0)
클릭 하여 다운로드 EasyX2011 경칩 판 graphics. h

좋은 웹페이지 즐겨찾기