Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. //forward declarion of functions and classes
  11. bool inputChecking(int value, int max, int min, string message);
  12. bool quit();
  13. void printInstruction();
  14. string strInputHandler(bool allowQuit);
  15. int intInputHandler(bool allowQuit);
  16. class board;
  17. class block;
  18. class controller;
  19.  
  20. class block
  21. {
  22. //a class that represet puzzle of the game
  23. private:
  24. int blockScore[19]={10,20,30,40,50,20,30,40,50,30,30,30,30,50,50,50,50,40,90};
  25. int blockIndex[3];
  26. public:
  27. // get function to allow access to private data members outside the class
  28. int getBlockScore(int n)
  29. {
  30. return blockScore[n];
  31. }
  32. int getBlockIndex(int n)
  33. {
  34. return blockIndex[n];
  35. }
  36. void generateBlocks(int i)
  37. {
  38. blockIndex[i] =rand()%19;
  39. }
  40. void printBlocks()
  41. {
  42. //a function to printBlocks under the game board and let the user choose which board to place next
  43. cout <<"Index: ";
  44. cout << "0 1 2" <<endl;
  45. cout << "Shape: ";
  46. //print the puzzle line by line for 5 times because the max heigh of blocks is 5
  47. for (int i =0; i<3; i++) printBlocksLineOne(blockIndex[i]); cout<<endl<<" ";
  48. for (int i =0; i<3; i++) printBlocksLineTwo(blockIndex[i]); cout<<endl<<" ";
  49. for (int i =0; i<3; i++) printBlocksLineThree(blockIndex[i]); cout<<endl<<" ";
  50. for (int i =0; i<3; i++) printBlocksLineFour(blockIndex[i]); cout<<endl<<" ";
  51. for (int i =0; i<3; i++) printBlocksLineFive(blockIndex[i]); cout<<endl<<" ";
  52. cout << endl;
  53. //Show the corrsponding score of the blocks
  54. cout <<"Score: ";
  55. for (int i=0;i<3;i++) cout <<blockScore[blockIndex[i]]<<" ";
  56. cout << endl;
  57. }
  58.  
  59. void printBlocksLineOne(int a)
  60. {
  61. //printing lint 1 of the block according the blocks shape
  62. if(a!=10 && a!=14)cout <<"X";
  63. else cout <<" ";
  64. cout << " ";
  65. if(a!=0&& a!=5&&a!=6&&a!=7&&a!=8&&a!=9&&a!=13&&a!=14) cout <<"X";
  66. else cout <<" ";
  67. cout <<" ";
  68. if( a==2|| a==3||a==4||a==18||a==14||a==15||a==16) cout <<"X";
  69. else cout <<" ";
  70. cout << " ";
  71. if(a==3||a==4) cout << "X";
  72. else cout <<" ";
  73. cout <<" ";
  74. if (a==4) cout <<"X";
  75. else cout <<" ";
  76. cout <<" ";
  77. }
  78. void printBlocksLineTwo(int a)
  79. {
  80. //printing lint 2 of the block according the blocks shape
  81. if(a!=0 && a!=1 &&a!=2 &&a!=3 &&a!=4 &&a!=12 &&a!=14 &&a!=16)cout <<"X";
  82. else cout <<" ";
  83. cout << " ";
  84. if(a==9||a==10||a==12||a==17||a==18) cout <<"X";
  85. else cout <<" ";
  86. cout <<" ";
  87. if(a==18||a==16||a==14) cout <<"X";
  88. else cout <<" ";
  89. cout << " ";
  90. }
  91. void printBlocksLineThree(int a)
  92. {
  93. //printing lint 3 of the block according the blocks shape
  94. if(a==18||a==15||a==14||a==13||a==8||a==7||a==6)cout <<"X";
  95. else cout <<" ";
  96. cout << " ";
  97. if(a==18||a==14||a==13) cout <<"X";
  98. else cout <<" ";
  99. cout <<" ";
  100. if( a==18||a==16||a==14||a==13) cout <<"X";
  101. else cout <<" ";
  102. cout << " ";
  103. }
  104. void printBlocksLineFour(int a)
  105. {
  106. //printing lint 4 of the block according the blocks shape
  107. if(a==7||a==8) cout <<"X";
  108. else cout <<" ";
  109. cout << " ";
  110. }
  111. void printBlocksLineFive(int a)
  112. {
  113. //printing lint 4 of the block according the blocks shape
  114. if(a==8) cout <<"X";
  115. else cout <<" ";
  116. cout << " ";
  117. }
  118. };
  119.  
  120. class board
  121. {
  122. private:
  123. int boardSize;
  124. char boards[10][10];
  125. int compleRow[10];
  126. int compleCol[10];
  127. int compleRowSize;
  128. int compleColSize;
  129. int score;
  130. int blockChoice;
  131. int y;
  132. int x;
  133. block* b;
  134. int demoMode;
  135. int gameMode;
  136. controller* g;
  137. char bombTimer;
  138. int round;
  139. public:
  140. //constructor
  141. board(controller* gm)
  142. {
  143. round=0;
  144. bombTimer='9';
  145. boardSize=10;
  146. clearAll();
  147. gameMode =1;
  148. demoMode=1;
  149. compleRowSize=0; compleColSize=0;
  150. score =0;
  151. b = new block();
  152. g=gm;
  153. }
  154. //a group of get and set function for accessing private data member
  155. char getBombTimer()
  156. {
  157. return bombTimer;
  158. }
  159. void setBombTimer(char c)
  160. {
  161. bombTimer=c;
  162. }
  163.  
  164. void setRound(int i)
  165. {
  166. round=i;
  167. }
  168. void setScore(int s)
  169. {
  170. score=s;
  171. }
  172. int getBoardSize()
  173. {
  174. return boardSize;
  175. }
  176. int getDemoMode()
  177. {
  178. return demoMode;
  179. }
  180. int getGameMode()
  181. {
  182. return gameMode;
  183. }
  184. void setDemoMode(int i)
  185. {
  186. demoMode=i;
  187. }
  188. void setGameMode(int i)
  189. {
  190. gameMode=i;
  191. }
  192. int getScore()
  193. {
  194. return score;
  195. }
  196. void setBoardSize(int s)
  197. {
  198. boardSize=s;
  199. }
  200.  
  201. void endGame()
  202. {
  203. //display the final score when the game ends
  204. cout << "The game ends because u fail to put block on the board" << endl;
  205. cout << "Your total score is: "<< getScore() << endl <<endl;
  206. return;
  207. }
  208. void placingBlock()
  209. {
  210. //putting the blocks into the boards
  211. score += b->getBlockScore(blockChoice);
  212. switch (blockChoice) {
  213. case 0: boards[x][y]='X'; break;
  214. case 1: boards[x][y]='X';boards[x][y+1]='X'; break;
  215. case 2: boards[x][y]='X'; boards[x][y+1]='X'; boards[x][y+2]='X'; break;
  216. case 3: boards[x][y]='X';boards[x][y+1]='X'; boards[x][y+2]='X'; boards[x][y+3]='X'; break;
  217. case 4: boards[x][y]='X';boards[x][y+1]='X'; boards[x][y+2]='X'; boards[x][y+3]='X';boards[x][y+4]='X'; break;
  218. case 5: boards[x][y]='X'; boards[x+1][y]='X';break;
  219. case 6: boards[x][y]='X'; boards[x+1][y]='X';boards[x+2][y]='X';break;
  220. case 7: boards[x][y]='X'; boards[x+1][y]='X';boards[x+2][y]='X';boards[x+3][y]='X';break;
  221. case 8: boards[x][y]='X'; boards[x+1][y]='X';boards[x+2][y]='X';boards[x+3][y]='X';boards[x+4][y]='X';break;
  222. case 9: boards[x][y]='X';boards[x+1][y]='X';boards[x+1][y+1]='X' ;break;
  223. case 10: boards[x][y]='X';boards[x][y+1]='X';boards[x-1][y+1]='X' ;break;
  224. case 11: boards[x][y]='X'; boards[x+1][y]='X';boards[x][y+1]='X' ;break;
  225. case 12: boards[x][y]='X';boards[x][y+1]='X';boards[x+1][y+1]='X' ;break;
  226. case 13: boards[x][y]='X'; boards[x+1][y]='X';boards[x+2][y]='X';boards[x+2][y+1]='X';boards[x+2][y+2]='X' ;break;
  227. case 14: boards[x][y]='X'; boards[x][y+1]='X'; boards[x][y+2]='X';boards[x-1][y+2]='X';boards[x-2][y+2]='X' ;break;
  228. case 15: boards[x][y]='X'; boards[x+1][y]='X';boards[x+2][y]='X';boards[x][y+1]='X';boards[x][y+2]='X' ;break;
  229. case 16: boards[x][y]='X'; boards[x][y+1]='X'; boards[x][y+2]='X';boards[x+1][y+2]='X';boards[x+2][y+2]='X' ;break;
  230. case 17: boards[x][y]='X';boards[x+1][y+1]='X';boards[x+1][y]='X';boards[x][y+1]='X' ;break;
  231. case 18: boards[x][y]='X';boards[x+1][y+1]='X';boards[x+1][y]='X';boards[x][y+1]='X';boards[x][y+2]='X';boards[x+1][y+2]='X';boards[x+2][y+2]='X';boards[x+2][y+1]='X';boards[x+2][y]='X' ;break;
  232. }
  233. }
  234.  
  235. bool placingBlockChecker()
  236. {
  237. //a function to check if the blocks can be put in a certain location
  238. // means the board need to be empty
  239. bool placeEmpty=true;
  240. switch (blockChoice) {
  241. case 0: if(boards[x][y]!=' ')placeEmpty=false; break;
  242. case 1: if(boards[x][y]!=' '||boards[x][y+1]!=' '||y>(boardSize-2))placeEmpty=false; break;
  243. case 2: if(boards[x][y]!=' '|| boards[x][y+1]!=' '|| boards[x][y+2]!=' '||y>(boardSize-3))placeEmpty=false; break;
  244. case 3: if(boards[x][y]!=' '||boards[x][y+1]!=' '|| boards[x][y+2]!=' '|| boards[x][y+3]!=' '||y>(boardSize-4))placeEmpty=false; break;
  245. case 4: if(boards[x][y]!=' '||boards[x][y+1]!=' '|| boards[x][y+2]!=' '|| boards[x][y+3]!=' '||boards[x][y+4]!=' '||y>(boardSize-5))placeEmpty=false; break;
  246. case 5: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||x>(boardSize-2))placeEmpty=false;break;
  247. case 6: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||boards[x+2][y]!=' '||x>(boardSize-3))placeEmpty=false;break;
  248. case 7: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||boards[x+2][y]!=' '||x>(boardSize-4)||boards[x+3][y]!=' ')placeEmpty=false;break;
  249. case 8: if(boards[x][y]!=' '|| x>(boardSize-5)||boards[x+1][y]!=' '||boards[x+2][y]!=' '||boards[x+3][y]!=' '||boards[x+4][y]!=' ')placeEmpty=false;break;
  250. case 9: if(boards[x][y]!=' '||boards[x+1][y]!=' '||x>(boardSize-2)||y>(boardSize-2)||boards[x+1][y+1]!=' ') placeEmpty=false;break;
  251. case 10: if(boards[x][y]!=' '||boards[x][y+1]!=' '||y>(boardSize-2)||x<1||boards[x-1][y+1]!=' ') placeEmpty=false;break;
  252. case 11: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||x>(boardSize-2)||y>(boardSize-2)||boards[x][y+1]!=' ') placeEmpty=false;break;
  253. case 12: if(boards[x][y]!=' '||boards[x][y+1]!=' '||x>(boardSize-2)||y>(boardSize-2)||boards[x+1][y+1]!=' ') placeEmpty=false;break;
  254. case 13: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||x>(boardSize-3)||y>(boardSize-3)||boards[x+2][y]!=' '||boards[x+2][y+1]!=' '||boards[x+2][y+2]!=' ') placeEmpty=false;break;
  255. case 14: if(boards[x][y]!=' '|| boards[x][y+1]!=' '||y>(boardSize-3)||x<2|| boards[x][y+2]!=' '||boards[x-1][y+2]!=' '||boards[x-2][y+2]!=' ') placeEmpty=false;break;
  256. case 15: if(boards[x][y]!=' '|| boards[x+1][y]!=' '||x>(boardSize-3)||y>(boardSize-3)||boards[x+2][y]!=' '||boards[x][y+1]!=' '||boards[x][y+2]!=' ') placeEmpty=false;break;
  257. case 16: if(boards[x][y]!=' '|| boards[x][y+1]!=' '|| x>(boardSize-3)||y>(boardSize-3)||boards[x][y+2]!=' '||boards[x+1][y+2]!=' '||boards[x+2][y+2]!=' ') placeEmpty=false;break;
  258. case 17: if(boards[x][y]!=' '||boards[x+1][y+1]!=' '||x>(boardSize-2)||y>(boardSize-2)||boards[x+1][y]!=' '||boards[x][y+1]!=' ') placeEmpty=false;break;
  259. case 18: if(boards[x][y]!=' '||boards[x+1][y+1]!=' '||x>(boardSize-3)||y>(boardSize-3)||boards[x+1][y]!=' '||boards[x][y+1]!=' '||boards[x][y+2]!=' '||boards[x+1][y+2]!=' '||boards[x+2][y+2]!=' '||boards[x+2][y+1]!=' '||boards[x+2][y]!=' ') placeEmpty=false;break;
  260. default:
  261. break;
  262. }
  263. return placeEmpty;
  264. }
  265.  
  266. bool checkBoardEmpty()
  267. {
  268. for(int i =0;i<boardSize;i++)
  269. {
  270. for(int j=0; j<boardSize;j++)
  271. {
  272. if(boards[i][j]!=' ') return false;
  273. }
  274. }
  275. return true;
  276. }
  277.  
  278. void generateBomb()
  279. {
  280. //select a random location to make a timer bomb there
  281. //if the board is empty it will be place a bomb
  282. if(checkBoardEmpty()) return;
  283. srand(time(NULL));
  284. int b_x,b_y;
  285. do{
  286. b_x= rand()%boardSize;
  287. b_y= rand()%boardSize;
  288. }while (boards[b_x][b_y]!='X');
  289. boards[b_x][b_y]=bombTimer;
  290. }
  291.  
  292. bool bombChecker()
  293. {
  294. //decrement the count down of the timer bomb and
  295. //check for bomb is time up
  296. for(int i =0;i<boardSize;i++)
  297. {
  298. for(int j=0;j<boardSize;j++)
  299. {
  300. if(boards[i][j]>'0'&& boards[i][j]<='9')
  301. {
  302. boards[i][j]=boards[i][j]-1;
  303. if(boards[i][j]=='0')
  304. return true;
  305. }
  306. }
  307. }
  308. return false;
  309. }
  310.  
  311. void bombExplo()
  312. {
  313. //a board explosion effect
  314. for (int i =0; i<boardSize; i++) {
  315. for(int j=0;j<boardSize;j++)
  316. boards[i][j]='*';
  317.  
  318. }
  319. }
  320.  
  321. void gameStart(int demo, int mode)
  322. {
  323. //the main game flow function with the first parameter as PC demo mode (1 for user playing , 2 for pc demo)
  324. //second parameter as game mode (1 for original, 2 for timer bomb)
  325. //first try to generate a bomb if the game in timer bomb mode
  326. if(mode==2)
  327. {
  328. if(round%5==0&&round!=0)
  329. generateBomb();
  330. }
  331. string x;
  332. string loc;
  333. string temp;
  334. char maxCol=char(boardSize+65-1);
  335. displayBoard(); //print the board
  336. srand(time(NULL));
  337. if(score==0) //generate three blocks
  338. for(int i =0;i<3;i++)
  339. b->generateBlocks(i);
  340. b->printBlocks();
  341. int index;
  342. if(demo==1) //in User playing mode
  343. {
  344. do{ //ask user to choose the blocks by index
  345. cout <<"Please input block choice by typing index number (0-2): ";
  346. index =intInputHandler(1);
  347. if(index==-1) return;
  348. }while(inputChecking(index, 2, 0,"INPUT ERROR! Please input number from 0-2" ));
  349. blockChoice= b->getBlockIndex(index);
  350. b->generateBlocks(index); // generate new block to replace the block being selected
  351. bool invalidLoc = false;
  352. do { //ask user to input location of placing the block
  353. cout <<"Please input location of placing the block (e.g. A0 , case sensetive): ";
  354. loc = strInputHandler(1);
  355. if(loc == "") return; //checking for invalid input
  356. invalidLoc= (loc[1] <'0'||loc[1]> (boardSize+'0') ||loc.length()!=2||loc[0]<'A'||loc[0]>maxCol);
  357. if(invalidLoc) cout << "Invalid input! Please input the location in this format (A0), case sensetive" << endl;
  358. } while (invalidLoc);
  359. this->x =int(loc[0])-65;
  360. this->y = loc[1]-'0';
  361. if(placingBlockChecker()) //check if the block can be place , if yes then place the block
  362. placingBlock();
  363. else // if no, then the user fails to place the block and the game end
  364. {
  365. endGame();
  366. return;
  367. }
  368. }
  369. else //in PC demo mode
  370. {
  371. bool blockPlaced =false;
  372. //the ides is to loop through all the block choice, and location to find a place to put block
  373. for(int bk =0; bk<3;bk++)
  374. {
  375. for(char let ='A'; let <maxCol;let++)
  376. {
  377. for (int num =0; num< boardSize; num++) {
  378. blockChoice= b->getBlockIndex(bk);
  379. this->x=int(let)-65; this->y=num;
  380. if(placingBlockChecker()) // check is the block can be place here
  381. {
  382. cout <<"The PC choose to put Block index " << bk<< " on " << let <<num <<endl;
  383. int wait;
  384. do{ // wait user to contiue or quit the demo
  385. cout << "Input (1) for continue demo, (quit) for quiting demo: " ;
  386. wait = intInputHandler(1);
  387. if(wait == -1) return;
  388. }while(inputChecking(wait,1, 1, "Input (1) or (quit) only." ));
  389. //once a block is placed, then the program will break all the loops
  390. blockPlaced=true;
  391. b->generateBlocks(bk);
  392. placingBlock();
  393. } if(blockPlaced) break;
  394. } if(blockPlaced) break;
  395. } if(blockPlaced) break;
  396. }
  397. if(!blockPlaced) //if no block can be placed at all , then end the game
  398. {
  399. endGame();
  400. return;
  401. }
  402.  
  403. }
  404. if(checkCompleteRowNCol()) // check for any completed row/column after player placing the block
  405. {
  406. for(int i =0; i< compleColSize;i++)
  407. {
  408. clearCol(compleCol[i]);
  409. }
  410. for(int j =0;j<compleRowSize;j++)
  411. {
  412. clearRow(compleRow[j]);
  413. }
  414. }
  415. round++;// increse round for a bomb in every five round
  416. if (gameMode==2) { // in timer bomb mode
  417. if(bombChecker()) // find time up bomb
  418. {
  419. bombExplo(); // explose and end the game
  420. displayBoard();
  421. cout << "The game ends because the timer bomb explosd." <<endl;
  422. cout << "Your total score is " << score << endl << endl;
  423. return;
  424. }
  425. }
  426. gameStart(demo,gameMode); // call itself for next round
  427. }
  428.  
  429. void displayBoard()
  430. {
  431. //print the game board
  432. //print the frame and the score
  433. cout <<" ";
  434. for (int i=0;i<boardSize;i++)
  435. {
  436. cout << i <<" ";
  437. }
  438. cout << " Score: ";
  439. cout <<score;
  440. cout <<endl;
  441. cout <<" +-";
  442. for (int i=0;i<boardSize;i++)
  443. {
  444. cout << "--";
  445. }
  446. cout << "+";
  447. //additional message when lines is completed
  448. if((compleRowSize+compleColSize)!=0)
  449. cout <<" "<< compleRowSize+compleColSize <<" lines are compolete and removed.";
  450. cout << endl;
  451. // print the board content
  452. for(int i =0;i<boardSize;i++)
  453. {
  454. for (int j=-2;j<boardSize+1;j++)
  455. {
  456. if(j==-2) cout << char(i+65) <<" ";
  457. else if(j==-1) cout << "| ";
  458. else if (j==boardSize) cout <<"|";
  459. else
  460. {
  461. cout << boards[i][j] << " ";
  462. }
  463.  
  464. }
  465. cout << endl;
  466. }
  467. //print the bottom line
  468. cout <<" +-";
  469. for (int i=0;i<boardSize;i++)
  470. {
  471. cout << "--";
  472. }
  473. cout << "+" << endl << endl;
  474.  
  475. }
  476.  
  477. void clearRow(int row)
  478. {
  479. //clear a row of the board after it is completed
  480. for(int i =0;i<boardSize;i++)
  481. {
  482. boards[row][i]=' ';
  483. }
  484. }
  485. void clearCol(int col)
  486. {
  487. //clear a column of the board
  488. for(int i =0;i<boardSize;i++)
  489. {
  490. boards[i][col]=' ';
  491. }
  492. }
  493.  
  494. void clearAll()
  495. {
  496. //clear all element of the board to reest
  497. for (int i = 0; i < boardSize; i++)
  498. {
  499. clearRow(i);
  500. }
  501. }
  502. bool checkCompleteRowNCol()
  503. {
  504. //checker whether completed row / col exist in the board
  505. int p=0, q=0;
  506. for(int i =0;i<boardSize;i++)
  507. {
  508. bool completeRow = true;
  509. for (int j =0; j<boardSize; j++) {
  510. if(boards[i][j] ==' ')
  511. { completeRow=false;
  512. break;
  513. }
  514. }
  515. if(completeRow)
  516. {
  517. compleRow[p]=i;
  518. p++;
  519. }
  520. }
  521. if(p!=0)
  522. compleRow[p]=NULL;
  523. compleRowSize=p;
  524.  
  525. for(int i=0;i<boardSize;i++)
  526. {
  527. bool completeCol = true;
  528. for (int j =0; j<boardSize; j++) {
  529. if(boards[j][i] ==' ')
  530. { completeCol=false;
  531. break;
  532. }
  533. }
  534. if(completeCol)
  535. {
  536. compleCol[q]=i;
  537. q++;
  538. }
  539. }
  540. if(q!=0)
  541. compleCol[q]=NULL;
  542. compleColSize=q;
  543. //depending on how many liens are completed, add the score
  544. switch (compleRowSize+compleColSize) {
  545. case 0:break;
  546. case 1: score+=100;break;
  547. case 2:score+=300;break;
  548. case 3:score +=600;break;
  549. case 4:score+=1000;break;
  550. case 5:score+=1500;break;
  551. default:
  552. break;
  553. }
  554. return (p!=0||q!=0);
  555. }
  556. };
  557.  
  558. class controller
  559. {
  560. //a controller class that handle menu and control the main flow of the program
  561. private:
  562. board* gameBoard;
  563. public:
  564. controller()
  565. {
  566. gameBoard = new board(this);
  567. }
  568.  
  569. void printCredits()
  570. {
  571. cout <<"Student Name:xxx ID:xxx " <<endl;
  572. cout <<"Student Name:xxx ID:xxx " <<endl;
  573. cout <<"Student Name:xxx ID:xxx " <<endl;
  574. cout <<"Student Name:xxx ID:xxx " <<endl;
  575. }
  576.  
  577. void changeBoardSize()
  578. {
  579. //allow the user to change the size of the board
  580. cout << "The board size is: " << gameBoard->getBoardSize() <<endl;
  581. int option;
  582. do{
  583. cout << "Would you like to change the size ? (7-10): ";
  584. option = intInputHandler(0);
  585. }while (inputChecking(option, 10, 7, "Please input (7-10) only."));
  586. if (gameBoard->getBoardSize()!=option)
  587. {
  588. gameBoard->setBoardSize(option);
  589. cout << "Board Size will change to: " << option <<endl;
  590. }
  591. else{
  592. cout << "Board Size is already in: " << option <<endl;
  593. }
  594. gameSetting();
  595. }
  596.  
  597. void changeBomb()
  598. {
  599. //allow the user to change the bomb timer from 6-9
  600. cout << "The bomb timer is: " << gameBoard->getBombTimer() <<endl;
  601. int option;
  602. do{
  603. cout << "Would you like to change the bomb timer ? (6-9): ";
  604. option = intInputHandler(0);
  605. }while (inputChecking(option, 9, 6, "Please input (6-9) only."));
  606. if (gameBoard->getBoardSize()!=option)
  607. {
  608. gameBoard->setBombTimer('0'+option);
  609. cout << "Bomb timer will change to: " << option <<endl;
  610. }
  611. else{
  612. cout << "Bomb timer is already in: " << option <<endl;
  613. }
  614. gameSetting();
  615. }
  616.  
  617. void gameSetting()
  618. {
  619. //game setting menu
  620. system("clear");
  621. int option;
  622. string errorInput ="Error input! Please input number from 1-5";
  623. cout << "*****SETTINGS MENU*****" << endl;
  624. cout << "[1] PC Game Demo" << endl;
  625. cout << "[2] Timer Bomb Mode" <<endl;
  626. cout << "[3] Change Board Size" <<endl;
  627. cout << "[4] Change Bomb Timer" <<endl;
  628. cout << "[5] Return to Game Menu" << endl;
  629. cout << "***********************" <<endl;
  630. do{
  631. cout << "Option (1-5): ";
  632. option =intInputHandler(0);
  633. }while(inputChecking(option,5,1,errorInput));
  634. switch(option)
  635. {
  636. case 1:{ gaemDemoSetting(); break;}
  637. case 2:{ gameModeSetting(); break;}
  638. case 3:{ changeBoardSize(); break;}
  639. case 4:{changeBomb(); break;}
  640. case 5:{ break;}
  641. default: cout <<"Need debug?";
  642. }
  643. }
  644. void gaemDemoSetting()
  645. {
  646. //allow the user to change between Player mode and PC demo mode
  647. string s[]= {"Player mode", "Demo mode"};
  648. cout << "The current setting is: " << s[gameBoard->getDemoMode()-1]<<endl;
  649. int option;
  650. do{
  651. cout << "Would you like to change the mode ? (1 for Player mode, 2 for Demo Mode): ";
  652. option = intInputHandler(0);
  653. }while (inputChecking(option, 2, 1, "Please input (1-2) only."));
  654. if (gameBoard->getDemoMode()!=option) {
  655. gameBoard->setDemoMode(option);
  656. cout << "Game mode will change to: " << s[gameBoard->getDemoMode()-1] <<endl;
  657. }
  658. else{
  659. cout << "The game is already in: " << s[gameBoard->getDemoMode()-1] <<endl;
  660. }
  661. gameSetting();
  662. }
  663.  
  664. void gameModeSetting()
  665. {
  666. // allow the user to change between Original mode and Timer bomb mode
  667. string s[]= {"Original Mode", "Timer Bomb Mode"};
  668. cout << "The current setting is: " << s[gameBoard->getGameMode()-1]<<endl;
  669. int option;
  670. do{
  671. cout << "Would you like to change the mode ? (1 for Original mode, 2 for Timer Bomb Mode): ";
  672. option = intInputHandler(0);
  673. }while (inputChecking(option, 2, 1, "Please input (1-2) only."));
  674. if (gameBoard->getGameMode()!=option)
  675. {
  676. gameBoard->setGameMode(option);
  677. cout << "Game mode will change to: " << s[gameBoard->getGameMode()-1] <<endl;
  678. }
  679. else{
  680. cout << "The game is already in: " << s[gameBoard->getGameMode()-1] <<endl;
  681. }
  682. gameSetting();
  683. }
  684.  
  685. void gameMenu()
  686. {
  687. //the starting game menu
  688. system("clear");
  689. int option;
  690. do{
  691. string errorInput ="Error input! Please input number from 1-5";
  692. cout << "***GAME MENU***" << endl;
  693. cout << "[1] Start Game" << endl;
  694. cout << "[2] Settings" <<endl;
  695. cout << "[3] Instructions" <<endl;
  696. cout << "[4] Credits" <<endl;
  697. cout << "[5] Exit" << endl;
  698. cout << "***************" <<endl;
  699. do{
  700. cout << "Option (1-5): ";
  701. option =intInputHandler(0);
  702. }while(inputChecking(option,5,1,errorInput));
  703.  
  704. switch(option)
  705. {
  706. case 1:{
  707. gameBoard->setRound(0);
  708. gameBoard->clearAll();
  709. gameBoard->setScore(0);
  710. gameBoard->gameStart(gameBoard->getDemoMode(),gameBoard->getGameMode());
  711. break;}
  712. case 2:{ gameSetting(); break;}
  713. case 3:{ printInstruction(); break;}
  714. case 4:{printCredits(); break;}
  715. case 5:{
  716. cout << "Thank you for playing this game!" << endl << "Goodbye!"; break;}
  717. default:
  718. cout <<"Need debug?";
  719. }
  720. }while(option!=5);
  721. }
  722. };
  723.  
  724.  
  725. int main()
  726. {
  727. controller * g= new controller();
  728. g->gameMenu();
  729. return 0;
  730. }
  731.  
  732. bool inputChecking(int value, int max, int min, string message)
  733. {
  734. //a helper function to do error checking and display appropriate message for user
  735. if(value <min || value >max) {
  736. cout << message <<endl;
  737. return true;
  738. }
  739. else
  740. return false;
  741. }
  742.  
  743. bool quit()
  744. {
  745. //comfirm if the user want to quit the game
  746. string s;
  747. cout << "Are you sure you want to quit the game? (Y/N)";
  748. do {
  749. cin >> s;
  750. transform(s.begin(), s.end(), s.begin(), ::toupper);
  751. if(s=="Y"){
  752. return true;
  753. }
  754. else if (s=="N")
  755. return false;
  756. else
  757. cout << "Invalid input, Please input again: ";
  758. } while (s!="Y" && s!="N");
  759. return false;
  760. }
  761.  
  762. void printInstruction()
  763. {
  764. cout <<"Hello! This is a Block Puzzle Game. You need to put a block onto the board each round. And when a horizontal/ vertical line of the board is completed. The whole line will be cleared. So your objective is too keep clearing the lines so that you have space to place the blocks. " <<endl<<endl;
  765. cout << "If you put a block which overlap with an existing block or putting a block out of the board, the game will end." << endl<<endl;
  766. cout << "If you still not so sure how to play the game, you can chooe to watch a demo game done by the PC by choose PC Demo Mode in setting menu." << endl<<endl;
  767. cout << "There is also a challenging Timer Bomb mode of the game. Every five round, a timer bomb will be placed randomly on a block that have been placed on the board and start counting down every round. If you can not clear the line containing the bomb before the bomb go zero. The bomb explode and the game will end. " << endl;
  768. }
  769.  
  770. string strInputHandler(bool allowQuit)
  771. {
  772. //a helper function to checking for user typing "quit"
  773. //with allowQuit as parameter means some stage in the game is not allowing quitting e.g. menu
  774. string input;
  775. cin >> input;
  776. transform(input.begin(), input.end(), input.begin(), ::toupper);
  777. if (allowQuit) {
  778. if (input=="QUIT") {
  779. if(quit()) return "";
  780. else{
  781. cout <<"Please continue input: ";
  782. cin >>input;
  783. }
  784. }
  785. }
  786. return input;
  787. }
  788.  
  789.  
  790. int intInputHandler(bool allowQuit)
  791. {
  792. //a helper function to handle error data type input and check for user typing "quit"
  793. //with allowQuit as parameter means some stage in the game is not allowing quitting e.g. menu
  794. bool badInput =false;
  795. do {
  796. string input;
  797. cin >> input;
  798. if (allowQuit) { //checking for "qut"
  799. transform(input.begin(), input.end(), input.begin(), ::toupper);
  800. if (input=="QUIT") {
  801. if(quit()) return -1;
  802. else{
  803. cout <<"Please continue input: ";
  804. cin >>input;
  805. }
  806. }
  807. }
  808. try { //try coverting to integer, if the input is not integer an exception will be thrown.
  809. return stoi(input);
  810. } catch (invalid_argument e) {
  811. cout << "Only integer input is allowed." <<endl;
  812. cout << "Please input again: ";
  813. badInput = true;
  814. }
  815. }while(badInput);
  816. return NULL;
  817. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement