Nabend, ich hatte gerade Lust zu Golfen, möchte das Ergebnis gerne teilen, und erwarte mit einer kürzeren Version konfrontiert zu werden ;-)
Hier das Kartenspiel "The Game 100" für einen Spieler auf der Konsole (730B):
use 5.012;use warnings;use List::Util qw(shuffle);my@c=shuffle(2..99);my@h=
splice@c,0,7;my@s=([1],[1],[100],[100]);while(){for(0,1){say
"\n\n\tA\tB\tC\tD\t\t",~~@c," cards";for my$i(0..99){my@s=map{$s[$_][$i]||" "
}0..3;(~~grep$_ ne" ",@s)?say"\t",join"\t",@s:(say"\nhand: ",join", ",sort{$a
<=>$b}@h)&&last}exit say"=== YOU LOSE ===\n","# Todo: ",@c+@h if sub{for my$c
(@h){p($_,$c)&&return 0for@s}1}->();my($s,$c);do{print"> ";($s,$c)= ~~<STDIN>
=~/([AaBbCcDd])\s*(\d\d?)/;$s=$s[ord(uc$s)-ord"A"]if$c}until$c&&p($s,$c)&&
grep$_ eq$c,@h;push@$s,$c;@h=grep$_ ne$c,@h;exit!say"=== YOU WIN ===\n"if!(
@c+@h)}push@h,pop@c while$#h<6&&@c}sub p{my($s,$c)=@_;my$t=$s->[-1];$s->[0]==
1?$t<$c||$c==$t-10:$t>$c||$c==$t+10}
Und eine lesbare Variante mit Erklärung zum Spiel (pod):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use 5.012;
use warnings;
use List::Util qw(shuffle);
my @cards = shuffle(2..99);
my @hand = splice @cards, 0, 7;
my @stacks = ([1], [1], [100], [100]);
while () {
for (0,1) {
say "\n\n\tA\tB\tC\tD\t\t", ~~@cards, " cards";
for my $i (0..99) {
my @row = map { $stacks[$_][$i] || " " } 0..3;
(~~grep $_ ne " ", @row)
? say "\t", join "\t", @row
: (say "\nhand: ", join ", ", sort {$a<=>$b} @hand)
&& last
}
exit say "=== YOU LOSE ===\n", "# Todo: ", @cards + @hand if sub {
for my $c (@hand) {
can_play($_, $c) && return 0 for @stacks
}
return 1
}->();
my ($stack, $card);
do {
print "> ";
($stack, $card) = ~~<STDIN> =~ /([AaBbCcDd])\s*(\d\d?)/;
$stack = $stacks[ord(uc $stack) - ord "A"] if $card;
} until $card && can_play($stack, $card) && grep $_ eq $card, @hand;
push @$stack, $card;
@hand = grep $_ ne $card, @hand;
exit !say "=== YOU WIN ===\n" if !(@cards + @hand);
}
push @hand, pop @cards while $#hand < 6 && @cards;
}
sub can_play {
my ($stack, $card) = @_;
my $top = $stack->[-1];
return $stack->[0] == 1
? $top < $card || $card == $top - 10
: $top > $card || $card == $top + 10;
}
__END__
=pod
Es ist noch ganz frisch, daher muss ich eventuell noch ein paar Fehler finden. In meinen Tests hat bislang aber alles funktioniert wie erwartet.
Falls jemand noch andere Kartenspiele kennt -- z.B. auch für Zweispieler (an der selben Tastatur) --, die sich gut zum Golfen eignen, würde ich mich über Vorschläge freuen :-)
best regards,
Matze
Last edited: 2017-01-13 08:17:04 +0100 (CET)