Go Fish/D: Difference between revisions

m
Fixed syntax highlighting.
(D version of the Go Fish game)
 
m (Fixed syntax highlighting.)
 
(3 intermediate revisions by one other user not shown)
Line 1:
{{collection|Go Fish}}
 
Translation of Python. The gameplay is the same as the Python entry. This entry retains most of the style of the Python entry, this means cards are strings instead of an enum, etc. Usually in idiomatic D code you use enums instead, to offer stronger compile-time guarantees.
<b>Translation of:</b> Python
<lang d>import std.stdio, std.random, std.range, std.string, std.array,
std.algorithm, std.exception;
 
In D strings are only partially reference types, unlike in Python, so I have used a small class to keep the shared deck. I have also used an abstract class to simplify and reduce the code compared to the Python entry that has some code duplication.
alias replicate = std.array.replicate; //
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.range, std.string, std.array,
std.algorithm, std.exception;
 
class EndGameException : Exception {
Line 95:
/// Displays current hand, cards separated by spaces.
string displayHand() {
//return " "hand.joinbyPair.map!(key for key{k, valv} in=> hand[k].iteritemsreplicate(v))
return hand.byKey.map!(k => [k].replicate(hand[k]))
// for i in xrange(val))
string[] result .joiner.join(" ");
foreach (key, val; hand)
result ~= [key].replicate(val);
return result.join(" ");
}
 
Line 225 ⟶ 222:
} catch (EndGameException)
writeln("Game finished.");
}</langsyntaxhighlight>
 
The gameplay is the same as the Python entry.
9,476

edits