Ce code est une version windows du jeu du taquin. J'ai utilisé DEV c++.
Le jeu est constitué de 15 pièces. On ne peut bouger une piéce que si dans sa colonne ou sa rangée se trouve la case libre. Le bouton mélange, permute les piéces aléatoirement. Le but du jeu est de replacer les piéces de 1 à 15 dans l'ordre.
Source / Exemple :
//jeu du taquin
//auteur: Philippe Pasty
//date: 26 janvier 2006
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int nHeight,nWidth;
HWND GlobalHwnd;
int random(int n)
{return rand()%n;}
void randomize() {
srand(time(0));
}
LONG APIENTRY WndProc( HWND, UINT , WPARAM, LONG);
const int xpiece=4,ypiece=4;
char piece[16];
int TaillePiece=50;
int TailleBouton=24;
int TailleFenetreX()
{return xpiece*TaillePiece+GetSystemMetrics(SM_CXBORDER)*2;}
int TailleFenetreY()
{return GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CXBORDER)*2+TailleBouton+ypiece*TaillePiece;}
#pragma argsused
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmd, int nCmdShow)
{
static char szAppName[] = "Taquin" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.lpfnWndProc = (WNDPROC)WndProc ;
wndclass.lpszClassName = szAppName ;
wndclass.hInstance = hInstance ;
wndclass.lpszMenuName = NULL ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hbrBackground =(HBRUSH)GetStockObject(LTGRAY_BRUSH);
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
RegisterClass (&wndclass) ;
hwnd = CreateWindow (szAppName,
szAppName,
WS_OVERLAPPEDWINDOW&(~WS_THICKFRAME)&(~WS_MAXIMIZEBOX),
CW_USEDEFAULT, //coord x de la fenetre
CW_USEDEFAULT, //coord y de la fenetre
TailleFenetreX(),
TailleFenetreY(),
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LONG APIENTRY WndProc(HWND hwnd, UINT message , WPARAM wparam,LONG lparam)
{
char chiffre[4],strGagne[80];
int x,y,n,index1,index2,temp,x0,y0,yy;
static HINSTANCE hInstance;
static HPEN hPen1,hPen2;
static char Melange=0;
static int Temps0;
RECT rectPiece;
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{case WM_CREATE:
GlobalHwnd = hwnd;
randomize();
for(x=0;x<xpiece*ypiece;x++)piece[x]=x;
hInstance=((LPCREATESTRUCT)lparam)->hInstance;
CreateWindow("button","Mélange",WS_CHILD|WS_VISIBLE,0,0,100,TailleBouton,hwnd,(HMENU)1,hInstance,0);
hPen1= CreatePen(PS_SOLID,2,RGB(255,255,255));
hPen2= CreatePen(PS_SOLID,2,RGB(0,0,0));
return 0;
case WM_COMMAND:
switch(LOWORD(wparam))
{
//mélange des piéces
case 1:
Melange=1;
Temps0=GetTickCount();
for(x=0;x<xpiece*ypiece;x++)piece[x]=x;
for(n=0;n<20;n++)
{
index1=random(xpiece*ypiece-2)+2;
index2=random(index1-1)+1;
temp=piece[index1];
piece[index1]=piece[index2];
piece[index2]=temp;
}
InvalidateRect(hwnd,0,TRUE);
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
SetWindowExtEx(hdc,TailleFenetreX(),TailleFenetreY(),NULL);
SetBkColor(hdc,RGB(192,192,192));
for(x=0;x<xpiece;x++)
{for(y=0;y<ypiece;y++)
{
n=piece[x+y*xpiece];
if(n)
{ wsprintf(chiffre,"%d",n);
rectPiece.left=x*TaillePiece;rectPiece.right=(x+1)*TaillePiece;
rectPiece.top=y*TaillePiece+TailleBouton;
rectPiece.bottom=(y+1)*TaillePiece+TailleBouton;
DrawText(hdc,chiffre,-1,&rectPiece,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
//dessin bordure
SelectObject(hdc,hPen1);
MoveToEx(hdc,x*TaillePiece,(y+1)*TaillePiece+TailleBouton-2,0);
LineTo(hdc,x*TaillePiece,y*TaillePiece+TailleBouton+1);
LineTo(hdc,(x+1)*TaillePiece-1,TailleBouton+TaillePiece*y+1);
SelectObject(hdc,hPen2);
LineTo(hdc,(x+1)*TaillePiece-1,(y+1)*TaillePiece-2+TailleBouton);
LineTo(hdc,x*TaillePiece,(y+1)*TaillePiece+TailleBouton-2);
}
}
}
if(Melange)
{
for(x=xpiece*ypiece-1;(piece[x]==x)&&x;x--);
if(!x)
{wsprintf(strGagne,"Gagné en %d secondes",(GetTickCount()-Temps0)/1000);
MessageBox(hwnd,strGagne,"",MB_OK);
Melange=0;
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_LBUTTONUP:
x=LOWORD(lparam)/TaillePiece; y=(HIWORD(lparam)-TailleBouton)/TaillePiece;
if((x>=xpiece)||(y>=ypiece)) return 0;
yy=y*xpiece;
for(x0=0;x0<xpiece;x0++)
{if(!piece[x0+yy])
{
if(x<x0)
for(;x0>x;x0--)
piece[x0+yy]=piece[x0-1+yy];
else
for(;x0<x;x0++)
piece[x0+yy]=piece[x0+1+yy];
piece[x+yy]=0;
InvalidateRect(hwnd,0,TRUE);
return 0;
} //if
} //for
for(y0=0;y0<ypiece;y0++)
{
if(!piece[x+y0*xpiece])
{
if(y<y0)
for(;y0>y;y0--)
{n=x+y0*xpiece;
piece[n]=piece[n-xpiece];
}
else
for(;y0<y;y0++)
{n=x+y0*xpiece;
piece[n]=piece[n+xpiece];
}
piece[x+yy]=0;
InvalidateRect(hwnd,0,TRUE);
return 0;
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0); // this is the end...
DeleteObject(hPen1);
DeleteObject(hPen2);
return(0L);
case WM_CLOSE:
DestroyWindow(hwnd);
return(0L);
}
return(DefWindowProc(hwnd, message, wparam, lparam));
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.