fork download
  1. //Dylan Lawrence IT312 Final Farkle
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <ctime> // ctime combines asctime and localtime functions for tracking purposes
  7.  
  8. using namespace std;
  9.  
  10. class Dice //rolls 6 Dice per player
  11. {
  12. private:
  13. int valueOfDice[6];
  14. bool valueOfReroll[6];
  15. int countPerDie;
  16. int rerollCount;
  17.  
  18. public:
  19. Dice() //constructor https://e...content-available-to-author-only...e.com/w/cpp/language/initializer_list
  20. {
  21. for (int i = 0; i < 6; i++) {
  22. valueOfDice[i] = 0; //aggregate initialization
  23. valueOfReroll[i] = false; //giving warnings on debugger.
  24. }
  25. countPerDie = 0;
  26. rerollCount = 0;
  27. }
  28.  
  29. void diceRoll() { //first roll of all six dice
  30. for (int i = 0; i < 6; i++) {
  31. valueOfDice[i] = (rand() % 6 + 1);
  32. if (valueOfDice[i] != 1 && valueOfDice[i] != 5) {
  33. valueOfReroll[i] = true;
  34. }
  35. }
  36. }
  37. void anotherDiceRoll() { //rolls dice again or multiple times when not worth points
  38. for (int i = 0; i < 6; i++) {
  39. if (valueOfReroll[i] == true)
  40. valueOfDice[i] = (rand() % 6 + 1);
  41. }
  42. }
  43. void valueOfReroll() { // set false to valueOfReroll
  44. for (int i = 0; i < 6; i++) {
  45. valueOfReroll[i] = false;
  46. }
  47. }
  48.  
  49. int pointsPerRound() { //acquires point value for the dice
  50. int numOne = 0, numTwo = 0, numThree = 0, numFour = 0, numFive = 0, numSix = 0;
  51. int points = 0;
  52. countPerDie = 0;
  53. for (int i = 0; i < 6; i++) {
  54. switch (valueOfDice[i]) {
  55. case 1:
  56. points += 100;
  57. numOne++;
  58. break;
  59.  
  60. case 2:
  61. numTwo++;
  62. countPerDie++;
  63. break;
  64.  
  65. case 3:
  66. numThree++;
  67. countPerDie++;
  68. break;
  69.  
  70. case 4:
  71. numFour++;
  72. countPerDie++;
  73. break;
  74.  
  75. case 5:
  76. points += 50;
  77. numFive++;
  78. break;
  79.  
  80. case 6:
  81. numSix++;
  82. countPerDie++;
  83. break;
  84. }
  85. }
  86. if (countPerDie == 6) {
  87. cout << "Well That's FARKLE!!!";
  88. points = 0;
  89. }
  90. else {
  91. if (numOne >= 3) { // paste to 5
  92. points = (points - 300) + 1000;
  93. }
  94. else if (numTwo >= 3) { //copy-pasta'd ahead
  95. points += 200;
  96. for (int i = 0; i < 6; i++) {
  97. if (valueOfDice[i] == 2) {
  98. valueOfReroll[i] = false;
  99. }
  100. }
  101. countPerDie -= 3;
  102. }
  103. else if (numThree >= 3) {
  104. points += 300;
  105. for (int i = 0; i < 6; i++) {
  106. if (valueOfDice[i] == 3) {
  107. valueOfReroll[i] = false;
  108. }
  109. }
  110. countPerDie -= 3;
  111. }
  112. else if (numFour >= 3) {
  113. points += 400;
  114. for (int i = 0; i < 6; i++) {
  115. if (valueOfDice[i] == 4) {
  116. valueOfReroll[i] = false;
  117. }
  118. }
  119. countPerDie -= 3;
  120. }
  121. else if (numFive >= 3) {
  122. points = (points - 150) + 500;
  123. }
  124. else if (numSix >= 3) {
  125. points += 600;
  126. for (int i = 0; i < 6; i++) {
  127. if (valueOfDice[i] == 6){
  128. valueOfReroll[i] = false;
  129. }
  130. }
  131. countPerDie -= 3;
  132. }
  133. }
  134. return points;
  135. }
  136. //not gonna lie, My tutor helped me out with this section
  137. void showDice() {//Shows the values of all six die
  138. for (int i = 0; i < 6; i++) {
  139. cout << valueOfDice[i] << " ";
  140. }
  141. }
  142. void setNumRoll(int value) { //think set and get in Blueprints via UE4
  143. countPerDie = value;
  144. }
  145. int getNumRoll() {
  146. return countPerDie;
  147. }
  148. };
  149.  
  150. class Player { //holds total points and accumulated points
  151. private:
  152. string name;
  153. int pointsAccrued;
  154. int totalPoints;
  155. public:
  156. Player() {
  157. name = "XXXXXX";
  158. pointsAccrued = 0;
  159. totalPoints = 0;
  160. }
  161. Player(string name) {
  162. this->name = name;
  163. pointsAccrued = 0;
  164. totalPoints = 0;
  165. }
  166. void setName(string name) {
  167. this->name = name;
  168. }
  169. string getName() {
  170. return this->name;
  171. }
  172. void addPoints(int pointsperround) {
  173. pointsAccrued = pointsperround;
  174. totalPoints += pointsperround;
  175. }
  176. void setPoints(int points)
  177. {
  178. this->pointsAccrued = points;
  179. }
  180. int getTotalPoints()
  181. {
  182. return totalPoints;
  183. }
  184. int getPointsAccrued()
  185. {
  186. return pointsAccrued;
  187. }
  188. };
  189.  
  190. class WellThatsFarkle { //should hold name, round, winner initilization
  191. private:
  192. Player* players;
  193. int playerCount;
  194. Dice dice;
  195. public:
  196. WellThatsFarkle(int playerAmt) {//Parameterized
  197. players = new Player[playerAmt];
  198. playerCount = playerAmt;
  199. }
  200. void readPlayerNames() {
  201. string name;
  202. for (int i = 0; i < playerCount; i++){
  203. cout << "Please name player " << (i + 1) << ": ";
  204. cin >> name;
  205. players[i].setName(name);
  206. }
  207. }
  208. void playersToEnterGame() { //should roll dice per player >= thousand
  209. int points = 0;
  210. for (int i = 0; i < playerCount; i++) {
  211. points = 0;
  212. while (points < 1000) {
  213. dice.diceRoll();
  214. dice.showDice();
  215. points = dice.pointsPerRound();
  216. cout << "\t Score: " << points << endl;
  217. }
  218. players[i].addPoints(points);
  219. cout << players[i].getName() << " want's to throw down some dice!!!" << endl << endl;
  220. }
  221. dice.setNumRoll(0);
  222. cout << "Everyone here? Then good luck ;)" << endl << endl;
  223. }
  224. bool hasWonFarkle(int* index) { //once player reached >= ten thousand, acquire winner index
  225. for (int i = 0; i < playerCount; i++) {
  226. if (players[i].getTotalPoints() >= 10000) {
  227. *index = i;
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. void gamePerPlayer(int i) {//rounds per player
  234. char wantsReroll;
  235. int points = 0;
  236. cout << players[i].getName() << " Throws down: " << endl;
  237. dice.diceRoll();
  238. dice.showDice();
  239. points = dice.pointsPerRound();
  240. cout << "\t Accumulated points: " << points << endl;
  241. players[i].setPoints(points);
  242.  
  243. if (dice.getNumRoll() == 0) {
  244. cout << "You got some points! If you're feeling lucky, play another round." << endl;
  245. players[i].addPoints(points);
  246. dice.anotherDiceRoll();
  247. dice.showDice();
  248. points += dice.pointsPerRound();
  249. cout << "\t Accumulated points: " << points << endl << endl;
  250. players[i].setPoints(points);
  251. }
  252. else if (dice.getNumRoll() != 6) {
  253. cout << endl << "Would you like to reroll the " << dice.getNumRoll()
  254. << " non-point dice? (Y/N): ";
  255. cin >> wantsReroll;
  256. cout << endl;
  257. if (wantsReroll == 'y' || wantsReroll == 'Y') {
  258. dice.anotherDiceRoll();
  259. dice.showDice();
  260. points = dice.pointsPerRound();
  261. cout << "\t Accumulated points: " << points << endl << endl;
  262. if (players[i].getPointsAccrued() > points)
  263. points = players[i].getPointsAccrued();
  264. players[i].setPoints(points);
  265. }
  266. else if (wantsReroll == 'n' || wantsReroll == 'N') {
  267. cout << players[i].getName() << " got no throws left!" << endl << endl;
  268. dice.setNumRoll(0);
  269. }
  270. }
  271. dice.valueOfReroll(); //Sometimes acts up via debugger?
  272. cout << players[i].getName() << " points from round: " << players[i].getPointsAccrued() << endl;
  273. players[i].addPoints(points);
  274. cout << "Total points after round: " << players[i].getTotalPoints() << endl << endl;
  275. cout << endl << "--------------------------------------------" << endl << endl;
  276. }
  277. void playEachRound(int index) {
  278. for (int i = index; i < playerCount; i++) {
  279. gamePerPlayer(i);
  280. }
  281. }
  282. void playTheGame() { //Winner winner chicken chinner
  283. int index = 0;
  284. while (!hasWonFarkle(&index)) {
  285. playEachRound(0);
  286. }
  287. int i = 0;
  288. int count = 0;
  289.  
  290. for (i = index + 1; i < playerCount; i++) {
  291. gamePerPlayer(i);
  292. count++;
  293. }
  294. if (count != playerCount - 1) {
  295. for (i = 0; i < index; i++)
  296. gamePerPlayer(i);
  297. }
  298. int max = 0;
  299. for (int i = 0; i < playerCount; i++) {
  300. if (players[i].getTotalPoints() > max)
  301. {
  302. max = players[i].getTotalPoints();
  303. index = i;
  304. }
  305. }
  306. cout << endl << players[index].getName() << " wins the game!" << endl;
  307. }
  308. };
  309. void showRules() //gathering and importing the read .txtfiles are still giving issues
  310. //needed help from online sources to cross reference and take your notes into consideration
  311. {
  312. string line_;
  313. ifstream file_;
  314. file_.open("FarklinRules.txt"); //located in same directories as 6-3 Final BB4
  315.  
  316. while (getline(file_, line_)) { // I assume I have it in the correct place because,
  317. cout << line_ << '\n'; //feedback received: "This one was a challenge and I understand there were issues."
  318. } //... was the only response I got for my questions/concerns
  319. file_.close();
  320. }
  321. int main(string* players[]) {
  322. srand(time(0));
  323. int playerAmt;
  324. showRules();
  325. cout << endl << "__________________________________________________" << endl << endl;
  326. cout << endl << "\t\t---------- OK Game on!!! ----------" << endl << endl;
  327. //get numPlayers from user
  328. cout << endl << endl << "How many Farkin people wanna play? ";
  329. cin >> playerAmt;
  330.  
  331. while (playerAmt < 2) {
  332. cin.clear();
  333. cin.ignore();
  334. cout << "Enter an integer" << endl;
  335. cin >> playerAmt;
  336. }
  337. WellThatsFarkle fg(playerAmt); //calls methods
  338. fg.readPlayerNames();
  339. fg.playersToEnterGame();
  340. fg.playTheGame();
  341. system("Hold up!");
  342. return 0;
  343. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
2 2 D Munny 2 2 2 2
compilation info
prog.cpp:47:2: error: ‘void Dice::valueOfReroll()’ conflicts with a previous declaration
  }
  ^
prog.cpp:14:22: note: previous declaration ‘bool Dice::valueOfReroll [6]’
  bool valueOfReroll[6];
                      ^
prog.cpp: In member function ‘void WellThatsFarkle::gamePerPlayer(int)’:
prog.cpp:271:8: error: ‘bool Dice::valueOfReroll [6]’ is private within this context
   dice.valueOfReroll();  //Sometimes acts up via debugger?
        ^~~~~~~~~~~~~
prog.cpp:14:22: note: declared private here
  bool valueOfReroll[6];
                      ^
prog.cpp:271:22: error: expression cannot be used as a function
   dice.valueOfReroll();  //Sometimes acts up via debugger?
                      ^
prog.cpp: At global scope:
prog.cpp:321:5: warning: first argument of ‘int main(std::__cxx11::string**)’ should be ‘int’ [-Wmain]
 int main(string* players[]) {
     ^~~~
prog.cpp:321:5: warning: ‘int main(std::__cxx11::string**)’ takes only zero or two arguments [-Wmain]
prog.cpp: In function ‘int main(std::__cxx11::string**)’:
prog.cpp:341:8: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result [-Wunused-result]
  system("Hold up!");
  ~~~~~~^~~~~~~~~~~~
stdout
Standard output is empty