War card game: Difference between revisions

Added AutoHotkey
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
(Added AutoHotkey)
Line 130:
Player 2 wins the game.
</pre>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>suits := ["♠", "♦", "♥", "♣"]
faces := [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
deck := [], p1 := [], p2 := []
for i, s in suits
for j, v in faces
deck.Push(v s)
deck := shuffle(deck)
deal := deal(deck, p1, p2)
p1 := deal.1, p2 := deal.2
 
for i, v in p2
{
if InStr(v, "A")
p2[i] := p1[i], p1[i] := v
if InStr(v, "K")
p2[i] := p1[i], p1[i] := v
if InStr(v, "Q")
p2[i] := p1[i], p1[i] := v
if InStr(v, "J")
p2[i] := p1[i], p1[i] := v
}
MsgBox % war(p1, p2)
return
war(p1, p2){
J:=11, Q:=11, K:=11, A:=12, stack := [], cntr := 0
output := "------------------------------------------"
. "`nRound# Deal Winner P1 P1"
. "`n------------------------------------------"
. "`nround0 26 26"
 
while (p1.Count() && p2.Count()) {
cntr++
stack.Push(c1:=p1.RemoveAt(1)), stack.Push(c2:=p2.RemoveAt(1))
r1:=SubStr(c1,1,-1) ,r2:=SubStr(c2,1,-1)
v1:=r1<11?r1:%r1% ,v2:=r2<11?r2:%r2%
output .= "`nround# " cntr "`t" SubStr(c1 " n", 1, 4) "vs " SubStr(c2 " ", 1, 4)
if (v1 > v2){
loop % stack.Count()
p1.Push(stack.RemoveAt(1))
output .= "`t`tP1 wins"
}
else if (v1 < v2){
loop % stack.Count()
p2.Push(stack.RemoveAt(1))
output .= "`t`tP2 wins"
}
if (v1 = v2){
output .= "`t`t**WAR**`t" P1.Count() "`t" P2.Count()
stack.Push(c1:=p1.RemoveAt(1)), stack.Push(c2:=p2.RemoveAt(1))
if !(p1.Count() && p2.Count())
break
output .= "`nround# " ++cntr "`t(" SubStr(c1 " ", 1, 3) ") - (" SubStr(c2 " ", 1, 3) ")"
output .= "`tFace Dn"
}
output .= "`t" P1.Count() "`t" P2.Count()
if !Mod(cntr, 20)
{
MsgBox % output
output := ""
}
}
output .= "`n" (P1.Count() ? "P1 Wins" : "P2 Wins")
output := StrReplace(output, " )", ") ")
output := StrReplace(output, " -", " -")
return output</lang>
{{out}}
<pre>------------------------------------------
Round# Deal Winner P1 P1
------------------------------------------
round0 26 26
round# 1 J♣ vs 3♠ P1 wins 27 25
round# 2 3♣ vs 10♦ P2 wins 26 26
round# 3 Q♣ vs 8♠ P1 wins 27 25
round# 4 A♦ vs 7♠ P1 wins 28 24
round# 5 J♥ vs 3♦ P1 wins 29 23
round# 6 8♦ vs 4♦ P1 wins 30 22
round# 7 4♥ vs 6♦ P2 wins 29 23
round# 8 7♥ vs 4♠ P1 wins 30 22
round# 9 A♠ vs 7♣ P1 wins 31 21
round# 10 K♣ vs 10♠ P1 wins 32 20
round# 11 4♣ vs 8♥ P2 wins 31 21
round# 12 6♠ vs 6♥ **WAR** 30 20
round# 13 (A♥) - (9♠) Face Dn 29 19
round# 14 5♣ vs 2♥ P1 wins 34 18
round# 15 A♣ vs 8♣ P1 wins 35 17
round# 16 J♦ vs 6♣ P1 wins 36 16
round# 17 Q♥ vs 9♣ P1 wins 37 15
round# 18 10♥ vs 5♠ P1 wins 38 14
round# 19 K♥ vs K♠ **WAR** 37 13
round# 20 (Q♠) - (9♥) Face Dn 36 12
round# 21 K♦ vs 3♥ P1 wins 41 11
round# 22 J♠ vs 2♦ P1 wins 42 10
round# 23 5♥ vs 5♦ **WAR** 41 9
round# 24 (2♣) - (7♦) Face Dn 40 8
round# 25 Q♦ vs 10♣ P1 wins 45 7
round# 26 9♦ vs 2♠ P1 wins 46 6
round# 27 J♣ vs 3♣ P1 wins 47 5
round# 28 3♠ vs 10♦ P2 wins 46 6
round# 29 Q♣ vs 4♥ P1 wins 47 5
round# 30 8♠ vs 6♦ P1 wins 48 4
round# 31 A♦ vs 4♣ P1 wins 49 3
round# 32 7♠ vs 8♥ P2 wins 48 4
round# 33 J♥ vs 3♠ P1 wins 49 3
round# 34 3♦ vs 10♦ P2 wins 48 4
round# 35 8♦ vs 7♠ P1 wins 49 3
round# 36 4♦ vs 8♥ P2 wins 48 4
round# 37 7♥ vs 3♦ P1 wins 49 3
round# 38 4♠ vs 10♦ P2 wins 48 4
round# 39 A♠ vs 4♦ P1 wins 49 3
round# 40 7♣ vs 8♥ P2 wins 48 4
round# 41 K♣ vs 4♠ P1 wins 49 3
round# 42 10♠ vs 10♦ **WAR** 48 2
round# 43 (6♠) - (7♣) Face Dn 47 1
round# 44 6♥ vs 8♥ P2 wins 46 6
round# 45 A♥ vs 10♠ P1 wins 47 5
round# 46 9♠ vs 10♦ P2 wins 46 6
round# 47 5♣ vs 6♠ P2 wins 45 7
round# 48 2♥ vs 7♣ P2 wins 44 8
round# 49 A♣ vs 6♥ P1 wins 45 7
round# 50 8♣ vs 8♥ **WAR** 44 6
round# 51 (J♦) - (9♠) Face Dn 43 5
round# 52 6♣ vs 10♦ P2 wins 42 10
round# 53 Q♥ vs 5♣ P1 wins 43 9
round# 54 9♣ vs 6♠ P1 wins 44 8
round# 55 10♥ vs 2♥ P1 wins 45 7
round# 56 5♠ vs 7♣ P2 wins 44 8
round# 57 K♥ vs 8♣ P1 wins 45 7
round# 58 K♠ vs 8♥ P1 wins 46 6
round# 59 Q♠ vs J♦ **WAR** 45 5
round# 60 (9♥) - (9♠) Face Dn 44 4
round# 61 K♦ vs 6♣ P1 wins 49 3
round# 62 3♥ vs 10♦ P2 wins 48 4
round# 63 J♠ vs 5♠ P1 wins 49 3
round# 64 2♦ vs 7♣ P2 wins 48 4
round# 65 5♥ vs 3♥ P1 wins 49 3
round# 66 5♦ vs 10♦ P2 wins 48 4
round# 67 2♣ vs 2♦ **WAR** 47 3
round# 68 (7♦) - (7♣) Face Dn 46 2
round# 69 Q♦ vs 5♦ P1 wins 51 1
round# 70 10♣ vs 10♦ **WAR** 50 0
P1 Wins</pre>
 
=={{header|Go}}==
299

edits