I'm trying to learn C++ with a background in python. This is my first program besides general practice. I wanted to remake the war game. I'm looking to get feedback on the C++ equivalent of its pythonicness. Some mistakes I believe I've made are possibly misusing the std functions, unnecessary type transformations, and unnecessary duplications of data, but I'm not sure where to begin.
For an explanation of the code, I didn't initialize cards themselves, I just used a 1- 52 range to signify cards.
To distribute the cards, I just thought it would be fun to try and use a random binary with a bitlength of 52 and distribute the cards from that. I was also hesitant on the while(true)
loop, I wanted a different way to do it but it works as intended.
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <set>
using namespace std;
int randNum(int min, int max) {
random_device r;
default_random_engine e1(r());
uniform_int_distribution<int> uniform_dist(min, max);
int mean = uniform_dist(e1);
return mean;
}
string toBinary(int n) {
string r;
while(n != 0) {
r = (n % 2 == 0 ? "0" : "1") + r;
n /= 2;
}
return r;
}
string deckShuffleSeed() {
string deck;
// Decimals to make a binary w/ bit length of 26
// /2 # of cards in deck, therefore: consolidate 2, so we can use ints
deck = toBinary(randNum(33554432, 67108863)) +
toBinary(randNum(33554432, 67108863));
return deck;
}
int randSetChoice(const set<int> &s) {
auto ItRandChoice = std::next(s.begin(), randNum(0, int(s.size())));
return *ItRandChoice;
}
string war() {
// hand initialization
set<int> p1, p2;
string deckDealingSeed = deckShuffleSeed();
for (int i = 1; i <= 52; i++) {
int convSeedIndex = deckDealingSeed[i] - '0';
if (convSeedIndex == 1) {
p2.insert(i);
} else {
p1.insert(i);
}
}
// game loop
while (!p1.empty() || !p2.empty()) {
int recursiveWarCount = 0;
set<int> cardsOnTable;
while(true) { // recursive war loop
if (p1.size() > 3 * recursiveWarCount && p2.size() > 3 * recursiveWarCount) {
int p1RandChoice = randSetChoice(p1), p2RandChoice = randSetChoice(p2);
// round loop
if (p1RandChoice / 4 > p2RandChoice / 4) { // p1 wins
p1.insert(p2RandChoice);
p2.erase(p2RandChoice);
if (recursiveWarCount > 0) {
p1.insert(cardsOnTable.begin(), cardsOnTable.end());
for (int i = 1; i < recursiveWarCount * 3; i++) {
int randTableChoice = randSetChoice(p2);
p1.insert(randTableChoice);
p2.erase(randTableChoice);
}
}
break;
} else if (p1RandChoice / 4 < p2RandChoice / 4) { // p2 wins
p2.insert(p1RandChoice);
p1.erase(p1RandChoice);
if (recursiveWarCount > 0) {
p2.insert(cardsOnTable.begin(), cardsOnTable.end());
for (int i = 1; i < recursiveWarCount * 3; i++) {
int randTableChoice = randSetChoice(p1);
p2.insert(randTableChoice);
p1.erase(randTableChoice);
}
}
break;
} else { // war
recursiveWarCount += 1;
cardsOnTable.insert(p1RandChoice);
cardsOnTable.insert(p2RandChoice);
}
} else {
if (p1.size() > p2.size()) {
return "p1";
} else {
return "p2";
}
}
}
}
if (p1.empty()) {
return "p2";
} else if(p2.empty()) {
return "p2";
} else {
return "Something is wrong!";
}
}
int main() {
cout << war() << endl;
return 1;
}