Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <cstddef>
- using namespace std;
- void shuffle(string input [], string output [], int length) {
- for(int i = 0; i < length; i++) {
- int target = rand() % length;
- while(output[target] == "") {
- target++;
- if(target == length) {
- target = 0;
- }
- }
- }
- }
- int main()
- {
- // Seed the Random Generator
- srand( time( NULL ) );
- string raw;
- cout << "v0.0.3" << endl;
- cout << "Enter the amount of pairs to play with > ";
- cin >> raw;
- int wordCount = atoi(raw.c_str());
- // The data to use, can be any amount. At least the value of word (int) as length.
- string wordBase [20] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"};
- string *tmpArr = new string[wordCount * 2];
- int i;
- for(i = 0; i < wordCount; i++) {
- tmpArr[i] = wordBase[i];
- tmpArr[wordCount + i] = wordBase[i];
- }
- string *wordsArr = new string[wordCount * 2];
- shuffle(tmpArr,wordsArr, wordCount * 2);
- // Amount of pairs found.
- int found = 0;
- // Current Player
- int player = 1;
- // Scores
- int player_one = 0;
- int player_two = 0;
- while(found < wordCount) {
- string raw;
- // Load and parse input for first 'card'
- cout << "Player " << player << "'s turn." << endl;
- int pick_one = -1;
- string res_one = "";
- while(pick_one < 0 || pick_one >= wordCount * 2 || res_one == "") {
- // Input
- cout << "Enter first ID > ";
- cin >> raw;
- // Convert to int
- pick_one = atoi(raw.c_str());
- // Check validity.
- if(pick_one < 0 || pick_one >= wordCount * 2) {
- cout << "Index cannot be processed. (Below 0 or too high)" << endl;
- } else {
- // Grab the 'card' from the array
- res_one = wordsArr[pick_one];
- if(res_one == "") {
- cout << "Card #" << pick_one << " was already taken." << endl;
- }
- }
- }
- cout << "#" << pick_one << ": " + res_one << endl;
- int pick_two = -1;
- string res_two = "";
- // Load and parse input for second 'card'
- while(pick_two == pick_one || pick_two < 0 || pick_two >= wordCount * 2 || res_two == "") {
- // Input
- cout << "Enter second ID > ";
- cin >> raw;
- // Convert to int
- pick_two = atoi(raw.c_str());
- // Check validity.
- if(pick_two < 0 || pick_two >= wordCount * 2) {
- cout << "Index cannot be processed. (Below 0 or too high)" << endl;
- } else if(pick_two == pick_one) {
- cout << "Please enter two DIFFERENT numbers." << endl;
- } else {
- // Grab the 'card' from the array
- res_two = wordsArr[pick_two];
- if(res_two == "") {
- cout << "Card #" << pick_two << " was already taken." << endl;
- }
- }
- }
- cout << "#" << pick_two << ": " + res_two << endl;
- // It's a pair
- if(res_one == res_two) {
- cout << "Congratulations, pair found!" << endl;
- // Increase score
- if(player == 1) {
- player_one++;
- } else {
- player_two++;
- }
- found++;
- // Remove from 'table'
- wordsArr[pick_one] = *"";
- wordsArr[pick_two] = *"";
- }
- player = (player == 1 ? 2 : 1);
- }
- cout << "ALL PAIRS FOUND!" << endl << "Player 1 Score: " << player_one << endl << "Player 2 Score: " << player_two << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement