War card game: Difference between revisions

m
Made code more C++ idiomatic.
m (Altered code to be more C++ idiomatic.)
m (Made code more C++ idiomatic.)
Line 277:
<syntaxhighlight lang="c++">
 
#include <cstdint>
#include <iostream>
#include <vectorstring>
#include <algorithmarray>
#include <iomanip>
 
typedef std::pair<std::string, bool> data;
class war_game {
public:
war_game() {
for ( char suit : SUITS ) {
for ( char pip : PIPS ) {
deck.emplace_back(card(suit, pip));
}
}
std::random_shuffle(deck.begin(), deck.end());
 
const std::array<const std::array<int32_t, 10>, 10> multiplication_table = { {
handA = { deck.begin(), deck.begin() + 26 };
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
handB = { deck.begin() + 26, deck.end() };
{ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
}
{ 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 },
{ 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 },
{ 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 },
{ 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 },
{ 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 },
{ 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 },
{ 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
} };
 
const std::array<int32_t, 10> inverse = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
void next_turn() {
card cardA = handA.front(); handA.erase(handA.begin());
card cardB = handB.front(); handB.erase(handB.begin());
tabledCards.emplace_back(cardA);
tabledCards.emplace_back(cardB);
int32_t rankA = getRank(cardA.pip);
int32_t rankB = getRank(cardB.pip);
std::cout << cardA.pip << cardA.suit << " " << cardB.pip << cardB.suit << std::endl;
 
const std::array<const std::array<int32_t, 10>, 8> permutation_table = { {
if ( rankA > rankB ) {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
std::cout << " Player A takes the cards" << std::endl;
{ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
std::random_shuffle(tabledCards.begin(), tabledCards.end());
{ 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 },
handA.insert(handA.end(), tabledCards.begin(), tabledCards.end());
{ 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 },
tabledCards.clear();
{ 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 },
} else if ( rankA < rankB ) {
{ 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 },
std::cout << " Player B takes the cards" << std::endl;
{ 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
std::random_shuffle(tabledCards.begin(), tabledCards.end());
{ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
handB.insert(handB.end(), tabledCards.begin(), tabledCards.end());;
} };
tabledCards.clear();
} else {
std::cout << " War!" << std::endl;
if ( game_over() ) {
return;
}
 
int32_t verhoeff_checksum(std::string number, const bool doValidation, const bool doDisplay) {
card cardAA = handA.front(); handA.erase(handA.begin());
if ( doDisplay ) {
card cardBB = handB.front(); handB.erase(handB.begin());
std::string calculationType = doValidation ? "Validation" : "Check digit";
tabledCards.emplace_back(cardAA);
std::cout << calculationType << " calculations for " << number << "\n" << std::endl;
tabledCards.emplace_back(cardBB);
std::cout << "? i ?ni p[i, Cardsni] are face downc" << std::endl;
std::cout << "-------------------" << std::endl;
if ( game_over() ) {
return;
}
 
next_turn();
}
}
 
if ( ! doValidation ) {
bool game_over() const {
number += "0";
return handA.size() == 0 || handB.size() == 0;
}
 
int32_t c = 0;
void declare_winner() const {
const int32_t le = number.length() - 1;
if ( handA.size() == 0 && handB.size() == 0 ) {
for ( int32_t i = le; i >= 0; i-- ) {
std::cout << "The game ended in a tie" << std::endl;
}const elseint32_t ifni (= handA.size()number[i] ==- '0 ) {';
const int32_t pi = permutation_table[(le - i) % 8][ni];
std::cout << "Player B has won the game" << std::endl;
c = multiplication_table[c][pi];
} else {
 
std::cout << "Player A has won the game" << std::endl;
if ( doDisplay ) {
std::cout << std::setw(2) << le - i << std::setw(3) << ni
<< std::setw(8) << pi << std::setw(6) << c << "\n" << std::endl;
}
}
private:
class card {
public:
card(const char suit, const char pip) : suit(suit), pip(pip) {};
char suit, pip;
};
 
if ( doDisplay && ! doValidation ) {
int32_t getRank(const char ch) const {
std::cout << "inverse[" << c << "] = " << inverse[c] << "\n" << std::endl;;
auto it = find(PIPS.begin(), PIPS.end(), ch);
if ( it != PIPS.end() ) {
return it - PIPS.begin();
}
return -1;
}
 
return doValidation ? c == 0 : inverse[c];
std::vector<card> deck, handA, handB, tabledCards;
}
inline static const std::vector<char> PIPS = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
inline static const std::vector<char> SUITS = { 'C', 'D', 'H', 'S' };
};
 
int main( ) {
const std::array<data, 3> tests = {
war_game wargame;
std::make_pair("123", true), std::make_pair("12345", true), std::make_pair("123456789012", false) };
 
whilefor ( !const wargame.game_over()data& test : tests ) {
int32_t digit = verhoeff_checksum(test.first, false, test.second);
wargame.next_turn();
std::cout << "The check digit for " << test.first << " is " << digit << "\n" << std::endl;
 
std::string numbers[2] = { test.first + std::to_string(digit), test.first + "9" };
for ( const std::string& number : numbers ) {
digit = verhoeff_checksum(number, true, test.second);
std::string result = ( digit == 1 ) ? "correct" : "incorrect";
std::cout << "The validation for " << number << " is " << result << ".\n" << std::endl;
}
}
 
wargame.declare_winner();
}
</syntaxhighlight>
871

edits