War card game: Difference between revisions
m
syntax highlighting fixup automation
(J draft) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25:
{{trans|Python}}
<
F nonrandom(n)
:seed = (1664525 * :seed + 1013904223) [&] FFFF'FFFF
Line 94:
V WG = WarCardGame()
L WG.turn()
L.continue</
{{out}}
Line 132:
=={{header|AutoHotkey}}==
<
faces := [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
deck := [], p1 := [], p2 := []
Line 196:
output := StrReplace(output, " )", ") ")
output := StrReplace(output, " -", " -")
return output</
{{out}}
<pre>------------------------------------------
Line 276:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 381:
rand.Seed(time.Now().UnixNano())
war()
}</
{{out}}
Line 414:
=={{header|J}}==
Implementation:<
rank=: DECK {{ {."1 ($m)#:(,m) i. y }}
shuffle=: {~ ?~@#
Line 440:
echo m,' takes ',;(<' and ') _2}}.,(<', '),.y
echo''
}}</
Example run:<
P1 plays 6 of ♦
P2 plays 9 of ♥
Line 481:
P1 takes 6 of ♠, K of ♠, 5 of ♥, 3 of ♣, 3 of ♦ and 2 of ♦
Player 1 wins after 86 turns</
=={{header|Julia}}==
<
using Random
Line 533:
warcardgame()
</
<pre>
5♦ 3♥ Player 1 takes the cards.
Line 571:
=={{header|Lua}}==
{{libheader|LÖVE}}
<
function newGame ()
-- new deck
Line 701:
end
end
</syntaxhighlight>
=={{header|Nim}}==
We use module "playing_cards" from task https://rosettacode.org/wiki/Playing_cards.
<
import playing_cards
Line 797:
if hands[Player2].len == 0:
echo "Player 1 wins this game."
break</
{{out}}
Line 903:
=={{header|Perl}}==
There are two players, 'one' and 'two'. This shows each players hand as the game progresses.
<
use strict; # https://rosettacode.org/wiki/War_Card_Game
Line 922:
s/^(.{4})(.*)\n(.{4})(.*)$/ print "WAR!!!\n\n"; $war .= "$1$3";
"$2\n$4" /e; # tie means war
print "player '", /^.{10}/ ? 'one' : 'two', "' wins in $cnt moves\n";</
{{out}}
<pre>
Line 974:
=={{header|Phix}}==
Shuffles on pickup to significantly shorten the games
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">deck</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">52</span><span style="color: #0000FF;">)),</span>
Line 1,032:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
{{out}}
<pre>
Line 1,054:
=={{header|Python}}==
{{trans|Julia}}
<
from numpy.random import shuffle
Line 1,119:
while WG.turn():
continue
</
<pre>
8♠ K♠ Player 2 takes the cards.
Line 1,164:
{{trans|Julia}} (and inspired by Raku's thnudergnat rules)
<
(define current-battle-length (make-parameter 3))
Line 1,202:
turn))
(displayln (war-card-game))</
{{out}}
Line 1,264:
In glorious ANSI color! (The output loses much when pasted in as text so show output as screenshot images.)
<syntaxhighlight lang="raku"
my %c = ( # convenience hash of ANSI colors
Line 1,377:
print-at $height + 2, $cols div 2 - 40, "{%c<blue>} Player 1: {+@player[0] ?? '52' !! "{%c<red>}0"}{%c<blue>} cards ";
print-at $height + 2, $cols div 2 + 20, "{%c<blue>} Player 2: {+@player[1] ?? '52' !! "{%c<red>}0"}{%c<blue>} cards ";
clean-up;</
{{out|Sample outout Bicycle}}
Line 1,391:
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.
<
import "/queue" for Deque
Line 1,466:
}
war.call()</
{{out}}
Line 1,509:
=={{header|XPL0}}==
<
Stack(2, 52); \each player's stack of cards (52 maximum)
int Inx(2), \index to last card (+1) for each stack
Line 1,575:
];
]
]</
{{out}}
|