War card game: Difference between revisions

Added FreeBASIC
m (changed summary: it shuffles the picked up cards, it's NOT thundergnat (thundercat))
(Added FreeBASIC)
 
(3 intermediate revisions by 3 users not shown)
Line 134:
{{trans|Python}}
* The Bicycle rules are set up by default.
* When the winning player picks up his cards, the cards are randomized when added to the bottom of histhe 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
Line 490:
int main() {
war_game wargame;
srand((unsigned) time(NULL));
 
while ( ! wargame.game_over() ) {
wargame.next_turn();
Line 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,785 ⟶ 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,130

edits