I. Introduction▲
Code d'un Taquin en pure Windows API.

II. Code complet▲
Sélectionnez
#include <windows.h>
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
#define MoveTo(hdc,x,y) MoveToEx(hdc,x,y,NULL)
#define IDM_QUIT 100
#define IDM_NEW 101
#define NBC 5
#define LARG 64
int tab[NBC][NBC];
void exchange(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void Play(int y, int x)
{
if(y>0 && tab[y-1][x]==0) exchange(&tab[y][x], &tab[y-1][x]);
if(x>0 && tab[y][x-1]==0) exchange(&tab[y][x], &tab[y][x-1]);
if(y<NBC-1 && tab[y+1][x]==0) exchange(&tab[y][x], &tab[y+1][x]);
if(x<NBC-1 && tab[y][x+1]==0) exchange(&tab[y][x], &tab[y][x+1]);
}
void Init()
{
int x = NBC-1;
int y = NBC-1;
for(int i=0; i<NBC*NBC; i++) *(tab[0]+i)=i+1;
tab[NBC-1][NBC-1]=0;
for(int i=0; i<10000; i++)
{
int rdv=rand()%4;
if(rdv==0 && y>0)
{
exchange(&tab[y][x], &tab[y-1][x]);
y--;
}
if(rdv==1 && x>0)
{
exchange(&tab[y][x], &tab[y][x-1]);
x--;
}
if(rdv==2 && y<NBC-1)
{
exchange(&tab[y][x], &tab[y+1][x]);
y++;
}
if(rdv==3 && x<NBC-1)
{
exchange(&tab[y][x], &tab[y][x+1]);
x++;
}
}
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
srand(GetTickCount());
Init();
return 0;
}
case WM_COMMAND :
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE, 0, 0);
if(LOWORD(wParam) == IDM_NEW)
{
Init();
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
case WM_CLOSE :
DestroyWindow(hwnd);
return 0;
case WM_LBUTTONUP:
{
int x = GET_X_LPARAM(lParam)/LARG;
int y = GET_Y_LPARAM(lParam)/LARG;
if(x<NBC && y<NBC) Play(y, x);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_PAINT:
{
LOGFONT lf= {0};
ZeroMemory(&lf, sizeof(LOGFONT));
lstrcpy(lf.lfFaceName, "Arial");
lf.lfHeight = LARG/2;
lf.lfWeight = FW_BOLD;
HFONT nbfont = CreateFontIndirect(&lf);
HPEN hpen = CreatePen(PS_SOLID, 2, 0x00000000);
RECT rect;
GetClientRect(hwnd, &rect);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcDB = CreateCompatibleDC(hdc);
HBITMAP bmDB = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
SelectObject(hdcDB, bmDB);
FillRect(hdcDB, &rect, GetStockObject(WHITE_BRUSH));
SelectObject(hdcDB,hpen);
for(int i=0; i<=NBC; i++)
{
MoveTo(hdcDB, 0, i*LARG);
LineTo(hdcDB, NBC*LARG, i*LARG);
MoveTo(hdcDB, i*LARG, 0);
LineTo(hdcDB, i*LARG, NBC*LARG);
}
SelectObject(hdcDB, nbfont);
for(int j=0; j<NBC; j++)
for(int i=0; i<NBC; i++)
{
RECT caseRect= {i*LARG, j*LARG, i*LARG+LARG, j*LARG+LARG};
char pion[3];
int strsize = 1;
wsprintf(pion, "%d", tab[j][i]);
if(tab[j][i]>9) strsize = 2;
DrawText(hdcDB, pion, strsize, &caseRect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
if(tab[j][i]==0)
{
SelectObject(hdcDB, GetStockObject(DKGRAY_BRUSH));
Rectangle(hdcDB, caseRect.left, caseRect.top,
caseRect.right, caseRect.bottom);
}
}
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdcDB, 0, 0, SRCCOPY);
DeleteDC(hdcDB);
DeleteObject(bmDB);
EndPaint(hwnd, &ps);
DeleteObject(nbfont);
DeleteObject(hpen);
return 0;
}
case WM_DESTROY :
PostQuitMessage(0);
return 0;
default :
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc = {0};
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hinstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "MaWinClass";
RegisterClass(&wc);
HMENU hSousMenu = CreateMenu();
AppendMenu(hSousMenu, MF_STRING, IDM_NEW, "Nouvelle partie");
AppendMenu(hSousMenu, MF_STRING, IDM_QUIT, "Quitter");
HMENU hMenu = CreateMenu();
AppendMenu(hMenu, MF_POPUP, (UINT)hSousMenu, "Fichier");
RECT winRect;
SetRect(&winRect, 0, 0, LARG*NBC, LARG*NBC);
AdjustWindowRect(&winRect, WS_CAPTION, TRUE);
HWND hwnd = CreateWindow("MaWinClass", "Taquin",
WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
winRect.right-winRect.left, winRect.bottom-winRect.top,
NULL, hMenu, hinstance, NULL);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
return msg.wParam;
}À vos compilateurs.


