Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 137 138 [139] 140 141 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 906688 times)

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2070 on: March 29, 2012, 04:53:30 pm »

Do you intend to keep the grammar stateless/single state when you implement it? Using states for the scanner would make strings, escape characters and comments easier to implement.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2071 on: March 29, 2012, 05:43:28 pm »

Words take up less space because you don't have to wrap them in quotes. You can also insert arbitrary white space between words for formatting purposes that would corrupt literal strings. One of my original goals was to have exactly 1 way to treat whitespace: ignore it. I may have to settle for slightly more complex set of rules if strings are allowed.

Yes, I would like to keep it stateless. But at the moment that isn't really going to be possible due to the "only 1 attribute with the same name in a container".
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2072 on: March 29, 2012, 06:06:04 pm »

Is that really and issue for the lex or the parser though? I'd save reporting conflicting attribute names for later on in the process.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2073 on: March 29, 2012, 06:23:18 pm »

You can actually cut down the amount of reserved characters to just three rarely used characters (being [ | and ]. Or alternatively { | and } if you prefer that) by using a combination of the square opening bracket and an identifier to handle the different functions (note, I don't know anything of markup languages, so this may look a bit apocryphal:

@[ title | this is the title of this document]
@[ body | This is the body of the document, in which we have among others a comment c[ This is an unmarked comment] c[ note 1 | This is a marked comment, which can be used for metadata] several elements [ This is an element in the body's root without identifier] [ note 2 | This is an element called note 2] r[ note 2] c[here we copy note 2 again] f[ echo | note 1, note 2]]


d[ echo | var 1, var 2 | \n r[ var 1 ] \n r[ var 2 ] \n]


In this short example, I've used the following identifiers:
@ for elements
c for comments
r for referencing named elements
f for function calls
d for defining functions
and no identifier for introducing elements.


And another example that hopefully makes more sense and has prettier formatting:
Code: [Select]
@[ Title | Example MNML document, with Virexian syntax]
@[ Body |
  [ Table of Contents | f[ Make Table of Contents | r[ Chapter 1 ] r[ Chapter 2 ]]]
  [ Chapter 1 |
    @[ Caption | This is the first chapter of this document ]
    @[ Body    | In which I would like to inform you that I have absolutely nothing to say   c[ I realy need to think of a more inspiring text to put here]
                 but damn it, I'm going to say it anyway!]]
  [ Chapter 2 |
    @[ Caption | This is the second chapter of this document, in which we have embedded a figure ]
    @[ Body    |
      [ Image 1 | @[ Picture | f[ Load Image | \foo\bar\quz\random_picture.jpg]]]
                  @[ Subscript | Look at that pretty picture!]]]]

Hmm, on second thoughts, this does yield a lot of superfluous parenthesis, but I guess it's better than xml.
« Last Edit: March 29, 2012, 06:49:46 pm by Virex »
Logged

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2074 on: March 29, 2012, 08:00:27 pm »

Hey programming thread, been a long time.  I've been kinda busy getting ready to start my actual programming job.  How cool is that?

But in the meantime, I've got a project I need finished, and it heavily relies on messing with Strings.  In this operation, I need to take a String of any possible length, a width of a column, and then break that String into rows of that width.  Ideally, I can do it in such a way that if a word falls on the breakpoint, it'll backout that row until it hits a space.  Also ideally, I can input a maximum number of rows, and if the last row extends beyond width, it'll both backout to the width and then back up thrice more for an ellipses.  The whole thing is inside an array for returning.

I haven't actually put this into the compiler yet, so there's probably a few library-methods with bad syntax or something but it should be obvious what I'm doing (in C#).  The real question is the logic.  The only thing that isn't obvious is the GFT(whatever) method, which is a preexisting method (GetFormattedText) for processing Strings into a printable form (I'm using the Windows Presentation Format library), which returns an object-thing including a value 'Width' describing how long it will be on the screen.

Code: [Select]
public string[] RowBreaker (string enterString, int maxRows, int colWidth)
{
char[] using = enterString.ToCharArray(); //breaks into chars
string[] output = new string[maxRows];

for(int i = 0; i < maxRows; i++) //initializes output
output[i] = null;

int position = 0;
int counter = 0;

while (counter < maxRows) //operates on each row
{
while ( GFT(output[counter]).Width < ( GFT("A").Width * (colWidth - 1) ) || position < using.Length ) //fills a slot with a string char by char
{
output[counter] += using[position];
position++;
}

string spaceCheck = output[counter];
int tempPosition = spaceCheck.Length;

while ( !( using[position + tempPosition].IsSpace() ) && spaceCheck.Length > 0 ) //backs up until it hits a space or the beginning of the row
{
char[] temp = spaceCheck.ToCharArray()
spaceCheck = null;

for(int i = 0; i < (temp.Length - 1); i++)
spaceCheck += temp[i];

tempPosition--;
}

if ( spaceCheck.Length > 0 ) //if it doesn't hit the end of the row, stores that result and notes the position in using[] for the next row
{
output[counter] = spaceCheck;
position -= tempPosition;
} //if the row would have backed to the beginning (finds no spaces), it ignores that process and leaves the original result in place

if ( counter = ( maxRows - 1 ) && !( output[counter].IsNull() ) && GFT(output[counter]).Width > ( GFT("A").Width * (colWidth - 1) ) ) //checks if this is the last row, and contains something
{
for (int j = 0; j < 3; j++) //backs up three spaces, the row should already have been the width of the column
{
char[] temp = output[counter].ToCharArray()
output[counter] = null;

for(int i = 0; i < (temp.Length - 1); i++)
output[counter] += temp[i];
}

for(int i = 0; i < 3; i++) //adds ellipses
output[counter] += ".";
}

counter++;
}

return output[];
}

So yeah, I'm a little out of practice, but I think this should work (once I have all the right terms in place).  Any thoughts at a glance?
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2075 on: March 29, 2012, 08:40:15 pm »

Disclaimer: We all know I don't know C#.  I'm inferring from what I know of other languages.

You initialize position outside of a big while loop.  It gets incremented in there, but never decremented or reinitialized.  That means it's still going to be quite large the second time through the while loop.  counter is getting modified in a few places in the loop, but never reinitialized.  I'm assuming you wouldn't place a while loop if you didn't intend to go through it a few times, so you might want to move those initializations.  I don't have time to look through it with much detail; those just stuck out at me.

I wish looking at my own stuff was as easy as looking at other peoples'.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2076 on: March 29, 2012, 08:52:05 pm »

So remember that project I was working on earlier, when the totality of my issues summed to linking stuff with codependent classes using interfaces?

Yeah, I miss that. Now I've got pointers that, when stored in a std::vector, magically transform into NULL pointers. Cue segfaults.

I'm going to post a link to download the project in tar.gz format. It's got a Code::Blocks project file included with it. For the life of me, I can't figure out why this is happening, so I'm hoping someone else can spot something.

A little background on the project, now. It's based off of the Runelords book series by David Farland. A major part of the series is the ability for one person to give another person one of their attributes (strength, sight, wit, etc.) through a magical process called giving an endowment. As a programming challenge, I wanted to see if I could recreate this process in a C++ environment, and if so, perhaps make a game based off the book series.

So, without further ado, Runelords++.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2077 on: March 29, 2012, 09:04:01 pm »

Mego, why the crap are you defining the Person class in fwd.h?

Anyway, for some reason, C++ is upset about you assigning a predefined list to that array.  I don't quite know enough C++ to know why.  What I do know is that when I replaced that line with a for loop that initialized all eleven elements to zero, it compiled.

Code: [Select]
Person::Person() {
    name = "";
    //attributes = {0,0,0,0,0,0,0,0,0,0,0};
    for(int i = 0; i < 11; i++) {
        attributes[i] = 0;
    }
    dedicated = NULL;
    hasDedicated = false;
}
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2078 on: March 29, 2012, 09:11:02 pm »

The project is not laid out well, I know. I just recently made the change where I scrapped the interface classes and just moved both the Person and Endowment classes to the same header. I didn't bother changing the name of the header, though.

As for the initializer list, that's valid using the C++11 standard. Pass the option to the compiler that makes it use the C++0x/C++11 standard (or use the compile.sh script I included, if you're using g++).

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2079 on: March 29, 2012, 09:21:49 pm »

Yeah, I'm running Linux and I did use your compile shell script.  It whined with the initializer list and compiled without it.  Running it didn't show anything too immediately obvious, other than it printing a memory address, but I didn't dig into what I actually should have been expecting.  What exactly am I looking for and where am I supposed to find it?  I don't see where you're actually using a vector and I'm too lazy/busy to look through all your code for it.

PS - That only sounds rude.  Not intending it to be.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2080 on: March 29, 2012, 09:32:08 pm »

It's odd that the compile script didn't work. I had no problem with it. What version of g++ are you using?

The vector is in the Person class (fwd.h and Person.cpp). It holds pointers to all the Endowments that Person has received. When Person::giveEndowment(Endowment* e) is called, e has an actual value in memory. But, when that method calls Person::getEndowment(Endowment* e), e is suddenly a NULL pointer. Sorry, I should've explained that earlier.

The executable should print something like the following:

Code: [Select]
King Sylvarresta -> Raj Ahten: WIT (50)

King Sylvarresta: {BRAWN:50, STAMINA:50, METABOLISM:50, GRACE:50, GLAMOUR:50, WIT:0, SIGHT:50, HEARING:50, TOUCH:50, TASTE:50, SMELL:50}
Raj Ahten: {BRAWN:90, STAMINA:90, METABOLISM:90, GRACE:90, GLAMOUR:90, WIT:90, SIGHT:90, HEARING:90, TOUCH:90, TASTE:90, SMELL:90}

0x80020438

0

King Sylvarresta: {BRAWN:50, STAMINA:50, METABOLISM:50, GRACE:50, GLAMOUR:50, WIT:0, SIGHT:50, HEARING:50, TOUCH:50, TASTE:50, SMELL:50}
Raj Ahten: {BRAWN:90, STAMINA:90, METABOLISM:90, GRACE:90, GLAMOUR:90, WIT:90, SIGHT:90, HEARING:90, TOUCH:90, TASTE:90, SMELL:90}

Those two hex values are the values of the Endowment pointer in Person::giveEndowment(Endowment* e) and Person::getEndowment(Endowment* e), respectively.

EDIT: Armokdamnit. I re-wrote the project in Java and it works perfectly. You're behind this somehow, Stargrasper.
« Last Edit: March 29, 2012, 09:55:54 pm by Mego »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2081 on: March 29, 2012, 10:05:41 pm »

It's black magic.  If I taught you about it before you started worshiping the JVM, a tear in space-time would appear and destroy us all.

Anyone awake here know C reasonably well?  I've got an array of this struct and I need to make the array bigger.  realloc and memcpy both hate me.  Any ideas?
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2082 on: March 29, 2012, 10:13:18 pm »

I can help you with that one, by using some C black magic.

You see, in C, pointers and arrays are functionally identical. What does that mean? Well...

Code: [Select]
/* Initialize */
int* arr = (int*)malloc(5*sizeof(int));

/* Use the pointer like an array */
int val = arr[2];

/* Realize you need more space */
arr = (int*)realloc(arr, 10*sizeof(int));

/* Enjoy! Don't forget to free! */
free(arr);

/* Set arr to NULL so you know not to try to use it again, since it currently points to unallocated memory. */
arr = NULL;

Replace int with the name of your struct, and you're golden.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2083 on: March 29, 2012, 10:24:48 pm »

Sorry, I imply that.  I never use actual arrays.  I take full advantage of the array-pointer duality.  Still can't figure out why realloc isn't working...but joy of joys, the server I ssh into to do homework just went offline...the assignment is due in about an hour and a half....I already haven't slept in three days...sometimes computer science pisses me off....
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2084 on: March 29, 2012, 10:47:43 pm »

What does your code look like?
Pages: 1 ... 137 138 [139] 140 141 ... 796