Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1.  
  2. #include <ctime>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstddef>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void shuffle(string input [], string output [], int length) {
  12. for(int i = 0; i < length; i++) {
  13. int target = rand() % length;
  14. while(output[target] == "") {
  15. target++;
  16. if(target == length) {
  17. target = 0;
  18. }
  19. }
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. // Seed the Random Generator
  26. srand( time( NULL ) );
  27. string raw;
  28. cout << "v0.0.3" << endl;
  29. cout << "Enter the amount of pairs to play with > ";
  30. cin >> raw;
  31. int wordCount = atoi(raw.c_str());
  32.  
  33. // The data to use, can be any amount. At least the value of word (int) as length.
  34. string wordBase [20] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"};
  35. string *tmpArr = new string[wordCount * 2];
  36. int i;
  37. for(i = 0; i < wordCount; i++) {
  38. tmpArr[i] = wordBase[i];
  39. tmpArr[wordCount + i] = wordBase[i];
  40. }
  41. string *wordsArr = new string[wordCount * 2];
  42. shuffle(tmpArr,wordsArr, wordCount * 2);
  43.  
  44. // Amount of pairs found.
  45. int found = 0;
  46. // Current Player
  47. int player = 1;
  48. // Scores
  49. int player_one = 0;
  50. int player_two = 0;
  51. while(found < wordCount) {
  52. string raw;
  53. // Load and parse input for first 'card'
  54. cout << "Player " << player << "'s turn." << endl;
  55. int pick_one = -1;
  56. string res_one = "";
  57. while(pick_one < 0 || pick_one >= wordCount * 2 || res_one == "") {
  58. // Input
  59. cout << "Enter first ID > ";
  60. cin >> raw;
  61. // Convert to int
  62. pick_one = atoi(raw.c_str());
  63. // Check validity.
  64. if(pick_one < 0 || pick_one >= wordCount * 2) {
  65. cout << "Index cannot be processed. (Below 0 or too high)" << endl;
  66. } else {
  67. // Grab the 'card' from the array
  68. res_one = wordsArr[pick_one];
  69. if(res_one == "") {
  70. cout << "Card #" << pick_one << " was already taken." << endl;
  71. }
  72. }
  73. }
  74. cout << "#" << pick_one << ": " + res_one << endl;
  75.  
  76. int pick_two = -1;
  77. string res_two = "";
  78. // Load and parse input for second 'card'
  79. while(pick_two == pick_one || pick_two < 0 || pick_two >= wordCount * 2 || res_two == "") {
  80. // Input
  81. cout << "Enter second ID > ";
  82. cin >> raw;
  83. // Convert to int
  84. pick_two = atoi(raw.c_str());
  85. // Check validity.
  86. if(pick_two < 0 || pick_two >= wordCount * 2) {
  87. cout << "Index cannot be processed. (Below 0 or too high)" << endl;
  88. } else if(pick_two == pick_one) {
  89. cout << "Please enter two DIFFERENT numbers." << endl;
  90. } else {
  91. // Grab the 'card' from the array
  92. res_two = wordsArr[pick_two];
  93. if(res_two == "") {
  94. cout << "Card #" << pick_two << " was already taken." << endl;
  95. }
  96. }
  97. }
  98. cout << "#" << pick_two << ": " + res_two << endl;
  99.  
  100. // It's a pair
  101. if(res_one == res_two) {
  102. cout << "Congratulations, pair found!" << endl;
  103. // Increase score
  104. if(player == 1) {
  105. player_one++;
  106. } else {
  107. player_two++;
  108. }
  109. found++;
  110. // Remove from 'table'
  111. wordsArr[pick_one] = *"";
  112. wordsArr[pick_two] = *"";
  113. }
  114. player = (player == 1 ? 2 : 1);
  115. }
  116. cout << "ALL PAIRS FOUND!" << endl << "Player 1 Score: " << player_one << endl << "Player 2 Score: " << player_two << endl;
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement