Advertisement
Guest User

Cube Game

a guest
Jan 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int choice;
  9. int loop;
  10. int remaining;
  11. int take_Amount;
  12. int randomized_Amount;
  13. int count;
  14. string firstplayer_Name;
  15. string secondplayer_Name;
  16. string play_Again;
  17. //ensures program repeats
  18. loop = 1;
  19. while(loop == 1){
  20.  
  21. //What would you like to do
  22. cout<<"Cube game"<<endl;
  23. cout<<"1) PLayer vs Player "<<endl;
  24. cout<<"2) Player vs Computer"<<endl;
  25. cout<<"3) Rules of Game"<<endl;
  26.  
  27. //choose where to go
  28. cout<<"Enter the number 1, 2, or 3: ";
  29. cin>>choice;
  30.  
  31. //Code run for player v player
  32. if(choice==1){
  33.  
  34. count = 1;
  35.  
  36. remaining = 21;
  37.  
  38. //Asks players for names
  39. cout<<"Enter user 1 name: ";
  40. getline(cin, firstplayer_Name);
  41. getline(cin, firstplayer_Name);
  42.  
  43.  
  44. cout<<"Enter user 2 name: ";
  45. getline(cin, secondplayer_Name);
  46.  
  47.  
  48.  
  49. cout<<"Number of cubes = "<< remaining <<endl;
  50.  
  51. //It is decided randomly who get sto go first
  52. randomized_Amount = (rand() % 4) + 1;
  53.  
  54. //Player 1 goes first
  55. if(randomized_Amount== 1 or randomized_Amount == 3){
  56.  
  57. //loop
  58. while(count == 1){
  59. //User 1 inputs number of cubes to take out
  60. //Loop ends when valid input is entered
  61. do{
  62. cout<< firstplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
  63. cin>>take_Amount;
  64. if(take_Amount < 1 or take_Amount > 3 ){
  65. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  66. }
  67. }while(take_Amount < 1 or take_Amount > 3 );
  68.  
  69. //Subtracts number of cubes by the number of cubes taken out
  70. remaining -=take_Amount;
  71. cout<<take_Amount<<endl;
  72.  
  73. //If number of cubes is 1 then player 1 wins
  74. if(remaining == 1){
  75. cout<< firstplayer_Name <<" wins"<<endl;
  76. break;
  77. }
  78.  
  79. //If number of cubes is 0 or less than 0 then player 2 wins
  80. if(remaining <= 0){
  81. cout<< secondplayer_Name <<" wins"<<endl;
  82. break;
  83. }
  84.  
  85. //Player 2 inputs number of cubes to take out
  86. //Loop ends when valid input is entered
  87. do{
  88. cout<< secondplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
  89. cin>> take_Amount;
  90. if(take_Amount < 1 or take_Amount > 3 ){
  91. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  92. }
  93. }while(take_Amount < 1 or take_Amount > 3 );
  94.  
  95. remaining -= take_Amount;
  96. cout<< remaining <<endl;
  97.  
  98. //If number of cubes is 1 then user 2 wins
  99. if(remaining == 1){
  100. cout<< secondplayer_Name <<" wins"<<endl;
  101. break;
  102. }
  103.  
  104. //If number of cubes is 0 or less than 0 then player 1 wins
  105. if(remaining <= 0){
  106. cout<< firstplayer_Name <<" wins"<<endl;
  107. break;
  108. }
  109.  
  110. }
  111.  
  112. //Gives option of playing another round
  113. //Loop ends when vaid input is entered
  114. do{
  115. cout<<"Do you want to play another round (yes or no): ";
  116. cin>> play_Again;
  117. if( play_Again != "yes" and play_Again != "Yes" and play_Again!= "No" and play_Again != "no"){
  118. cout<<"Invalid input since you did not enter yes or no";
  119. }
  120. }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
  121.  
  122. //Goes back to start of loop if yes
  123. if(play_Again == "yes" or play_Again == "Yes"){
  124. continue;
  125. }
  126.  
  127. //If yes the while loop at program keeps going so programs starts from top
  128. //If no, loop variable is set to 2 which ends while loop
  129. if(play_Again == "no" or play_Again == "No"){
  130. loop++;
  131. }
  132. }
  133.  
  134. //Code for if random_start = 2 or 4
  135. //User 2 goes first
  136. if(randomized_Amount == 2 or randomized_Amount == 4){
  137.  
  138. while(count == 1){
  139.  
  140. //User 2 inputs number of cubes to take out
  141. //Loop ends when valid input is entered
  142. do{
  143. cout<< secondplayer_Name <<" enter number of cubes you want to take out from 1-3: ";
  144. cin>>take_Amount;
  145. if(take_Amount < 1 or take_Amount> 3 ){
  146. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  147. }
  148. }while(take_Amount< 1 or take_Amount > 3 );
  149.  
  150. remaining -= take_Amount;
  151. cout<<remaining<<endl;
  152.  
  153. //If number of cubes is 1 then player 2 wins
  154. if( remaining == 1){
  155. cout<< secondplayer_Name <<" wins"<<endl;
  156. break;
  157. }
  158.  
  159. //If number of cubes is 0 or less than 0 then user 1 wins
  160. if( remaining <= 0){
  161. cout<< firstplayer_Name <<" wins"<<endl;
  162. break;
  163. }
  164.  
  165. //User 1 inputs number of cubes to take out
  166. //Loop ends when valid input is entered
  167. do{
  168. cout<< firstplayer_Name <<" enter number of cubes you want to take out(1-3): ";
  169. cin>> take_Amount;
  170. if( take_Amount < 1 or take_Amount > 3 ){
  171. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  172. }
  173. }while(take_Amount < 1 or take_Amount > 3 );
  174.  
  175. remaining -= take_Amount;
  176. cout<<remaining<<endl;
  177.  
  178. //If number of cubes is 1 then player 1 wins
  179. if(remaining == 1){
  180. cout<< firstplayer_Name <<" wins"<<endl;
  181. break;
  182. }
  183.  
  184. //If number of cubes is 0 or less than 0 then player 2 wins
  185. if( remaining <= 0){
  186. cout<< secondplayer_Name <<" wins"<<endl;
  187. break;
  188. }
  189.  
  190. }
  191.  
  192. do{
  193. cout<<"Do you want to play another round (yes or no): ";
  194. cin>>play_Again;
  195. if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
  196. cout<<"Invalid input since you did not enter yes or no";
  197. }
  198. }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
  199.  
  200. if(play_Again == "yes" or play_Again == "Yes"){
  201. continue;
  202. }
  203.  
  204. if(play_Again == "no" or play_Again == "No"){
  205. loop++;
  206. }
  207.  
  208. }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. //Code run for player v computer
  215. if(choice==2){
  216. count = 1;
  217. remaining = 21;
  218. cout<<"Enter player 1 name: ";
  219. getline(cin, firstplayer_Name);//The first get line command doesnt work so this is important
  220. getline(cin, firstplayer_Name);
  221.  
  222.  
  223. cout<<"Number of cubes = "<<remaining<<endl;
  224.  
  225. randomized_Amount = (rand() % 4) + 1;;
  226.  
  227. //Code for if random start = 1 or 3
  228. //Player 1 goes first
  229. if(randomized_Amount == 1 or randomized_Amount == 3){
  230.  
  231. while(count == 1){
  232.  
  233. //Loop ends when valid input is entered
  234. do{
  235. cout<<firstplayer_Name<<" enter number of cubes you want to take out(1-3): ";
  236. cin>>take_Amount;
  237. if(take_Amount< 1 or take_Amount > 3 ){
  238. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  239. }
  240. }while(take_Amount < 1 or take_Amount > 3 );
  241.  
  242. remaining -= take_Amount;
  243. cout<<remaining<<endl;
  244.  
  245. if(remaining == 1){
  246. cout<<firstplayer_Name<<" wins"<<endl;
  247. break;
  248. }
  249.  
  250. //If number of cubes is 0 or less than 0 then computer wins
  251. if(remaining <= 0){
  252. cout<<"Computer wins"<<endl;
  253. break;
  254. }
  255.  
  256. //Code for computer
  257. //Computer takes out the certain amount cubes so that the two turns equals 4
  258. take_Amount = 4 - take_Amount;
  259. remaining -= take_Amount;
  260. cout<<"Computer takes out "<< take_Amount <<" cubes."<<endl;
  261. cout<< remaining <<endl;
  262.  
  263. //If number of cubes is 1 then computer wins
  264. if(remaining == 1){
  265. cout<<"Computer wins"<<endl;
  266. break;
  267. }
  268.  
  269. //If number of cubes is 0 or less than 0 then user 1 wins
  270. if(remaining <= 0){
  271. cout<<firstplayer_Name<<" wins"<<endl;
  272. break;
  273. }
  274.  
  275. }
  276.  
  277. do{
  278. cout<<"Do you want to play another round(yes or no): ";
  279. cin>> play_Again ;
  280. if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
  281. cout<<"Invalid input since you did not enter yes or no";
  282. }
  283. }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
  284.  
  285. if(play_Again == "yes" or play_Again == "Yes"){
  286. continue;
  287. }
  288.  
  289. if(play_Again == "no" or play_Again == "No"){
  290. loop++;
  291. }
  292. }
  293.  
  294.  
  295.  
  296. //Code for if random start = 2 or 4
  297. //Computer goes first
  298. if(randomized_Amount == 2 or randomized_Amount == 4){
  299.  
  300. while(count == 1){
  301.  
  302. //Number of sticks is over 4 then computer takes out a random number between 1 and 3
  303. if(remaining>4){
  304. take_Amount = (rand() % 3) + 1;
  305. }
  306.  
  307. //Number of sticks is equal to 4 then it takes out 3
  308. if(remaining==4){
  309. take_Amount=3;
  310. }
  311.  
  312. if(remaining ==3){
  313. take_Amount=2;
  314. }
  315.  
  316. //Number of sticks is equal to 2 then it takes out 1
  317. if(remaining ==2){
  318. take_Amount =1;
  319. }
  320.  
  321. remaining -=take_Amount;
  322. cout<<"Computer takes out "<<take_Amount<<" cubes"<<endl;
  323. cout<< remaining <<endl;
  324.  
  325. //If number of sticks is 1 then computer wins
  326. if(remaining == 1){
  327. cout<<"Computer wins"<<endl;
  328. count = 2;
  329. break;
  330. }
  331.  
  332. //If number of sticks is 0 or less than 0 then Player 1 wins
  333. if(remaining <= 0){
  334. cout<< firstplayer_Name <<" wins"<<endl;
  335. count = 2;
  336. break;
  337. }
  338.  
  339. //User 1 inputs number of sticks to take out
  340. //Loop ends when valid input is entered
  341. do{
  342. cout<< firstplayer_Name <<" enter number of cubes you want to take out(1-3): ";
  343. cin>>take_Amount;
  344. if(take_Amount < 1 or take_Amount > 3 ){
  345. cout<<"Invalid input since it is not inbetween 1-3."<<endl;
  346. }
  347. }while( take_Amount < 1 or take_Amount > 3 );
  348.  
  349. remaining -= take_Amount;
  350. cout<< remaining <<endl;
  351.  
  352. //If number of cubes is 1 then user 1 wins
  353. if(remaining == 1){
  354. cout<< firstplayer_Name <<" wins"<<endl;
  355. count = 2;
  356. break;
  357. }
  358.  
  359. //If number of cubes is 0 or less than 0 then computer wins
  360. if( remaining <= 0){
  361. cout<<"Computer wins"<<endl;
  362. count = 2;
  363. break;
  364. }
  365.  
  366. }
  367.  
  368. do{
  369. cout<<"Do you want to play another round(yes or no): ";
  370. cin>> play_Again;
  371. if(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no"){
  372. cout<<"Invalid input since you did not enter yes or no";
  373. }
  374. }while(play_Again!= "yes" and play_Again != "Yes" and play_Again != "No" and play_Again != "no");
  375.  
  376. if(play_Again == "yes" or play_Again == "Yes"){
  377. continue;
  378. }
  379.  
  380. if(play_Again == "no" or play_Again == "No"){
  381. loop++;
  382. }
  383. }
  384.  
  385. }
  386.  
  387. //Code for rules of game
  388. if(choice==3){
  389. cout<<"Rules of 21 cubes"<<endl;
  390. cout<<"User must take out between 1-3 sticks from the total."<<endl;
  391. cout<<"Last person to take out cubes loses the game. "<<endl;
  392. }
  393.  
  394. else{
  395. cout<<"Invalid input since you did not enter 1, 2, or 3. "<<endl;
  396. }
  397.  
  398. }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement