Rebol3 Code Examplex
War card game
Simulate the card game War.
Rebol [
title: "Rosetta code: War card game"
file: %War_card_game.r3
url: https://rosettacode.org/wiki/War_card_game
]
war-card-game: function/with [
"Play a game of War"
][
;; shuffle and deal alternating cards to each player
deck: fisher-yates-shuffle copy ordered-deck
clear deck1
clear deck2
foreach [a b] deck [
append deck1 a
append deck2 b
]
;; play turns until someone runs out of cards
pending: copy []
n: 1
while [not any [empty? deck1 empty? deck2]][
;; flip top card from each deck
r1: rank? c1: take deck1
r2: rank? c2: take deck2
prin format [5 5 5] reduce [++ n c1 c2]
case [
r1 > r2 [
;; player 1 wins: takes both cards plus any pending
print as-yellow "Player 1 takes the cards."
repend deck1 [c1 c2]
append deck1 take/all pending
]
r1 < r2 [
;; player 2 wins: takes both cards plus any pending
print as-blue "Player 2 takes the cards."
repend deck2 [c2 c1]
append deck2 take/all pending
]
true [
;; tie: hold cards face-down, continue to next turn
print as-red "This is a war!"
if any [empty? deck1 empty? deck2][ break ]
c3: take deck1
c4: take deck2
printf [5 5 5 "Cards are face down."] reduce [++ n #"?" #"?"]
repend pending [c1 c2 c3 c4]
]
]
;wait-for-key
]
;; announce winner
print "========================================"
print case [
all [empty? deck1 empty? deck2] ["Game ends as a tie."]
empty? deck2 ["Player 1 wins the game."]
true ["Player 2 wins the game."]
]
exit
][
deck: none
deck1: [] deck2: [] ordered-deck: []
;; build ordered deck: suits vary fastest, ranks slowest
foreach s "♣♦♥♠" [ foreach r "23456789TJQKA" [ append ordered-deck ajoin [r s] ] ]
probe ordered-deck
;; rank lookup
rank-modifiers: #[
#"A" 130 #"K" 120 #"Q" 110 #"J" 100 #"T" 90
#"9" 80 #"8" 70 #"7" 60 #"6" 50 #"5" 40
#"4" 30 #"3" 20 #"2" 10
]
rank?: func [card] [ rank-modifiers/(card/1) ]
;; standard Fisher-Yates in-place shuffle
fisher-yates-shuffle: function [list [block!]] [
len: length? list
repeat i len [ swap at list i at list (i + random (len - i)) ]
list
]
]
war-card-gameOutput:
["2♣" "3♣" "4♣" "5♣" "6♣" "7♣" "8♣" "9♣" "T♣" "J♣" "Q♣" "K♣" "A♣" "2♦" "3♦" "4♦" "5♦" "6♦" "7♦" "8♦" "9♦" "T♦" "J♦" "Q♦" "K♦" "A♦" "2♥" "3♥" "4♥" "5♥" "6♥" "7♥" "8♥" "9♥" "T♥" "J♥" "Q♥" "K♥" "A♥" "2♠" "3♠" "4♠" "5♠" "6♠" "7♠" "8♠" "9♠" "T♠" "J♠" "Q♠" "K♠" "A♠"]
1 8♣ Q♦ Player 2 takes the cards.
2 6♥ 4♣ Player 1 takes the cards.
3 3♠ J♣ Player 2 takes the cards.
4 T♥ 8♥ Player 1 takes the cards.
5 Q♣ 4♦ Player 1 takes the cards.
6 7♥ 6♠ Player 1 takes the cards.
7 9♠ 9♥ This is a war!
8 ? ? Cards are face down.
9 9♣ K♠ Player 2 takes the cards.
10 9♦ T♠ Player 2 takes the cards.
11 7♠ 7♣ This is a war!
12 ? ? Cards are face down.
13 J♦ 5♠ Player 1 takes the cards.
14 Q♠ T♣ Player 1 takes the cards.
15 K♦ A♦ Player 2 takes the cards.
16 A♥ 5♣ Player 1 takes the cards.
17 A♣ 4♥ Player 1 takes the cards.
18 5♦ 8♦ Player 2 takes the cards.
19 K♥ 3♥ Player 1 takes the cards.
20 2♠ T♦ Player 2 takes the cards.
21 K♣ 2♣ Player 1 takes the cards.
22 Q♥ A♠ Player 2 takes the cards.
23 J♥ 4♠ Player 1 takes the cards.
24 6♦ 2♦ Player 1 takes the cards.
25 2♥ 7♦ Player 2 takes the cards.
26 5♥ 3♣ Player 1 takes the cards.
27 6♥ Q♦ Player 2 takes the cards.
28 4♣ 8♣ Player 2 takes the cards.
29 T♥ J♣ Player 2 takes the cards.
30 8♥ 3♠ Player 1 takes the cards.
31 Q♣ K♠ Player 2 takes the cards.
32 4♦ 9♣ Player 2 takes the cards.
33 7♥ 9♠ Player 2 takes the cards.
34 6♠ 9♥ Player 2 takes the cards.
35 J♦ 6♣ Player 1 takes the cards.
36 5♠ J♠ Player 2 takes the cards.
37 7♠ T♠ Player 2 takes the cards.
38 7♣ 9♦ Player 2 takes the cards.
39 3♦ A♦ Player 2 takes the cards.
40 8♠ K♦ Player 2 takes the cards.
41 Q♠ 8♦ Player 1 takes the cards.
42 T♣ 5♦ Player 1 takes the cards.
43 A♥ T♦ Player 1 takes the cards.
44 5♣ 2♠ Player 1 takes the cards.
45 A♣ A♠ This is a war!
46 ? ? Cards are face down.
47 K♥ 7♦ Player 1 takes the cards.
48 3♥ 2♥ Player 1 takes the cards.
49 K♣ Q♦ Player 1 takes the cards.
50 2♣ 6♥ Player 2 takes the cards.
51 J♥ 8♣ Player 1 takes the cards.
52 4♠ 4♣ This is a war!
53 ? ? Cards are face down.
54 2♦ T♥ Player 2 takes the cards.
55 5♥ K♠ Player 2 takes the cards.
56 3♣ Q♣ Player 2 takes the cards.
57 8♥ 9♣ Player 2 takes the cards.
58 3♠ 4♦ Player 2 takes the cards.
59 J♦ 9♠ Player 1 takes the cards.
60 6♣ 7♥ Player 2 takes the cards.
61 Q♠ 9♥ Player 1 takes the cards.
62 8♦ 6♠ Player 1 takes the cards.
63 T♣ J♠ Player 2 takes the cards.
64 5♦ 5♠ This is a war!
65 ? ? Cards are face down.
66 T♦ 7♠ Player 1 takes the cards.
67 5♣ 9♦ Player 2 takes the cards.
68 2♠ 7♣ Player 2 takes the cards.
69 K♥ A♦ Player 2 takes the cards.
70 7♦ 3♦ Player 1 takes the cards.
71 A♣ K♦ Player 1 takes the cards.
72 A♠ 8♠ Player 1 takes the cards.
73 4♥ 6♥ Player 2 takes the cards.
74 Q♥ 2♣ Player 1 takes the cards.
75 3♥ T♥ Player 2 takes the cards.
76 2♥ 2♦ This is a war!
77 ? ? Cards are face down.
78 Q♦ 4♣ Player 1 takes the cards.
79 J♥ 6♦ Player 1 takes the cards.
80 8♣ J♣ Player 2 takes the cards.
81 J♦ K♠ Player 2 takes the cards.
82 9♠ 5♥ Player 1 takes the cards.
83 Q♠ Q♣ This is a war!
84 ? ? Cards are face down.
85 8♦ 9♣ Player 2 takes the cards.
86 6♠ 8♥ Player 2 takes the cards.
87 T♦ 4♦ Player 1 takes the cards.
88 7♠ 3♠ Player 1 takes the cards.
89 5♦ 7♥ Player 2 takes the cards.
90 5♠ 6♣ Player 2 takes the cards.
91 A♥ J♠ Player 1 takes the cards.
92 T♠ T♣ This is a war!
93 ? ? Cards are face down.
94 3♦ 5♣ Player 2 takes the cards.
95 A♣ 7♣ Player 1 takes the cards.
96 K♦ 2♠ Player 1 takes the cards.
97 A♠ A♦ This is a war!
98 ? ? Cards are face down.
99 Q♥ 6♥ Player 1 takes the cards.
100 2♣ 4♥ Player 2 takes the cards.
101 Q♦ T♥ Player 1 takes the cards.
102 4♣ 3♥ Player 1 takes the cards.
103 2♥ J♣ Player 2 takes the cards.
104 2♦ 8♣ Player 2 takes the cards.
105 K♣ K♠ This is a war!
106 ? ? Cards are face down.
107 J♥ 9♣ Player 1 takes the cards.
108 6♦ 8♦ Player 2 takes the cards.
109 9♠ Q♠ Player 2 takes the cards.
110 5♥ Q♣ Player 2 takes the cards.
111 T♦ 9♥ Player 1 takes the cards.
112 4♦ 3♣ Player 1 takes the cards.
113 7♠ 8♥ Player 2 takes the cards.
114 3♠ 6♠ Player 2 takes the cards.
115 A♥ 7♥ Player 1 takes the cards.
116 J♠ 5♦ Player 1 takes the cards.
117 A♣ 6♣ Player 1 takes the cards.
118 7♣ 5♠ Player 1 takes the cards.
119 K♦ 5♣ Player 1 takes the cards.
120 2♠ 3♦ Player 2 takes the cards.
121 Q♥ T♠ Player 1 takes the cards.
122 6♥ T♣ Player 2 takes the cards.
123 A♠ 7♦ Player 1 takes the cards.
124 A♦ 9♦ Player 1 takes the cards.
125 8♠ 4♥ Player 1 takes the cards.
126 K♥ 2♣ Player 1 takes the cards.
127 Q♦ J♣ Player 1 takes the cards.
128 T♥ 2♥ Player 1 takes the cards.
129 4♣ 8♣ Player 2 takes the cards.
130 3♥ 2♦ Player 1 takes the cards.
131 J♥ 8♦ Player 1 takes the cards.
132 9♣ 6♦ Player 1 takes the cards.
133 K♣ Q♠ Player 1 takes the cards.
134 K♠ 9♠ Player 1 takes the cards.
135 4♠ Q♣ Player 2 takes the cards.
136 J♦ 5♥ Player 1 takes the cards.
137 T♦ 8♥ Player 1 takes the cards.
138 9♥ 7♠ Player 1 takes the cards.
139 4♦ 6♠ Player 2 takes the cards.
140 3♣ 3♠ This is a war!
141 ? ? Cards are face down.
142 7♥ 2♠ Player 1 takes the cards.
143 J♠ T♣ Player 1 takes the cards.
144 5♦ 6♥ Player 2 takes the cards.
145 A♣ 8♣ Player 1 takes the cards.
146 6♣ 4♣ Player 1 takes the cards.
147 7♣ Q♣ Player 2 takes the cards.
148 5♠ 4♠ Player 1 takes the cards.
149 K♦ 6♠ Player 1 takes the cards.
150 5♣ 4♦ Player 1 takes the cards.
151 Q♥ 6♥ Player 1 takes the cards.
152 T♠ 5♦ Player 1 takes the cards.
153 A♠ Q♣ Player 1 takes the cards.
154 7♦ 7♣ This is a war!
========================================
Player 1 wins the game.