Napisałem kolejny program, który tym razem gra w dość głupiego i prostego pasjansa. Zgodnie z sugestiami forumowiczów zmienne i funkcje mają angielskie nazwy. Funkcje pozamykałem w klasę. Ale nie wiem czy to dobrze wyszło. Proszę o konstruktywną krytykę.
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine gen(seed);
class Pasjans {
std::vector<int> deck;
std::vector<int> table[2];
int howmanycards{1};
uint64_t successTOTAL{1};
uint64_t failureTOTAL{1};
double successratio{0};
int flag{0};
int Lays{0};
void ToggleFlag(int &flag) {
flag = !flag;
}
void ShuffleDeck(std::vector<int> &deck, int howmanycards) {
int howmanyfigures = howmanycards / 4;
for (int i = 0; i < howmanycards; i++) deck.push_back(i % howmanyfigures);
std::shuffle(deck.begin(), deck.end(), gen);
}
void LayDownCard(std::vector<int> &deck, std::vector<int> table[], int flag) {
table[flag].push_back(*(deck.end() - 1));
deck.pop_back();
}
void ShiftCard(std::vector<int> table[], int flag) {
table[flag].push_back(*(table[!flag].end() - 1));
table[!flag].pop_back();
CompareCards(table, flag);
ToggleFlag(flag);
}
void TakeTwoCards(std::vector<int> table[], int flag) {
table[flag].pop_back();
table[flag].pop_back();
ShiftCard(table, flag);
}
void CompareCards(std::vector<int> table[], int flag) {
if (table[flag].size() >= 2)
if (*(table[flag].end() - 1) == *(table[flag].end() - 2)) {
TakeTwoCards(table, flag);
}
}
bool CollectCards(std::vector<int> table[], std::vector<int> &deck) {
if (table[0].size() > 1) {
for (unsigned int i = 0; i < table[0].size(); i++) deck.push_back(table[0][i]);
table[0].clear();
for (unsigned int i = 0; i < table[1].size(); i++) deck.push_back(table[1][i]);
table[1].clear();
return true;
} else return false;
}
public:
void Play() {
std::cout << std::fixed;
while (howmanycards % 4) {
std::cout << "Podaj liczbe kart podzielna przez 4:\n";
std::cin >> howmanycards;
}
while (std::cin.get() != '\n') continue;
for (int rounds = 0; rounds < 10000000; rounds++) {
ShuffleDeck(deck, howmanycards);
do {
while (!deck.empty()) {
LayDownCard(deck, table, flag);
CompareCards(table, flag);
ToggleFlag(flag);
}
Lays++;
} while ((CollectCards(table, deck)) && (Lays < howmanycards));
if (table[0].size() == 1) successTOTAL++; else failureTOTAL++;
successratio = (double) successTOTAL / (double) failureTOTAL;
if (!(rounds % (4000000 / howmanycards)))
std::cout << "Success ratio: " << successratio << " Rounds: " << rounds << "\n";
table[0].clear();
table[1].clear();
deck.clear();
Lays = 0;
}
}
};
int main() {
Pasjans obiekt;
obiekt.Play();
return 0;
}