I'm not really sure exactly what parts are the issue, but I'll point out
what I can:
On 05/07/2015 11:24 PM, avarisclari wrote:
Hello,
Sorry to bother you with something trivial, but I am having trouble
translating a block of code I wrote in Python over to D. Everything else
I've figured out so far. Could someone help me understand how to get
this right?
Here's the python:
scene = scenes["title"]
It looks like scenes is a dictionary that stores dictionaries of
strings? If so, then in D, scenes would be declared like this:
string[string][string] scenes;
Then the above line would be:
auto scene = scenes["title"]
while 1 == 1:
In D, while(true) is probably prefered, but that's just a matter of
style. 1==1 will work too.
next_choice = None
paths = scene["paths"]
description = scene["description"]
lines = string.split("\n")
Phobos (D's standard library) has a split:
http://dlang.org/phobos/std_array.html#split
Also one specifically for splitting lines:
http://dlang.org/phobos/std_string.html#splitLines
They're easier to use than it looks:
auto lines = description.split("\n");
or better yet (also handles mac and win-style properly):
auto lines = description.splitLines();
for line in lines:
Standard foreach:
for(ref line; lines) {
if len(line > 55):
w = textwrap.TextWrapper(width=45, break_long_words=False)
Exists in Phobos:
http://dlang.org/phobos/std_string.html#wrap
I've never actually used it myself though.
line = '\n'.join(w.wrap(line))
Join is easy:
http://dlang.org/phobos/std_array.html#join
result = (whatever array or range you want to join).join("\n")
But really, I don't think you'll need this entire loop at all, though. I
would just strip all the newlines and feed the result to Phobos's
wrap(). Probably something like:
auto description =
scene["description"].replace("\n", " ").wrap(45);
That should be all you need for the word wrapping.
decription += line +"\n"
D uses ~ to concatenate strings instead of +. In D, + is just for
mathematical addition.
print description
writeln(description);
#Shows the choices
for i in range(0, len(paths)):
foreach(i; 0..paths.length) {
path = paths[i]
menu_item = i + 1
print "\t", menu_item, path["phrase"]
writeln("\t", menu_item, path["phrase"]);
print "\t(0 Quit)"
#Get user selection
// Get user selection
prompt = "Make a selection ( 0 - %i): \n" % len(paths)
This is in phobos's std.conv: http://dlang.org/phobos/std_conv.html
So, either:
auto prompt = "Make a selection ( 0 - " ~
paths.length.to!string() ~ "): \n";
or:
auto prompt = text("Make a selection ( 0 - ",
paths.length, ~ "): \n")
while next_choice == None:
try:
choice = raw_input(prompt)
The basic equivalent to raw_input would be readln():
http://dlang.org/phobos/std_stdio.html#readln
But, the library Scriptlike would make this super easy, thanks to a
contributer:
https://github.com/Abscissa/scriptlike
Pardon the complete lack of styling in the docs (my fault):
http://semitwist.com/scriptlike/interact.html
So, it'd be:
import scriptlike.interact;
auto choice = userInput!int(prompt);
menu_selection = int(choice)
if menu_selection == 0:
next_choice = "quit"
else:
index = menu_selection - 1
next_choice = paths[ index ]
except(IndexError, ValueError):
print choice, "is not a valid selection"
if next_choice == "quit":
print "Thanks for playing."
sys.exit()
I forget the function to exist a program, but if this is in your main()
function, then you can just:
return;
else:
scene = scenes[ next_choice["go_to"] ]
print "You decided to:", next_choice["phrase"], "\n"
if sys.platform == 'win32':
os.system("cls")
else:
os.system("clear")
I've got the very last piece set up, using consoleD to use
clearScreen(), but The rest I'm not sure how to translate. Sorry for my
incompetence in advance.