Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. int losowanie (int piles[], int pilesAmount);
  7. void wyswietl(int piles[], int pilesAmount);
  8. bool isEmpty(int piles[], int pilesAmount);
  9.  
  10. int main(int argc, char** argv) {
  11. srand(time(NULL));
  12.  
  13. int pilesAmount = rand() % 6 + 3;
  14. int piles[pilesAmount];
  15. losowanie(piles, pilesAmount);
  16. int player = 1;
  17.  
  18. while(true)
  19. {
  20. wyswietl(piles, pilesAmount);
  21. cout << "PLAYER'S " << player << " TURN." << endl;
  22.  
  23. int pile, pawnAmount;
  24. cin >> pile;
  25. cin >> pawnAmount;
  26.  
  27. if(pawnAmount > piles[pile - 1] || pawnAmount <= 0)
  28. {
  29. cout << "Invalid value of pawns." << endl << endl;
  30. }
  31. if(pile > pilesAmount || pile <= 0)
  32. {
  33. cout << "Invalid value of piles." << endl << endl;
  34. continue;
  35. }
  36. if(pawnAmount > piles[pile - 1] || pawnAmount <= 0)
  37. continue;
  38.  
  39. piles[pile - 1] -= pawnAmount;
  40.  
  41.  
  42. switch(player)
  43. {
  44. case 1:
  45. player = 2;
  46. break;
  47. case 2:
  48. player = 1;
  49. break;
  50. }
  51.  
  52. if(isEmpty(piles, pilesAmount))
  53. break;
  54. }
  55. cout << endl << endl << "####################################### " << "PLAYER " << player << " WINS!" << " #######################################";
  56. return 0;
  57. }
  58.  
  59. int losowanie (int piles[], int pilesAmount)
  60. {
  61. for(int i = 0; i < pilesAmount; i++)
  62. {
  63. int pawns;
  64. bool shuffle = true;
  65. while(shuffle){
  66. shuffle = false;
  67. pawns = rand() % 26 + 4;
  68. for(int j = 0; j < i; j++)
  69. {
  70. if(pawns == piles[j])
  71. {
  72. shuffle = true;
  73. break;
  74. }
  75. }
  76. }
  77. piles[i] = pawns;
  78. }
  79.  
  80. }
  81.  
  82.  
  83. void wyswietl(int piles[], int pilesAmount)
  84. {
  85. for(int i = 1; i <= pilesAmount; i++)
  86. {
  87. cout << i << " : " << piles[i - 1] << endl;
  88. }
  89. cout << endl;
  90. }
  91.  
  92. bool isEmpty(int piles[], int pilesAmount)
  93. {
  94. for(int i = 0; i < pilesAmount; i++)
  95. if(piles[i] != 0)
  96. return false;
  97. return true;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement