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