Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (8 votes)
See more:
I need to create an 8 puzzle game (in windows application). I created the interface, but i don't know how to move the buttons.
I also need to know how to end the game (ie.. how to let the program know that the puzzle is solved ).
OK
Posted
Updated 19-Apr-11 2:47am
v4
Comments
OriginalGriff 19-Apr-11 8:32am    
Reason for my vote of one: too lazy to do own research.
Guyverthree 19-Apr-11 8:51am    
move what type of buttons ?
how do you know it's solved ? would a message box be enough.

comment more on the method you want to use and the exact problem that you are having.

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A quick Google using "8 puzzle game c++" gave 6,000,000 hits.
 
Share this answer
 
Solving what problem? If you've actually tried to write the code, and now have a problem, show us the code that's giving you the problem.
 
Share this answer
 
Comments
[no name] 28-Apr-11 9:06am    
He asks algorithm, not code.
#include<iostream>
#include <windows.h>


#define MY_WIN_CLASS        "MyWinClass"

/* Command identifiers */
#define CMD_GAME_OVER   101
#define CMD_WHAT 102
#define ID_LIFE 103
#define ID_ITEM_b     301
#define CMD_level 1098
#define CMD_level_1 1034
#define CMD_level_2 1094
#define CMD_level_3 1095



int Init(HINSTANCE hInstance);
HWND CreateMainWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void PUZZLE(int x, int y);

HWND hMainWin=NULL , hButton=NULL;    /* main window handle */
 int x=0,y=0,bx=100,by=170,w=340,h=70,wX=600,wY=450;  

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                          LPSTR lpszCmdLine, int nCmdShow )
{
    MSG msg;
    
    if ( Init(hInstance) == 0 ) {
        MessageBox(NULL,"Failed to initialize","Error",MB_OK);
        return 0;
    } else {
        while (GetMessage(&msg,NULL,0,0)) {         
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        return msg.wParam;
    }   
}



int Init(HINSTANCE hInstance)
{
    WNDCLASS WinClass;
    int Ok;
    
    /* registers a window class */
    WinClass.style = NULL;
    WinClass.lpfnWndProc = WndProc;
    WinClass.cbClsExtra = 0;
    WinClass.cbWndExtra = 0;
    WinClass.hInstance = hInstance;
    WinClass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
    WinClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    WinClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WinClass.lpszMenuName = (LPCSTR)NULL;   
    WinClass.lpszClassName = (LPCSTR)MY_WIN_CLASS;
    
    
    if ( Ok = RegisterClass(&WinClass) ) {
        /* Create the main window */
        hMainWin = CreateMainWindow(hInstance);
        if ( hMainWin == NULL ) Ok = 0;
    }
    return Ok;
}



 
HWND CreateMainWindow(HINSTANCE hInstance)
{
    HWND hWin;
    HMENU hMainMenu, hSubMenu;
   
     hMainMenu = CreateMenu();
     
     
    hSubMenu = CreateMenu();
    
    // GAME menu
    AppendMenu(hSubMenu,MF_STRING,CMD_GAME_OVER,"&Over");
    AppendMenu(hMainMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Game");
     
    
    // GAME menu
    AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Easy");
     AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Medium");
      AppendMenu(hSubMenu,MF_STRING,CMD_level,"&Hard");
    AppendMenu(hMainMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Level");
    
    hSubMenu = CreateMenu();  //ABOUT menu
    AppendMenu(hMainMenu,MF_STRING,CMD_WHAT,"&About");
       

        if (hWin = CreateWindow((LPCSTR)MY_WIN_CLASS,
        " Midterm Test",
        (DWORD)WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL|WS_CLIPSIBLINGS ,
        60, 30,600 , 450,NULL, hMainMenu, hInstance, (LPVOID)NULL)) {  
          hButton=CreateWindow("BUTTON","  1  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  2  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            211,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  3  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,5,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);  
            
       hButton=CreateWindow("BUTTON","  4  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  5  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            211,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  6  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,116,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
            
        hButton=CreateWindow("BUTTON","  7  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY|WS_GROUP,
            100,227,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL);
        
        hButton=CreateWindow("BUTTON","  8  ",
            WS_CHILD|WS_VISIBLE|BS_NOTIFY,
            322,227,110,110,hWin,(HMENU)ID_ITEM_b,hInstance,(LPSTR)NULL); 
        ShowWindow(hWin, SW_SHOWNORMAL);
        SetActiveWindow(hWin);        
        //System.Windows.Forms.TextBox;      
    }
    return hWin;
}





LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{   
    switch (message) {      
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CTLCOLORSTATIC:         
            return (LRESULT)GetStockObject(WHITE_BRUSH);    
        case WM_MOUSEMOVE:
        		x = LOWORD(lParam);
        		y = HIWORD(lParam);
                PUZZLE(x,y); 
                return 0;      
        case WM_COMMAND:
            switch (LOWORD(wParam)) {   
                case CMD_WHAT:
                     MessageBox(NULL," PROGRAMMED BY:: OUSMAN KASSAMA ","User Interface Prototype",MB_OK);
                    break;
               case CMD_GAME_OVER:
                    DestroyWindow(hMainWin);
                    break;      
                       
            }
            return 0;
    }
    
    return (DefWindowProc(hWnd, message, wParam, lParam));
}

void PUZZLE(int x, int y)  
{
     int x1=x,y1=y;
     int x2=x1,y2=y1;
     int x3=x1,y3=y1;
     int posX=0,posY=0;
if((x1 >= bx-20) && (x1 <= 420) && (y1 >= by-20) && (y1 <= (by-20)+h)){
      if(y1<= 225){posX=600-x1;posY=450-y1; }
 else{posX=(300)-x1;posY=(225)-y1;}
if (posX <= 0)posX = 0;
if (posY <= 0)posY = 0;
if (posX >= wX-w)posX = 260;
if (posY >= wY-h)posY = 360;

  
      SetWindowPos(hButton,NULL,posX,posY,0,0,SWP_NOSIZE|SWP_NOZORDER);
      bx=posX;
      by=posY;
}

}</windows.h></iostream>
 
Share this answer
 
v2
You need to use bitmaps.
1 bitmap - 1 puzzle block.
If you have 10x10 bitmaps, then use array of 10x10 bitmap handles to store current position and 10x10 array to store final position (entered manualy).
Moving: first click on bitmap to activate it, second click to set it.
 
Share this answer
 
Is this a school project???????????
 
Share this answer
 
Comments
#realJSOP 19-Apr-11 8:38am    
Yes.
oskassama 19-Apr-11 9:03am    
Yeah it is. It's giving me trouble.
Member 10576661 9-Feb-14 1:54am    
same for me, r u have finish it???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900