Jump to content

War card game: Difference between revisions

3,140 bytes added ,  11 months ago
New post.
m (Corrected minor error in code.)
(New post.)
Line 273:
round# 70 10♣ vs 10♦ **WAR** 50 0
P1 Wins</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <iostream>
#include <vector>
#include <algorithm>
 
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());
 
handA = { deck.begin(), deck.begin() + 26 };
handB = { deck.begin() + 26, deck.end() };
}
 
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;
 
if ( rankA > rankB ) {
std::cout << " Player A takes the cards" << std::endl;
std::random_shuffle(tabledCards.begin(), tabledCards.end());
handA.insert(handA.end(), tabledCards.begin(), tabledCards.end());
tabledCards.clear();
} else if ( rankA < rankB ) {
std::cout << " Player B takes the cards" << std::endl;
std::random_shuffle(tabledCards.begin(), tabledCards.end());
handB.insert(handB.end(), tabledCards.begin(), tabledCards.end());;
tabledCards.clear();
} else {
std::cout << " War!" << std::endl;
if ( game_over() ) {
return;
}
 
card cardAA = handA.front(); handA.erase(handA.begin());
card cardBB = handB.front(); handB.erase(handB.begin());
tabledCards.emplace_back(cardAA);
tabledCards.emplace_back(cardBB);
std::cout << "? ? Cards are face down" << std::endl;
if ( game_over() ) {
return;
}
 
next_turn();
}
}
 
bool game_over() {
return handA.size() == 0 || handB.size() == 0;
}
 
void declare_winner() {
if ( handA.size() == 0 && handB.size() == 0 ) {
std::cout << "The game ended in a tie" << std::endl;
} else if ( handA.size() == 0 ) {
std::cout << "Player B has won the game" << std::endl;
} else {
std::cout << "Player A has won the game" << std::endl;
}
}
private:
class card {
public:
card(const char suit, const char pip) : suit(suit), pip(pip) {};
char suit, pip;
};
 
int32_t getRank(char ch) {
auto it = find(PIPS.begin(), PIPS.end(), ch);
if ( it != PIPS.end() ) {
return it - PIPS.begin();
}
return -1;
}
 
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() {
war_game wargame;
 
while ( ! wargame.game_over() ) {
wargame.next_turn();
}
 
wargame.declare_winner();
}
</syntaxhighlight>
{{ out }}
<pre>
JD 9D
Player A takes the cards
TH 7S
Player A takes the cards
6H QC
Player B takes the cards
9C 9H
War!
? ? Cards are face down
AC 3C
Player A takes the cards
... many lines elided
3D 6C
Player B takes the cards
6D 4D
Player A takes the cards
JD 2C
Player A takes the cards
3C 3D
War!
? ? Cards are face down
Player A has won the game
</pre>
 
=={{header|Go}}==
894

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.