Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- int main() {
- string gameLine, command;
- getline(cin, gameLine);
- vector<string> game;
- for (int i = 0; i < gameLine.length(); i += 2) {
- game.push_back("");
- game[game.size() - 1] += gameLine[i];
- }
- int counter = 1;
- cin >> command;
- while (command != "end") {
- int index1 = stoi(command), index2 = stoi(command);
- cin >> command;
- if (index1 > stoi(command)) {
- index1 = stoi(command);
- }
- else {
- index2 = stoi(command);
- }
- if (index1 >= 0 && index1 < game.size() && index2 >= 0 && index2 < game.size() && index1 != index2) {
- if (game[index1] == game[index2]) {
- cout << "Congrats! You have found matching elements - " << game[index1] << "!\n";
- game.erase(game.begin() + index2);
- game.erase(game.begin() + index1);
- if (game.size() == 0) {
- cout << "You have won in " << counter << " turns!\n";
- return 0;
- }
- }
- else {
- cout << "Try again!\n";
- }
- }
- else {
- cout << "Invalid input! Adding additional elements to the board\n";
- string elementValue = '-' + to_string(counter) + 'a';
- game.insert(game.begin() + game.size() / 2, { elementValue, elementValue});
- }
- counter++;
- cin >> command;
- }
- cout << "Sorry you lose :(\n";
- for (auto& s : game) {
- cout << s << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement