Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- class StateGame
- {
- private:
- string state;
- string capital;
- string symbol;
- string pop;
- string size;
- string zone;
- string nick;
- int score;
- string bor;
- ofstream myfile;
- string s[51][8];
- public:
- StateGame();
- void readLine();
- string getState(int);
- string getSymbol(int);
- string getCapital(int);
- string getPop(int);
- string getSize(int);
- int getScore();
- string getNick(int);
- string getZone(int);
- string getborState(int);
- void updateScore();
- int check(int);
- int check1(int,int);
- string removeT(string);
- };
- StateGame :: StateGame()
- {
- score=100;
- string State1;
- string Symbol1;
- string Capital1;
- string Pop1;
- string Size1;
- string Zone1;
- string Nickname1;
- string BorderingStates1;
- int i=0;
- string line;
- ifstream myfile ("input.txt");
- if (myfile.is_open())
- {
- for (int lineno = 0; getline (myfile,line); lineno++)
- {
- int j=0;
- State1 = line.substr(0,15);
- Symbol1 = line.substr(15,7);
- Capital1 = line.substr(23,15);
- Pop1 = line.substr(38,5);
- Size1 = line.substr(42,7);
- Zone1 = line.substr(49,9);
- Nickname1 = line.substr(58,25);
- BorderingStates1 = line.substr(83,40);
- s[i][j] = State1;
- j++;
- s[i][j] = Symbol1;
- j++;
- s[i][j] = Capital1;
- j++;
- s[i][j] = Pop1;
- j++;
- s[i][j] = Size1;
- j++;
- s[i][j] = Zone1;
- j++;
- s[i][j] = Nickname1;
- j++;
- s[i][j] = BorderingStates1;
- j++;
- i++;
- }
- myfile.close();
- }
- }
- string StateGame :: removeT(string n)
- {
- int t=0,i=0;
- for(i=0;n[i]!='\0';i++)
- {
- if(n[i]==' ' && n[i+1]==' ' && n[i+2]==' ')
- {
- t=i;
- break;
- }
- }
- return n.substr(0,t);
- }
- string StateGame :: getState(int num2)
- {
- state=s[num2][0];
- return state;
- }
- string StateGame :: getCapital(int num2)
- {
- capital=s[num2][2];
- return capital;
- }
- string StateGame :: getSymbol(int num2)
- {
- symbol=s[num2][1];
- return symbol;
- }
- string StateGame :: getZone(int num2)
- {
- zone=s[num2][5];
- return zone;
- }
- string StateGame :: getborState(int num2)
- {
- bor=s[num2][7];
- return bor;
- }
- string StateGame :: getNick(int num2)
- {
- nick=s[num2][6];
- return nick;
- }
- string StateGame :: getSize(int num2)
- {
- size=s[num2][4];
- return size;
- }
- string StateGame :: getPop(int num2)
- {
- pop=s[num2][3];
- return pop;
- }
- int StateGame :: getScore()
- {
- return score;
- }
- void StateGame :: updateScore()
- {
- score=score-5;
- }
- int StateGame :: check(int num2)
- {
- char fs,ls,lc,fc;
- int cnt=0;
- int i=1,j=0,g=0;
- string guess;
- string st,ca,sy;
- st=removeT(getState(num2));
- sy=removeT(getSymbol(num2));
- ca=removeT(getCapital(num2));
- vector<int> ar(9);
- for(int i=0;i<9;i++)
- {
- ar[i]=i+1;
- }
- random_shuffle(ar.begin(),ar.end());
- fs=st.at(0);
- fc=ca.at(0);
- ls=st.at(st.length()-1);
- lc=ca.at(ca.length()-1);
- do
- {
- cout<<"Clue#"<<i;
- switch(ar[j++])
- {
- case 1:
- cout<<"the first letter of the state is "<<fs<<"\n";
- break;
- case 2:
- cout<<"the last letter of the state is "<<ls<<"\n";
- break;
- case 3:
- cout<<"the first letter of the capital is "<<fc<<"\n";
- break;
- case 4:
- cout<<"the last letter of the capital is "<<lc<<"\n";
- break;
- case 5:
- cout<<"The state's rank in population, where 1 is the largest, is "<<getPop(num2)<<"\n";
- break;
- case 6:
- cout<<"The state's rank in size (or area), where 1 is the largest, is "<<getSize(num2)<<"\n";
- break;
- case 7:
- cout<<"The state is in the following time zone(s) "<<getZone(num2)<<"\n";
- break;
- case 8:
- cout<<"The state's nickname is "<<getNick(num2)<<"\n";
- break;
- case 9:
- cout<<"The state is bordered by the following states: "<<getborState(num2)<<"\n";
- break;
- }
- cout<<"Current Score "<<getScore()<<"\n";
- cout<<"Would you like to guess the state (1) or do you want another clue (2)? "<<"\n";
- cin>>g;
- if(g==2)
- updateScore();
- else if(g==1)
- {
- cout<<"Enter your guess name or symbol"<<"\n";
- cin>>guess;
- if((st.compare(guess)==0) || (sy.compare(guess)==0))
- {
- cnt=1;
- cout<<"Correct! Congratulations! Your final score "<<getScore()<<"\n";
- }
- else
- {
- updateScore();
- cout<<"Incorrect. Current Score: "<<getScore()<<"\n";
- }
- }
- i++;
- if(i>9)
- j=0;
- }while(cnt==0 && score!=0);
- if(cnt==1)
- {
- myfile.open ("output.txt",ios :: app);
- myfile << getState(num2)<<"\t"<<getScore()<<"\n";
- myfile.close();
- }
- return getScore();
- }
- int StateGame :: check1(int num2 ,int num1)
- {
- string n,n2,name;
- int score1=100;
- int score2=100;
- cout<<"Enter the name of player 1"<<"\n";
- cin>>n;
- cout<<"enter the name of player 2"<<"\n";
- cin>>n2;
- char fs,ls,lc,fc;
- vector<int> ar1(9);
- string st,ca,sy;
- st=removeT(getState(num2));
- sy=removeT(getSymbol(num2));
- ca=removeT(getCapital(num2));
- for(int i=0;i<9;i++)
- {
- ar1[i]=i+1;
- }
- random_shuffle(ar1.begin(),ar1.end());
- fs=st.at(0);
- fc=ca.at(0);
- ls=st.at(st.length()-1);
- lc=ca.at(ca.length()-1);
- int cnt=0;
- int i=1,j=0,g=0;
- string guess;
- do
- {
- if(num1==0)
- {
- if(i%2!=0)
- name=n;
- else
- name=n2;
- score=score1;
- }
- else
- {
- if(i%2!=0)
- name=n2;
- else
- name=n;
- score=score2;
- }
- cout<<"Clue#"<<i;
- switch(ar1[j++])
- {
- case 1:
- cout<<"for "<<name<<" is "<<"the first letter of the state is "<<fs<<"\n";
- break;
- case 2:
- cout<<"for "<<name<<" is "<<"the last letter of the state is "<<ls<<"\n";
- break;
- case 3:
- cout<<"for "<<name<<" is "<<"the first letter of the capital is "<<fc<<"\n";
- break;
- case 4:
- cout<<"for "<<name<<" is "<<"the last letter of the capital is "<<lc<<"\n";
- break;
- case 5:
- cout<<"for "<<name<<" is "<<"The state’s rank in population, where 1 is the largest, is "<<getPop(num2)<<"\n";
- break;
- case 6:
- cout<<"for "<<name<<" is "<<"The state’s rank in size (or area), where 1 is the largest, is "<<getSize(num2)<<"\n";
- break;
- case 7:
- cout<<"for "<<name<<" is "<<"The state is in the following time zone(s) "<<getZone(num2)<<"\n";
- break;
- case 8:
- cout<<"for "<<name<<" is "<<"The state’s nickname is "<<getNick(num2)<<"\n";
- break;
- case 9:
- cout<<"for "<<name<<" is "<<"The state is bordered by the following states: "<<getborState(num2)<<"\n";
- break;
- }
- cout<<"Current Score "<<getScore()<<"\n";
- cout<<"Would you like to guess the state (1) or do you want another clue (2)? "<<"\n";
- cin>>g;
- if(g==2)
- {
- if(i%2!=0)
- {
- score1=score1-5;
- score=score1;
- }
- else
- {
- score2=score2-5;
- score=score2;
- }
- }
- else if(g==1)
- {
- cout<<"Enter your guess name or symbol"<<"\n";
- cin>>guess;
- if((st.compare(guess)==0) || (sy.compare(guess)==0))
- {
- cnt=1;
- cout<<"Correct! Congratulations! Your final score "<<getScore()<<"\n";
- }
- else
- {
- if(i%2!=0)
- {
- score1=score1-5;
- score=score1;
- }
- else
- {
- score2=score2-5;
- score=score2;
- }
- cout<<"Incorrect. Current Score: "<<getScore()<<"\n";
- }
- }
- i++;
- if(i>9)
- j=0;
- }while(cnt==0 && score!=0);
- if(cnt==1)
- {
- myfile.open ("output.txt",ios :: app);
- myfile << getState(num2)<<"\t"<<getScore()<<"\n";
- myfile.close();
- }
- return getScore();
- }
- int main()
- {
- int c=0,cnt=0,t_score=0,tem;
- char ch,chr='Y';
- double av;
- int num1=rand()%2;
- StateGame ob;
- do{
- srand(time(0));
- int num2=rand()%50;
- cout<<"Welcome to the Name The State Game!"<<"\n";
- cout<<"would you like the rules of the game (Y or N)"<<"\n";
- cin>>ch;
- if(ch=='N')
- cout<<"Lets get started . a state has been randomly selected. here is your first clue"<<"\n";
- else
- cout<<"You have to guess the state name using the hints given.with every pass and wrong answer your score will be deducted by 5"<<"\n";
- cout<<"You want to play 1-player (1) or 2-player (2)"<<"\n";
- cin>>c;
- if(c==1)
- tem=ob.check(num2);
- else if(c==2)
- tem=ob.check1(num2,num1);
- cnt++;
- cout<<"Do you want to play again (Y | N)"<<"\n";
- cin>>chr;
- t_score=t_score+tem;
- }while(chr=='Y' && cnt < 5);
- ifstream in("output.txt");
- int cc=1;
- string line;
- if (in.is_open())
- {
- for (int lineno = 0; getline (in,line); lineno++)
- {
- cout<<line<<"\n";
- cc++;
- }
- in.close();
- }
- av=t_score/cc;
- cout<<"You have played "<<cc<<" gamess and average is "<<av<<"\n";
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment