Advertisement
Guest User

GameMenu.cpp

a guest
May 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "GameMenu.h"
  4. // stuff to add:
  5. // scoring function, checking around current tile, testArray[i+1][j] check etc, do testArray[]
  6.  
  7.  
  8. using std::string;
  9. using std::cout;
  10. using std::endl;
  11. using std::cin;
  12.  
  13. string stringInput1;
  14. string stringInput2;
  15. bool endLoop;
  16. bool endGameplay;
  17. bool currentGame;
  18. bool playerTurn;
  19. bool nodeCheck;
  20. bool isValid;
  21. int replaceIndex;
  22. string tile2d;
  23. char x;
  24. int y;
  25. int sizeOfMaze = 24;
  26. int tileType;
  27. int tileLocationX;
  28. int tileLocationY;
  29. int boardCounter;
  30.  
  31.  
  32. Player p;
  33. // Board b;
  34. Tile t;
  35. Randomiser r;
  36. LinkedList l;
  37.  
  38. Tile nullTile = Tile (' ',0);
  39. Tile testArray[26][26];
  40.  
  41. string letters[26] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  42. "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
  43. void initiliaseBoard(){
  44. for (int i = 0; i < 26; i++){
  45. for (int j = 0; j < 26; j++){
  46. testArray[i][j] = nullTile;
  47. }
  48.  
  49. }
  50. }
  51.  
  52. void displayBoard() {
  53.  
  54. cout << " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25" << endl;
  55. for (unsigned int i = 0; i < 26; i++) {
  56. cout << letters[i] << " ";
  57. //cout << endl;
  58. for (unsigned int j = 0; j < 26; j++) {
  59. if (j == 0){
  60. cout << "|" << testArray[i][j].colour;
  61. }else{
  62. cout << testArray[i][j].colour;
  63. }
  64. if (testArray[i][j].shape == 0){
  65. cout << " " << "|";
  66. } else {
  67. cout << testArray[i][j].shape << "|";
  68. }
  69. }
  70. cout << endl;
  71. }
  72. cout << endl;
  73. }
  74.  
  75.  
  76. void GameMenu::testBoard() {
  77. tileCheck = false;
  78. isValid = false;
  79. Node* temp;
  80.  
  81.  
  82. do{
  83. do{
  84. cout << "tileCheck is: " << tileCheck << endl;
  85. cout << "Which colour would you like to place" << endl;
  86. cin >> x;
  87. cout << "Which shape would you like to place" << endl;
  88. cin >> y;
  89. Tile* tmpTile = new Tile(x, y);
  90. if (p.currentPlayer == p.player1){
  91. temp = l.p1Head;
  92. tileCheck = l.tileComparePlace(temp, tmpTile, tileCheck);
  93. }else if (p.currentPlayer == p.player2){
  94. temp = l.p2Head;
  95. tileCheck = l.tileComparePlace(temp, tmpTile,tileCheck);
  96. }
  97. }while (tileCheck != true);
  98.  
  99. Tile placeTile = Tile (x,y);
  100. cout << "Okay! and what is the X coordinate? (starts at 0, so 0-23)" << endl;
  101. cin >> tileLocationX;
  102. tileLocationX = tileLocationX;
  103. cout << "And what is the Y cooardinate? (starts at 0, so 0-23)" << endl;
  104. cin >> tileLocationY;
  105. tileLocationY = tileLocationY;
  106.  
  107. if (boardCounter == 0) {
  108. isValid = true;
  109. }
  110.  
  111. if (boardCounter != 0) {
  112.  
  113. if (placeTile.colour == testArray[tileLocationX + 1][tileLocationY].colour
  114. || placeTile.shape == testArray[tileLocationX + 1][tileLocationY].shape
  115. || placeTile.colour == testArray[tileLocationX - 1][tileLocationY].colour
  116. || placeTile.shape == testArray[tileLocationX - 1][tileLocationY].shape
  117. || placeTile.colour == testArray[tileLocationX][tileLocationY + 1].colour
  118. || placeTile.shape == testArray[tileLocationX][tileLocationY + 1].shape
  119. || placeTile.colour == testArray[tileLocationX][tileLocationY - 1].colour
  120. || placeTile.shape == testArray[tileLocationX][tileLocationY - 1].shape) {
  121. cout << "Valid to place! :D" << endl;
  122. isValid = true;
  123. } else {
  124. cout << "Invalid to place!" << endl;
  125. tileCheck = false;
  126.  
  127. }
  128. }
  129. boardCounter++;
  130. testArray[tileLocationY][tileLocationX] = placeTile;
  131. } while (isValid == false);
  132.  
  133. //cout << "Placing " << placeTile.colour << placeTile.shape << " at X: " << tileLocationX << " Y: " << tileLocationY << endl;
  134.  
  135. //cout << "AFTER RESULTS" << endl << endl;
  136.  
  137. //for (unsigned int i = 0; i < 6; i++) {
  138. //cout << endl;
  139. //for (unsigned int j = 0; j < 6; j++) {
  140. //cout << "|" <<testArray[i][j] << "|";
  141. //}
  142. //}
  143. cout << endl;
  144. index++;
  145. handSize--;
  146. }
  147.  
  148. void GameMenu::mainMenu()
  149. {
  150. do
  151. {
  152.  
  153. cout << endl;
  154. cout << "Welcome to Qwirkle!" << endl;
  155. cout << "-------------------" << endl;
  156. cout << endl;
  157. cout << "Menu" << endl;
  158. cout << "----" << endl;
  159.  
  160. cout << "1. New Game " << endl;
  161. cout << "2. Load Game " << endl;
  162. cout << "3. Show student information " << endl;
  163. cout << "4. Quit " << endl;
  164. cout << endl;
  165. cout << ">" << endl;
  166. cout << endl;
  167.  
  168. //read playerInput
  169. cout << "Enter Input: ";
  170. cin >> stringInput1;
  171.  
  172. if(stringInput1 == "1")
  173. {
  174. Tile bagOfTiles[72];
  175. Tile* bagOfTilesptr[72];
  176. t.createTiles(bagOfTiles);
  177. r.shuffleBag(bagOfTiles);
  178. for (int i = 0; i < 72; i++){
  179. bagOfTilesptr[i] = &bagOfTiles[i];
  180. }
  181.  
  182. for (int i = 0; i < 72; i++){
  183. Tile* temp = bagOfTilesptr[i];
  184. l.createBag(temp);
  185. }
  186.  
  187. for (int i = 0; i < 6; i++){
  188. l.p1Head = l.deal(l.p1Head);
  189. l.p2Head = l.deal(l.p2Head);
  190. }
  191.  
  192. GameMenu::newGame();
  193. }
  194. else if(stringInput1 == "2")
  195. {
  196. GameMenu::loadGame();
  197. }
  198. else if(stringInput1 == "3")
  199. {
  200. //printArray(l.p1Head);
  201. //printArray(l.p2Head);
  202. GameMenu::studentInfo();
  203. }
  204. else if (stringInput1 == "4")
  205. {
  206. GameMenu::exitProgram();
  207. }
  208. else if(stringInput1 == "^D") {
  209. GameMenu::exitProgram();
  210. }
  211. else if(stringInput1 == "help") {
  212. GameMenu::helpFunction1();
  213. } else {
  214. cout << endl;
  215. cout << "Invalid Input! You are not allowed to enter " << "''" << stringInput1 << "'', please try one of the inputs listed above" << endl;
  216. }
  217. }
  218. while(endLoop == false);
  219.  
  220. }
  221.  
  222. void GameMenu::newGame() { // need to do: validation check for numbers or symbols
  223. cout << "> 1" << endl;
  224. cout << endl;
  225. cout << "Starting a New Game" << endl;
  226. cout << endl;
  227. cout << "Enter a name for player 1 (uppercase characters only)" << endl;
  228. cin >> p.player1;
  229. cout << endl;
  230. cout << "Enter a name for Player 2 (uppercase characters only)" << endl;
  231. cin >> p.player2;
  232. cout << endl;
  233. p.currentPlayer = p.player1;
  234. cout << "Let's Play!" << endl;
  235. cout << endl;
  236. initiliaseBoard();
  237. GameMenu::continueGameplay();
  238. boardCounter = 0;
  239. }
  240.  
  241. void GameMenu::continueGameplay() {
  242. do {
  243. cout << endl;
  244. cout << p.currentPlayer << ", it's your turn" << endl;
  245. cout << "Score for " << p.player1 << ": " << p.player1Score << endl;
  246. cout << "Score for " << p.player2 << ": " << p.player2Score << endl;
  247. cout << "BOARD" << endl;
  248. displayBoard();
  249. cout << endl;
  250. cout << "Your hand is: ";
  251. if (p.currentPlayer == p.player1) {
  252. printArray(l.p1Head);
  253. } else {
  254. printArray(l.p2Head);
  255. }
  256. cout << endl;
  257. cout << "Options" << endl;
  258. cout << "1: Place a tile onto the board" << endl;
  259. cout << "2: Replace a tile in your hand" << endl;
  260. cout << "3: Save the game to a file" << endl;
  261. cout << "4: Quit the game" << endl;
  262. cout << "Enter Input: " << endl;
  263. cin >> stringInput2;
  264.  
  265. if(stringInput2 == "1")
  266. {
  267. GameMenu::placeTileOntoBoard();
  268. }
  269. else if(stringInput2 == "2")
  270. {
  271. GameMenu::replaceTileInHand();
  272. }
  273. else if(stringInput2 == "3")
  274. {
  275. GameMenu::saveCurrentGame();
  276. }
  277. else if (stringInput2 == "4")
  278. {
  279. GameMenu::exitTheProgram();
  280. }
  281. else if (stringInput2 == "^D")
  282. {
  283. GameMenu::exitTheProgram();
  284. }
  285. //else if(stringInput2 == "5")
  286. //{
  287. // GameMenu::endTurn();
  288. // }
  289. else if(stringInput2 == "help")
  290. {
  291. GameMenu::helpFunction2();
  292. } else {
  293. cout << endl;
  294. cout << "Invalid Input! You are not allowed to enter " << "'" << stringInput2 << "', type help to get a list of all the valid inputs" << endl;
  295. cout << endl;
  296. }
  297.  
  298. } while(endGameplay == false);
  299. }
  300.  
  301.  
  302. void GameMenu::loadGame() {
  303. cout << "Enter the filename from which load a game" << endl;
  304. }
  305.  
  306. void GameMenu::studentInfo() {
  307. cout << endl;
  308. cout << "Name: George Korovinis" << endl;
  309. cout << "Student ID: s3658218" << endl;
  310. cout << "Email: s3658218@student.rmit.edu.au\n" << endl;
  311.  
  312. cout << "Name: Tylar Lariba-Taing" << endl;
  313. cout << "Student ID: s3604139" << endl;
  314. cout << "Email: s3604139@student.rmit.edu.au\n" << endl;
  315.  
  316. cout << "Name: Lorenzo Vossos" << endl;
  317. cout << "Student ID: s3792797" << endl;
  318. cout << "Email: s3792797@student.rmit.edu.au\n" << endl;
  319. cout << endl;
  320.  
  321. }
  322.  
  323. void GameMenu::exitProgram() {
  324. cout << "Goodbye" << endl;
  325. endLoop = true;
  326. }
  327.  
  328. void GameMenu::helpFunction1() {
  329. cout << endl;
  330. cout << "------------" << endl;
  331. cout << "Help Display" << endl;
  332. cout << "You may execute the following commands: 1, 2, 3, 4, ^D and help" << endl;
  333. cout << endl;
  334. }
  335.  
  336. void GameMenu::placeTileOntoBoard() {
  337. testBoard();
  338. p.updateScore();
  339. GameMenu::checkForEndTurn();
  340. }
  341.  
  342.  
  343. void GameMenu::replaceTileInHand() {
  344. tileCheck = false;
  345. do{
  346. cout << "Which colour would you like to replace" << endl;
  347. cin >> x;
  348. cout << "Which shape would you like to replace" << endl;
  349. cin >> y;
  350. Tile* tmpTile = new Tile(x, y);
  351. if (p.currentPlayer == p.player1){
  352. Node* n = l.p1Head;
  353. tileCheck = l.tileCompareReplace(n, tmpTile, tileCheck);
  354. }else if (p.currentPlayer == p.player2){
  355. Node* n = l.p2Head;
  356. tileCheck = l.tileCompareReplace(n, tmpTile,tileCheck);
  357. }
  358. }while (tileCheck != true);
  359. index = 0;
  360. handSize = 6;
  361. p.switchPlayer();
  362. }
  363.  
  364. void GameMenu::saveCurrentGame() {
  365. cout << "SAVING THE CURRENT GAME" << endl;
  366. cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  367. }
  368.  
  369. void GameMenu::exitTheProgram() {
  370. cout << "Goodbye" << endl;
  371. endGameplay = true;
  372. exit(0);
  373. }
  374.  
  375. //void GameMenu::endTurn() {
  376. //p.switchPlayer();
  377. //cout << "Current Players turn ending!" << endl;
  378. //cout << endl;
  379. //}
  380.  
  381. void GameMenu::helpFunction2() {
  382. cout << endl;
  383. cout << "------------" << endl;
  384. cout << "Help Display" << endl;
  385. cout << "You may execute the following commands: 1, 2, 3, 4, ^D and help" << endl;
  386. cout << endl;
  387. }
  388.  
  389. void GameMenu::printList(Node *n){
  390. int test=0;
  391. while (n != nullptr){
  392. std::cout << n -> tile << "" << std::endl;
  393. n = n -> next;
  394. test++;
  395. }
  396. std::cout << test << std::endl;
  397. }
  398.  
  399. void GameMenu::printArray(Node* n){
  400. Node* temp = n;
  401. //creating an array just for this hand
  402. Tile hand[6];
  403. //creating a pointer array to store the node value
  404. Tile* handptr[6];
  405. for (int i = 0; i < handSize; i++){
  406. //storing node value into pointer array
  407. handptr[i] = temp -> tile;
  408. //storing dereferencing the pointer and storing it into a temp hand
  409. hand[i] = *handptr[i];
  410. std::cout << hand[i].colour << hand[i].shape << " ";
  411. //going to the next node
  412. temp = temp -> next;
  413. }
  414. std::cout << std::endl;
  415. }
  416.  
  417. void GameMenu::checkForEndTurn() {
  418. string response;
  419. if (handSize == 0){
  420. for (int i = 0; i < index; i++){
  421. if (p.currentPlayer == p.player1){
  422. l.deal(l.p1Head);
  423. } else {
  424. l.deal(l.p2Head);
  425. }
  426. }
  427. index = 0;
  428. handSize = 6;
  429. cout << p.currentPlayer << " " << ", your hand is empty!" << endl;
  430. p.switchPlayer();
  431. } else {
  432. cout << "Would you like to place another tile? (y/n)" << endl;
  433. cin >> response;
  434. if (response == "y") {
  435. cout << "Okay, your turn continues and you may place down another tile" << endl;
  436. } else if (response == "n") {
  437. cout << "Very well, your turn is now over" << endl;
  438. cout << endl;
  439. for (int i = 0; i < index; i++){
  440. if (p.currentPlayer == p.player1){
  441. l.deal(l.p1Head);
  442. } else {
  443. l.deal(l.p2Head);
  444. }
  445. }
  446. index = 0;
  447. handSize = 6;
  448. p.switchPlayer();
  449. } else {
  450. cout << "Invalid Input!, you are not allowed to enter " << response << ", please respond with either y or n" << endl;
  451. }
  452. }
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement