War card game: Difference between revisions

Added FreeBASIC
m (Made code more C++ idiomatic.)
(Added FreeBASIC)
 
(7 intermediate revisions by 4 users not shown)
Line 129:
 
Player 2 wins the game.
</pre>
 
=={{header|Applesoft BASIC}}==
{{trans|Python}}
* The Bicycle rules are set up by default.
* When the winning player picks up cards, the cards are randomized when added to the bottom of the player's deck. see [[War_card_game#Raku|Raku]] for details.
* Other variations can be played by passing different parameters.
<syntaxhighlight lang="autohotkey"> 100 R = RND (0): REM SEED
110 W = 1: REM BICYCLE RULE: ONLY ONE EXTRA CARD GIVEN UP IN WAR
120 P = 2: REM BICYCLE RULE: ONLY TWO PLAYERS
130 D = 1: REM BICYCLE RULE: ONLY ONE DECK OF CARDS
140 DIM D$(P),C$(P),G(P),L(P)
150 SUIT$ = "CDHS"
160 FACE$ = "23456789TJQKA"
170 M = LEN (SUIT$)
180 FOR I = 1 TO D: FOR S = 1 TO M: FOR F = 1 TO LEN (FACE$):P$ = P$ + CHR$ ((F - 1) * M + (S - 1)): NEXT F,S,I
190 TEXT : HOME : POKE 34,12
REM DEAL TO PLAYERS
200 GOSUB 700"SHUFFLE
210 E = INT (N / P)
220 FOR I = 1 TO P
230 D$(I) = MID$ (P$,(I - 1) * E + 1,E)
240 NEXT
250 P$ = MID$ (P$,(I - 1) * E + 1): REM WINNER OF THE FIRST PLAY KEEPS THESE REMAINING CARDS
260 P$ = "": REM REMOVE REMAINING CARDS FROM THE GAME
REM PLAY
300 FOR T = 0 TO 1E38
310 GOSUB 400"TURN
320 IF Q = 0 THEN NEXT T
330 PRINT " IN "T" TURNS": TEXT : VTAB 11: PRINT : CALL - 868: PRINT "...": VTAB 23
340 END
 
REM TURN
400 PRINT : GOSUB 900"IS GAME OVER?
410 IF Q THEN RETURN
420 U = 0: REM UTMOST CARD
430 C = 0: REM COUNT THE PLAYERS WITH THE UTMOST CARD
440 FOR I = 1 TO P
450 C$(I) = "" : IF NOT L(I) THEN C$(I) = MID$ (D$(I),1,1)
460 IF LEN (C$(I)) THEN GOSUB 800"DRAW CARD
470 NEXT I
480 IF C = 1 GOTO 600"WINNER TAKE CARDS
490 FOR I = 1 TO P:L(I) = G(I) < > U: NEXT I
500 PRINT "WAR! ";
510 GOSUB 900"IS GAME OVER?
520 IF Q THEN RETURN
530 C = 0
540 FOR I = 1 TO P:P$ = P$ + C$(I): NEXT I
550 FOR I = 1 TO P
REM DOES NOT APPLY TO 2 PLAYER GAMES (BICYCLE RULE): Y MEANS IGNORE THE RULE THAT ONLY THE WINNERS PLACE CARD(S) FACE DOWN
560 IF Y OR NOT L(I) THEN FOR J = 1 TO W:C$ = MID$ (D$(I),1,1) :C = C + LEN(C$) : P$ = P$ + C$ :D$(I) = MID$ (D$(I),2): NEXT J
570 NEXT I
580 PRINT C" CARDS PLACED FACE DOWN.";
590 RETURN
 
REM WINNER TAKE CARDS
600 FOR I = 1 TO P
610 L(I) = 0
620 P$ = P$ + C$(I)
630 NEXT I
640 PRINT "PLAYER "A" TAKES "LEN(P$);
650 IF NOT Z THEN GOSUB 710"SHUFFLE
660 D$(A) = D$(A) + P$
670 P$ = ""
680 RETURN
 
REM SHUFFLE
700 PRINT "SHUFFLING ";
710 N = LEN (P$)
720 FOR I = 1 TO N
730 IF I - INT (I / 4) * 4 = 1 THEN PRINT ".";
740 R = INT ( RND (1) * N + 1)
750 R$ = MID$ (P$,R,1) : C$ = MID$ (P$,I,1)
760 P$ = MID$ (P$,1,I - 1) + R$ + MID$ (P$,I + 1)
770 P$ = MID$ (P$,1,R - 1) + C$ + MID$ (P$,R + 1)
780 NEXT
790 RETURN
 
REM DRAW CARD
800 G = ASC (C$(I))
810 G(I) = INT (G / M) + 1
820 PRINT MID$ (FACE$,G(I),1) MID$ (SUIT$,G - (G(I) - 1) * M + 1,1)" ";
830 D$(I) = MID$ (D$(I),2)
840 C = C + (G(I) = U)
850 IF G(I) > U THEN U = G(I):C = 1:A = I
860 RETURN
 
REM IS GAME OVER?
900 C = 0
910 FOR I = 1 TO P
920 IF LEN (D$(I)) THEN A = I:C = C + 1
930 NEXT I
940 IF C > 1 THEN RETURN
REM GAME OVER - WHO WON MESSAGE
950 Q = 1
960 IF C THEN PRINT "PLAYER "A" WINS THE GAME";: RETURN
970 PRINT "GAME ENDS AS A TIE";: RETURN</syntaxhighlight>
<syntaxhighlight lang="autohotkey">CLEAR : R = RND ( - 58) : GOTO 110</syntaxhighlight>
{{out}}
<pre>SHUFFLING .............
JD 9H PLAYER 1 TAKES 2.
4C 5C PLAYER 2 TAKES 2.
2D 8C PLAYER 2 TAKES 2.
TH AS PLAYER 2 TAKES 2.
7D QS PLAYER 2 TAKES 2.
4S 8H PLAYER 2 TAKES 2.
AD 7C PLAYER 1 TAKES 2.
JS 2S PLAYER 1 TAKES 2.
3C 3S WAR! 2 CARDS PLACED FACE DOWN.
TD KH PLAYER 2 TAKES 6..
...
8D 2D PLAYER 1 TAKES 2.
TD 7C PLAYER 1 TAKES 2.
7S 6C PLAYER 1 TAKES 2.
9H 8H PLAYER 1 TAKES 2.
3D 8C PLAYER 2 TAKES 2.
AD 3C PLAYER 1 TAKES 2.
4H 4C WAR! 2 CARDS PLACED FACE DOWN.
7D 7H WAR! 2 CARDS PLACED FACE DOWN.
JH 3D PLAYER 1 TAKES 10...
9D 8C PLAYER 1 TAKES 2.
PLAYER 1 WINS THE GAME IN 432 TURNS
</pre>
 
Line 277 ⟶ 399:
<syntaxhighlight lang="c++">
 
#include <cstdint>
#include <iostream>
#include <stringvector>
#include <arrayalgorithm>
#include <iomanip>
 
class war_game {
typedef std::pair<std::string, bool> data;
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 };
const std::array<const std::array<int32_t, 10>, 10> multiplication_table = { {
handB = { deck.begin() + 26, deck.end() };
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
}
{ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
{ 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 },
{ 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 },
{ 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 },
{ 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 },
{ 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 },
{ 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 },
{ 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
} };
 
void next_turn() {
const std::array<int32_t, 10> inverse = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
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 ) {
const std::array<const std::array<int32_t, 10>, 8> permutation_table = { {
std::cout << " Player A takes the cards" << std::endl;
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
std::random_shuffle(tabledCards.begin(), tabledCards.end());
{ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
handA.insert(handA.end(), tabledCards.begin(), tabledCards.end());
{ 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 },
tabledCards.clear();
{ 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 },
} else if ( rankA < rankB ) {
{ 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 },
std::cout << " Player B takes the cards" << std::endl;
{ 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 },
std::random_shuffle(tabledCards.begin(), tabledCards.end());
{ 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
handB.insert(handB.end(), tabledCards.begin(), tabledCards.end());;
{ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
tabledCards.clear();
} };
} else {
std::cout << " War!" << std::endl;
if ( game_over() ) {
return;
}
 
card cardAA = handA.front(); handA.erase(handA.begin());
int32_t verhoeff_checksum(std::string number, const bool doValidation, const bool doDisplay) {
card cardBB = handB.front(); handB.erase(handB.begin());
if ( doDisplay ) {
tabledCards.emplace_back(cardAA);
std::string calculationType = doValidation ? "Validation" : "Check digit";
tabledCards.emplace_back(cardBB);
std::cout << calculationType << " calculations for " << number << "\n" << std::endl;
std::cout << "? i ni? p[i, ni]Cards are cface down" << std::endl;
if ( game_over() ) {
std::cout << "-------------------" << std::endl;
return;
}
 
next_turn();
}
}
 
bool game_over() const {
if ( ! doValidation ) {
return handA.size() == 0 || handB.size() == 0;
number += "0";
}
 
void declare_winner() const {
int32_t c = 0;
if ( handA.size() == 0 && handB.size() == 0 ) {
const int32_t le = number.length() - 1;
std::cout << "The game ended in a tie" << std::endl;
for ( int32_t i = le; i >= 0; i-- ) {
const} int32_telse niif =( number[i]handA.size() -== '0'; ) {
std::cout << "Player B has won the game" << std::endl;
const int32_t pi = permutation_table[(le - i) % 8][ni];
} else {
c = multiplication_table[c][pi];
std::cout << "Player A has won the game" << std::endl;
 
if ( doDisplay ) {
std::cout << std::setw(2) << le - i << std::setw(3) << ni
<< std::setw(8) << pi << std::setw(6) << c << "\n" << std::endl;
}
}
private:
class card {
public:
card(const char suit, const char pip) : suit(suit), pip(pip) {};
char suit, pip;
};
 
int32_t getRank(const char ch) const {
if ( doDisplay && ! doValidation ) {
auto it = find(PIPS.begin(), PIPS.end(), ch);
std::cout << "inverse[" << c << "] = " << inverse[c] << "\n" << std::endl;;
if ( it != PIPS.end() ) {
return it - PIPS.begin();
}
return -1;
}
 
std::vector<card> deck, handA, handB, tabledCards;
return doValidation ? c == 0 : inverse[c];
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;
const std::array<data, 3> tests = {
srand((unsigned) time(NULL));
std::make_pair("123", true), std::make_pair("12345", true), std::make_pair("123456789012", false) };
while ( ! wargame.game_over() ) {
 
wargame.next_turn();
for ( const data& test : tests ) {
int32_t digit = verhoeff_checksum(test.first, false, test.second);
std::cout << "The check digit for " << test.first << " is " << digit << "\n" << std::endl;
 
std::string numbers[2] = { test.first + std::to_string(digit), test.first + "9" };
for ( const std::string& number : numbers ) {
digit = verhoeff_checksum(number, true, test.second);
std::string result = ( digit == 1 ) ? "correct" : "incorrect";
std::cout << "The validation for " << number << " is " << result << ".\n" << std::endl;
}
}
 
wargame.declare_winner();
}
</syntaxhighlight>
Line 387 ⟶ 525:
Player A has won the game
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPLo}}
<syntaxhighlight lang="vbnet">Dim Shared As Integer stack(1, 51) ' each player's stack of cards (52 maximum)
Dim Shared As Integer inx(1) ' index to last card (+1) for each stack
Dim Shared As Integer carta
 
Sub MoveCard(hacia As Integer, desde As Integer) ' Move top card desde stack to bottom of To stack
Dim As Integer i
carta = stack(desde, 0) ' take top carta from desde stack
For i = 0 To inx(desde) - 2 ' shift remaining cards over
stack(desde, i) = stack(desde, i + 1)
Next i
If inx(desde) > 0 Then inx(desde) -= 1 ' remove desde card from its stack
stack(hacia, inx(hacia)) = carta ' add carta to bottom of To stack
If inx(hacia) < 52 Then inx(hacia) += 1 ' remove desde card from its stack
End Sub
 
Dim As String suit = "HDCS "
Dim As String rank = "23456789TJQKA "
Dim As Integer top ' index to compared cards, = stack top if not war
Dim As Integer deck(51) ' initial card deck (low 2 bits = suit)
For carta = 0 To 51 ' make a complete deck of cards
deck(carta) = carta
Next carta
Dim As Integer n, i, j, p, t
 
Randomize Timer
For n = 0 To 10000 ' shuffle the deck by swapping random locations
i = Int(Rnd * 52): j = Int(Rnd * 52)
Swap deck(i), deck(j)
Next n
For n = 0 To 51 ' deal deck into two stacks
carta = deck(n)
i = n \ 2
p = n Mod 2
stack(p, i) = carta
Next n
inx(0) = 52 \ 2: inx(1) = 52 \ 2 ' set indexes to last card +1
 
Do
For p = 0 To 1 ' show both stacks of cards
For i = 0 To inx(p) - 1
carta = stack(p, i)
Print Left(rank, carta Shr 2);
Next i
Print
For i = 0 To inx(p) - 1
carta = stack(p, i)
Print Mid(suit, (carta And 3) + 1, 1);
Next i
Print
Next p
If inx(0) = 0 Or inx(1) = 0 Then Exit Do ' game over
top = 0 ' compare card ranks (above 2-bit suits)
Do
If (stack(0, top) Shr 2) = (stack(1, top) Shr 2) Then
Color 3 : Print "War!" : Print : Color 7
top += 2 ' play a card down and a card up
Elseif (stack(0, top) Shr 2) > (stack(1, top) Shr 2) Then
For i = 0 To top ' move cards to stack 0
MoveCard(0, 0): MoveCard(0, 1)
Next i
Exit Do
Else
For i = 0 To top ' move cards to stack 1
MoveCard(1, 1): MoveCard(1, 0)
Next i
Exit Do
End If
Loop
Sleep 1000, 1
Print
Loop
 
End</syntaxhighlight>
 
=={{header|Go}}==
Line 1,647 ⟶ 1,862:
 
I've also assumed that if a player wins a round, his/her own cards (in the order played) are added back to the bottom of his/her hand before the other player's cards.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./queue" for Deque
 
var rand = Random.new()
2,133

edits