Showing posts with label Gaming Program using C. Show all posts
Showing posts with label Gaming Program using C. Show all posts

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);
}






 

Sunday, December 13, 2015

Progress Bar with Execution time in C Program

Program #31

Description:
System will randomly generates unique (x,y) co-ordinates and in the front user can see the Progress Bar and Execution time.

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

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
/* Defined constants */
#define MAXDOTS 5;
#define ASCII_POSITION  122;
#define PROGRESS_ROW     13
#define PROGRESS_COLUMN  28

/* !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, rowTrack[5], colTrack[5];

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

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

  /* Function Prototype declaration */
  void getX();
  void getY();
  int duplicateCheck(int, int);
  void printCoOrdinates();
  void initialMessage();
  void drawProgressBar(int);
  void percentCompleted();
  void illusionProgressBar();
  void printExecutionTime(clock_t);

  /* Clear the Screen */
  clrscr();

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

  //printf("Start Time %f",startTime);
  /* Print Initial Message to the User */
  initialMessage();

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

  /* 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){

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

    /* program ends, once counter reaches MAXDOTS */
    if (counter==maxDots){

      /* Print Dynamic Co-Ordinates */
      printCoOrdinates();
      break;
    }

     /* Display Execution time */
     printExecutionTime(startTime);
  }
  getch();
  return 1;
}

/* Print Execution Time */
void printExecutionTime(clock_t startTime){
   double execTim;
   int finalSec, color;

   clock_t stopTime = clock();

   /* Convert Milliseconds into Seconds, for user friendly */
   execTim =  ((double)(stopTime-startTime))/CLOCKS_PER_SEC;
   finalSec = (int)execTim;

   gotoxy(30, 10);

   /*  print execution message */
   printf("Execution Time: %d (secs)",finalSec);
}


/* 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 = 25;


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

  /* Delay after illusion progress Bar */
  delay(300);
}


/* 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 * 20;

  /* 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 * 5;


  /* 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 *plsMessage = "Please wait ... (Press ANY Key to Exit)";
  int i, columnPos=20;
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay(50);
  }
}

/* Print dynamic co-ordinates */
void printCoOrdinates(){
  int i=0;
  /* Print the Result from 1st Row to 5th Row */
  for (; i<5; 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 1 to 25 only */
  if (y<1 || y>25){
    getY();
  }
  row = y;
}

/* Duplicate Position Check */
int duplicateCheck(int r, int c) {
  int i;
  int rowMatch=0, colMatch=0;

  /* check Row Value match with Existing Value */
  for (i=0; i<5; i++) {
     if (rowTrack[i] == r){
       rowMatch = 1;
     }
  }

  /* check Column Value match with Existing Value */
  for (i=0; i<5; 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;
}




 

Dynamic positioning x,y with Progress Bar using C Program

Program #30

Description:
Make 5 dots randomly in different (x,y) positions in the Screen.  Also Program will dynamically show the current progress using Progress Bar Logic (illusion progress bar as well as completion progress bar) using C Program.


/*
 Make 5 dots randomly in different (x,y) positions in the Screen
 Also Program will dynamically show the progress using Progress Bar Logic
 using C Program
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

/* Defined constants */
#define MAXDOTS 5;
#define ASCII_POSITION  122;
#define PROGRESS_ROW     13
#define PROGRESS_COLUMN  28

/* !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, rowTrack[5], colTrack[5];

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

  /* Local variable declaration */
  int counter=0, dupCheck=0, maxDots;
  char asciiChar;

  /* Function Prototype declaration */
  void getX();
  void getY();
  int duplicateCheck(int, int);
  void printCoOrdinates();
  void initialMessage();
  void drawProgressBar(int);
  void percentCompleted();
  void illusionProgressBar();

  /* Clear the Screen */
  clrscr();

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

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

  /* 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){
      /* Move Cursor to random position */
      gotoxy(col, row);
      printf("*");
    }
    maxDots = MAXDOTS;

    /* program ends, once counter reaches MAXDOTS */

    if (counter==maxDots){

      /* Print Dynamic Co-Ordinates */
      printCoOrdinates();
      break;
    }
  }
  getch();
  return 1;
}

/* 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 = 25;

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

  /* Delay after illusion progress Bar */
  delay(300);
}

/* 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 * 20;

  /* 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 * 5;

  /* 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 *plsMessage = "Please wait ... (Press ANY Key to Exit)";
  int i, columnPos=20;
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay(50);
  }
}

/* Print dynamic co-ordinates */
void printCoOrdinates(){
  int i=0;

  /* Print the Result from 1st Row to 5th Row */
  for (; i<5; 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 1 to 25 only */
  if (y<1 || y>25){
    getY();
  }
  row = y;
}

/* Duplicate Position Check */
int duplicateCheck(int r, int c) {
  int i;
  int rowMatch=0, colMatch=0;

  /* check Row Value match with Existing Value */
  for (i=0; i<5; i++) {
     if (rowTrack[i] == r){
       rowMatch = 1;
     }
  }

  /* check Column Value match with Existing Value */
  for (i=0; i<5; 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