File: FA21_BSE_004.
cpp Page 1 of 3
#include<iostream>
#include<windows.h>
#include<cstdlib>
#include<conio.h>
#include<ctime>
using namespace std;
char characters[15][20]={};
int missed=0, caught=0;
bool result;
int level;
void nextLine();
void checkWin();
void escape(char esc);
void genChar(int lev);
void welcome();
void board();
void disappearChar(char ch);
int main(){
welcome();
int keyHit;
while(true){
genChar(level);
board();
if(kbhit()){
keyHit = getche();
if(keyHit == 27){
escape(keyHit);
}
disappearChar(keyHit);
}
if(missed == 5){
result = false;
board();
break;
}
if(caught == 5){
result = true;
board();
break;
}
Sleep(1000);
nextLine();
}
checkWin();
}
void board(){
system("cls");
for(int i=0 ; i<15 ; i++){
for(int j=0 ; j<20 ; j++){
cout<<characters[i][j]<<" ";
}
cout<<"\n";
}
for(int k=0 ; k<=40 ; k++){
cout<<"=";
}
File: FA21_BSE_004.cpp Page 2 of 3
cout<<"\n\n";
cout<<" Words Matched: Words Missed:\n";
cout<<" "<<caught<<" "<<missed;
cout<<"\n\n";
}
void genChar(int lev){
srand(time(0));
int col;
col = rand() % 20;
if(lev == 1){
characters[0][col]= 65 + rand() % 24;
}
if(lev == 2){
characters[0][col]= 32 + rand() % 90;
}
}
void nextLine(){
srand(time(0));
for(int k=0 ; k<20 ; k++){
if(characters[14][k] != '\0'){
characters[14][k] = '\0';
missed++;
}
}
for(int i=13 ; i>=0 ; i--){
for(int j=0 ; j<20 ; j++){
if(characters[i][j] != '\0'){
int nextCol = rand() %20;
characters[i+1][nextCol] = characters[i][j];
characters[i][j] = '\0';
break;
}
}
}
}
void disappearChar(char ch){
bool found = false;
for(int i=14 ; i>=0 ; i--){
for(int j=0 ; j<20 ; j++){
if(characters[i][j] == ch){
characters[i][j] = '\0';
found = true;
caught++;
goto endll;
}
}
}
if(found == false){ missed++; }
endll:
cout<<"";
}
void checkWin(){
if(result == true){
cout<<"\n\n CONGRATULATIONS\n\n You Have Won!!!\n\n";
}
if(result == false){
cout<<"\n\n SORRY\n\n You Have Lost!!!\n\n";
}
}
void escape(char esc){
File: FA21_BSE_004.cpp Page 3 of 3
cout<<"AAre you sure you want to exit?(Press esc again to end)\n";
esc = getche();
if(esc == 27){
exit(0);
}
}
void welcome(){
again:
system("cls");
cout<<"\n\n Welcome To Typing Tutor\n\n";
cout<<" Please enter the level you want to play(1 & 2):
\n\n ";
cin>>level;
if(level!=1 && level!=2){
goto again;
}
for(int i=3 ; i>=0 ; i--){
system("cls");
cout<<"\n\n Game starting in "<<i;
Sleep(1000);
}
}