In this post, we will write a C++ program to create a puzzle game. In this game we need to press the right arrow to move the next box in the same row, press the left arrow to move the previous box in the same row, press up arrow to move next box in the same column and press down arrow to move the previous box in the same column. To exist in the game we need to press the E button. 

To win the game we need to sort the hexadecimal number in ascending order.

C++ program to create puzzle game - create games in c++


C++ program to crate puzzle game.

#include<iostream>
#include<conio.h>
#include<windows.h>

BOOL gotoxy(const WORD x, const WORD y) {
    COORD xy;
    xy.X = x;
    xy.Y = y;
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}

int main()
{
    char a[17] = {' ','c','a','9','1','2','7','b','4','e','5','d','8','f','3','6'};
    int i,j,k,l,temp;
    char ch,ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10;
    char cho = 15;

    system("cls");

    a[16] = 255;

    system("cls");

    ch = 179;
    ch1 = 196;
    ch2 = 197;
    ch3 = 194;
    ch4 = 193;
    ch5 = 218;
    ch6 = 191;
    ch7 = 192;
    ch8 = 217;
    ch9 = 195;
    ch10 = 180;

    gotoxy(2,1);
    std::cout<<ch5;

    gotoxy(30,1);
    std::cout<<ch6;

    gotoxy(2,9);
    std::cout<<ch7;

    gotoxy(30,9);
    std::cout<<ch8;

    for(i=2;i<=30;i+=7)
        for(j=2;j<=8;j++)
        {
            gotoxy(i,j);
            std::cout<<ch;
        }
    for(k=1;k<=27;k+=1)
        for(l=1;l<=9;l+=2)
        {
            gotoxy(k+2,l);
            std::cout<<ch1;
        }
    for(i=2;i<=21;i+=7)
        for(j=1;j<=7;j+=2)
        {
            gotoxy(i+7,j+2);
            std::cout<<ch2;
        }
    for(i=2;i<=22;i+=7)
    {
        gotoxy(i+7,1);
        std::cout<<ch3;
    }
    for(i=2;i<=21;i+=7)
    {
        gotoxy(i+7,9);
        std::cout<<ch4;
    }
    for(i=2;i<=7;i+=2)
    {
        gotoxy(2,i+1);
        std::cout<<ch9;
    }
    for(i=2;i<=7;i+=2)
    {
        gotoxy(30,i+1);
        std::cout<<ch10;
    }

    gotoxy(20,14);
    std::cout<<"Sort the hexadecimal number in accending order"<<std::endl;
    gotoxy(35,17);
    std::cout<<"CONTROLLER"<<std::endl;
    gotoxy(20,19);
    std::cout<<"Press Right arrow to move next box in the same row"<<std::endl;
    gotoxy(20,20);
    std::cout<<"Press Left arrow to move previous box in the same row"<<std::endl;
    gotoxy(20,21);
    std::cout<<"Press Up arrow to move next box in the same col"<<std::endl;
    gotoxy(20,22);
    std::cout<<"Press Down arrow to move previous box in the same col"<<std::endl;
    gotoxy(20,24);
    std::cout<<"Press E button for exit"<<std::endl;

    int p = 1;

    for(i=1;i<=28;i+=7)
    {
        gotoxy(5+i,2);
        std::cout<<a[p];
        p++;
    }
    for(i=1;i<28;i+=7)
    {
        gotoxy(5+i,4);
        std::cout<<a[p];
        p++;
    }
    for(i=1;i<28;i+=7)
    {
        gotoxy(5+i,6);
        std::cout<<a[p];
        p++;
    }
    for(i=1;i<21;i+=7)
    {
        gotoxy(5+i,8);
        std::cout<<a[p];
        p++;
    }

    i = 27;
    j = 8;

    while(cho != 101)
    {
        if(cho == 'M')
        {
            if(i>21)
                i=i+0;
            else
            {
                i = i+7;
                temp = a[p];
                a[p] = a[p+1];
                a[p+1] = temp;
                gotoxy(i,j);

                std::cout<<a[p+1];
                gotoxy(i-7,j);
                std::cout<<a[p];
                p++;
            }
        }

        gotoxy(i,j);

        if(cho=='K')
        {
            if(k<7)
                i = i-0;
            else
            {
                i = i-7;
                temp = a[p];
                a[p] = a[p-1];
                a[p-1] = temp;
                gotoxy(i+7,j);
                std::cout<<a[p];
                gotoxy(i,j);
                std::cout<<a[p-1];
                p--;
            }
        }
        if(cho=='H')
        {
            if(j<3)
                j = j-0;
            else
            {
                j = j-2;
                temp = a[p];
                a[p-4] = temp;
                gotoxy(i,j+2);
                std::cout<<a[p];
                gotoxy(i,j);
                std::cout<<a[p-4];
                p-=4;
            }
        }
        if(cho=='P')
        {
            if(j>6)
                j = j+0;
            else
            {
                j = j+2;
                temp = a[p];
                a[p] = a[p+4];
                a[p+4] = temp;
                gotoxy(i,j-2);
                std::cout<<a[p];
                gotoxy(i,j);
                std::cout<<a[p+4];
                p+=4;
            }
        }
        cho = getch();
    }

    return 0;
}



Output