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.
<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.
class EndGameException : Exception {
Line 95:
/// Displays current hand, cards separated by spaces.
string displayHand() {
//return
return hand.byKey.map!(k => [k].replicate(hand[k]))
}
Line 225 ⟶ 222:
} catch (EndGameException)
writeln("Game finished.");
}</
|