Friday, December 11, 2015

Simple Animation to Fill Box color using C Program



Program #18

Description:
Dynamically plotting dots on a specific area in the screen using multi-colors.  Once all positions plotting is done.  Program will come to an end.

#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;

  /* 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(10);
   
/* 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;
}

 

No comments:

Post a Comment

Popular Posts