Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. // NathanWood_Final.cpp : Defines the entry point for the console application.
  2. //
  3. #include "Player.h"
  4. #include "PlayerOptions.h"
  5. #include "OtherPlayerOptions.h"
  6. #include "stdafx.h"
  7. #include "GameLoop.h"
  8. #include <string>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <ctime>
  12.  
  13.  
  14. using namespace std;
  15.  
  16.  
  17.  
  18. void Game::WelcomePlayer()
  19. {
  20. std::string line;
  21. std::ifstream myfile("Rules.txt");
  22. if (myfile.is_open())
  23. {
  24. while (getline(myfile, line))
  25. {
  26. std::cout << line << '\n';
  27. }
  28. myfile.close();
  29. }
  30. else std::cout << "Unable to open file";
  31.  
  32. std::cout << "\nWelcome to liars dice. How many players are playing?";
  33. int noOfPlayers;
  34. std::cin >> noOfPlayers;
  35. m_noOfPlayers = noOfPlayers;
  36.  
  37.  
  38. if (noOfPlayers > 0) m_player = new Player[noOfPlayers];
  39. else
  40. std::cout << "That is not a valid choice";
  41.  
  42.  
  43. }
  44.  
  45. void Game::RollDice()
  46. {
  47. srand((unsigned int)time(NULL)); //seed the random # generator
  48. for (int i = 0; i < m_noOfPlayers; ++i)
  49. {
  50. for (int j = 0; j < 5; ++j)
  51. {
  52. m_player[i].dice[j] = (rand() % 6) + 1;
  53. }
  54. }
  55. }
  56.  
  57.  
  58. void Game::ShowDice(int i)
  59. {
  60. string temp;
  61. std::cout << "Player " << i + 1 << ", your dice are: ";
  62. std::cout << m_player[i].dice[0] << m_player[i].dice[1];
  63. std::cout << m_player[i].dice[2] << m_player[i].dice[3];
  64. std::cout << m_player[i].dice[4] << endl << endl;
  65. std::cin.ignore();
  66. std::cout << "Press Enter to Continue..." << endl;
  67. std::cin.get();
  68. std::cout << string(40, '\n');
  69. }
  70.  
  71.  
  72. void Game::GiveOptions(int i) const
  73. {
  74. std::cout << string(40, '\n');
  75. std::cout << "Player " << i + 1 << " choose an option:" << endl << endl;
  76. std::cout << "1: Show Dice" << endl;
  77. std::cout << "2: Enter Guess" << endl;
  78. std::cout << "3: Quit" << endl;
  79. }
  80.  
  81. void Game::GiveOtherOptions(int i)
  82. {
  83. std::cout << string(40, '\n');
  84. std::cout << "the current guess is " << m_prevAmountGuess << " " << m_prevValueGuess << "'s" << endl;
  85. std::cout << "Player " << (i % m_noOfPlayers) + 1 << " choose an option:" << endl << endl;
  86. std::cout << "1: Show Dice" << endl;
  87. std::cout << "2: Pass" << endl;
  88. std::cout << "3: Call Liar" << endl;
  89. std::cout << "4: Quit" << endl;
  90. }
  91.  
  92.  
  93. void Game::GetPlayerInput(string& playerInput) const
  94. {
  95. std::cin >> playerInput;
  96. }
  97.  
  98. void Game::GetOtherPlayerInput(string& otherPlayerInput) const
  99. {
  100. std::cin >> otherPlayerInput;
  101. }
  102.  
  103.  
  104. PlayerOptions Game::EvaluateInput(string& playerInput) const
  105. {
  106. PlayerOptions chosenOption = PlayerOptions::None;
  107.  
  108. if (playerInput.compare("1") == 0)
  109. {
  110. std::cout << "You have chosen to view your dice" << endl << endl;
  111. chosenOption = PlayerOptions::ShowDice;
  112. }
  113. else if (playerInput.compare("2") == 0)
  114. {
  115. cout << "You have chosen to enter a guess" << endl << endl;
  116. chosenOption = PlayerOptions::EnterGuess;
  117. }
  118.  
  119. else if (playerInput.compare("3") == 0)
  120. {
  121. cout << "You have chosen to quit." << endl << endl;
  122. chosenOption = PlayerOptions::Quit;
  123. }
  124. else
  125. {
  126. cout << "I do not recognise that option, try again!" << endl << endl;
  127. }
  128.  
  129. return chosenOption;
  130. }
  131.  
  132. OtherPlayerOptions Game::EvaluateOtherInput(string& otherPlayerInput) const
  133. {
  134. OtherPlayerOptions otherChosenOption = OtherPlayerOptions::None;
  135.  
  136. if (otherPlayerInput.compare("1") == 0)
  137. {
  138. std::cout << "You have chosen to view your dice" << endl << endl;
  139. otherChosenOption = OtherPlayerOptions::ShowDice;
  140. }
  141. else if (otherPlayerInput.compare("2") == 0)
  142. {
  143. cout << "You have chosen to enter a guess" << endl << endl;
  144. otherChosenOption = OtherPlayerOptions::Pass;
  145. }
  146. else if (otherPlayerInput.compare("3") == 0)
  147. {
  148. cout << "You have Called the previous player a Liar!" << endl << endl;
  149. otherChosenOption = OtherPlayerOptions::CallLiar;
  150. }
  151. else if (otherPlayerInput.compare("4") == 0)
  152. {
  153. cout << "You have chosen to quit." << endl << endl;
  154. otherChosenOption = OtherPlayerOptions::Quit;
  155. }
  156. else
  157. {
  158. cout << "I do not recognise that option, try again!" << endl << endl;
  159. }
  160.  
  161. return otherChosenOption;
  162. }
  163.  
  164.  
  165. void Game::EnterGuess()
  166. {
  167.  
  168. do
  169. {
  170. if (m_turn > 1) {
  171. std::cout << "previous guess is " << m_prevAmountGuess << " " << m_prevValueGuess << endl;
  172. }
  173.  
  174. std::cout << "Enter a number of dice" << endl;
  175. std::cin >> m_currentAmountGuess;
  176. std::cout << "Enter a value of dice ( 1 through 6 )" << endl;
  177. std::cin >> m_currentValueGuess;
  178.  
  179. if (m_currentAmountGuess <= m_prevAmountGuess) {
  180. std::cout << "You must guess a number of Dice greater than the previous player. Try again.";
  181. }
  182.  
  183.  
  184. } while (m_currentAmountGuess <= m_prevAmountGuess);
  185.  
  186. m_prevAmountGuess = m_currentAmountGuess;
  187. m_prevValueGuess = m_currentValueGuess;
  188. }
  189.  
  190. void Game::Liar(int i)
  191. {
  192. int counter = 0;
  193.  
  194.  
  195. for (int i = 0; i < m_noOfPlayers; ++i)
  196. {
  197. std::cout << "player " << i + 1 << ": ";
  198. std::cout << m_player[i].dice[0] << m_player[i].dice[1];
  199. std::cout << m_player[i].dice[2] << m_player[i].dice[3];
  200. std::cout << m_player[i].dice[4] << endl;
  201. }
  202.  
  203.  
  204.  
  205. for (int i = 0; i < m_noOfPlayers; ++i)
  206. {
  207. for (int j = 0; j < 5; ++j)
  208. {
  209. if (m_player[i].dice[j] == m_prevValueGuess)
  210. {
  211. ++counter;
  212. if (counter == m_prevAmountGuess)
  213. {
  214. std::cout << "You win!";
  215. return;
  216. }
  217. }
  218. }
  219. }
  220. std::cout << "You lose!";
  221.  
  222.  
  223. }
  224.  
  225.  
  226. void Game::RunGame()
  227. {
  228.  
  229. WelcomePlayer();
  230. RollDice();
  231. m_turn = 1;
  232. int i = 0;
  233.  
  234. bool bShouldEnd = false;
  235. while (bShouldEnd == false)
  236. {
  237.  
  238. GiveOptions(i);
  239. string playerInput;
  240. GetPlayerInput(playerInput);
  241. PlayerOptions tmpOption = EvaluateInput(playerInput);
  242.  
  243. if (tmpOption == PlayerOptions::ShowDice)
  244. {
  245. if ( i > m_noOfPlayers ) i = 0;
  246. ShowDice(i);
  247. }
  248.  
  249. else if (tmpOption == PlayerOptions::EnterGuess)
  250. {
  251. string otherPlayerInput;
  252. int callcounter = 0;
  253. m_CurrentPlayer = i;
  254. EnterGuess();
  255.  
  256.  
  257. //m_CurrentPlayer % m_noOfPlayers
  258. while (callcounter < m_noOfPlayers - 1)
  259. {
  260. std::cout << "entered other while loop" << endl;
  261. if (i != m_CurrentPlayer)
  262. {
  263. std::cout << "give other options" << endl;
  264. GiveOtherOptions(i);
  265. GetOtherPlayerInput(otherPlayerInput);
  266. OtherPlayerOptions OthertmpOption = EvaluateOtherInput(otherPlayerInput);
  267.  
  268. if (OthertmpOption == OtherPlayerOptions::ShowDice)
  269. {
  270. ShowDice(i);
  271. }
  272. else if (OthertmpOption == OtherPlayerOptions::Pass)
  273. {
  274. ++i;
  275. ++callcounter;
  276. }
  277. else if (OthertmpOption == OtherPlayerOptions::CallLiar)
  278. {
  279. std::cout << "A liar has been called!" << endl;
  280. Liar(i);
  281. bShouldEnd = true;
  282. }
  283. else if (OthertmpOption == OtherPlayerOptions::Quit)
  284. {
  285. break;
  286. bShouldEnd = true;
  287. }
  288.  
  289. }
  290. else if (i == m_CurrentPlayer)
  291. {
  292. ++i;
  293. }
  294. if (i == m_noOfPlayers) i = 0;
  295.  
  296.  
  297. }
  298.  
  299. //Liar(i);
  300.  
  301. i = i + 1;
  302. if (i >= m_noOfPlayers) {
  303. i = 0;
  304. }
  305.  
  306. m_turn = m_turn + 1; //increment the turn
  307. }
  308.  
  309. /*else if (tmpOption == PlayerOptions::CallLiar)
  310. {
  311. if (i = 1) i = i + m_noOfPlayers;
  312. std::cout << "You called player " << i - 1 << " a liar! The dice are: " << endl;
  313. Liar(i);
  314. bShouldEnd = true;
  315. }*/
  316.  
  317. else if (tmpOption == PlayerOptions::Quit)
  318. {
  319. bShouldEnd = true;
  320. break;
  321. }
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328. }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement