C++ Program to Build Alphabet Puzzle Game in Command Line

Using this tutorial, We are going to discuss the concept to learn “C++ Program to Build Alphabet Puzzle Game in Command Line”. Shall we start the discussion below? Let’s go.

C++ Program to Build Alphabet Puzzle Game in Command Line

C++ Program to Build Alphabet Puzzle Game in Command Line

Program Code

//J.M.S.
//Programming.OM
#include<iostream>
#include<windows.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
 using namespace std;
  int  puzzle[][3]={70,68,71,
                    66,72,65,
                    69,67,
                  };
 void win(int  m[][3]);
 int getkey();
 int check(int m[][3],int move);
 int swap(int m[][3],int move,int loc);
 void print(int m[][3]);
 void printBoard(int m[][3]);
 int main()
  {
      int move_pos,temp,loc=9;
      printBoard(puzzle);
      do
 {
          loc=swap(puzzle,move_pos,loc);
          system("cls");
          printBoard(puzzle);
          win(puzzle);
          move_pos=getkey();
       }while(move_pos!=27);
    return 0;
  }

  int getkey()
   {
       int ch;
       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])
     {
         printf("nnt Alphabet Puzzle Gamen");
         printf("t _________________n");
         printf("t|     |     |     |n");
         printf("t|  %c  |  %c  |  %c  |n",m[0][0],m[0][1],m[0][2]);
         printf("t|_____|_____|_____|n");
         printf("t|     |     |     |n");
         printf("t|  %c  |  %c  |  %c  |n",m[1][0],m[1][1],m[1][2]);
         printf("t|_____|_____|_____|n");
         printf("t|     |     |     |n");
         printf("t|  %c  |  %c  |  %c  |n",m[2][0],m[2][1],m[2][2]);
         printf("t|_____|_____|_____|n");
     }

   int swap(int m[][3],int move,int loc){
        int temp=0,m=0;
        if(check(m,move)==1){
            cout<<"Invalid swap...";
            return loc;
        }
        else{
            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
  void 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<<"Congratulations! You won.";
        exit(0);
    }
  }

Final Words

I hope this article helps you to know about “C++ Program to Build Alphabet Puzzle Game in Command Line”. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment