Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Thursday, November 5, 2020

Fill Color or Fore color using C Program

 #include<conio.h>

#include<stdlib.h>

#include<dos.h>

/* Global Array Declaration */

int Box[50][50];

int main(){

  int counter=0, rowValue=1, colValue=1;

  int boxStatus, colorValue;

  int getColor();

  /* function prototype declaration */

  int getRow();

  int getColumn();

  int checkBoxCompletion();

 /* Clear the Screen */

  clrscr();

 /* Iterate Indefinite loop */

  for(;!kbhit();){

    rowValue = getRow();

    colValue = getColumn();

    /* set Box position using Currrent Row,Column */

    if (Box[rowValue][colValue] != 1){

      Box[rowValue][colValue] = 1;

    } else {


 /* avoid overwritting plotted color

      get a new blank position.

      If you wish, just comment below "continue"

      and see the difference

      */

      continue;

    }

  /* Re-initialize counter as 0 once it reaches INT max value */

    if (counter == 32766) counter = 0;


  /* Increase the Counter */

    counter++;

 /* get random color */

    colorValue = getColor();

   /* set color using randow value */

    textcolor(colorValue);

    /* Move the cursor position */

    gotoxy(colValue, rowValue);

   /* do color print */

    cprintf("*");

   /* set some delay in Milliseconds

    as much you decrease, that much fast system will

    fill the color + add mild-sound

    */

    sound(200*rowValue);

    delay(50);

   /* after delay, mute the sound */

    nosound();

   /* check box completion station after every dot plot */

    boxStatus = checkBoxCompletion();

    if (boxStatus == 1) {

      gotoxy(18, 18);

 /* print the success message using last dot color, Interesting!! */

      cprintf("Box Color Fill: Completed Successfully !!");

      getch();

  /* stop the execution */

      return 1;

    }

   }

   return 0;

}


int getRow(){

  int rowNum;

  /* getting a randow row */

   rowNum = random(25);

  if (rowNum<8 || rowNum>15){

 /* get next random */

    getRow();

  } else {

    return rowNum;

  }

  /* worst case, default return value */

  return 8;

}


int getColumn(){

  int colNum;

 /* getting a randow column */

   colNum = random(80);

   if (colNum<25 || colNum>50){

  /* get Next Column */

      getColumn();

   } else {

      return colNum;

   }

   /* worst case, default return value */

  return 25;

}


int getColor(){

   int color;

 /* getting a randow color */

   color = random(15);

   if (color == 0){

   /* 0 means BLACK color, it will spoil the Spot */

     getColor();

   } else {

     return color;

   }

  /* worst case, default return value */

   return 1;

}


int checkBoxCompletion(){

  int i,j;

  for (i=8;i<=15;i++){

    for (j=25; j<=50; j++){

      if(Box[i][j] != 1){

/* Color fill not yet completed */

 return 0;

      }

    }

  }

 

/* Color fill Finished */

  return 1;

}





Custom Clear Screen using C Program

 /*

  *****************************************************

    CUSTOM CLRSCR() function

    Clear the Screen using HORIZONTAL & VERTICAL wise

  *****************************************************

*/

#include <stdio.h>

#include <conio.h>

#include <dos.h>

void main(){

  int i,j;

  /* ------------------------------ */

  /* Message about Horizontal Clear */

  /* ------------------------------ */

     /* Fill the screen with @ symbol */

     for (i=1; i<=80;i++){

      for (j=1; j<=25; j++){

printf("@");

      }

    }


     gotoxy(20,13);

     textcolor(9);

     cprintf("Custom CLRSCR using HORIZONTAL !");

    /* Perform custom clrscr() by clearing in HORIZONTAL */

    for (j=1; j<=25; j++){

     for (i=1; i<=80;i++){

gotoxy(i,j); /* COLUMN, ROW */

printf(" ");

delay(2);

      }

    }

    /* ------------------------------ */

  /* Message about Vertical Clear */

  /* ------------------------------ */

     /* Fill the screen with @ symbol */

     for (i=1; i<=80;i++){

      for (j=1; j<=25; j++){

printf("@");

      }

    }

     gotoxy(20,13);

     textcolor(9);

     cprintf("Custom CLRSCR using Vertical !");

    /* Perform custom clrscr() by clearing in Vertical */

    for (i=1; i<=80;i++){

    for (j=1; j<=25; j++){

gotoxy(i,j); /* COLUMN, ROW */

printf(" ");

delay(2);

      }

    }

   getch();

}






Sunday, July 24, 2016

Pascal Triangle using C Program

Program #39

Description:
Print Pascal Triangle using C Program

/*
C Program full source code for Pascal Triangle
Pascal Triangle using C program
Coding for Pascal Triangle
*/

#include <stdio.h>
#include <stdlib.h>
/* Global Declaration */
int n, x[25][25];
int main()
{
    /* Get Input value from User i.e. N */
    printf("Please enter the N value:\n");
    scanf("%d", &n);
   
    /* Local Prototype Declaration */
    void validateInput(void);
    void intializeDefaultValues(void);
    void fillRemainingValues(void);
    void printResultMatrixFormat(void);
    void printResultTriangleFormat(void);
   
    validateInput();
    intializeDefaultValues();
    fillRemainingValues();
    printResultMatrixFormat();
    printResultTriangleFormat();
    return 0;
   
}

/*
Function #1
-----------
Validate the Input value i.e N
*/
void validateInput() {
    if (n<1 || n > 25) {
        printf("Input is beyond this program Assumption !! \nPlease enter input value {1 to 25}\n");
        exit(0);
    }
}

/*
Function #2
-----------
Initialize default values
*/
void intializeDefaultValues(){
    int i;
   
    /* Fill default values in Row wise */
    for (i=1; i<=n; i++){
        x[i][1] = 1;
    }
   
    /* Fill default values in Column wise */
    for (i=1; i<=n; i++){
        x[1][i] = 1;
    }
   
    /*
      Note:
      In above code, we can use single FOR LOOP to initialize the Row/Column wise values.
      Just making it simplified; It is your wish to simply this code further.
    */
}

/*
Function #3
-----------
Fill remaining values
*/
void fillRemainingValues(){
    int i, j;
   
    for(i=2; i<=n; i++){
       
        for (j=2; j<=n; j++){
          
           /*
           Skip the x[i][j] position Summation
              if (i-1) is zero (or)
              if (j-1) is zero
           */
          
           if ( ((i-1) == 0) || ((j-1) == 0)){
               break;
           } else {
          
               /* Summation */
               x[i][j] = x[i][j-1] + x[i-1][j];
           }
          
        }
    }
}

/*
Function #4
-----------
Print Result in Matrix Format
*/
void printResultMatrixFormat(){
    int i,j;
   
    printf("\n\n");
    for (i=1; i<=n+1; i++){
        for (j=1; j<=n-i+1; j++){
            printf("%5d",x[i][j]);
        }
       
        printf("\n");
    }
}

/*
Function #5
-----------
Print Result in Triangle Format
*/
void printResultTriangleFormat(){
    int i,j,k;
   
    printf("\n\n");
    for (i=1; i<=n; i++){
       
        /*
        Identify the Row position
        To Print the result in Diagonal wise!!
        */
        k=i;
        for (j=1; j<=i; j++){
            printf("%5d",x[k][j]);
           
            /* Move towards Diagonally Upside */
            k--;
           
            /* Stop the Movement if it reaches 1st Row */
            if(k==0){
                /* Move Cursor to Next Row */
                printf("\n");
               
                /* Move to Next Row */
                break;
            }
        }
       
        printf("\n");
    }
}

Sample Output using Input value as 5:



Sample Output using Input value as 10:



 

Sunday, May 29, 2016

Print * based on occurrence of a Number in given Input

Program #38


Print * based on occurrence of a Number in Given Input. 
Note: This program was asked in Interview.  May-2016.

Example:

1600514367

Output:

0: **
1: **
2:
3: *
4: *
5: *
6: **
7: *
8:
9:


Source Code:

#include <stdio.h>
#include <string.h>
int main()
{
    int i, j;
   
    /* declare a string variable to get Input */
    char str[20];
   
    /* static array of all number to compare */
    char staticArray[]= {'0', '1','2', '3', '4', '5', '6', '7', '8', '9'};
   
    /* Get Input String */
    printf("Please enter a numbers to Parse!\n");
    scanf("%s", str);
   
    /* Iterate the 1st loop 9 times (0 to 9) */
    for (i=0; i<= 9; i++){
       
        /* print every number from 1 to 9 */
        printf("%d: ", i);
       
        /* Iterate the 2nd loop upto string length of Input */
        for (j=0; j<strlen(str); j++){
           
            /* Compare both the characters
               Note: Both are characters, So we can directly compare it using ==
            */
            if (staticArray[i] == str[j]){
               
                /* Print the * */
                printf("*");
            }
        }
       
        /* Move cursor to next line */
        printf("\n");
       
    }
   
    return 0;
}

Thursday, December 31, 2015

Music Visualizer Simulation using C Program

Program #37

Description:
Music Visualization Simulation using C Program.  Dynamically it will pick the Themes.

/*
Music Visualizer Simulation using C Program
Music Visualization Dynamic Effect using C Program
Track Music using Simple Animation in C
Music Visualize Print using C Program
Vertical Progress Bar Print using C Program
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

/* Defined Constants */
#define TRACK_DELAY 15;

/* Global Function Prototype Declaration */
void clearColumnTrack();
void setColor();
void setTrackCharacter();
void printBaseLine();

/* Global Variables */
int trackChar, cols[25], rows[15];
int main(){

 /* Local Function Prototype Declaration */
 void printRandomTrack();
 void setColumnPosition();
 void setRowPosition();

 /* Clear the Screen */
 clrscr();

 /* randomize */
 randomize();

 /* Set Track Character */
 setTrackCharacter();

 /* Initialize Row & Column Positions */
 setColumnPosition();
 setRowPosition();
 for ( ; !kbhit() ; ){
   printBaseLine();
   printRandomTrack();
 }
 return 1;
}

/* Print Specific Track Dynamics - Specific COLUMN */
void printRandomTrack(){
 int i, row=20, randCols, randRows;
 int trackDelay;

 /* Set Track Speed i.e. Delay */
 trackDelay = TRACK_DELAY;

 /* Set a Random Track Color */
 setColor();

 /* Get a Column Randomly */
 randCols = random(20);

 /* Get a Row Randomly */
 randRows = random(10);

 /* Clear Column Track */
 clearColumnTrack(cols[randCols]);

 /* Print Track Bar */
 for (i=row; i<=rows[randRows]; i++){
   gotoxy(cols[randCols], row--);
   cprintf("%c", trackChar);
   /* Delay */
   delay( trackDelay );
 }
}

/* Clear Specific Column */
void clearColumnTrack(int column){
 int i, row=20;
 for (i=1;i<=10;i++){
   gotoxy(column, row--);
   printf(" ");
 }
}

/* Set a Track Color Randomly */
void setColor(){
  int color;
  color = random(15);
  /* Avoid BLACK color */
  if (color == 0) color = 1;
  /* Set Color */
  textcolor(color);
}

/* Get Random Track Character */
int getTrackCharacter(){
  int rands, trackChars[]={176, 177, 178, 179, 220, 223};
  randomize();
  /* Get a random Character */
  rands = random(6);
  /* Set Random Track Characters */
  return trackChars[rands];
}

/* Get Track Character */
void setTrackCharacter(){
  trackChar = getTrackCharacter();
}

/* Set Column Position */
void setColumnPosition(){
 int i, randCols=0;
 /* Column Array values */
  for (i=31; i<=51; i++){
    cols[randCols]=i;
    randCols++;
  }
}

/* Set Row Position */
void setRowPosition(){
 int i, randRows=0;
 /* Row Array values */
  for (i=20; i<=30; i++){
    rows[randRows]=i;
    randRows++;
  }
}

/* Draw Base Line */
void printBaseLine(){
  int i, colVal=26;
  for (i=1; i<=30; i++){
    gotoxy(colVal++, 20);
    printf("-");
  }
}






Sunday, December 27, 2015

15 Puzzle Game in C Program

Program #36

Description:
15 Puzzle Game with full source source.  By Default program will load 1-15 number in randomly in 4x4 matrix.  User can use Arrow Keys (Up, Left, Right and Down) to arrange the Number in a proper order starts from 1,1 to 4,3.  Once all the values are arranged then Program will ends. 

Help Option !!  Free Moves option introduced to help user when user stuck!! By pressing ENTER key system will arrange next SEQUENCE number and use can continue the game!!.  Free Move is present in Defined constant and By default as 3 free moves.

Invalid Arrow Option!!  Program will dynamically display the Current Keypress in a User friendly manner and if user press invalid Arrow Key it will display it accordingly !!

Move Count!!  Program will count the Total number of Valid Key press and it will show in a User friendly manner and in case Invalid Key pressed, system will not counter it!!  In case user uses Free Move then only the Move Counter will get incremented to 1.. That's not a Problem!!

/*
15 Puzzle Problem in C
15 Puzzle Game Online
15 Puzzle Game Source Code in C
15 Puzzle Game using C Program
15 Puzzle Simple Animation Program using C
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>

/* Defined Constants */
#define true  1;
#define false 0;
#define FREE_MOVES 3;

/* Global Variable Declaration */
int puzArr[5][5], row=1, column=1;
int curRow=4, curCol=4, totalMoves=0, freeMoves=0;

/* Global Prototype Declaration */
int getRandVal();
int setRandVal(int);
void clearScreen(int);
void draw15PuzzleBox();
void movePosition(int);
void swapValue(int,int);
void printPosition(char *);
int IsDone(void);

void main(){
  char inputKey, arrowKey;

  /* Local Prototype Declaration */
  int loadDefaultData();
  void resetRowColumn();
  void printTotalMoves();
  void printTotalFREEMoves();
  int doFREEMove();

  /* Clear the Screen */
  clrscr();

  /* Randomize */
  randomize();

  /* Set Free Moves */
  freeMoves = FREE_MOVES;

  /* Load Default Data */
  gotoxy(1,1);
  printf("Please wait.. Loading Data !!");
  loadDefaultData();

  /* Print Loaded Data */
  draw15PuzzleBox();
  /* Detect Arrow Keys in C Program
     Press ESCAPE - 27
     Press ENTER  - 13
     Get out from the Indefinite loop
  */
  while ( (inputKey = getch()) != 27){

    /* Set Default Text color as WHITE */
    textcolor(15);
    clrscr();

    /* Find Arrow Key or not */
    if (inputKey == '\0'){

      /* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72:
   movePosition(72);
   draw15PuzzleBox();
   printTotalMoves();
   printTotalFREEMoves();
   break;

 case 80:
   movePosition(80);
   draw15PuzzleBox();
   printTotalMoves();
   printTotalFREEMoves();
   break;

 case 75:
   movePosition(75);
   draw15PuzzleBox();
   printTotalMoves();
   printTotalFREEMoves();
   break;

 case 77:
   movePosition(77);
   draw15PuzzleBox();
   printTotalMoves();
   printTotalFREEMoves();
   break;

      }
    } else if (inputKey == 13){
      //printf("Enter key pressed..");
      clrscr();
      if (freeMoves > 0){
 doFREEMove();

 /* Increment Moves */
 totalMoves++;
 draw15PuzzleBox();
 printTotalMoves();
 printTotalFREEMoves();
      } else {
 draw15PuzzleBox();
 printTotalMoves();
 printTotalFREEMoves();
      }
    }

    /* Check 15-Puzzle Completion */
    if (IsDone()) {

      /* Clear the Screen and Announce the Result */
      clrscr();

      /* Print Total Moves */
      printTotalMoves();
      gotoxy(35, 13);
      textcolor(2);
      printPosition("Great!!");
      getch();

      /* Smoothly End the Program Execution ;) */
      exit(1);
    }
  }
}
/* Assign Random Value */
int loadDefaultData(){
  int i=1, j=1, randVal;

  /* Custom Clear Screen
   0 - Row Wise Clear
   1 - Column Wise Clear
   */
  randVal = random(1);
  clearScreen(randVal);
  for (; i<=4;) {
    for (; j<=4;) {

      /* Draw Puzzle Box */
      draw15PuzzleBox();
      /* get Random value */
      randVal = getRandVal();
      if ( setRandVal(randVal) ){

 /* Set the Next Value */
 puzArr[i][j] = randVal;

 /* Increment next Column */
 column = j;
 j++;

 /* Check Is all 15 values set */
 if (i==4 && j==4) {

   /* Set Zero to Final Position */
   puzArr[i][j] = 0;
   movePosition(0);
   return 1;
 }
      }
    }

    /* increment to Next Row */
    row = i;
    i++;

    /* Re-initialize the Column value */
    j=1;
  }

  /* Normal Data Load Completion */
  return 1;
}

/* Get a Random Number from 1 to 15 */
int getRandVal(){
  int i, randVal;
  int dataArr[15];
  for (i=0; i<15; i++){
    dataArr[i] = i+1;
  }
  randVal = random(15);
  randVal = dataArr[randVal];
  return randVal;
}

/* Check and Set a Random Value to next EMPTY position */
int setRandVal(int checkVal){
   int i, j;
   for (i=1; i<=4; i++){
     for (j=1; j<=4; j++) {

 /* Is this value already present and Allocated */
 if (puzArr[i][j] == checkVal) {
   return false;
 }
     }
   }

   /* Yes, We got a next Rand value for Next position */
   return true;
}

/* Draw the Current Data */
void draw15PuzzleBox() {
  int i, j, charPos=177, row=3;
  //int charSmile=2;

  /* Make Position to Draw the 15-Puzzle Box */
  gotoxy(3, row++);
  for (i=1; i<=20;i++) printf("%c",charPos);
  gotoxy(3, row);
  printf("%c", charPos);
  gotoxy(4, row++);
  for (i=1; i<=18;i++) printf(" ");
  printf("%c", charPos);

  for(i=1; i<=4; i++){
    gotoxy(3, row++);
    printf("%c", charPos);
    for(j=1; j<=4; j++){
      if (puzArr[i][j]){
 printf("%4d", puzArr[i][j]);
      } else {

 //printf("%4c",charSmile);
 printf("    ");
      }
    }
    printf("  %c", charPos);
  }
  gotoxy(3, row);
  printf("%c", charPos);
  gotoxy(4, row++);
  for (i=1; i<=18;i++) printf(" ");
  printf("%c", charPos);
  gotoxy(3, row++);
  for (i=1; i<=20;i++) printf("%c",charPos);

  /* Show Current Position Information */
  gotoxy(35, 13);
}

/* Clear the Screen */
void clearScreen(int rowOrColumn){
 int i, j;
 for(i=1; i<= 25; i++){
  for (j=1; j<=80; j++){

    /* Row Wise - 0 */
    if (rowOrColumn == 0){
 gotoxy(i, j);
    }

    /* Column Wise - 1 */
    else if (rowOrColumn == 1){
 gotoxy(j, i);
    }
    printf(" ");
  }
  delay(10);
 }

 /* Show Current Position Information */
  gotoxy(35, 13);
}

/* Reset Global Row & Column */
void resetRowColumn(){
  row    = 1;
  column = 1;
}

/* Move position */
void movePosition(int pos){
  int iRow, iCol;

  /* Keep Current Row & Column Positions for SWAP */
  iRow = curRow;
  iCol = curCol;

  /* Show Current Position Information */
  gotoxy(35, 13);
  if (pos == 0) {

    /* Set Color as LightGreen */
    textcolor(10);
    printPosition("READY ?");
  }

  /* Up - 72 */
  else if (pos == 72) {
    if (curRow<4) {
      curRow++;

      /* Ok, Do Swap */
      swapValue(iRow, iCol);

      /* Increment Total Move */
      totalMoves++;
      //printf("UP (%d,%d)             ", curRow, curCol);
      printPosition("  UP   ");
    } else {

      /* Set Text color as RED */
      textcolor(4);
      printPosition("INVALID");
    }

  }

  /* Down - 80 */
  else if (pos == 80) {
    if (curRow>1) {
      curRow--;

      /* Ok, Do Swap */
      swapValue(iRow, iCol);

      /* Increment Total Move */
      totalMoves++;
      printPosition(" DOWN  ");
    } else {

      /* Set Text color as RED */
      textcolor(4);
      printPosition("INVALID");
    }

  }

  /* Left - 75 */
  else if (pos == 75) {
    if (curCol<4) {
      curCol++;

      /* Ok, Do Swap */
      swapValue(iRow, iCol);

      /* Increment Total Move */
      totalMoves++;
      printPosition(" LEFT  ");
    } else {

      /* Set Text color as RED */
      textcolor(4);
      printPosition("INVALID");
    }

  }

  /* Right - 77 */
  else if (pos == 77) {
    if (curCol>1) {
      curCol--;

      /* Ok, Do Swap */
      swapValue(iRow, iCol);

      /* Increment Total Move */
      totalMoves++;
      printPosition(" RIGHT ");
    } else {

      /* Set Text color as RED */
      textcolor(4);
      printPosition("INVALID");
    }
  }
}

/* Swap the Value */
void swapValue(int prevRow, int prevCol){
  int prevValue, curValue;

  /* getCurrent Swap Value */
  prevValue = puzArr[prevRow][prevCol];
  curValue  = puzArr[curRow][curCol];

  /* Swap it, Simply !! */
  puzArr[prevRow][prevCol] = curValue;
  puzArr[curRow][curCol]  = prevValue;

  /* Draw Puzzle Box */
  draw15PuzzleBox();
}

/* Check 15-Puzzle Completion */
int IsDone(){
  int i, j, iSequence=1;
  for (i=1; i<=4; i++) {
    for (j=1; j<=4; j++) {
      if (puzArr[i][j] != iSequence) {
 return false;
      }

      /* Done, That's it!! */
      if (iSequence == 15){
 return true;
      }
      iSequence++;
    }
  }

  /* Yes, You have done the Magic !! */
  return true;
}

/* Print Total Moves */
void printTotalMoves(){
  int i, charPos=176, heart=3;
  gotoxy(38, 3);
  for (i=1; i<=18;i++) printf("%c",charPos);
  gotoxy(38, 4);
  printf("%c                %c",charPos, charPos);
  gotoxy(38, 5);
  printf("%c  T%ctal M%cves:  %c",charPos, heart, heart, charPos);
  gotoxy(38, 6);
  printf("%c     %4d       %c",charPos, totalMoves, charPos);
  gotoxy(38, 7);
  printf("%c                %c",charPos, charPos);
  gotoxy(38, 8);
  for (i=1; i<=18;i++) printf("%c",charPos);
}

/* Print Position in Text */
void printPosition(char *position){
  int i, charPos=176;
  gotoxy(38, 12);
  for (i=1; i<=18;i++) printf("%c",charPos);
  gotoxy(38, 13);
  printf("%c                %c",charPos, charPos);
  gotoxy(38, 14);
  cprintf("%c    %7s     %c",charPos, position, charPos);
  gotoxy(38, 15);
  printf("%c                %c",charPos, charPos);
  gotoxy(38, 16);
  for (i=1; i<=18;i++) printf("%c",charPos);
}

/* Total Free Moves */
void printTotalFREEMoves(){
  int i, row=14, charPos=176, smile=3;
  gotoxy(6, row++);
  for (i=1; i<=25;i++) printf("%c",charPos);
  gotoxy(6, row++);
  printf("%c                       %c",charPos, charPos);
  if (freeMoves > 0) {
    gotoxy(6, row++);
    printf("%c      Fr%c%c M%cves       %c",charPos, smile, smile, smile, charPos);
    gotoxy(6, row++);
    printf("%c%11d            %c",charPos, freeMoves, charPos);
  }
  if (freeMoves > 0) {
    gotoxy(6, row++);
    printf("%c      Press ENTER      %c",charPos, charPos);
    gotoxy(6, row++);
    printf("%c     Get FREE Move     %c",charPos, charPos);
  } else {
    gotoxy(6, row++);
    printf("%c     -NO FREE Move-    %c",charPos, charPos);
  }
  gotoxy(6, row++);
  printf("%c                       %c",charPos, charPos);
  gotoxy(6, row++);
  for (i=1; i<=25;i++) printf("%c",charPos);

}

/* Do FREE Move - Help When User Stuck!! */
int doFREEMove(){
  int i, j, k, l, iSequence=1, toSwap;
  for (i=1; i<=4; i++) {
    for (j=1; j<=4; j++) {
      if (puzArr[i][j] != iSequence) {

 /* get iSequence value's Row & Column */
 for (k=1; k<=4; k++){
   for (l=1; l<=4; l++) {
     if (puzArr[k][l] == iSequence){

       /* Do FREE Swap */
       toSwap = puzArr[i][j];
       puzArr[i][j]=iSequence;
       puzArr[k][l]=toSwap;
       freeMoves--;
       return true;
     }
   }
 }
      }
      iSequence++;
    }
  }

  /* Yes, You have done the Magic !! */
  return true;
}




 

Wednesday, December 23, 2015

Star Message Print using C Program


Program #35


Description:
Print the Message in a 4 different angle and approximately like a Star !!.  And print will happen one after another in Multi-Color.  This Program will Indefinitely print (Slow Motion using Delay) until User press any Key. 

#include <conio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

/* Global Constant */
#define PRINT_SPEED 50

/* Global Variable Declaration */
char inputStr[25];
int col, row;

/* Global Prototype Declaration */
void identifyCoOrdinates(int);

int main(void){

  /* Local Variable Declaration */
  int i, yPos;

  /* Local Prototype Declaration */
  void validateStrLength();
  void printHorizontal();
  void printVertical();
  void printVerticalFromTopLeft();
  void printVerticalFromTopRight();
  void printVerticalFromBottomLeft();
  clrscr();

  /* get Input String */
  printf("Please Enter a Message:\n");
  gets(inputStr);

  /*  Validate Character Length */
  validateStrLength();
  for (; !kbhit(); ){

    /* Clear the Screen */
    clrscr();

    /* Print Horizontal Message */
    printHorizontal();

    /* Print Vertical Message */
    printVertical();

    /* Print Vertical Message From Left */
    printVerticalFromTopLeft();

    /* Print Vertical Message From Right */
    printVerticalFromTopRight();
  }
  getch();
  return 0;
}

void setColor(){
  int color, colorCode[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
  color = random(15);
  textcolor( colorCode[color] );
}

/* Validate String Lengh */
void validateStrLength(){
  if ( strlen(inputStr) > 25){
    clrscr();
    cprintf("Invalid Input! Bye!!");
    getch();
    exit(0);
  }
}

/* Print Message in Vertical-from-right */
void printVerticalFromTopRight(){
 int speed, i, colVal, rowVal;
 speed = PRINT_SPEED;

 /* setColor */
 setColor();

 /* Identify Position */
 identifyCoOrdinates(4);

 /* get Column Value */
 colVal = col;

 /* get Row Value */
 rowVal = row;

 for (i=0; i< strlen(inputStr); i++){
   gotoxy(colVal--, rowVal++);
   cprintf("%c", inputStr[i]);
   delay(speed);
 }
}

/* Print Message in Vertical-from-left */
void printVerticalFromTopLeft(){
 int speed, i, colVal, rowVal;
 speed = PRINT_SPEED;

 /* setColor */
 setColor();

 /* Identify Position */
 identifyCoOrdinates(3);

 /* get Column Value */
 colVal = col;

 /* get Row Value */
 rowVal = row;
 for (i=0; i< strlen(inputStr); i++){
   gotoxy(colVal++, rowVal++);
   cprintf("%c", inputStr[i]);
   delay(speed);
 }
}

/* Print Message in Vertical */
void printVertical(){
 int speed, i, colVal, rowVal;
 speed = PRINT_SPEED;

 /* setColor */
 setColor();

 /* Identify Position */
 identifyCoOrdinates(2);

 /* get Column Value */
 colVal = col;

 /* get Row Value */
 rowVal = row;
 for (i=0; i< strlen(inputStr); i++){
   gotoxy(colVal, rowVal++);
   cprintf("%c", inputStr[i]);
   delay(speed);
 }
}

/* Identify Right Co-Ordinate */
void identifyCoOrdinates(int Place){
  int rowCalc=12, colCalc=39;

  /* Horizontal */
  if (Place == 1){
    colCalc = strlen(inputStr);
    colCalc = 80 - colCalc;
    colCalc = (int)(colCalc/2);

    /* Assign to Global Column Variable */
    col = colCalc-strlen(inputStr)/2+1;

    /* Assign to Global Row Variable */
    row = rowCalc;
  }

  /* Vertical */
  else if (Place == 2){
    rowCalc = strlen(inputStr);
    rowCalc = 25 - rowCalc;
    rowCalc = (int)(rowCalc/2);

    /* Assign to Global Column Variable */
    col = colCalc;

    /* Assign to Global Row Variable */
    row = rowCalc;
  }

  /* Vertical From Left */
  else if (Place == 3){
    rowCalc = strlen(inputStr);
    rowCalc = 25 - rowCalc;
    rowCalc = (int)(rowCalc/2);

    colCalc = strlen(inputStr);
    colCalc = 80 - colCalc;
    colCalc = (int)(colCalc/2);

    /* Assign to Global Column Variable */
    col = colCalc;

    /* Assign to Global Row Variable */
    row = rowCalc;
  }

  /* Vertical From Right */
  else if (Place == 4){
    rowCalc = strlen(inputStr);
    rowCalc = 25 - rowCalc;
    rowCalc = (int)(rowCalc/2);

    colCalc = strlen(inputStr);
    colCalc = 80 - colCalc;
    colCalc = (int)(colCalc/2);

    /* Assign to Global Column Variable */
    col = colCalc+strlen(inputStr);

    /* Assign to Global Row Variable */
    row = rowCalc;
  }

}

/* Print Message in Horizontal */
void printHorizontal(){
 int speed, i, colVal, rowVal;
 speed = PRINT_SPEED;

 /* setColor */
 setColor();

 /* Identify Position */
 identifyCoOrdinates(1);

 /* get Column Value */
 colVal = col;

 /* get Row Value */
 rowVal = row;
 for (i=0; i< strlen(inputStr); i++){
   gotoxy(colVal, rowVal);
   cprintf("%c", inputStr[i]);
   colVal += 2;
   delay(speed);
 }
}







 

Saturday, December 19, 2015

Draw Triangle Wave using C Program

Program #34


/*
//////////////////////////////////////
// **** DRAW TRIANGLE WAVE ****** //
//////////////////////////////////////
This Program will Indefinitely Draw Triangle (Sample Sin) Wave in Multi-Colors
Also Additional information will keeps on show the Current Drawing Position
*/

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

/* Defined Constants */
#define WAVE_SPEED   100
#define DEFAULT_ROW   12
#define DEFAULT_COLUMN 1

/* Global Variable */
int column=5, row=12;

/* Global Function Prototypes */
void goStraight();
void goDown();
void goUp();
void drawCurrentPosition();
void drawSquareWave();
void drawUpperPortion();
void drawLowerPortion();
void setColor();
int main(){

  /* Local Function Prototypes */
  int checkColumn();

  /* Clear Screen */
  clrscr();

  /* Set Row and Column values */
  row    = DEFAULT_ROW;
  column = DEFAULT_COLUMN;

  /* Position the Cursor */
  gotoxy(column, row);

  /* Initiate Drawing Square Wave */
  drawSquareWave();
  return 1;
}

/* Initiate Indefinite Drawing */
void drawSquareWave(){
 for(; !kbhit(); ){
    goStraight();
    drawUpperPortion();
    goStraight();
    drawLowerPortion();
  }
}

/* Print Current Position */
void printCurrentPosition(int position){
 /* 1. Straight */
 if (position == 1){
  gotoxy(35, 20);
  printf("Draw Straight! ", column, row);
 }
 /* 2. Up */
 else if (position == 2){
  gotoxy(35, 20);
  printf("Draw Up!       ", column, row);
 }
 /* 3. Down */
 else if (position == 3) {
  gotoxy(35, 20);
  printf("Draw Down!     ", column, row);
 }
 /* 4. End of the Wave */
 else if (position == 4){
   gotoxy(30, 20);
   printf("Square Wave Completed !!");
 }
}

/* Check Column */
int checkColumn(){
  if ( (column+5) > 80) {
    printCurrentPosition(4);
    /* Clear the Screen and Continue the Indefinite Wave */
    clrscr();
    /* Reset the Square Wave Position once again */
    row    = DEFAULT_ROW;
    column = DEFAULT_COLUMN;
    gotoxy(column, row);
    /* Call Drawing Once again */
    drawSquareWave();
  }

  /* Ok, Valid Column */
  return 1;
}

/* Go Straight */
void goStraight(){
  int col, i, waveSpeed;
  /* Set Color */
  setColor();
  col = column;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=col; i< (col+4); i++){
      /* increase column */
      column++;
      /* Print Position */
      printCurrentPosition(1);
      /* draw Wave */
      gotoxy(i, row);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
    }
  }
}

/* Draw Upper Portion */
void drawUpperPortion(){
  goUp();
  goDown();
}
/* Draw Lower Portion */
void drawLowerPortion(){
  goDown();
  goUp();
}

/* Go Up */
void goUp(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i > (rows-4); i--){
      /* Print Position */
      printCurrentPosition(2);
      /* Draw Wave */
      gotoxy(column++, i);
      cprintf("*");
      /* Add Delay */
      delay( waveSpeed );
      /* decrease row */
      row--;
    }
  }
}

/* Go Down */
void goDown(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i< (rows+4); i++){
      /* Print Position */
      printCurrentPosition(3);
      /* Draw Wave */
      gotoxy(column++, i);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
      /* increase row */
      row++;
    }
  }
}

/* getColor */
void setColor(){
 int color;
 int colorArray[14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
 color = random(14);
 /* Restrict Zero - BLACK color */
 color = colorArray[color];
 textcolor(color);
}







 

Draw Square Wave using C Program

Program #33

/*
//////////////////////////////////////////
// **** DRAW SQUARE WAVE ****** //
//////////////////////////////////////////
This Program will Indefinitely Draw Square Wave in Multi-Colors
Also Additional information will keeps on show the Current Drawing Position
*/
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

/* Defined Constants */
#define WAVE_SPEED   100
#define DEFAULT_ROW   12
#define DEFAULT_COLUMN 1

/* Global Variable */
int column=5, row=12;

/* Global Function Prototypes */
void goStraight();
void goDown();
void goUp();
void drawCurrentPosition();
void drawSquareWave();
void drawUpperPortion();
void drawLowerPortion();
void setColor();

int main(){

  /* Local Function Prototypes */
  int checkColumn();

  /* Clear Screen */
  clrscr();

  /* Set Row and Column values */
  row    = DEFAULT_ROW;
  column = DEFAULT_COLUMN;

  /* Position the Cursor */
  gotoxy(column, row);

  /* Initiate Drawing Square Wave */
  drawSquareWave();
  return 1;
}

/* Initiate Indefinite Drawing */
void drawSquareWave(){
 for(; !kbhit(); ){
    goStraight();
    drawUpperPortion();
    goStraight();
    drawLowerPortion();
  }
}

/* Print Current Position */
void printCurrentPosition(int position){
 /* 1. Straight */
 if (position == 1){
  gotoxy(35, 20);
  printf("Draw Straight! ", column, row);
 }
 /* 2. Up */
 else if (position == 2){
  gotoxy(35, 20);
  printf("Draw Up!       ", column, row);
 }
 /* 3. Down */
 else if (position == 3) {
  gotoxy(35, 20);
  printf("Draw Down!     ", column, row);
 }
 /* 4. End of the Wave */
 else if (position == 4){
   gotoxy(30, 20);
   printf("Square Wave Completed !!");
 }
}

/* Check Column */
int checkColumn(){
  if ( (column+5) > 80) {
    printCurrentPosition(4);
    /* Clear the Screen and Continue the Indefinite Wave */
    clrscr();
    /* Reset the Square Wave Position once again */
    row    = DEFAULT_ROW;
    column = DEFAULT_COLUMN;
    gotoxy(column, row);
    /* Call Drawing Once again */
    drawSquareWave();
  }
  /* Ok, Valid Column */
  return 1;
}

/* Draw Upper Portion */
void drawUpperPortion(){
  goUp();
  goStraight();
  goDown();
}

/* Draw Lower Portion */
void drawLowerPortion(){
  goDown();
  goStraight();
  goUp();
}

/* Go Straight */
void goStraight(){
  int col, i, waveSpeed;
  /* Set Color */
  setColor();
  col = column;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=col; i< (col+4); i++){
      /* increase column */
      column++;
      /* Print Position */
      printCurrentPosition(1);
      /* draw Wave */
      gotoxy(i, row);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
    }
  }
}

/* Go Up */
void goUp(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i > (rows-4); i--){
      /* Print Position */
      printCurrentPosition(2);
      /* Draw Wave */
      gotoxy(column, i);
      cprintf("*");
      /* Add Delay */
      delay( waveSpeed );
      /* decrease row */
      row--;
    }
  }
}

/* Go Down */
void goDown(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i< (rows+4); i++){
      /* Print Position */
      printCurrentPosition(3);
      /* Draw Wave */
      gotoxy(column, i);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
      /* increase row */
      row++;
    }
  }
}

/* getColor */
void setColor(){
 int color;
 int colorArray[14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
 color = random(14);
 /* Restrict Zero - BLACK color */
 color = colorArray[color];
 textcolor(color);
}






 

Friday, December 18, 2015

DOT Magic Game using C Program


Program #32


/*
 **********************
 * DOT MAGIC GAME *
 **********************

 Make 5 dots randomly in different (x,y) positions in the Screen
 Also Program will dynamically show the progress using Progress Bar Logic

 1. Program will show the Dynamic Dots
 2. User need to touch the dots using Arrow Keys (UP, DOWN, LEFT and RIGHT)
 3. System will calculate time and
 4. Show the Time taken by the User to finish the Game

 ** C Program **
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

/* Defined constants */
#define MAXDOTS 10;
#define ASCII_POSITION  122;
#define PROGRESS_ROW     13
#define PROGRESS_COLUMN  28
#define DISPLAY_DOT_INPROGRESS 0

/* !Important!
This Program defines the Progress Bar ASCII Positions as
79, 80, 81 and 122
79  - illusion Progress Bar
122 - Completion Progress Bar
*/
/* Global variable declaration */
int row=1, col=1, remainingDots, rowTrack[10], colTrack[10];

/* Global function declaration */
char getAscii(int);
void initialMessage();
int main(){

  /* Local variable declaration */
  int counter=0, dupCheck=0, maxDots, flagDisplayDot;
  char asciiChar, inputKey, arrowKey;
  clock_t startTime, stopTime;

  /* Function Prototype declaration */
  void getX();
  void getY();
  int duplicateCheck(int, int);
  void printCoOrdinates();
  void drawProgressBar(int);
  void percentCompleted();
  void illusionProgressBar();
  void printExecutionTime(clock_t);
  void printDotsOnCoOrdinates();
  void moveUp();
  void moveDown();
  void moveLeft();
  void moveRight();

  /* Clear the Screen */
  clrscr();

  /* Start Execution Time */
  startTime = clock();

  /* Assign Remaining Dots using Defined value */
  remainingDots = MAXDOTS;

  /* Print Initial Message to the User */
  initialMessage(" ", 0, 0, 0);

  /* Print illusion Progress Bar */
  illusionProgressBar();

  /* Clear Inbut Buffer */
  //fflush(stdin);

  /* Indefinite loop */
  for (;!kbhit(); ){

    /* Randomize */
    randomize();

    /* get a next random (x,y) position */
    getX();
    getY();

    /* Initial value to Duplicate check */
    dupCheck = 0;

    /* Track Positions, avoid duplicate co-ordinates */
    if ( !duplicateCheck(row, col) ){

      /* no duplicate matched */
      dupCheck = 1;
    }

    if (dupCheck == 1){
      /* Sets current position to Positioning Array */
      rowTrack[counter] = row;
      colTrack[counter] = col;

      /* Increase DOT counter */
      counter++;
    }

    /* Draw Progress Bar about the Processing */
    drawProgressBar(counter);

    /* Print Percentage Completed */
    percentCompleted(counter);
    if (dupCheck == 1){
      flagDisplayDot = DISPLAY_DOT_INPROGRESS;
      if (flagDisplayDot == 1) {

 /* Move Cursor to random position */
 gotoxy(col, row);
 printf("*");
      }
    }
    maxDots = MAXDOTS;
    if (counter==maxDots){

      /* Clear the Screen and Print Dynamic Co-Ordinates */
      clrscr();
      initialMessage(" Processing Completed!", 30, 50, 0);
      initialMessage(" Your task to Clear all DOTS appear in the Screen!", 15, 50, 0);
      initialMessage(" Use KeyBoard Direction Keys (UP, DOWN, LEFT and RIGHT Arrows!!", 5, 50, 0);
      initialMessage(" Press ESCAPE to EXIT!!", 35, 50, 0);
      initialMessage(" Your time starts Now!!", 30, 50, 0);
      clrscr();
      printDotsOnCoOrdinates();
      //printCoOrdinates();

   /* Reset Row and Column Global Position and allow user to Start the Game */
   row = rowTrack[0];
   col = colTrack[0]-1;
   gotoxy(col, row);

   /* Start New Time before Game Starts */
   startTime = clock();

   /* Detect Arrow Keys in C Program
   Press ESCAPE - 27 to
   Get out from the Indefinite loop
   */
   while ( (inputKey = getch()) != 27){

  /* Display Execution time */
  printExecutionTime(startTime);

  /* Find Arrow Key or not */
  if (inputKey == '\0'){

    /* Extract actual Arrow Key */
    arrowKey = getch();
    switch(arrowKey){
  case 72:
     //printf("UP");
     moveUp();
     break;
  case 80:
     //printf("DOWN");
     moveDown();
     break;
  case 75:
     //printf("LEFT");
     moveLeft();
     break;
  case 77:
     //printf("RIGHT");
     moveRight();
     break;
    } /* End switch(arrowKey){ */
  } /* End if (inputKey == '\0'){ */
   } /* End of While loop */

   /* Once user press ESCAPE, Game Ends */
   break;

    } /* End if (counter==maxDots){ */
     /* Display Execution time */
     printExecutionTime(startTime);
  }
  return 1;
}

void checkDotsandMessage(){
  int i, maxDots;
  /* maxDots using Defined value */
  maxDots = MAXDOTS;
  for (i=0; i<maxDots; i++) {
    /* Check Task Completion Status */
    if (rowTrack[i] == 0 && colTrack[i] == 0) {
      //break;
    }
    if (rowTrack[i] == row && colTrack[i] == col){
      /* Decrement Remaining DOTS counter */
      remainingDots--;
      /* Print Specific Dot Completion */
      if (remainingDots <= 0) {
 /* Clear last DOT */
 gotoxy(col, row);
 printf(" ");
 initialMessage(" WOW !", 40, 200, 0);
 initialMessage("Game Ends, Bye !!", 33, 20, 0);
 /* Game ENDS on Completion of DOT tasks */
 getch();
 exit(0);
      }
      /* Clear Dynamic Position, once completed the Task */
      rowTrack[i] = 0;
      colTrack[i] = 0;
      break;
    }
  }
}

/* Move Up and Clear Data on the Position */
void moveUp(){
  gotoxy(col, row);
  printf(" ");
  if (row==1) {
    row = 25;
  } else if (row > 1) {
    row--;
  }
  /* Move to lastest */
  gotoxy(col, row);
  /* identify Right DOTS */
  checkDotsandMessage();
}

/* Move Down and Clear Data on the Position */
void moveDown(){
  gotoxy(col, row);
  printf(" ");
  if (row == 25) {
    row = 1;
  } else if (row <= 25) {
    row++;
  }
  /* Move to lastest */
  gotoxy(col, row);
  /* identify Right DOTS */
  checkDotsandMessage();
}

/* Move Left and Clear Data on the Position */
void moveLeft(){
  gotoxy(col, row);
  printf(" ");
  if (col == 1) {
    col = 80;
  } else if (col > 1) {
    col--;
  }
  /* Move to lastest */
  gotoxy(col, row);
  /* identify Right DOTS */
  checkDotsandMessage();
}

/* Move Right and Clear Data on the Position */
void moveRight(){
  gotoxy(col, row);
  printf(" ");
  if (col == 80) {
    col = 1;
  } else if (col < 80) {
    col++;
  }
  /* Move to lastest */
  gotoxy(col, row);
  /* identify Right DOTS */
  checkDotsandMessage();
}

/* Print Dots on dynamic co-ordinates */
void printDotsOnCoOrdinates(){
  int i=0, rowPos, colPos, maxDots;
  /* maxDots using Defined value */
  maxDots = MAXDOTS;
  /* Print the Result from 1st Row to 5th Row */
  for (; i<maxDots; i++) {
    rowPos = rowTrack[i];
    colPos = colTrack[i];
    gotoxy(colPos, rowPos);
    printf("*");
  }
}

/* Print Execution Time */
void printExecutionTime(clock_t startTime){
   double execTim;
   int i, finalSec, color;
   clock_t stopTime = clock();
   /* Convert Milliseconds into Seconds, for user friendly */
   execTim =  ((double)(stopTime-startTime))/CLOCKS_PER_SEC;
   finalSec = (int)execTim;
   /* Hide Time in case if is behaves wrongly */
   if(finalSec > 0){
     gotoxy(65, 1);
     /*  print execution message */
     printf("Time: %d sec(s)",finalSec);
     /* Draw underline to the Time */
     for(i=65; i< 80; i++){
       gotoxy(i, 2);
       printf("-");
     }
   }
}

/* Print illusion Progress Bar, for User Friendly ;) */
void illusionProgressBar(){
 int i, columnPos, rowPos, dynamicProgress;
 char asciiVal;
 /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN;
  rowPos    = PROGRESS_ROW;
  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = 30;
  /* Get Ascii character */
  asciiVal = getAscii(79);
  /* default progress bar position */
  for(i=0; i<dynamicProgress; i++){
   gotoxy(columnPos++, rowPos);
   printf("%c",asciiVal);
   /* Add delay to draw illusion progress bar, smoothly */
   delay(10);
  }
}

/* Print Percentage Completion */
void percentCompleted(int completion){
 int columnPos, rowPos, dynamicProgress;
 /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN + 27;
  rowPos    = PROGRESS_ROW;
  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = completion * 10;
  /* Positioning % completion */
  gotoxy(columnPos, rowPos);
  printf("%d% Completed", dynamicProgress);
}

/* Draw Progress Bar to show Current Completion status to the User */
void drawProgressBar(int completion){
  int i, columnPos, rowPos, dynamicProgress;
  char asciiVal;
  /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN;
  rowPos    = PROGRESS_ROW;
  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = completion * 3;
  /* Get Ascii character */
  asciiVal = getAscii(0);
  /* default progress bar position */
  gotoxy(columnPos, rowPos);
  for(i=0; i<dynamicProgress; i++){
   gotoxy(columnPos++, 13);
   printf("%c",asciiVal);
   /* Add delay to draw progress bar, smoothly */
   delay(100);
  }
}

/* Print Intial Message to the User */
void initialMessage(char *messageToPrint, int newColumn, int messageDelay, int printRes){
  char *plsMessage = "Please wait ... (Press ANY Key to Exit)";
  int i, columnPos=20, timeDelay=50;
  /* Re-use the function to print different message purpose */
  if ( strcmp(messageToPrint, " ") != 0){
    plsMessage = messageToPrint;
  }
  /* Overwrite Column Position based on Text to make it Center */
  if (newColumn != 0){
    columnPos = newColumn;
  }
  /* Overwrite Delay based on Text to print in slow/fast */
  if (messageDelay != 0){
    timeDelay = messageDelay;
  }
  /* Blank out the whole row to print new text */
  for(i=1; i<=80; i++){
    gotoxy(i, 12);
    printf(" ");
  }

  /* print Remaining Dot Counter */
    if (printRes != 0 && remainingDots > 0){
      gotoxy(columnPos++,12);
      printf("%d ", remainingDots);
    }
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay( timeDelay );
  }
}

/* Print dynamic co-ordinates */
void printCoOrdinates(){
  int i=0, maxDots;
  /* maxDots using Defined value */
  maxDots = MAXDOTS;
  /* Print the Result from 1st Row to 5th Row */
  for (; i<maxDots; i++) {
    gotoxy(1,(i+1));
    printf("Dynamic Position #%d: (%d,%d)", (i+1), colTrack[i], rowTrack[i]);
  }
}

/*
  get Random x position
  'x' means column value
*/
void getX() {
  int x=1;
  x = random(80);
  /* randVal range from 0 to 99
   but we are expecting COLUMN value
   but it ranges from 1 to 80 only */
  if (x<1 || x>80){
    getX();
  }
  col = x;
}

/*
  get Random y position
  'y' means row value
*/
void getY() {
  int y=1;
  /* get random for Row */
  y = random(25);
  /* randVal range from 0 to 99
   but we are expecting ROW value
   but it ranges from 3 to 25 only
   Row Number 1 and 2 - Used to Print
   a. Execution Time
   b. User time taken
   To avoid clashes.. !!
   */
  if (y<3 || y>25){
    getY();
  }
  row = y;
}

/* Duplicate Position Check */
int duplicateCheck(int r, int c) {
  int i, maxDots;
  int rowMatch=0, colMatch=0;
  /* maxDots using Defined value */
  maxDots = MAXDOTS;
  /* check Row Value match with Existing Value */
  for (i=0; i<maxDots; i++) {
     if (rowTrack[i] == r){
       rowMatch = 1;
     }
  }
  /* check Column Value match with Existing Value */
  for (i=0; i<maxDots; i++) {
     if (colTrack[i] == c){
       colMatch = 1;
     }
  }
  if (rowMatch == 0 && colMatch == 0) {
    return 0;
  }
  return 1;
}

/* Iterate the loop until expected ASCII character */
char getAscii(int overWritePos){
  int i, ascii_pos;
  /* Default position to the Ascii, !Important! */
  char cc='a';
  /* Assign default character position */
  ascii_pos = ASCII_POSITION;
  /* Overwrite Ascii Position, if function gets some position */
  if (overWritePos != 0){
    ascii_pos = overWritePos;
  }
  for (i=0; i<256; i++) {
    /*  Break the Loop, once we got the Character */
    if (i == ascii_pos)  break;
    cc = cc + 1;
  }
  return cc;
}








 

Popular Posts