War card game: Difference between revisions

Line 960:
{{trans|Julia}} (and inspired by Raku's thnudergnat rules)
 
<lang racket></#lang> racket
 
(define current-battle-length (make-parameter 3))
 
(define cards (string->list (string-append "🂢🂣🂤🂥🂦🂧🂨🂩🂪🂫🂭🂮🂡" "🂲🂳🂴🂵🂶🂷🂸🂹🂺🂻🂽🂾🂱"
"🃂🃃🃄🃅🃆🃇🃈🃉🃊🃋🃍🃎🃁" "🃒🃓🃔🃕🃖🃗🃘🃙🃚🃛🃝🃞🃑")))
 
(define face (curry hash-ref (for/hash ((c cards) (i (in-naturals))) (values c (+ 2 (modulo i 13))))))
 
(define (print-draw-result turn-number d1 c1 d2 c2 → (pending null))
(printf "#~a\t~a ~a ~a\t~a|~a|~a~%" turn-number c1 → c2 (length d1) (length pending)(length d2)))
 
(define (turn d1 d2 (n 1) (pending null) (battle 0))
(match* (d1 d2)
[('() '()) "Game ends in a tie!"]
[(_ '()) "Player 1 wins."]
[('() _) "Player 2 wins."]
[((list c3 d1- ...) (list c4 d2- ...))
#:when (positive? battle)
(define pending+ (list* c3 c4 pending))
(print-draw-result n d1- "🂠" d2- "🂠" "?" pending+)
(turn d1- d2- (add1 n) pending+ (sub1 battle))]
[((list (and c1 (app face r)) d1- ...) (list (and c2 (app face r)) d2- ...))
(define pending+ (list* c1 c2 pending))
(print-draw-result n d1- c1 d2- c2 #\= pending+)
(turn d1- d2- (add1 n) pending+ (current-battle-length))]
[((list (and c1 (app face r1)) d1- ...) (list (and c2 (app face r2)) d2- ...))
(define spoils (shuffle (list* c1 c2 pending)))
(define p1-win? (> r1 r2))
(define d1+ (if p1-win? (append d1- spoils) d1-))
(define d2+ (if p1-win? d2- (append d2- spoils)))
(print-draw-result n d1+ c1 d2+ c2 (if p1-win? "←" "→"))
(turn d1+ d2+ (add1 n))]))
 
(define (war-card-game)
(call-with-values (λ () (split-at (shuffle cards) (quotient (length cards) 2)))
turn))
 
(displayln (war-card-game))</lang>
 
{{out}}
<pre>#1 🃉 ← 🃔 27|0|25
<pre></pre>
#2 🂡 ← 🃘 28|0|24
<pre></pre>
#3 🂹 → 🃁 27|0|25
#4 🃋 → 🂽 26|0|26
#5 🂫 ← 🂪 27|0|25
#6 🃒 → 🃛 26|0|26
#7 🃑 ← 🃖 27|0|25
#8 🂢 → 🂻 26|0|26
#9 🃍 ← 🃗 27|0|25
#10 🃊 → 🃝 26|0|26
#11 🃎 ← 🂳 27|0|25
#12 🃅 → 🂶 26|0|26
#13 🃙 ← 🂨 27|0|25
#14 🂦 → 🂺 26|0|26</pre>
...
<pre>#305 🂧 → 🂾 45|0|7
#306 🂦 = 🃖 44|2|6
#307 🂠 ? 🂠 43|4|5
#308 🂠 ? 🂠 42|6|4
#309 🂠 ? 🂠 41|8|3
#310 🃁 ← 🃃 50|0|2
#311 🂽 ← 🂧 51|0|1
#312 🃘 → 🂾 50|0|2
#313 🂷 → 🂾 49|0|3
#314 🂴 → 🃘 48|0|4
#315 🃊 → 🂾 47|0|5
#316 🂱 ← 🂷 48|0|4
#317 🃋 ← 🂴 49|0|3
#318 🃒 → 🃘 48|0|4
#319 🂶 → 🃊 47|0|5
#320 🂢 → 🂾 46|0|6
#321 🂺 ← 🃒 47|0|5
#322 🂩 ← 🃘 48|0|4
#323 🃛 ← 🂶 49|0|3
#324 🃝 ← 🃊 50|0|2
#325 🃞 = 🂾 49|2|1
#326 🂠 ? 🂠 48|4|0
Player 1 wins.</pre>
 
(And the draw-3 rules were meant to lead to short games hmm?)
 
=={{header|Raku}}==
569

edits