Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 40 41 [42] 43 44 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100914 times)

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #615 on: April 20, 2012, 10:13:40 pm »

This is actually a bit of dark compiler magic. Whenever you have identical string constants in a program, they are all assigned the same memory address (reference), so the == comparison works since they're all the same object. However, if you were to enter the string during runtime, then it would not work.
So you mean that I can't use this method for comparing a user-inputted String to a String in my program? Ok, I can live with that contstraint - currently all my String compares are just within the program, and in my other project (which actually uses user input) I'm just grabbing chars and ints, which seems to work just fine.

Quote
As for a condescending attitude, blame it on the internet. Imagine me reading them not in a haughty tone but in a helpful tone. I'm trying to be as straightforward as possible, so I am indeed being quite terse and unapologetic, which more often then not comes across as condescending without the benefit of body language or intonation.
Ah, ok - no offense taken. I agree and totally blame it on the text format.

GalenEvil

  • Bay Watcher
    • View Profile
    • Mac-Man Games
Re: Programming Help Thread (For Dummies)
« Reply #616 on: April 20, 2012, 11:57:44 pm »

I had a problem like that with a program I was working on. Turned out it was because of static cling also :P took an hour to find the problem, and decided it would take another hour to fix the problem so i just duct-taped it in place with a few extra variables and a temporary Object to use as a transfer object from place to place wherever it was needed. It worked but it's messy, so should probably go and rework that. Maybe it will actually be able to parse through the few hundred thousand things it needs to do in a little less time if i remove duct-tape from the equation :P

GalenEvil
Logged
Fun is Fun......Done is Done... or is that Done is !!FUN!!?
Quote from: Mr Frog
Digging's a lot like surgery, see -- you grab the sharp thing and then drive the sharp end of the sharp thing in as hard as you can and then stuff goes flying and then stuff falls out and then there's a big hole and you're done. I kinda wish there was more screaming, but rocks don't hurt so I guess it can't be helped.

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #617 on: May 29, 2012, 08:12:50 pm »

I've been wanting to return to fumbling around with C++ for a while now, but I'm not sure about how to start. I'm planning on making a roguelike game, but last time I tried I just used the console to display the text before reading about how it's better to use an ASCII graphics library or something. I feel like an idiot for asking this, but what am I supposed to use? I've tried Ncurses and OpenGL for graphics in the past, but I wasn't really able to get either of them to work.

And on an unrelated note, does anyone here know how to get HTML to backtrack through folders to load files?
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #618 on: May 29, 2012, 08:13:13 pm »

EDIT: Gah, stupid internet. Please ignore the accidental double-post.
« Last Edit: May 29, 2012, 08:41:51 pm by Araph »
Logged

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #619 on: May 30, 2012, 05:39:02 am »

For the unrelated note, use: ../../../image.png to load image.png from three folders up.

IMO using the console is fine, but you could also use something like SDL if you wanted freeform control, or libtcod if you wanted something that behaves more like a console.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #620 on: May 30, 2012, 05:43:03 pm »

IMO using the console is fine, but you could also use something like SDL if you wanted freeform control, or libtcod if you wanted something that behaves more like a console.
Thanks! I think I'll try using libtcod.

For the unrelated note, use: ../../../image.png to load image.png from three folders up.
I understand how to load from folders that are within the same folder the HTML file is in, but I don't know how to load from previous folders. Like, where there are two paths:

.../folder1/folder2/main.html
and
.../folder1/folder3/image.png

Like that.
« Last Edit: May 30, 2012, 06:43:52 pm by Araph »
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #621 on: May 30, 2012, 06:48:48 pm »

For the unrelated note, use: ../../../image.png to load image.png from three folders up.
I understand how to load from folders that are within the same folder the HTML file is in, but I don't know how to load from previous folders. Like, where there are two paths:

.../folder1/folder2/main.html
and
.../folder1/folder3/image.png

Like that.

"../../../image.png" isn't in the same folder.  ".." (that's specifically two dots), here actually means "up a level"[1], as distinct from your recognisably informal way of using "..." (three dots, as an ellipsis) to indicate a "here be dragons" unknown/unsaid in your example.  I've a feeling that the confusion about these two is the reason why you didn't recognise Thief^'s answer for what it was, though.

Extending your example a bit, imagine you have

.../folder-1/folder0/folder1/folder2/main.html <= Your page
.../folder-1/folder0/folder1/folder3/image.png <= An image of a blue ball
.../folder-1/image.png <= An image of a red ball

Using "../../../image.png" would go up[2] to the folder1 level (the first "..") and again to the folder0 level (the second "..") and up again to the folder-1 level (the third "..") and then retrieve the "image.png" from there (the image of the red ball).

To get "image.png" of the blue ball, "../folder3/image.png" would go up a level to folder1 and then into folder3 and get the "image.png" from there.  This would work regardless of the names of folders, and number of layers, above folder1 (and doesn't even need to know the name of "folder1").

If "folder1" is directly under the root of the file-system (for all intents and purposes), then "/folder1/folder3/image/png" would also work (i.e. "from the root" with "/" and straight on down), whereas if the "..." vagueness in the modified examples, that I put, were actually irrelevant because folder-1 were sitting directly in the root, then it would be "/folder-1/folder0/folder1/folder3/image/png".

In general, though, I'd avoid absolute paths unless you know that they'll be as you currently see them forever more.  You could move/copy the contents of "folder1" into a different folder called "folderX" and the "../folder3/image.png" would work just as well (up into folderX, down into folder3, access the image.png file there), albeit that you might now not have "../../../image.png" accessible, unless either the folder-1 version of the file were transplanted to a location similarly above the folderX as it were above folder1, or if (as it happens) folderX is also a subdirectory of folder0.

If you write your code to use relative directories (up and down), but never going higher than the top level of the common directory which you might move around, or onto different systems, you don't need to worry about whether your pages are stored under the "/var/web/html1/" structure or the "/home/yourusername/public_html/" one or (perhaps, on an MS system) "C:\webfolder\" (assuming your particular implementation does the logical thing and let you use unix-style forward-slash directory separators, even while the command-prompt or folder address bar details insists on using the backslash system.)



Sorry, I've a feeling I've just confused the issue there, but I think you can still follow it, barring any silly typos or thinking-related errors....


[1] In most circumstances.  Practically all circumstances, to be honest, at least within the scope of how the OS understands (or wishes to convey) the file-structure.  About the only time you don't have a ".." to go to a parent folder is if the folder you are 'currently' in, or in the process of moving through with your breadcrumb-trail, is the 'root' folder which has nothing above it, insofar as you are concerned.  I don't think you'll encounter the other situations often/regognisably enough to need to know the differences from the norm.

[2] And when I say "it would go up", and then up/down some more, I don't mean that future file-references would start from that new location.  It counts for this file reference only.  To use the same file a second time in a row would need the same (or a functionally equivalent) path given.
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #622 on: May 30, 2012, 06:53:59 pm »

D'oh! My bad, and thanks for clarifying! I didn't notice the difference between two and three periods.

Sorry, I've a feeling I've just confused the issue there, but I think you can still follow it, barring any silly typos or thinking-related errors....

I think I understand what you've been saying.
« Last Edit: May 30, 2012, 06:55:52 pm by Araph »
Logged

Whitefoxsniper

  • Bay Watcher
  • something meaningful.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #623 on: May 30, 2012, 11:27:06 pm »

Is there a scripting language that a non programmer user would be able to use that I could then parse? Sortof like XML but even easier for a non programmer to write?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #624 on: May 31, 2012, 02:05:43 am »

No. The moment you start writing in a scripting language you're a programmer.
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #625 on: May 31, 2012, 04:42:50 am »

I read that question as "is there an easier markup format than XML, for others to use, I am the programmer who will then brute-force others' creations into 'running', through my own existing programming skills".  But I might be wrong.

However, if I'm along the right lines, you might want to go the whole hog, Wfs, and make your own "building block" interface.  Depends what the markup (if so it truly is) you want is intended to do, of course.  And there are plenty of visual XML editors out there (if that's what you're actually building upon), as well as any number of XML-parsing libraries.

Or you can design your own scheme from scratch.  For an application of my own (something I wrote in Perl, which I didn't want to have to re-write every time the format of the material I grabbed changed) I bunged together my own 'script' language, below which is one example:
Code: [Select]
SET |chan|4
SET |schedhead|Programmes - FM Schedule,
SET |band|/fm
SET |root|http://www.bbc.co.uk/radio{chan}/programmes/schedules{band}/
SET |page|
SET |prev|

INFORM Forward Loop Is Starting
LOOP nextday START
  INFORM Forward Loop Has Started

  SET |this|{root}{page}
  INFORM Looking for "{this}"
  INFORM Coming from "{prev}"

  GET ?|{this}|{prev}
  FAILURE REPORTS |Unable to get page "{this}"
  FAILURE ENDS LOOP nextday |
  SAVE radio/temp{chan}.txt

  CLEAR |ch,dd,mon,yyyy
  IDENTIFY ?|Radio (.+) {schedhead} \w+ \s?(\d+) (\w+) (\d+)|ch,dd,mon,yyyy
  FAILURE QUITS |Schedule header mismatch on {page}

  IDENTIFY ?|<li class="(first-child)">|dummy
  FAILURE ENDS LOOP nextday |No "first-child" link on {page}, so probably has no programme info

  CONVERT |mon|mm|
  FORMAT |datechan|{yyyy:04d}{mm:02d}{dd:02d}-{ch:s}
  SAVE radio/{datechan}.txt

  IDENTIFY ?|<link rel="next" title="\w+ \s?\d+ \w+ \d+" href="/radio{chan}/programmes/schedules{band}/(\d+/\d+/\d+)"|nextlink
  FAILURE QUITS |No "Next" link on {page}

  SET |page|{nextlink}

  IDENTIFY ?|<link rel="prev" title="\w+ \s?\d+ \w+ \d+" href="/radio{chan}/programmes/schedules{band}/(\d+/\d+/\d+)"|prevlink
  FAILURE QUITS |No "Prev" link on {page}

  SET 1|back|{prevlink}
  SET 1|backfrom|{this}
  INFORM Identified Backlink of {back} to come from {backfrom}

  SET |prev|{this}

  INFORM Forward Loop Is Restarting
LOOP nextday END
INFORM Forward Loop Has Ended

SET |backcount|10
INFORM Backward Loop Is Starting
LOOP prevday START
  INFORM Backward Loop Has Started

  DEC |backcount|
  TEST |{backcount} > 0
  FAILURE ENDS LOOP prevday |

  INFORM Backward Loop Is Restarting
LOOP prevday END
INFORM Backward Loop Has Ended

As you can see, a bit of a kludge (note the difference between a simple "SET" which assigns to a 'variable' without question and a "SET 1" assignation that only assigns if not already done so, but you can really ignore that and the "prevday" loop, which I was just using to test features that I had ensorcelled into the parsing script that I wasn't otherwise using when I was trying that out), and I'm not even sure if it would still correctly grab and handle the chosen target, or if I'd have to make changes to the basic spec (as mostly stored within the variables at the top, so that I can easily swap and change as needed).


I'm sure you could do better. ;)
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #626 on: May 31, 2012, 07:57:53 am »

YAML is probably one of the easiest to teach as markup languages go, if you don't have any intricate data structures you need to use.
Logged

Whitefoxsniper

  • Bay Watcher
  • something meaningful.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #627 on: May 31, 2012, 02:26:03 pm »

I'm doing this for a job and I think I'm going to end up making it myself here's exactly what I'm doing:

The program is supposed to use a set of user commands to check whether the web aplication is running correctly. We want non programmers to be able to change the steps. An example of one of the commands would be like :

goto url=www.subdomain.website.com/index


except if someone has already made a extendable user friendly scripting language I'd rather use that.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #628 on: May 31, 2012, 02:38:19 pm »

You'll have to roll your own set of tools I'm afraid. While there are some languages that are simple enough for this, using a full-blown scripting language would introduce all kinds of security risks and depending on the functionality needed, hardening the system may cost more time than implementing the tools needed.
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #629 on: May 31, 2012, 03:19:46 pm »

You may not want anything like what I showed being used, but I've just dug up the raw Perl for my previous handler (in case I didn't make it clear, I'd converted it to a self-contained executable, and that was the reason why I couldn't easily modify the interior rules).
 
While I won't put the specifics of the getting web-pages and such here (it'd depend on your style with whatever language you want to implement it in, anyway), I think that you can probably take the following as pseudo-code, if you wanted to pinch any conceptual ideas from it.  (I recall being quite happy with the looping and logic constructs.)  Not that it's particularly brilliant, but sounds like (if you're going to be "rolling your own") you'd be having to work something out not unlike this, anyway.

Code: [Select]
sub InstructHandle { my @Instructs = @INSTS;
  INSTRUCT: while (@Instructs || @LOOP) {
    unless (@Instructs) { @Instructs = @LOOP }
    my $Instruct = shift @Instructs;
       $Instruct=~s/^\s+//;

    # Comments and spacer lines
    if ($Instruct=~m/^#/)    { next INSTRUCT; }
    if ($Instruct=~m/^\s*$/) { next INSTRUCT; }

#print "Instruction: '$Instruct'\n"; next INSTRUCT;

    ## Inward
    if ($Instruct=~m/^SET (.*)\|(.+)\|(.*)$/) { Instruct_SET($1,$2,$3); next INSTRUCT; }
    if ($Instruct=~m/^DEC (.*)\|(.+)\|(.*)$/) { Instruct_DEC($1,$2,$3); next INSTRUCT; }
    if ($Instruct=~m/^CLEAR (.*)\|(.+)$/)     { Instruct_CLEAR($1,$2);  next INSTRUCT; }
    if ($Instruct=~m/^GET (.+)\|(.+)\|(.*)$/) { Instruct_GET($1,$2,$3); next INSTRUCT; }
    if ($Instruct=~m/^LOAD (.+)\|(.*)$/)      { Instruct_LOAD($1,$2);   next INSTRUCT; }

    ## Process
    if ($Instruct=~m/^IDENTIFY (.*)\|(.+)\|(.+)$/)       { Instruct_IDENTIFY($1,$2,$3);   next INSTRUCT; }
    if ($Instruct=~m/^TEST (.*)\|(\S+) ([<=>]+) (\S+)$/) { Instruct_TEST($1,$2,$3,$4);    next INSTRUCT; }
    if ($Instruct=~m/^CONVERT (.*)\|(.+)\|(.+)\|(.*)$/)  { Instruct_CONVERT($1,$2,$3,$4); next INSTRUCT; }
    if ($Instruct=~m/^FORMAT (.*)\|(.+)\|(.+)$/)         { Instruct_FORMAT($1,$2,$3);     next INSTRUCT; }

    ## Display
    if ($Instruct=~m/^INFORM (.+)$/) { Instruct_INFORM($1); next INSTRUCT; }
#    if ($Instruct=~m/^PRINT$/)      { Instruct_PRINT();   next INSTRUCT; }
#    if ($Instruct=~m/^PRINT (.*)$/) { Instruct_PRINT($1); next INSTRUCT; }
 
    ## Outward
    if ($Instruct=~m/^SAVE (.*)$/) { Instruct_SAVE($1); next INSTRUCT; }

    ## Nonlinear Control
    if ($Instruct=~m/^FAILURE REPORTS \|(.*)$/) { Instruct_INFORM($1."\n\t/!\\ $ERROR") if $WFAIL; next INSTRUCT }
    if ($Instruct=~m/^FAILURE QUITS \|(.*)$/) { Instruct_QUIT($1) if $WFAIL; next INSTRUCT }
    if ($Instruct=~m/^LOOP (\w+) START$/) { my $loopname = $1;
      print "\t(i) Entering loop '$loopname'\n";
      # First, store away any current @LOOP.
      push @STACK, [@LOOP] if @LOOP; @LOOP = ();
      # Now, separate out the inner loop and store the remainder of the @Instructs and this loopname
      SUB: while (@Instructs) { my $SubInstruct = shift @Instructs;
        unless ($SubInstruct=~m/^LOOP $loopname END$/) { push @LOOP, $SubInstruct; next SUB }
        push @STACK, [@Instructs]; @Instructs = ();
        push @STACK, $loopname;
        next INSTRUCT; # Which will take head of any known @LOOP instructions
      } # END SUB: while (@Instructs)
      die "\t/!\\ No LOOP $loopname END encountered before end of Instructs\n";
    } # END if ($Instruct=~#LOOP $loopname START#)
    if ($Instruct=~m/^FAILURE ENDS LOOP (.+) \|(.*)$/) { my ($loopname,$msg) = ($1,$2); if ($WFAIL) {
      Instruct_INFORM($msg); $WFAIL = 0; @LOOP = ();
      while (@STACK) { my ($instarray,$loopid) = (shift @STACK,shift @STACK); # loopname and remaining instructions in container
        if ($loopid eq $loopname) { @Instructs = @$instarray; @LOOP = shift @STACK if (@STACK); next INSTRUCT }
        shift @STACK if @STACK; # now unnecessary @LOOP storage
      } # END while (@STACK)
      die "\t/!\\ De-STACKing error while exiting loop $loopname!\n";
    } next INSTRUCT } # END if ($Instruct=~#FAILURE ENDS LOOP $loopname#)
  #  if ($Instruct=~m/^COUNT (\w+) SET (\d+)$/) { my ($countname,$val) = ($1,$2); $COUNT{$countname}=$val; next INSTRUCT; }

    ## Error Trap
    die "\t/!\\ Unknown I: [$Instruct]\n";
  } # END while (@Instructs)
} # END sub InstructHandle

Note some comments that I never expunged from testing/alternative implementations... ;) However, there's also (I'm pleased to see!) a decent compliment of explanatory comments and labels, too, which I must say I don't always bother with for code that I was never thinking I'd share!  Also, while I could have written the code tighter, it looks like I was going for readable instead.  How well I did, I leave up to you. ;)

You should of course realise that any given "Instruct_FOO(...)" is of course the 'handler' function, for the outputs (if any[1]) of the relevant regexp that came to be tested true, with @LOOP and @STACK and various other variables being elsewhere-defined globals ("Bad Starver, don't use global variables, Starver!") to contain state information.  I think they're largely self-explanatory.


Oh, one more thing I used, was for the "places where a {variable} would be added back in".  Each Instruct_FOO() modified their params, so that they had the ability to pre-process these incoming parameters and resolve such assignments before getting on with business.  Given that the the SET functions pushed their assignments into a hash called %WVARS (i.e. $WVARS{'name'}='value', in the manner of Perl code), this was done with the following method to Interpolate (wrong word, but it worked for me at the time) the values...

Code: [Select]
sub Interpolate { for my $i (0..$#_) { while ($_[$i]=~m/\{(\w+)\}/) {
  my $var0 = $1;
die "\t/!\\ Interpolation could not find var [$var0]!\n" unless exists($WVARS{$var0});
  my $var1 = $WVARS{$var0};
  $_[$i]=~s/\{$var0\}/$var1/;
  print "\t(i) Interpolation: [{$var0}]=>[$var1] ==> [$_[$i]]\n" if ($ILEVEL>1);
} } } # END sub Interpolate
I squashed the block-delimiters together a little, for some reason, it operates directly on the essentially called-by-reference implicit array of @_ (i.e. $_[0] through to $_[last]), and I don't like the variable names I used, but it should still otherwise be readable[2]!


[1] In these functions, even though you can't see, I have pipe-delimited the input parameters.  Often the very first (between the full FOO-statement and the first pipe delimiter) I've reserved for a character such as "?", "1", "+" or whatever, which (according to the context of the called FOO-statement) would say "I expect a true/false out of this", "write-once only", "append to existing" or whatever.  But probably none of that is of any use to you.

[2] The most obscure part of this might be that $ILEVEL is the "Inform Level, which was set by me at development time", which here gives an equivalent to an "if debug>0"-like control over how much automatic output there is over and above the scripted INFORM messages!  I had different bits functions respond to different $ILEVELs, while working on this.  I forget if there was any logic behind those relative values, however. ;)  Absolutely fatal errors, I appear to have left as a standard "die" 'print comment and terminate' instruction in the bare code, though.
Logged
Pages: 1 ... 40 41 [42] 43 44 ... 91