#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
static TCHAR GameName[] = TEXT ("汉诺塔") ;
int pole[3][10]={0};
void DrawRect(HWND hwnd,POINT prectstart , POINT prectend , long color);
void Drawbase(HWND hwnd);
void Drawpole(HWND hwnd);
void Towers(HWND hwnd,int n , int fromPeg , int toPeg , int auxPeg);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg ;
HWND hwnd ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, GameName) ;
wndclass.hCursor = LoadCursor (hInstance,GameName) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (COLOR_GRAYTEXT) ;
wndclass.lpszMenuName = GameName ;
wndclass.lpszClassName = GameName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
GameName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (GameName, GameName,
WS_OVERLAPPED| WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
150, 150,600 , 420, NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HINSTANCE hInstance;
RECT rc ;
HBRUSH hBrush ;
HDC hdc ;
HMENU hMenu ;
PAINTSTRUCT ps ;
static int i,j;
static int idCount[9] = {2,3,4,5,6,7,8,9,10};
static int idDefault=ID_2,PoleCount=1;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
return 0 ;
case WM_COMMAND :
hMenu = GetMenu (hwnd) ;
switch (LOWORD (wParam))
{
case ID_ABOUT:
MessageBeep (0) ;
MessageBox (NULL, TEXT ("制作: 饶 波\n\nQQ : 119002376\n\nE-mail:raobo8888@163.com"), TEXT ("汉诺塔 演示"),0) ;
break ;
case ID_START :
for(i=0;i<3;i++)
for(j=0;j<10;j++)
pole[i][j]=0;
for(j=0;j<PoleCount;j++)
pole[0][j]=PoleCount-j;
SendMessage(hwnd,WM_PAINT,0,0);
Towers(hwnd,PoleCount,0,2,1);
break ;
case ID_2:
case ID_3:
case ID_4:
case ID_5:
case ID_6:
case ID_7:
case ID_8:
case ID_9:
case ID_10:
CheckMenuItem (hMenu,idDefault, MF_UNCHECKED) ;
idDefault=LOWORD (wParam) ;
CheckMenuItem (hMenu, idDefault, MF_CHECKED) ;
PoleCount=idCount[idDefault-ID_2];
for(i=0;i<3;i++)
for(j=0;j<10;j++)
pole[i][j]=0;
SendMessage(hwnd,WM_PAINT,0,0);
for(j=0;j<PoleCount;j++)
pole[0][j]=PoleCount-j;
SendMessage(hwnd,WM_PAINT,0,0);
break ;
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rc) ;
hBrush = CreateSolidBrush (RGB(255,180,50)) ;
FillRect (hdc, &rc, hBrush) ;
Drawbase(hwnd);
Drawpole(hwnd);
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
void DrawRect(HWND hwnd,POINT prectstart , POINT prectend , long color)
{
HDC hdc ;
HBRUSH hBrush;
RECT rc ;
rc.bottom=prectend.y;
rc.left=prectstart.x;
rc.right=prectend.x;
rc.top=prectstart.y;
hdc = GetDC (hwnd) ;
hBrush = CreateSolidBrush (color) ;
FillRect (hdc, &rc, hBrush);
DeleteDC (hdc) ;
DeleteObject (hBrush) ;
}
void Drawbase(HWND hwnd)
{
POINT prectstart,prectend;
long color;
color=RGB(80,10,0);
prectstart.x=50,prectstart.y=200;
prectend.x=550, prectend.y=280;
DrawRect(hwnd,prectstart,prectend ,color);
prectstart.x=100,prectstart.y=280;
prectend.x=150, prectend.y=350;
DrawRect(hwnd,prectstart,prectend ,color);
prectstart.x=450,prectstart.y=280;
prectend.x=500, prectend.y=350;
DrawRect(hwnd,prectstart,prectend ,color);
}
void Drawpole(HWND hwnd)
{
POINT prectstart,prectend;
long color;
static int i,j,base;
for(i=0;i<3;i++)
{
j=0;
switch(i)
{
case 0:base=125;
break;
case 1:base=300;
break;
case 2:base=475;
break;
}
while(j<10)
{
if(pole[i][j]!=0)
{
if(pole[i][j]%3==0) color=RGB(255,0,0);
else if(pole[i][j]%3==1) color=RGB(0,0,0);
else color=RGB(255,255,255);
prectstart.x=base-8*pole[i][j],prectstart.y=185-15*j;
prectend.x=base+8*pole[i][j], prectend.y=200-15*j;
}
else
{
color=RGB(255,180,50);
prectstart.x=base-80, prectstart.y=185-15*j;
prectend.x=base+80, prectend.y=200-15*j;
}
DrawRect(hwnd,prectstart,prectend ,color);
j++;
}
}
}
void Towers(HWND hwnd,int n , int fromPeg , int toPeg , int auxPeg)
{
static int i;
if(n==1)
{
for(i=0;i<10;i++)
if(pole[fromPeg][i]==n)
{
pole[fromPeg][i]=0;
break;
}
for(i=0;i<10;i++)
if(pole[toPeg][i]==0)
{
pole[toPeg][i]=n;
break;
}
Sleep(1000);
SendMessage(hwnd,WM_PAINT,0,0);
return ;
}
Towers(hwnd,n-1,fromPeg,auxPeg,toPeg);
for(i=0;i<10;i++)
if(pole[fromPeg][i]==n)
{
pole[fromPeg][i]=0;
break;
}
for(i=0;i<10;i++)
if(pole[toPeg][i]==0)
{
pole[toPeg][i]=n;
break;
}
Sleep(1000);
SendMessage(hwnd,WM_PAINT,0,0);
Towers(hwnd,n-1,auxPeg,toPeg,fromPeg);
}