Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 78 additions & 7 deletions dogclass-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ Overloading the << Operator for Your Own Classes (https://learn.microsoft.com/en
#include <stack>
#include <vector>
#include <random>
#include <algorithm> // for std::shuffle

using namespace std;

class War {
private:
int myCurrent;
int otherCurrent;
static constexpr int NO_CARD = -1; // Sentinel that cannot be a real card
vector<int> newdecks;
stack<int> myPlayingPile;
queue<int> myStoragePile;
Expand Down Expand Up @@ -97,7 +99,7 @@ class War {
if (myPlayingPile.empty()) {
move_my_storage();
if (myPlayingPile.empty())
return 0; // Return 0 to indicate no cards left
return NO_CARD; // Explicit sentinel distinct from real cards (0-9)
}
int card = myPlayingPile.top();
myPlayingPile.pop();
Expand All @@ -108,7 +110,7 @@ class War {
if (otherPlayingPile.empty()) {
move_other_storage();
if (otherPlayingPile.empty())
return 0; // Return 0 to indicate no cards left
return NO_CARD; // Explicit sentinel distinct from real cards (0-9)
}
int card = otherPlayingPile.top();
otherPlayingPile.pop();
Expand Down Expand Up @@ -155,20 +157,90 @@ class War {

// Check again after moving storage cards
if (myPlayingPile.size() < 2 || otherPlayingPile.size() < 2) {
cout << "Not enough cards, Game Over!" << endl;
// If either player can't continue the war, award the pot to the player who can.
bool myCanContinue = myPlayingPile.size() >= 2;
bool otherCanContinue = otherPlayingPile.size() >= 2;

if (myCanContinue && !otherCanContinue) {
// Other player loses: award everything to me
myStoragePile.push(myCurrent);
myStoragePile.push(otherCurrent);
while (!lootPile.empty()) { myStoragePile.push(lootPile.front()); lootPile.pop(); }
// Move all remaining other cards to me so game ends next turn
while (!otherPlayingPile.empty()) { myStoragePile.push(otherPlayingPile.top()); otherPlayingPile.pop(); }
while (!otherStoragePile.empty()) { myStoragePile.push(otherStoragePile.front()); otherStoragePile.pop(); }
cout << "Opponent lacks cards for war. I collect the pot and remaining cards." << endl;
} else if (!myCanContinue && otherCanContinue) {
// I lose: award everything to opponent
otherStoragePile.push(myCurrent);
otherStoragePile.push(otherCurrent);
while (!lootPile.empty()) { otherStoragePile.push(lootPile.front()); lootPile.pop(); }
while (!myPlayingPile.empty()) { otherStoragePile.push(myPlayingPile.top()); myPlayingPile.pop(); }
while (!myStoragePile.empty()) { otherStoragePile.push(myStoragePile.front()); myStoragePile.pop(); }
cout << "I lack cards for war. Opponent collects the pot and my remaining cards." << endl;
} else {
// Neither can continue: clear both players so the game ends promptly
while (!lootPile.empty()) lootPile.pop();
while (!myPlayingPile.empty()) myPlayingPile.pop();
while (!otherPlayingPile.empty()) otherPlayingPile.pop();
while (!myStoragePile.empty()) myStoragePile.pop();
while (!otherStoragePile.empty()) otherStoragePile.pop();
cout << "Neither player has enough cards for war. Clearing piles; game will end." << endl;
}
return;
}

lootPile.push(myCurrent);
lootPile.push(otherCurrent);

// Add one hidden card from each player
lootPile.push(remove_my_card());
lootPile.push(remove_other_card());
int hiddenMy = remove_my_card();
int hiddenOther = remove_other_card();
if (hiddenMy == NO_CARD || hiddenOther == NO_CARD) {
// If a player runs out while drawing hidden cards, resolve as above
if (hiddenMy != NO_CARD) myStoragePile.push(hiddenMy);
if (hiddenOther != NO_CARD) otherStoragePile.push(hiddenOther);
cout << "A player ran out of cards during war draw. Resolving." << endl;
if (hiddenMy == NO_CARD && hiddenOther != NO_CARD) {
otherStoragePile.push(myCurrent);
otherStoragePile.push(otherCurrent);
while (!lootPile.empty()) { otherStoragePile.push(lootPile.front()); lootPile.pop(); }
while (!myPlayingPile.empty()) { otherStoragePile.push(myPlayingPile.top()); myPlayingPile.pop(); }
while (!myStoragePile.empty()) { otherStoragePile.push(myStoragePile.front()); myStoragePile.pop(); }
} else if (hiddenOther == NO_CARD && hiddenMy != NO_CARD) {
myStoragePile.push(myCurrent);
myStoragePile.push(otherCurrent);
while (!lootPile.empty()) { myStoragePile.push(lootPile.front()); lootPile.pop(); }
while (!otherPlayingPile.empty()) { myStoragePile.push(otherPlayingPile.top()); otherPlayingPile.pop(); }
while (!otherStoragePile.empty()) { myStoragePile.push(otherStoragePile.front()); otherStoragePile.pop(); }
} else {
while (!lootPile.empty()) lootPile.pop();
}
return;
}
lootPile.push(hiddenMy);
lootPile.push(hiddenOther);

// Face-up war card
myCurrent = remove_my_card();
otherCurrent = remove_other_card();
if (myCurrent == NO_CARD || otherCurrent == NO_CARD) {
cout << "A player ran out of cards during war face-up draw. Resolving." << endl;
if (myCurrent == NO_CARD && otherCurrent != NO_CARD) {
otherStoragePile.push(otherCurrent);
while (!lootPile.empty()) { otherStoragePile.push(lootPile.front()); lootPile.pop(); }
while (!myPlayingPile.empty()) { otherStoragePile.push(myPlayingPile.top()); myPlayingPile.pop(); }
while (!myStoragePile.empty()) { otherStoragePile.push(myStoragePile.front()); myStoragePile.pop(); }
} else if (otherCurrent == NO_CARD && myCurrent != NO_CARD) {
myStoragePile.push(myCurrent);
while (!lootPile.empty()) { myStoragePile.push(lootPile.front()); lootPile.pop(); }
while (!otherPlayingPile.empty()) { myStoragePile.push(otherPlayingPile.top()); otherPlayingPile.pop(); }
while (!otherStoragePile.empty()) { myStoragePile.push(otherStoragePile.front()); otherStoragePile.pop(); }
} else {
while (!lootPile.empty()) lootPile.pop();
}
return;
}

cout << "My card: " << display_card(myCurrent) << " Opponent's card: " << display_card(otherCurrent) << endl;

Expand Down Expand Up @@ -220,8 +292,7 @@ int main() {
game.deal();

while (game.make_move()) {
game.move_my_storage();
game.move_other_storage();
// No explicit moves needed here; make_move handles refills as needed
}

return 0;
Expand Down
Binary file added war
Binary file not shown.