Jump to content

War card game: Difference between revisions

m
syntax highlighting fixup automation
(J draft)
m (syntax highlighting fixup automation)
Line 25:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
F nonrandom(n)
:seed = (1664525 * :seed + 1013904223) [&] FFFF'FFFF
Line 94:
V WG = WarCardGame()
L WG.turn()
L.continue</langsyntaxhighlight>
 
{{out}}
Line 132:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">suits := ["♠", "♦", "♥", "♣"]
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</langsyntaxhighlight>
{{out}}
<pre>------------------------------------------
Line 276:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 381:
rand.Seed(time.Now().UnixNano())
war()
}</langsyntaxhighlight>
 
{{out}}
Line 414:
 
=={{header|J}}==
Implementation:<langsyntaxhighlight Jlang="j">DECK=: '2 3 4 5 6 7 8 9 10 J K Q A' (,' of ',])&.>/&cut '♣ ♦ ♥ ♠'
rank=: DECK {{ {."1 ($m)#:(,m) i. y }}
shuffle=: {~ ?~@#
Line 440:
echo m,' takes ',;(<' and ') _2}}.,(<', '),.y
echo''
}}</langsyntaxhighlight>
 
Example run:<langsyntaxhighlight Jlang="j"> cardwar''
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</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># https://bicyclecards.com/how-to-play/war/
 
using Random
Line 533:
 
warcardgame()
</langsyntaxhighlight>{{out}}
<pre>
5♦ 3♥ Player 1 takes the cards.
Line 571:
=={{header|Lua}}==
{{libheader|LÖVE}}
<langsyntaxhighlight Lualang="lua">-- main.lua
function newGame ()
-- new deck
Line 701:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
We use module "playing_cards" from task https://rosettacode.org/wiki/Playing_cards.
<langsyntaxhighlight Nimlang="nim">import strformat
import playing_cards
 
Line 797:
if hands[Player2].len == 0:
echo "Player 1 wins this game."
break</langsyntaxhighlight>
 
{{out}}
Line 903:
=={{header|Perl}}==
There are two players, 'one' and 'two'. This shows each players hand as the game progresses.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
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";</langsyntaxhighlight>
{{out}}
<pre>
Line 974:
=={{header|Phix}}==
Shuffles on pickup to significantly shorten the games
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,054:
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">""" https://bicyclecards.com/how-to-play/war/ """
 
from numpy.random import shuffle
Line 1,119:
while WG.turn():
continue
</langsyntaxhighlight>{{out}}
<pre>
8♠ K♠ Player 2 takes the cards.
Line 1,164:
{{trans|Julia}} (and inspired by Raku's thnudergnat rules)
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define current-battle-length (make-parameter 3))
Line 1,202:
turn))
 
(displayln (war-card-game))</langsyntaxhighlight>
 
{{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" perl6line>unit sub MAIN (:$war where 2..4 = 4, :$sleep = .1);
 
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;</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/queue" for Deque
 
Line 1,466:
}
 
war.call()</langsyntaxhighlight>
 
{{out}}
Line 1,509:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">char Deck(52), \initial card deck (low 2 bits = suit)
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:
];
]
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.