Number reversal game: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,149: | Line 1,149: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
===Version 1 (crude)=== |
|||
The C code can be used with C++, although the following uses proper C++ iostreams: |
The C code can be used with C++, although the following uses proper C++ iostreams: |
||
<syntaxhighlight lang="cpp">void number_reversal_game() |
<syntaxhighlight lang="cpp">void number_reversal_game() |
||
Line 1,187: | Line 1,188: | ||
This uses the same helper functions as the C version. |
This uses the same helper functions as the C version. |
||
=== |
===Version 2 (with C++ standard library)=== |
||
====Version 2.1==== |
|||
This version uses the C++ standard library (note that none of the C helper functions are needed). |
This version uses the C++ standard library (note that none of the C helper functions are needed). |
||
<syntaxhighlight lang="cpp"> |
<syntaxhighlight lang="cpp"> |
||
Line 1,239: | Line 1,241: | ||
<< "You needed " << score << " reversals." << std::endl; |
<< "You needed " << score << " reversals." << std::endl; |
||
return 0; |
return 0; |
||
} |
|||
</syntaxhighlight> |
|||
====Version 2.2==== |
|||
<syntaxhighlight lang="cpp"> |
|||
// Written by Katsumi -- twitter.com/realKatsumi_vn |
|||
// Compile with: g++ -std=c++20 -Wall -Wextra -pedantic NumberReversal.cpp -o NumberReversal |
|||
#include <iostream> |
|||
#include <algorithm> |
|||
#include <utility> |
|||
#include <functional> |
|||
#include <iterator> |
|||
#include <random> |
|||
#include <vector> |
|||
#include <string> |
|||
template <class T> |
|||
bool Sorted(std::vector<T> list) { |
|||
return std::adjacent_find(list.begin(), list.end(), std::greater<T>()) == list.end(); |
|||
} |
|||
template <class T> |
|||
std::string VectorRepr(std::vector<T> list) { |
|||
auto Separate = [](std::string a, int b) { |
|||
return std::move(a) + ", " + std::to_string(b); |
|||
}; |
|||
return std::accumulate(std::next(list.begin()), list.end(), std::to_string(list[0]), Separate); |
|||
} |
|||
int main() { |
|||
const std::string IntroText = "NUMBER REVERSAL GAME\n" |
|||
"based on a \"task\" on Rosetta Code -- rosettacode.org\n" |
|||
"by Katsumi -- twitter.com/realKatsumi.vn\n\n"; |
|||
// Don't ever write this s**tty code... |
|||
// std::srand(std::time(0)); |
|||
// Do this instead: |
|||
std::random_device Device; |
|||
std::mt19937_64 Generator(Device()); |
|||
std::vector<int> List = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
|||
std::shuffle(List.begin(), List.end(), Generator); |
|||
std::cout << IntroText; |
|||
int Moves, PlayerInput; |
|||
while (!Sorted(List)) { |
|||
std::cout << "Current list: [" << VectorRepr(List) << "]\n" |
|||
"Digits to reverse? (2-9) "; |
|||
while (true) { |
|||
std::cin >> PlayerInput; |
|||
if (PlayerInput < 2 || PlayerInput > 9) |
|||
std::cout << "Please enter a value between 2 and 9.\n" |
|||
"Digits to reverse? (2-9) "; |
|||
else |
|||
break; |
|||
} |
|||
std::reverse(List.begin(), List.begin()+PlayerInput); |
|||
++Moves; |
|||
} |
|||
std::cout << "Yay! You sorted the list! You've made " << Moves << " moves.\n"; |
|||
return 0; |
|||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |