War card game: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
(Added FreeBASIC) |
||
Line 525: | Line 525: | ||
Player A has won the game |
Player A has won the game |
||
</pre> |
</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}}== |
=={{header|Go}}== |