Monday, November 27, 2017

Sliding Puzzle - A C++ console game on Dev C++

// Sliding puzzle. A C++ console game.  
// Slide the numbers till they are in the correct order.
// Instructions for keyboard keys given on output screen.

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
using namespace std;

char Verify(int A[][4])
{ int val = 0;
  
  for(int i=0; i<4; i++)
  { for(int j=0; j<4; j++)
      if(A[i][j] == (i*4)+(j+1))
        val++;
  }
  if(val == 16)
    return 'Y';
  else
    return 'N';
}

void Display(int A[][4])
{ for(int i=0; i<4; i++)
  { for(int j=0; j<4; j++)
      if(A[i][j] == 16)
        cout << setw(4) << "";
      else
        cout << setw(4) << A[i][j];
    cout << "\n\n";
  }
  cout << "\n\n";
}

void Shuffle(int A[][4],int r, int c, int R, int C)
{ int temp = A[r][c];
  A[r][c] = A[R][C];
  A[R][C] = temp;
}

void Check(int A[][4], int r, int c)
{ if(r == 0)
  { if(c == 0)
    { int Random = rand() % 2;
      (Random == 0)? Shuffle(A, r, c, 0, 1): Shuffle(A, r, c, 1, 0);
    }
    else if(c == 1)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 0, 0): (Random == 1)? Shuffle(A, r, c, 0, 2): Shuffle(A, r, c, 1, 1);
    }
    else if(c == 2)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 0, 1): (Random == 1)? Shuffle(A, r, c, 0, 3): Shuffle(A, r, c, 1, 2);
    }
    else if(c == 3)
    { int Random = rand() % 2;
      (Random == 0)? Shuffle(A, r, c, 0, 2): Shuffle(A, r, c, 1, 3);
    }
  }
  else if(r == 1)
  { if(c == 0)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 0, 0): (Random == 1)? Shuffle(A, r, c, 1, 1): Shuffle(A, r, c, 2, 0);
    }
    else if(c == 1)
    { int Random = rand() % 4;
      (Random == 0)? Shuffle(A, r, c, 0, 1): (Random == 1)? Shuffle(A, r, c, 1, 0): (Random == 2)? Shuffle(A, r, c, 1, 2): Shuffle(A, r, c, 2, 1);
    }
    else if(c == 2)
    { int Random = rand() % 4;
      (Random == 0)? Shuffle(A, r, c, 0, 2): (Random == 1)? Shuffle(A, r, c, 1, 1): (Random == 2)? Shuffle(A, r, c, 1, 3): Shuffle(A, r, c, 2, 2);
    }
    else if(c == 3)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 0, 3): (Random == 1)? Shuffle(A, r, c, 1, 2): Shuffle(A, r, c, 2, 3);
    }
  }
  else if(r == 2)
  { if(c == 0)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 1, 0): (Random == 1)? Shuffle(A, r, c, 2, 1): Shuffle(A, r, c, 3, 0);
    }
    else if(c == 1)
    { int Random = rand() % 4;
      (Random == 0)? Shuffle(A, r, c, 1, 1): (Random == 1)? Shuffle(A, r, c, 2, 0): (Random == 2)? Shuffle(A, r, c, 2, 2): Shuffle(A, r, c, 3, 2);
    }
    else if(c == 2)
    { int Random = rand() % 4;
      (Random == 0)? Shuffle(A, r, c, 1, 2): (Random == 1)? Shuffle(A, r, c, 2, 1): (Random == 2)? Shuffle(A, r, c, 2, 3): Shuffle(A, r, c, 3, 2);
    }
    else if(c == 3)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 1, 3): (Random == 1)? Shuffle(A, r, c, 2, 2): Shuffle(A, r, c, 3, 3);
    }
  }
  else if(r == 3)
  { if(c == 0)
    { int Random = rand() % 2;
      (Random == 0)? Shuffle(A, r, c, 2, 0): Shuffle(A, r, c, 3, 1);
    }
    else if(c == 1)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 2, 1): (Random == 1)? Shuffle(A, r, c, 3, 0): Shuffle(A, r, c, 3, 2);
    }
    else if(c == 2)
    { int Random = rand() % 3;
      (Random == 0)? Shuffle(A, r, c, 2, 2): (Random == 1)? Shuffle(A, r, c, 3, 1): Shuffle(A, r, c, 3, 3);
    }
    else if(c == 3)
    { int Random = rand() % 2;
      (Random == 0)? Shuffle(A, r, c, 2, 3): Shuffle(A, r, c, 3, 2);
    }
  }
}

int main()
{ char ch;
  int A[4][4] = { {  1,  2,  3,  4 },
                  {  5,  6,  7,  8 },
                  {  9, 10, 11, 12 },
                  { 13, 14, 15, 16 },
                };
  
  int AtRow, AtCol;
  srand(time(NULL));
  
  for(int ctr=0; ctr<100; ctr++)
  { for(AtRow=0; AtRow<4; AtRow++)
    { for(AtCol=0; AtCol<4; AtCol++)
       { if(A[AtRow][AtCol] == 16)
           Check(A, AtRow, AtCol);
       }
    }
  }
  Display(A);
  cout << "This is your starting grid\n\n";
  cout << "Press 'I' to move number up.\n";
  cout << "Press \'J\' to move number left.\n";
  cout << "Press \'L\' to move number right.\n";
  cout << "Press \'K\' to move number down.\n\n";
  cout << "Press any key to start game.\n\n\n";
  getch();
   
  system("CLS");
  int FoundAtRow, FoundAtCol;
  
  while(1)
  { Display(A);
    char ch = getch();
    for(int i=0; i<4; i++)
    { for(int j=0; j<4; j++)
        if(A[i][j] == 16)
        { FoundAtRow = i;
          FoundAtCol = j;
        }
    }

    if(toupper(ch) == 'I')  // UP
    { if(FoundAtRow != 3)
        Shuffle(A, FoundAtRow+1, FoundAtCol, FoundAtRow, FoundAtCol);
    }
    else if(toupper(ch) == 'K')  // DOWN
    { if(FoundAtRow != 0)
        Shuffle(A, FoundAtRow-1, FoundAtCol, FoundAtRow, FoundAtCol);
    }
    else if(toupper(ch) == 'J')  // LEFT
    { if(FoundAtCol != 3)
        Shuffle(A, FoundAtRow, FoundAtCol+1, FoundAtRow, FoundAtCol);
    }
    else if(toupper(ch) == 'L')  // RIGHT
    { if(FoundAtCol != 0)
        Shuffle(A, FoundAtRow, FoundAtCol-1, FoundAtRow, FoundAtCol);
    }
    
    system("CLS");

    char val = Verify(A);
    if(val == 'Y')
    { Display(A);
      cout << "CONGRATULATIONS!!";
      getch();
      break;
    }
  }
  
  getch();
}

Tuesday, November 14, 2017

Animation of dipping tea-bag in Dev C++

C++ program to animate dipping of tea-bag. Runs on Dev C++. You may have to adjust Height of the output screen by clicking the top left corner of the output screen. Choose Properties -> Layout tab -> Window size. Increase the value of height to about 50. You may have to also change the Window Position by decreasing the value of Top.


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

void Cup1()
{ cout << "\n";
  cout << "                      .------.____\n";
  cout << "                   .-'       \\ ___)\n";
  cout << "                .-'         \\\\\\\n";
  cout << "             .-'        ___  \\\\)\n";
  cout << "          .-'          /  (\\  |)\n";
  cout << "        .-         __  \\  ( | |\n";
  cout << "                  /  \\  \\__'| |\n";
  cout << "                 /    \\____).-'\n";
  cout << "               .'       /   |\n";
  cout << "              /     .  /    |\n";
  cout << "            .'     / \\/     |\n";
  cout << "           /      /   \\     |\n";
  cout << "                 /    /    _|_\n";
  cout << "                 \\   /    /\\ /\\\n";
  cout << "                  \\ /    /__v__\\\n";
  cout << "                   '    |       |\n";
  cout << "                        |     .#|\n";
  cout << "                        |#.  .##|\n";
  cout << "                        |#######|\n";
  cout << "                        |#######|\n";
  cout << "                      ( ---------\n";
  cout << "                        )     (\n";
  cout << "                 ___...(-------)-....___\n";
  cout << "             .-\"\"       )    (          \"\"-.\n";
  cout << "       .-'``'|-._             )         _.-|\n";
  cout << "      /  .--.|   `\"\"---...........---\"\"`   |\n";
  cout << "     /  /    |                             |\n";
  cout << "     |  |    |                             |\n";
  cout << "      \\  \\   |                             |\n";
  cout << "       `\\ `\\ |                             |\n";
  cout << "         `\\ `|                             |\n";
  cout << "         _/ /\\                             /\n";
  cout << "        (__/  \\                           /\n";
  cout << "     _..---\"\"` \\                         /`\"\"---.._\n";
  cout << "  .-'           \\                       /          '-.\n";
  cout << " :               `-.__             __.-'              :\n";
  cout << " :                  ) \"\"---...---\"\" (                 :\n";
  cout << "  '._               `\"--...___...--\"`              _.'\n";
  cout << "    \\""--..__                              __..--\"\"/\n";
  cout << "     '._     \"\"\"----.....______.....----\"\"\"     _.'\n";
  cout << "        `\"\"--..,,_____            _____,,..--""`\n";
  cout << "                      `\"\"\"----\"\"\"`\n";
}

void Cup2()
{ cout << "\n\n\n";
  cout << "                      .------.____\n";
  cout << "                   .-'       \\ ___)\n";
  cout << "                .-'         \\\\\\\n";
  cout << "             .-'        ___  \\\\)\n";
  cout << "          .-'          /  (\\  |)\n";
  cout << "        .-         __  \\  ( | |\n";
  cout << "                  /  \\  \\__'| |\n";
  cout << "                 /    \\____).-'\n";
  cout << "               .'       /   |\n";
  cout << "              /     .  /    |\n";
  cout << "            .'     / \\/     |\n";
  cout << "           /      /   \\     |\n";
  cout << "                 /    /    _|_\n";
  cout << "                 \\   /    /\\ /\\\n";
  cout << "                  \\ /    /__v__\\\n";
  cout << "                   '    |       |\n";
  cout << "                        |     .#|\n";
  cout << "                        |#.  .##|\n";
  cout << "                        |#######|\n";
  cout << "                        |#######|\n";
  cout << "                 ___...(---------....___\n";
  cout << "             .-\"\"       )    (          \"\"-.\n";
  cout << "       .-'``'|-._             )         _.-|\n";
  cout << "      /  .--.|   `\"\"---...........---\"\"`   |\n";
  cout << "     /  /    |                             |\n";
  cout << "     |  |    |                             |\n";
  cout << "      \\  \\   |                             |\n";
  cout << "       `\\ `\\ |                             |\n";
  cout << "         `\\ `|                             |\n";
  cout << "         _/ /\\                             /\n";
  cout << "        (__/  \\                           /\n";
  cout << "     _..---\"\"` \\                         /`\"\"---.._\n";
  cout << "  .-'           \\                       /          '-.\n";
  cout << " :               `-.__             __.-'              :\n";
  cout << " :                  ) \"\"---...---\"\" (                 :\n";
  cout << "  '._               `\"--...___...--\"`              _.'\n";
  cout << "    \\""--..__                              __..--\"\"/\n";
  cout << "     '._     \"\"\"----.....______.....----\"\"\"     _.'\n";
  cout << "        `\"\"--..,,_____            _____,,..--""`\n";
  cout << "                      `\"\"\"----\"\"\"`\n";
}

void Cup3()
{ cout << "\n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                      .------.____\n";
  cout << "                   .-'       \\ ___)\n";
  cout << "                .-'         \\\\\\\n";
  cout << "             .-'        ___  \\\\)\n";
  cout << "          .-'          /  (\\  |)\n";
  cout << "        .-         __  \\  ( | |\n";
  cout << "                  /  \\  \\__'| |\n";
  cout << "                 /    \\____).-'\n";
  cout << "               .'       /   |\n";
  cout << "              /     .  /    |\n";
  cout << "            .'     / \\/     |\n";
  cout << "           /      /   \\     |\n";
  cout << "                 /    /    _|_\n";
  cout << "                 \\   /    /\\ /\\\n";
  cout << "                  \\ /    /__v__\\\n";
  cout << "                   '    |       |\n";
  cout << "                        |     .#|\n";
  cout << "                        |#.  .##|\n";
  cout << "                 ___...(|#######|....___\n";
  cout << "             .-\"\"       |#######|       \"\"-.\n";
  cout << "       .-'``'|-._       ---------       _.-|\n";
  cout << "      /  .--.|   `\"\"---...........---\"\"`   |\n";
  cout << "     /  /    |                             |\n";
  cout << "     |  |    |                             |\n";
  cout << "      \\  \\   |                             |\n";
  cout << "       `\\ `\\ |                             |\n";
  cout << "         `\\ `|                             |\n";
  cout << "         _/ /\\                             /\n";
  cout << "        (__/  \\                           /\n";
  cout << "     _..---\"\"` \\                         /`\"\"---.._\n";
  cout << "  .-'           \\                       /          '-.\n";
  cout << " :               `-.__             __.-'              :\n";
  cout << " :                  ) \"\"---...---\"\" (                 :\n";
  cout << "  '._               `\"--...___...--\"`              _.'\n";
  cout << "    \\""--..__                              __..--\"\"/\n";
  cout << "     '._     \"\"\"----.....______.....----\"\"\"     _.'\n";
  cout << "        `\"\"--..,,_____            _____,,..--""`\n";
  cout << "                      `\"\"\"----\"\"\"`\n";
}

void Cup4()
{ cout << "\n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                      .------.____\n";
  cout << "                   .-'       \\ ___)\n";
  cout << "                .-'         \\\\\\\n";
  cout << "             .-'        ___  \\\\)\n";
  cout << "          .-'          /  (\\  |)\n";
  cout << "        .-         __  \\  ( | |\n";
  cout << "                  /  \\  \\__'| |\n";
  cout << "                 /    \\____).-'\n";
  cout << "               .'       /   |\n";
  cout << "              /     .  /    |\n";
  cout << "            .'     / \\/     |\n";
  cout << "           /      /   \\     |\n";
  cout << "                 /    /    _|_\n";
  cout << "                 \\   /    /\\ /\\\n";
  cout << "                  \\ /    /__v__\\\n";
  cout << "                   '    |       |\n";
  cout << "                 ___...(|    . #|....___\n";
  cout << "             .-\"\"       |#.  .##|       \"\"-.\n";
  cout << "       .-'``'|-._       |#######|       _.-|\n";
  cout << "      /  .--.|   `\"\"---...........---\"\"`   |\n";
  cout << "     /  /    |                             |\n";
  cout << "     |  |    |                             |\n";
  cout << "      \\  \\   |                             |\n";
  cout << "       `\\ `\\ |                             |\n";
  cout << "         `\\ `|                             |\n";
  cout << "         _/ /\\                             /\n";
  cout << "        (__/  \\                           /\n";
  cout << "     _..---\"\"` \\                         /`\"\"---.._\n";
  cout << "  .-'           \\                       /          '-.\n";
  cout << " :               `-.__             __.-'              :\n";
  cout << " :                  ) \"\"---...---\"\" (                 :\n";
  cout << "  '._               `\"--...___...--\"`              _.'\n";
  cout << "    \\""--..__                              __..--\"\"/\n";
  cout << "     '._     \"\"\"----.....______.....----\"\"\"     _.'\n";
  cout << "        `\"\"--..,,_____            _____,,..--""`\n";
  cout << "                      `\"\"\"----\"\"\"`\n";
}

void Cup5()
{ cout << "\n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                                   \n";
  cout << "                      .------.____\n";
  cout << "                   .-'       \\ ___)\n";
  cout << "                .-'         \\\\\\\n";
  cout << "             .-'        ___  \\\\)\n";
  cout << "          .-'          /  (\\  |)\n";
  cout << "        .-         __  \\  ( | |\n";
  cout << "                  /  \\  \\__'| |\n";
  cout << "                 /    \\____).-'\n";
  cout << "               .'       /   |\n";
  cout << "              /     .  /    |\n";
  cout << "            .'     / \\/     |\n";
  cout << "           /      /   \\     |\n";
  cout << "                 /    /    _|_\n";
  cout << "                 \\   /    /\\ /\\\n";
  cout << "                 ___...( /__v__ \\....___\n";
  cout << "             .-\"\"       |       |       \"\"-.\n";
  cout << "       .-'``'|-._       |    . #|       _.-|\n";
  cout << "      /  .--.|   `\"\"---...........---\"\"`   |\n";
  cout << "     /  /    |                             |\n";
  cout << "     |  |    |                             |\n";
  cout << "      \\  \\   |                             |\n";
  cout << "       `\\ `\\ |                             |\n";
  cout << "         `\\ `|                             |\n";
  cout << "         _/ /\\                             /\n";
  cout << "        (__/  \\                           /\n";
  cout << "     _..---\"\"` \\                         /`\"\"---.._\n";
  cout << "  .-'           \\                       /          '-.\n";
  cout << " :               `-.__             __.-'              :\n";
  cout << " :                  ) \"\"---...---\"\" (                 :\n";
  cout << "  '._               `\"--...___...--\"`              _.'\n";
  cout << "    \\""--..__                              __..--\"\"/\n";
  cout << "     '._     \"\"\"----.....______.....----\"\"\"     _.'\n";
  cout << "        `\"\"--..,,_____            _____,,..--""`\n";
  cout << "                      `\"\"\"----\"\"\"`\n";
}

int main()
{ while(!kbhit())
  { Cup1();  Sleep(200);  system("CLS");
    Cup2();  Sleep(200);  system("CLS");
    Cup3();  Sleep(200);  system("CLS");
    Cup4();  Sleep(200);  system("CLS");
    Cup5();  Sleep(200);  system("CLS");
    Cup4();  Sleep(200);  system("CLS");
    Cup3();  Sleep(200);  system("CLS");
    Cup2();  Sleep(200);  system("CLS");
    Cup1();  Sleep(200);  system("CLS");    
  }
}