Hi everyone!
I am a class XII student. I have been asked to make a project using c++. I have decided to make a word game. The game's objective is:
* show a random jumbled word stored in a file
* ask the user to enter any valid words that comprise only of the letters available in that word
* no repetitions allowed
* score should be incremented by 10 times the length of the string
The file I used are: data.txt, words.txt, score.txt.
data.txt: for inputting the words
words.txt: list of 58,000 words
score.txt: to save the scores
This is my source code:
// *************************** C++ project *********************
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
struct string
{
char a[20];
};
struct points
{
char name[6];
int num;
};
void start();
void help();
void high(int =0);
void menu();
void main()
{
clrscr();
menu();
getch();
}
void start()
{
// variables: n-> arbitrary variable, k-> running variable
int score=0, times=0, n=0, k=0;
char jum[20], temp[20];
string read[10];
// generate random word from data.txt
randomize();
n = random(100);
ifstream file("data.txt");
for(int i=0; i<n; ++i)
{
file>>jum;
}
do
{
clrscr();
int check1=0, check2=1, check3=0;
cout<<"\n\n\t LeTs pLaY ThE gAmE... :D ";
cout<<"\n\t Score: "<<score<<"\t\t Chances Left: "<<10-times;
cout<<"\n\n\t Your Word is: "<<jum;
cout<<"\n\t Enter text: ";
cin>>read[k].a;
cout<<"\n initiating check(s)... ";
// check(1) if all the letters are available in the word
for(int q=0; q<strlen(read[k].a); ++q)
{
check1=0;
for(int p=0; p<strlen(jum); ++p)
{
if( read[k].a[q] == jum[p])
{ check1=1; }
}
if(check1 == 0)
{
cout<<"\n\n\t all char(s) in text aren't available!!! ";
break;
}
}
cout<<"\n Check1 completed... ";
cout<<"\n Check1= "<<check1;
// check(2) if it has been entered already
for(int r=0; r<=k; ++r)
{
if(strcmp(read[k].a, read[r].a)==0)
{
check2=0;
break;
}
}
cout<<"\n Check2 completed... ";
cout<<"\n Check2= "<<check2;
// check(3) if the word exists
ifstream word("word.txt");
while(word)
{
word>>temp;
if(strcmp(temp, read[k].a)==0)
{
check3=1;
break;
}
}
cout<<"\n Check3 completed... ";
cout<<"\n Check3= "<<check3;
// increment score if all check(s) are valid( i.e. ==1 )
cout<<"\n Check1 + Check2 + Check3 = "<<check1 + check2 + check3;
if(check1 + check2 + check3 == 3)
{
score = score + 10*strlen(read[k].a);
}
++k;
++times;
word.seekg(0);
word.close();
}// end of do loop
while(times<10);
cout<<"\n\n\t cOnGrAtS!!! yOu hAvE sEcUrEd "<<score<<" pOiNtS... ";
file.close();
high(score);
}
void high(int score)
{
clrscr();
points sco1,sco2;
fstream ifile("score.txt", ios::in);
ofstream ofile("temp.txt", ios::out);
char name[20];
int k=0;
if(score!=0)
{
cout<<"\n\n\t\t CONGRATS... YOU MADE IT TO THE HIGH SCORES... ";
cout<<"\n\n\t\t Enter Your Name: ";
cin>>sco2.name;
sco2.num=score;
while(ofile)
{
ifile.read((char *)&sco1, sizeof(sco1));
if(sco1.num>score)
{
ofile.write((char *)&sco1, sizeof(sco1));
}
if(sco1.num<sco2.num && k==0)
{
ofile.write((char *)&sco2, sizeof(sco2));
k++;
}
if(sco1.num<sco2.num )
{
ofile.write((char *)&sco1, sizeof(sco1));
}
}
}
ifile.close(); ofile.close();
remove("score.txt");
rename("temp.txt","score.txt");
ifstream file("score.txt", ios::in);
if(score==0)
{
cout<<"\n\n\t\t\t HIGH SCORES ";
cout<<"\n\t\t\t ==== ======";
cout<<"\n\n PLAYER";cout<<"\t\t\t SCORE";
cout<<"\n ------"; cout<<"\t\t\t -----";
while(file)
{
file.read((char *)&sco1, sizeof(sco1));
cout<<sco1.name<<"\t\t\t "<<sco1.num;
}
}
cout<<"\n\n\t Press a key to proceed to main menu... ";
getch();
menu();
}
void help()
{
clrscr();
cout<<"\n\n\t RULES OF PLAY ";
cout<<"\n\t ============= ";
cout<<"\n\n\t You will be given a 10 charachter word in its jumbled form. ";
cout<<"\n\t You must enter words of four charachters or above using the letters in the word";
cout<<"\n\t If you can guess the jumbled word correctly or manage to score more than 100 points, You win!!! ";
cout<<"\n\n\n\t Press any key to proceed to main menu... ";
getch();
menu();
}
void menu()
{
clrscr();
cout<<"\n\t\t SCRABBLE GAME IN C++";
cout<<"\n\t\t ======== ==== == ===";
cout<<"\n\n\t\t 1. Start"<<endl;
cout<<"\n\t\t 2. Help"<<endl;
cout<<"\n\t\t 3. High Scores"<<endl;
cout<<"\n\t\t 4. Exit"<<endl;
cout<<"\n\t\t Enter Choice: ";
int ch;
cin>>ch;
switch(ch)
{
case 1: start();
break;
case 2: help();
break;
case 3: high(0);
break;
case 4: exit(0);
break;
default: clrscr(); menu(); cout<<"\n\t\t Wrong Choice!!! ";
}
}
The compiler shows no errors at all! But something is not right with my source code. I request you all to kindly go through it patiently and help me debug it. I experience the following problems:
1) When I Run start(); I don't get all the outputs, like check1, etc.
2) score is always zero
3) the high score displayed after start() is not the same as that viewed directly from menu()
Please help me out!!!