Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7. void hideandSeek();
  8. void tictacToe();
  9. int tttWin();
  10. void tttBoard();
  11.  
  12. char matrix[10] = {'0','1','2','3','4','5','6','7','8','9'};
  13.  
  14.  
  15. int main()
  16. {
  17. string gameSelect;
  18.  
  19. while (true)
  20. {
  21. cout << "\nWelcome to the game pack featuring Tic Tac Toe and Hide and Seek" << endl;
  22. cout << "Please choose Tic Tac Toe by entering 1 or Hide and Seek by entering 2" << endl;
  23. cin >> gameSelect;
  24. if (gameSelect == "1")
  25. {
  26. hideandSeek();
  27. gameSelect = "0";
  28. }
  29.  
  30. else if (gameSelect == "2")
  31. {
  32. tictacToe();
  33. gameSelect = "0";
  34. }
  35. else if (gameSelect == "0")
  36. {
  37. cout << "\n\nWelcome to the game pack featuring Tic Tac Toe and Hide and Seek" << endl;
  38. cout << "Please choose Tic Tac Toe by entering 1 or Hide and Seek by entering 2" << endl;
  39. cin >> gameSelect;
  40. }
  41. else
  42. {
  43. cout << "Invalid selection, please enter again" << endl;
  44. }
  45. }
  46. return 0;
  47. }
  48.  
  49. void hideandSeek()
  50. {
  51. cout << "\nWelcome to the hide and seek game!\nGuess where the computer is hiding among the 4 quadrants.\n";
  52. cout << "-----------------" << endl;
  53. cout << " | " << endl;
  54. cout << " | " << endl;
  55. cout << "-----------------" << endl;
  56. cout << " | " << endl;
  57. cout << " | " << endl;
  58. cout << "-----------------" << endl;
  59. string player_choice;
  60. srand (time(NULL));
  61. int comp_choice = rand() % 4 + 1;
  62. int user_choice;
  63. while (true)
  64. {
  65. cin >> user_choice;
  66. if (user_choice != comp_choice)
  67. {
  68. cout << "Incorrect choice! Try again.";
  69. }
  70. else
  71. {
  72. break;
  73. }
  74. }
  75. cout << "You guessed it! The computer was hiding in quadrant " << comp_choice;
  76. }
  77.  
  78. void tictacToe()
  79. {
  80. int tttPlayer = 1, i;
  81. string tttChoice;
  82. char gameMark;
  83.  
  84. do
  85. {
  86. tttBoard();
  87. tttPlayer = (tttPlayer%2) ?1:2;
  88. cout << "Welcome to the Tic Tac Toe game!\nGuess Requires two players to play. X is the first player.\n";
  89. cout << "Player " << tttPlayer << " , enter a number: ";
  90. cin >> tttChoice;
  91. gameMark = (tttPlayer == 1) ? 'X' : 'O';
  92. if (tttChoice == "1" && matrix[1] == '1')
  93. matrix[1] = gameMark;
  94. else if (tttChoice == "2" && matrix[2] == '2')
  95. matrix[2] = gameMark;
  96. else if (tttChoice == "3" && matrix[3] == '3')
  97. matrix[3] = gameMark;
  98. else if (tttChoice == "4" && matrix[4] == '4')
  99. matrix[4] = gameMark;
  100. else if (tttChoice == "5" && matrix[5] == '5')
  101. matrix[5] = gameMark;
  102. else if (tttChoice == "6" && matrix[6] == '6')
  103. matrix[6] = gameMark;
  104. else if (tttChoice == "7" && matrix[7] == '7')
  105. matrix[7] = gameMark;
  106. else if (tttChoice == "8" && matrix[8] == '8')
  107. matrix[8] = gameMark;
  108. else if (tttChoice == "9" && matrix[9] == '9')
  109. matrix[9] = gameMark;
  110. else
  111. {
  112. cout << "Invalid move ";
  113. tttPlayer --;
  114. cin.ignore();
  115. cin.get();
  116. }
  117. i=tttWin();
  118. tttPlayer++;
  119.  
  120. }
  121. while (i== -1);
  122. tttBoard();
  123. if(i==1)
  124. {
  125. cout << "==>\aPlayer " <<--tttPlayer<<" win ";
  126. }
  127. else
  128. {
  129. cout << "==>\aGame draw";
  130. }
  131. cin.ignore();
  132. cin.get();
  133. }
  134.  
  135. int tttWin()
  136. {
  137. if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
  138. return 1;
  139. else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
  140. return 1;
  141. else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
  142. return 1;
  143. else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
  144. return 1;
  145. else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
  146. return 1;
  147. else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
  148. return 1;
  149. else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
  150. return 1;
  151. else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
  152. return 1;
  153. else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3'&& matrix[4] != '4' && matrix[5] != '5' && matrix[6] !='6'&& matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
  154. return 0;
  155. else
  156. return -1;
  157. }
  158.  
  159. void tttBoard()
  160. {
  161. cout << " | | " << endl;
  162. cout << " " << matrix[1] << " | " << matrix[2] << " | " << matrix[3] << endl;
  163. cout << " | | " << endl;
  164. cout << "--------------------" << endl;
  165. cout << " | | " << endl;
  166. cout << " " << matrix[4] << " | " << matrix[5] << " | " << matrix[6] << endl;
  167. cout << " | | " << endl;
  168. cout << "--------------------" << endl;
  169. cout << " | | " << endl;
  170. cout << " " << matrix[7] << " | " << matrix[8] << " | " << matrix[9] << endl;
  171. cout << " | | " << endl;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement