War card game: Difference between revisions

3,700 bytes added ,  11 months ago
New post.
m (Removed post due to incorrect display of unicode characters.)
Tag: Manual revert
(New post.)
Line 482:
 
Player 1 wins after 86 turns</syntaxhighlight>
 
=={{header|Java}}==
 
<syntaxhighlight lang="java">
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
public final class WarCardGame {
 
public static void main(String[] args) {
WarGame warGame = new WarGame();
while ( ! warGame.gameOver() ) {
warGame.nextTurn();
}
warGame.declareWinner();
}
}
 
final class WarGame {
public WarGame() {
deck = new ArrayList<Card>(52);
for ( Character suit : suits ) {
for ( Character pip : pips ) {
deck.add( new Card(suit, pip) );
}
}
Collections.shuffle(deck);
handA = new ArrayList<Card>(deck.subList(0, 26));
handB = new ArrayList<Card>(deck.subList(26, 52));
tabledCards = new ArrayList<Card>();
}
public void nextTurn() {
Card cardA = handA.remove(0);
Card cardB = handB.remove(0);
tabledCards.add(cardA);
tabledCards.add(cardB);
int rankA = pips.indexOf(cardA.pip);
int rankB = pips.indexOf(cardB.pip);
System.out.print(cardA + " " + cardB);
if ( rankA > rankB ) {
System.out.println(" Player A takes the cards");
Collections.shuffle(tabledCards);
handA.addAll(tabledCards);
tabledCards.clear();
} else if ( rankA < rankB ) {
System.out.println(" Player B takes the cards");
Collections.shuffle(tabledCards);
handB.addAll(tabledCards);
tabledCards.clear();
} else {
System.out.println(" War!");
if ( gameOver() ) {
return;
}
Card cardAA = handA.remove(0);
Card cardBB = handB.remove(0);
tabledCards.add(cardAA);
tabledCards.add(cardBB);
System.out.println("? ? Cards are face down");
if ( gameOver() ) {
return;
}
nextTurn();
}
}
public boolean gameOver() {
return handA.size() == 0 || handB.size() == 0;
}
public void declareWinner() {
if ( handA.size() == 0 && handB.size() == 0 ) {
System.out.println("The game ended in a tie");
} else if ( handA.size() == 0 ) {
System.out.println("Player A has won the game");
} else {
System.out.println("Player B has won the game");
}
}
private record Card(Character suit, Character pip) {
@Override
public String toString() {
return pip + "" + suit;
}
}
private List<Card> deck, handA, handB, tabledCards;
private final List<Character> pips =
Arrays.asList( '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' );
private final List<Character> suits = List.of( 'C', 'D', 'H', 'S' );
}
</syntaxhighlight>
{{ out }}
<pre>
5H 7D Player B takes the cards
QS 4C Player A takes the cards
KS 4H Player A takes the cards
7S 7C War!
? ? Cards are face down
2D 4D Player B takes the cards
3H JC Player B takes the cards
JD AH Player B takes the cards
8C 5S Player A takes the cards
TH KH Player B takes the cards
9C QD Player B takes the cards
3C QH Player B takes the cards
JH 9H Player A takes the cards
7H KC Player B takes the cards
9D 2C Player A takes the cards
AS AD War!
? ? Cards are face down
5C QC Player B takes the cards
2S 9S Player B takes the cards
8D KD Player B takes the cards
 
... many lines elided
 
7S 9D Player B takes the cards
9H 2S Player A takes the cards
8D KS Player B takes the cards
AH AC War!
? ? Cards are face down
2S 2H War!
Player A has won the game
</pre>
 
=={{header|Julia}}==
871

edits