diff --git a/dogclass-main.cpp b/dogclass-main.cpp index d98633c..3abccaf 100644 --- a/dogclass-main.cpp +++ b/dogclass-main.cpp @@ -16,6 +16,7 @@ Overloading the << Operator for Your Own Classes (https://learn.microsoft.com/en #include #include #include +#include // for std::shuffle using namespace std; @@ -23,6 +24,7 @@ class War { private: int myCurrent; int otherCurrent; + static constexpr int NO_CARD = -1; // Sentinel that cannot be a real card vector newdecks; stack myPlayingPile; queue myStoragePile; @@ -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(); @@ -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(); @@ -155,7 +157,36 @@ 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; } @@ -163,12 +194,53 @@ class War { 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; @@ -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; diff --git a/war b/war new file mode 100755 index 0000000..7afad1b Binary files /dev/null and b/war differ