Guest User

Untitled

a guest
Dec 14th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.00 KB | None | 0 0
  1. using namespace std;
  2.  
  3. class StateGame
  4. {
  5. private:
  6. string state;
  7. string capital;
  8. string symbol;
  9. string pop;
  10. string size;
  11. string zone;
  12. string nick;
  13. int score;
  14. string bor;
  15. ofstream myfile;
  16. string s[51][8];
  17. public:
  18. StateGame();
  19. void readLine();
  20. string getState(int);
  21. string getSymbol(int);
  22. string getCapital(int);
  23. string getPop(int);
  24. string getSize(int);
  25. int getScore();
  26. string getNick(int);
  27. string getZone(int);
  28. string getborState(int);
  29. void updateScore();
  30. int check(int);
  31. int check1(int,int);
  32. string removeT(string);
  33. };
  34. StateGame :: StateGame()
  35. {
  36. score=100;
  37. string State1;
  38. string Symbol1;
  39. string Capital1;
  40. string Pop1;
  41. string Size1;
  42. string Zone1;
  43. string Nickname1;
  44. string BorderingStates1;
  45.  
  46. int i=0;
  47. string line;
  48. ifstream myfile ("input.txt");
  49. if (myfile.is_open())
  50. {
  51. for (int lineno = 0; getline (myfile,line); lineno++)
  52. {
  53. int j=0;
  54. State1 = line.substr(0,15);
  55. Symbol1 = line.substr(15,7);
  56. Capital1 = line.substr(23,15);
  57. Pop1 = line.substr(38,5);
  58. Size1 = line.substr(42,7);
  59. Zone1 = line.substr(49,9);
  60. Nickname1 = line.substr(58,25);
  61. BorderingStates1 = line.substr(83,40);
  62.  
  63. s[i][j] = State1;
  64. j++;
  65. s[i][j] = Symbol1;
  66. j++;
  67. s[i][j] = Capital1;
  68. j++;
  69. s[i][j] = Pop1;
  70. j++;
  71. s[i][j] = Size1;
  72. j++;
  73. s[i][j] = Zone1;
  74. j++;
  75. s[i][j] = Nickname1;
  76. j++;
  77. s[i][j] = BorderingStates1;
  78. j++;
  79. i++;
  80. }
  81. myfile.close();
  82. }
  83.  
  84. }
  85. string StateGame :: removeT(string n)
  86. {
  87.  
  88. int t=0,i=0;
  89. for(i=0;n[i]!='\0';i++)
  90. {
  91. if(n[i]==' ' && n[i+1]==' ' && n[i+2]==' ')
  92. {
  93. t=i;
  94. break;
  95. }
  96. }
  97.  
  98. return n.substr(0,t);
  99. }
  100. string StateGame :: getState(int num2)
  101. {
  102. state=s[num2][0];
  103. return state;
  104. }
  105.  
  106. string StateGame :: getCapital(int num2)
  107. {
  108. capital=s[num2][2];
  109. return capital;
  110. }
  111. string StateGame :: getSymbol(int num2)
  112. {
  113. symbol=s[num2][1];
  114.  
  115. return symbol;
  116. }
  117. string StateGame :: getZone(int num2)
  118. {
  119. zone=s[num2][5];
  120.  
  121. return zone;
  122. }
  123. string StateGame :: getborState(int num2)
  124. {
  125.  
  126. bor=s[num2][7];
  127. return bor;
  128. }
  129. string StateGame :: getNick(int num2)
  130. {
  131.  
  132. nick=s[num2][6];
  133. return nick;
  134. }
  135. string StateGame :: getSize(int num2)
  136. {
  137.  
  138. size=s[num2][4];
  139. return size;
  140. }
  141. string StateGame :: getPop(int num2)
  142. {
  143. pop=s[num2][3];
  144. return pop;
  145. }
  146. int StateGame :: getScore()
  147. {
  148. return score;
  149. }
  150. void StateGame :: updateScore()
  151. {
  152. score=score-5;
  153. }
  154. int StateGame :: check(int num2)
  155. {
  156.  
  157. char fs,ls,lc,fc;
  158. int cnt=0;
  159. int i=1,j=0,g=0;
  160. string guess;
  161. string st,ca,sy;
  162.  
  163. st=removeT(getState(num2));
  164. sy=removeT(getSymbol(num2));
  165. ca=removeT(getCapital(num2));
  166. vector<int> ar(9);
  167. for(int i=0;i<9;i++)
  168. {
  169. ar[i]=i+1;
  170. }
  171. random_shuffle(ar.begin(),ar.end());
  172. fs=st.at(0);
  173. fc=ca.at(0);
  174. ls=st.at(st.length()-1);
  175. lc=ca.at(ca.length()-1);
  176.  
  177.  
  178. do
  179. {
  180.  
  181. cout<<"Clue#"<<i;
  182. switch(ar[j++])
  183. {
  184. case 1:
  185.  
  186. cout<<"the first letter of the state is "<<fs<<"\n";
  187. break;
  188. case 2:
  189. cout<<"the last letter of the state is "<<ls<<"\n";
  190. break;
  191. case 3:
  192. cout<<"the first letter of the capital is "<<fc<<"\n";
  193. break;
  194. case 4:
  195. cout<<"the last letter of the capital is "<<lc<<"\n";
  196. break;
  197. case 5:
  198. cout<<"The state's rank in population, where 1 is the largest, is "<<getPop(num2)<<"\n";
  199. break;
  200. case 6:
  201. cout<<"The state's rank in size (or area), where 1 is the largest, is "<<getSize(num2)<<"\n";
  202. break;
  203. case 7:
  204. cout<<"The state is in the following time zone(s) "<<getZone(num2)<<"\n";
  205. break;
  206. case 8:
  207. cout<<"The state's nickname is "<<getNick(num2)<<"\n";
  208. break;
  209. case 9:
  210. cout<<"The state is bordered by the following states: "<<getborState(num2)<<"\n";
  211. break;
  212. }
  213. cout<<"Current Score "<<getScore()<<"\n";
  214. cout<<"Would you like to guess the state (1) or do you want another clue (2)? "<<"\n";
  215. cin>>g;
  216. if(g==2)
  217. updateScore();
  218. else if(g==1)
  219. {
  220. cout<<"Enter your guess name or symbol"<<"\n";
  221. cin>>guess;
  222. if((st.compare(guess)==0) || (sy.compare(guess)==0))
  223. {
  224. cnt=1;
  225. cout<<"Correct! Congratulations! Your final score "<<getScore()<<"\n";
  226. }
  227. else
  228. {
  229. updateScore();
  230. cout<<"Incorrect. Current Score: "<<getScore()<<"\n";
  231. }
  232. }
  233. i++;
  234. if(i>9)
  235. j=0;
  236. }while(cnt==0 && score!=0);
  237. if(cnt==1)
  238. {
  239.  
  240. myfile.open ("output.txt",ios :: app);
  241. myfile << getState(num2)<<"\t"<<getScore()<<"\n";
  242. myfile.close();
  243.  
  244. }
  245. return getScore();
  246. }
  247. int StateGame :: check1(int num2 ,int num1)
  248. {
  249. string n,n2,name;
  250. int score1=100;
  251. int score2=100;
  252. cout<<"Enter the name of player 1"<<"\n";
  253. cin>>n;
  254. cout<<"enter the name of player 2"<<"\n";
  255. cin>>n2;
  256.  
  257. char fs,ls,lc,fc;
  258. vector<int> ar1(9);
  259. string st,ca,sy;
  260.  
  261. st=removeT(getState(num2));
  262. sy=removeT(getSymbol(num2));
  263. ca=removeT(getCapital(num2));
  264. for(int i=0;i<9;i++)
  265. {
  266. ar1[i]=i+1;
  267. }
  268. random_shuffle(ar1.begin(),ar1.end());
  269. fs=st.at(0);
  270. fc=ca.at(0);
  271. ls=st.at(st.length()-1);
  272. lc=ca.at(ca.length()-1);
  273.  
  274. int cnt=0;
  275. int i=1,j=0,g=0;
  276. string guess;
  277. do
  278. {
  279. if(num1==0)
  280. {
  281. if(i%2!=0)
  282. name=n;
  283. else
  284. name=n2;
  285. score=score1;
  286. }
  287. else
  288. {
  289. if(i%2!=0)
  290. name=n2;
  291. else
  292. name=n;
  293. score=score2;
  294.  
  295. }
  296. cout<<"Clue#"<<i;
  297. switch(ar1[j++])
  298. {
  299. case 1:
  300.  
  301. cout<<"for "<<name<<" is "<<"the first letter of the state is "<<fs<<"\n";
  302.  
  303. break;
  304.  
  305. case 2:
  306. cout<<"for "<<name<<" is "<<"the last letter of the state is "<<ls<<"\n";
  307. break;
  308. case 3:
  309. cout<<"for "<<name<<" is "<<"the first letter of the capital is "<<fc<<"\n";
  310. break;
  311. case 4:
  312. cout<<"for "<<name<<" is "<<"the last letter of the capital is "<<lc<<"\n";
  313. break;
  314. case 5:
  315. cout<<"for "<<name<<" is "<<"The state’s rank in population, where 1 is the largest, is "<<getPop(num2)<<"\n";
  316. break;
  317. case 6:
  318. cout<<"for "<<name<<" is "<<"The state’s rank in size (or area), where 1 is the largest, is "<<getSize(num2)<<"\n";
  319. break;
  320. case 7:
  321. cout<<"for "<<name<<" is "<<"The state is in the following time zone(s) "<<getZone(num2)<<"\n";
  322. break;
  323. case 8:
  324. cout<<"for "<<name<<" is "<<"The state’s nickname is "<<getNick(num2)<<"\n";
  325. break;
  326. case 9:
  327. cout<<"for "<<name<<" is "<<"The state is bordered by the following states: "<<getborState(num2)<<"\n";
  328. break;
  329. }
  330. cout<<"Current Score "<<getScore()<<"\n";
  331. cout<<"Would you like to guess the state (1) or do you want another clue (2)? "<<"\n";
  332. cin>>g;
  333. if(g==2)
  334. {
  335. if(i%2!=0)
  336. {
  337.  
  338. score1=score1-5;
  339. score=score1;
  340. }
  341. else
  342. {
  343.  
  344. score2=score2-5;
  345. score=score2;
  346. }
  347. }
  348. else if(g==1)
  349. {
  350. cout<<"Enter your guess name or symbol"<<"\n";
  351. cin>>guess;
  352. if((st.compare(guess)==0) || (sy.compare(guess)==0))
  353. {
  354. cnt=1;
  355. cout<<"Correct! Congratulations! Your final score "<<getScore()<<"\n";
  356. }
  357. else
  358. {
  359. if(i%2!=0)
  360. {
  361.  
  362. score1=score1-5;
  363. score=score1;
  364. }
  365. else
  366. {
  367.  
  368. score2=score2-5;
  369. score=score2;
  370. }
  371. cout<<"Incorrect. Current Score: "<<getScore()<<"\n";
  372. }
  373. }
  374. i++;
  375. if(i>9)
  376. j=0;
  377. }while(cnt==0 && score!=0);
  378. if(cnt==1)
  379. {
  380.  
  381. myfile.open ("output.txt",ios :: app);
  382. myfile << getState(num2)<<"\t"<<getScore()<<"\n";
  383. myfile.close();
  384.  
  385. }
  386. return getScore();
  387. }
  388. int main()
  389. {
  390. int c=0,cnt=0,t_score=0,tem;
  391. char ch,chr='Y';
  392. double av;
  393.  
  394. int num1=rand()%2;
  395. StateGame ob;
  396.  
  397. do{
  398. srand(time(0));
  399. int num2=rand()%50;
  400.  
  401. cout<<"Welcome to the Name The State Game!"<<"\n";
  402. cout<<"would you like the rules of the game (Y or N)"<<"\n";
  403. cin>>ch;
  404. if(ch=='N')
  405. cout<<"Lets get started . a state has been randomly selected. here is your first clue"<<"\n";
  406. else
  407. 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";
  408.  
  409. cout<<"You want to play 1-player (1) or 2-player (2)"<<"\n";
  410. cin>>c;
  411. if(c==1)
  412. tem=ob.check(num2);
  413. else if(c==2)
  414. tem=ob.check1(num2,num1);
  415. cnt++;
  416. cout<<"Do you want to play again (Y | N)"<<"\n";
  417. cin>>chr;
  418. t_score=t_score+tem;
  419. }while(chr=='Y' && cnt < 5);
  420. ifstream in("output.txt");
  421. int cc=1;
  422. string line;
  423. if (in.is_open())
  424. {
  425. for (int lineno = 0; getline (in,line); lineno++)
  426. {
  427. cout<<line<<"\n";
  428. cc++;
  429. }
  430. in.close();
  431. }
  432. av=t_score/cc;
  433. cout<<"You have played "<<cc<<" gamess and average is "<<av<<"\n";
  434.  
  435.  
  436.  
  437. getch();
  438. return 0;
  439. }
Advertisement
Add Comment
Please, Sign In to add comment