adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Number puzzle game in C++ (with level)

Number puzzle game in C++ (with level)

#include<iostream>
#include<windows.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
 using namespace std;
   int move_count=0;
   int puzzle[3][3];
   void welcomeTest();
   void Notes();
   int getkey();
   int check(int m[][3],int move);
   int swap(int m[][3],int move,int loc);
   void printBoard(int m[][3],int lvl);
   void set(int lv[]);
   void level(int lvl);
   void move_print();
   int win(int  m[][3]);

   int main()
    {
      char move_pos,option;
   int temp,loc=9,lvl=0;
   level(lvl);
   bool b1=true;
   welcomeTest();
   Notes();
          printBoard(puzzle,lvl);
  do{
     loc=9;
     do
     {
     move_pos=getch();
     if(move_pos==80||move_pos==72||move_pos==75||move_pos==77||move_pos==27||move_pos=='r'||move_pos=='R')
         {
           if(move_pos==80||move_pos==72||move_pos==75||move_pos==77){
                    loc=swap(puzzle,move_pos,loc);
            }
          //For Exit
          if(move_pos==27){
           printf("\n\n   ------- Programming.OM ------");
           printf("\n\n      ------- End Game ------");
                exit(0);
           }
          system("cls");
          printBoard(puzzle,lvl);
          int w = win(puzzle);
          if(w == 0){
           getch();
           system("cls");
           break;
    }
       //move++;
       //move_print();

       //If Reset game
       if(move_pos=='r'||move_pos=='R'){
          loc=9;
          level(lvl);
      system("cls");
      printBoard(puzzle,lvl);
             move_count=0;
            }
   }
       }while(1);
     printf("\n\nYou want to play next level :- ");
     scanf("%c",&option);
     if(option=='y'||option=='Y'){
        move_count=0;
        lvl++;
     level(lvl);
        printBoard(puzzle,lvl);
        b1=true;
  }
     else{
        b1=false;
        printf("\n\n   ------- Programming.OM ------");
    printf("\n\n      ------- End Game ------");
      }
    }while(b1);
   return 0;
  }

  int getkey()
   {
       int ch;
       printf("\n");
    ch=getch();
    if(ch==0)
       {
           ch=getch();
           return ch;
       }
      return ch;
   }

   int check(int m[][3],int move)
    {
       //Down move
       if(move==80){
         for(int i=0;i<3;i++){
            if(m[0][i]==0)
                return 1;
         }
       }
       //Up move
       if(move==72){
        for(int i=0;i<3;i++){
            if(m[2][i]==0)
                return 1;
        }
       }
      //Right move
       if(move==77){
        for(int i=0;i<3;i++){
            if(m[i][0]==0)
                return 1;
        }
       }
       //Left move
       if(move==75){
        for(int i=0;i<3;i++){
            if(m[i][2]==0)
                return 1;
        }
       }
    return 0;
   }

    void printBoard(int m[][3],int lvl)
      {
        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
        printf("\n  %c   Number Puzzle Game   %c",186,186);
        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);

        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,203,205,205,205,203,205,205,205,187);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",186,0,m[0][0],0,186,0,m[0][1],0,186,0,m[0][2],0,186);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",204,205,205,205,206,205,205,205,206,205,205,205,185);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",186,0,m[1][0],0,186,0,m[1][1],0,186,0,m[1][2],0,186);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",204,205,205,205,206,205,205,205,206,205,205,205,185);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",186,0,m[2][0],0,186,0,m[2][1],0,186,0,m[2][2],0,186);
        printf("\n\t%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,202,205,205,205,202,205,205,205,188);

        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,203,205,205,205,205,205,205,205,205,205,205,205,187);
        printf("\n  %c Level No.  %c%5d\t   %c",186,186,lvl,186);
        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,202,205,205,205,205,205,205,205,205,205,205,205,188);
        move_print();
      }
   int swap(int m[][3],int move,int loc){
        int temp=0;
        if(check(m,move)==1){
            cout<<"Invalid swap...";
            return loc;
        }
        else{
            move_count+=1;
            if(loc==1){
                temp=m[0][0];
                if(move==75){
                    m[0][0]=m[0][1];
                    m[0][1]=temp;
                    return 2;
                }
                if(move==72){
                    m[0][0]=m[1][0];
                    m[1][0]=temp;
                    return 4;
                }
            }
              if(loc==2){
                temp=m[0][1];
                if(move==77){
                    m[0][1]=m[0][0];
                    m[0][0]=temp;
                    return 1;
                }
                if(move==72){
                    m[0][1]=m[1][1];
                    m[1][1]=temp;
                    return 5;
                }
                if(move==75){
                    m[0][1]=m[0][2];
                    m[0][2]=temp;
                    return 3;
                }
            }
            if(loc==3){
                temp=m[0][2];
                if(move==77){
                    m[0][2]=m[0][1];
                    m[0][1]=temp;
                    return 2;
                }
                if(move==72){
                    m[0][2]=m[1][2];
                    m[1][2]=temp;
                    return 6;
                }
            }
            if(loc==4){
                temp=m[1][0];
                if(move==80){
                    m[1][0]=m[0][0];
                    m[0][0]=temp;
                    return 1;
                }
                if(move==75){
                    m[1][0]=m[1][1];
                    m[1][1]=temp;
                    return 5;
                }
                if(move==72){
                    m[1][0]=m[2][0];
                    m[2][0]=temp;
                    return 7;
                }
            }
            if(loc==5){
                temp=m[1][1];
                if(move==72){
                    m[1][1]=m[2][1];
                    m[2][1]=temp;
                    return 8;
                }
                if(move==77){
                    m[1][1]=m[1][0];
                    m[1][0]=temp;
                    return 4;
                }
                if(move==75){
                    m[1][1]=m[1][2];
                    m[1][2]=temp;
                    return 6;
                }
                if(move==80){
                    m[1][1]=m[0][1];
                    m[0][1]=temp;
                    return 2;
                }
            }
            if(loc==6){
                temp=m[1][2];
                if(move==72){
                    m[1][2]=m[2][2];
                    m[2][2]=temp;
                    return 9;
                }
                if(move==77){
                    m[1][2]=m[1][1];
                    m[1][1]=temp;
                    return 5;
                }
                if(move==80){
                    m[1][2]=m[0][2];
                    m[0][2]=temp;
                    return 3;
                }
            }
            if(loc==7){
                temp=m[2][0];
                if(move==80){
                    m[2][0]=m[1][0];
                    m[1][0]=temp;
                    return 4;
                }
                if(move==75){
                    m[2][0]=m[2][1];
                    m[2][1]=temp;
                    return 8;
                }
            }
            if(loc==8){
                temp=m[2][1];
                if(move==80){
                    m[2][1]=m[1][1];
                    m[1][1]=temp;
                    return 5;
                }
                if(move==77){
                    m[2][1]=m[2][0];
                    m[2][0]=temp;
                    return 7;
                }
                if(move==75){
                    m[2][1]=m[2][2];
                    m[2][2]=temp;
                    return 9;
                }
            }
            if(loc==9){
               // temp=m[2][2];
                if(move==80){
                    temp=m[1][2];
                    m[1][2]=m[2][2];
                    m[2][2]=temp;
                    return 6;
                }
                if(move==77){
                    temp=m[2][1];
                    m[2][1]=m[2][2];
                    m[2][2]=temp;
                    return 8;
                }
            }

        }
    return loc;
   }
  //For win
  int win(int m[][3]){
      int full=0,k,i,j;
      if(m[0][0]==49&&m[0][1]==50&&m[0][2]==51)
        full+=3;
      if(m[1][0]==52&&m[1][1]==53&&m[1][2]==54)
        full+=3;
      if(m[2][0]==55&&m[2][1]==56)
        full+=2;
      if(full==8){
        cout<<"\n  Congratulations! You won."<<endl;
        return 0;
    }
    return 1;
  }

    void set(int lv[])
   {
     int i,j,k=0;
     for(i=0;i<3;i++)
     {
      for(j=0;j<3;j++)
       {
             puzzle[i][j]=lv[k++];
   }
  }
   }
  void level(int lvl)
  {
   move_count=0;
   int level0[]={55,53,50,52,54,49,56,51,0};
        int level1[]={50,53,49,55,52,56,54,51,0};
   int level2[9]={51,50,52,49,54,53,56,55,0};
   int level3[9]={52,51,50,53,49,56,54,55,0};
   int level4[9]={49,51,50,56,52,56,54,53,0};
   int level5[9]={50,51,53,49,52,55,54,56,0};
        int level6[9]={53,51,49,50,56,54,52,55,0};
        int level7[9]={56,55,50,53,52,49,54,51,0};
        int level8[9]={49,53,52,51,56,55,54,50,0};
        int level9[9]={55,50,49,53,52,51,55,54,0};
        int level10[9]={56,54,50,49,53,52,51,55,0};
      if(lvl==0){
       set(level0);
 }
   else if(lvl==1)
    {
      set(level1);
        }
      else if(lvl==2)
       {
          set(level2);
    }
      else if(lvl==3)
       {
          set(level3);
  }
      else if(lvl==4)
       {
          set(level4);
    }
      else if(lvl==5)
       {
          set(level5);
    }
      else if(lvl==6)
       {
          set(level6);
    }
      else if(lvl==7)
       {
          set(level7);
    }
      else if(lvl==8)
       {
          set(level8);
    }
      else if(lvl==9)
       {
          set(level9);
    }
 else //if(lvl==10)
       {
          set(level10);
    }
      }

    void move_print()
     {
        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,203,205,205,205,205,205,205,205,205,205,205,205,187);
        printf("\n  %c Total Move %c%5d\t   %c",186,186,move_count,186);
        printf("\n  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205,205,205,205,205,205,205,205,205,205,202,205,205,205,205,205,205,205,205,205,205,205,188);

    }
   void welcomeTest()
  {
     printf("\n\n\n\n\n\t\t\t<---- Programming.OM ---->\n");
            printf("\t\t<<<< WELCOME IN NUMBER PUZZLE GAME >>>>");
           getch();
           system("cls");
  }

   void Notes()
  {
    printf("\nInstructions of this Game..");
    printf("\n\nMove left using left arrow key.");
    printf("\nMove right using right arrow key.");
    printf("\nMove up using up arrow key.");
    printf("\nMove down using down arrow key.");
    printf("\nReset using r and R key.");
    printf("\nExit using escape key.");
          getch();
          system("cls");
          }
Output :-




Post a Comment

0 Comments