// ■ハノイの塔問題(マルチスレッド版) #include "myWin.h" #include "math.h" #include "process.h" #define RED(X) ( X & 0xFF) #define GREEN(X) ((X>> 8) & 0xFF) #define BLUE(X) ((X>>16) & 0xFF) #define MAX 5 //円盤の数 #define SLEEP 100 //Sleepする時間(円盤の動く速度を変えるときはこれを変更する) static int diskCtab[]={ 0xFF77FF, 0x00FFFF, 0x00FF00, 0x0000FF, 0xFF5555, 0xFFFF00, 0x7777FF, 0xFFFF77, 0x77FF77, 0xFF00FF}; static int WFlag, ExeThread,DrawEnd; static int colDisk[3][MAX], numDisk[3], Nst; static HDC hBuff; static HBITMAP hBM; int colorBright(int color, double BR){ if(BR>1)BR=1; int R = (int)(BR*RED(color)), G=(int)(BR*GREEN(color)),B=(int)(BR*BLUE(color)); return RGB(R,G,B); } void drawLine(int X1, int Y1, int X2, int Y2, int color){ HPEN pen=CreatePen(PS_SOLID, 1, color); SelectObject(hBuff, pen); MoveToEx(hBuff,X1,Y1,NULL); LineTo(hBuff,X2,Y2); DeleteObject(pen); } void drawRectangle(int X1, int Y1, int X2, int Y2, int color){ HPEN pen=CreatePen(PS_SOLID, 1, 0); SelectObject(hBuff, pen); HBRUSH brush=CreateSolidBrush(color); SelectObject(hBuff, brush); Rectangle(hBuff,X1,Y1,X2,Y2); DeleteObject(brush);DeleteObject(pen); } void drawDisk(int X0, int Y0, int R, int TH, int color){ double DR = R, RR =DR*DR; int X1=X0-R, X2=X0+R, Y2=Y0-TH; for(int X=X1; X<=X2; X++){ double XX=X-X0;double Bright=sqrt((double)(RR-XX*XX))/R; drawLine(X, Y0, X, Y0 - TH, colorBright(color, Bright)); } drawLine(X1,Y0, X1,Y2, 0);drawLine(X1,Y2, X2,Y2, 0); drawLine(X2,Y2, X2,Y0, 0);drawLine(X2,Y0, X1,Y0, 0); } void crearBitmap(){ SelectObject(hBuff,GetStockObject(NULL_PEN)); PatBlt(hBuff,0, 0,1000,400,WHITENESS); } void drawState(){ int DX=Nst*20,XST=Nst*15, poleH=10*Nst+10, Xed=70*Nst, Yed=20*Nst+40; crearBitmap(); drawRectangle(5, Yed, Xed, Yed+20, 0x5577); for(int i=0; i<3;i++) drawDisk(i*DX+XST, Yed,3, poleH, 0x225577); for(int i=0;i<3;i++)for(int j=0; j