C++ code for Scramble game using filing and C++ basics (with algorithm)

Here is the C++ code for Scramble game . This game includes multiple levels and each level gives you score . You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User showed with a Scrambled word and asked to enter correct one.
2: If user enters correct word levels up and score increased by 100
3: If user enters wrong word , this game gives another chance
4: If user enters correct word , levels up and score increased by 70
5: If user enters wrong word , this game gives another chance
6: If user enters correct word , levels up and score increased by 40
5: If user enters wrong word , the game ends here


Developer side
1: Declare variables that are going to be used in program (three character arrays one to store  Scrambled word one to store  correct word and another one for input )
2: Open scrambled word file "scramble word.txt" using following statement in.open("scramble word.txt");
3: Open correct word file "correct.txt" using following statement  inc.open("correct.txt");
4: Start a while loop at the end of any file (because both file are of same size)
5: Here read scrambled and correct word from respective files
6: Show scrambled word
7: Call enterguess(); function to input user's guess
8: Compare input with  correct word
9: If user enters correct word levels up and score increased by 100
10: If user enters wrong word , this game gives another chance
11: If user enters correct word , levels up and score increased by 70
12: If user enters wrong word , this game gives another chance
13: If user enters correct word , levels up and score increased by 40
14: If user enters wrong word , the game ends here

Code:

#include<iostream>
#include<fstream>
using namespace std;
int level=1,score=0;
int i=0,cal=0,c=0;
ifstream in,inc;
char ch[50],input[50],chc[50];
void enterguess();
int main()
{
       int firstcase=0;
       in.open("scramble word.txt");
       inc.open("correct.txt");

       while(!in.eof())
       {      system("CLS");
             
       if(level%2==0)
       {system("color a");}
       else {system("color 4");}
              cout<<"\t\tLevel : "<<level<<"\t\tScore : "<<score<<endl;
              i=0;cal=0;c=0;
              //Read scrambled word
              for(;i<50;i++)
              {in>>ch[i];
              if(ch[i]==','){break;}
              cout<<ch[i];
              }
              // Read corret word
              for(;c<50;c++)
              {inc>>chc[c];
              if(chc[c]==','){break;}
              }
              enterguess();
              if(firstcase==0)
              {i=i-1; firstcase=1;}
              if(cal==i)
              {cout<<"You entered right"<<endl; score=score+100;system("pause");}
              else
              {      cout<<"wrong! Try again"<<endl;
                           enterguess();
                           if(cal==i)
                           {cout<<"You entered right"<<endl;score=score+70;system("pause");}
                           else
                                  {      cout<<"wrong! Try again"<<endl;
                                                enterguess();
                                                if(cal==i)
                                                       {cout<<"You entered right"<<endl;score=score+40;system("pause");}
                                                else {cout<<"Game is over! And your score is : "<<score<<endl;break;}

                                 
                                  }
              }
              if(level==100)
       {cout<<"You won the game and your score is"<<score<<endl;break;}
              level=level+1;
             
       }

       in.close();
       inc.close();
       system("pause");
       return 0;
}
void enterguess()
{      cal=0;
       cout<<endl<<"Enter your guess";
              for(int a=0;a<i;a++)
              {cin>>input[a];}
              cin.ignore();
              //comparison
              for(int a=0;a<i;a++)
              {if(chc[a]==input[a])
                     {cal=cal+1;}
              }
      
}



Output:




Comments

  1. Bahi is code ma khuch missing ha output ma koi scramble word show hi nhi hota ha plz bata da kiu plzzz help chaya ap ki

    ReplyDelete
    Replies
    1. You need the following two text files for this program to run.. "correct.txt", "scramble word.txt" . You can create these files for your own. Just put correct words in "correct.txt" and scramble those words and put in "scramble word.txt". Put these two files in the same directory as of your program.

      Delete

Post a Comment