RCRPG/D: Difference between revisions

m
→‎Code: Oops!
(Added a br to improve formatting)
m (→‎Code: Oops!)
 
(3 intermediate revisions by 2 users not shown)
Line 3:
[[D]] version of [[:Category:RCRPG|RCRPG]], with very basic input validation.
 
===Code===
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.typecons, std.random, std.string, std.conv,
std.array, std.range, std.algorithm, std.traits, std.typetuple;
 
Line 37:
bool[string] passages;
 
this(in string[] items = null) {
this.passages = directions.byKey.zip(repeat(false))
.assocArray();
this.items = items.dup;
}
 
string describe(in P3 location) const {
auto result = "You are at ";
if (location in roomNames)
Line 55:
result ~= "\nExits are: ";
string[] exits = this.passages.byKey
.filter!(k => passages[k])()
.array();
if (exits.empty) exits = ["None"];
result ~= exits.map!capitalize().join(", ");
return result;
}
Line 132:
}
 
void newalias(in string newAlias, in string[] command...) {
if (command.length == 2) {
if (command[0] in aliases) { // avoid recursive aliasing
Line 178:
if (idx != -1) {
if (!this.equipped.empty)
this.unequip();
this.inv = this.inv.remove(idx);
this.equipped = itemName;
Line 196:
}
 
void name(in string[] newRoomNameTokens...) {
roomNames[this.currentPos] = newRoomNameTokens.join("' "');
}
 
Line 210:
direction);
if (joinRoomPos !in this.rooms)
this.rooms[joinRoomPos]= new Room(make_random_items());
this.rooms[joinRoomPos]
.passages[opposite_dir(direction)] = true;
Line 219:
}
 
void processArguments(in string[] a, World w) {
foreach (f; __traits(derivedMembers, World)) {
static if (isCallable!(mixin("World." ~ f))) {
Line 237:
 
void main() {
auto world = new World();
writeln("Welcome to the dungeon!\nGrab the sledge && make your" ~
" way to room 1,1,5 for a non-existent prize!");
while (true) {
writeln("\n", world.look());
write("> ");
auto tokens = readln().strip().toLower().split();
if (tokens[0] == "quit")
break;
Line 265:
 
writeln("Thanks for playing!");
}</langsyntaxhighlight>
9,476

edits