Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 164 165 [166] 167 168 ... 796

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

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2475 on: May 29, 2012, 02:37:40 pm »

I haven't tested it due to having written it up in about 5 minutes and not having time for testing, but this C++ code *should* work for base conversion, if you want to try to use it as a reference.

Spoiler (click to show/hide)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2476 on: May 29, 2012, 04:29:07 pm »

Pseudocode:

Code: [Select]
string numToBaseStr (int baseTo, int num) {
    if (num < baseTo) return toDigit(num);
    lastDigit = num % baseTo;
    return numToBaseStr(baseTo, num/baseTo).append(toDigit(lastDigit));
}

num BaseStrToNum (int baseFrom, string numStr) {
    if (numStr.length() = 1) return fromDigit(numStr);
    lastDigit = fromDigit(numStr.lastChar());
    restOfNum = numStr.withoutLastChar();
    return baseFrom * baseStrToNum(baseFrom, restOfNum) + lastDigit;
}
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2477 on: June 01, 2012, 05:16:14 pm »

Nope. I have absolutely no clue how a pointer goes from non-NULL to NULL, just from being passed to a function. After the function call, it returned to its normal, non-NULL value.

Sounds like variable shadowing from a global or something to me.
Logged
Through pain, I find wisdom.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2478 on: June 01, 2012, 05:47:21 pm »

Nope. I have absolutely no clue how a pointer goes from non-NULL to NULL, just from being passed to a function. After the function call, it returned to its normal, non-NULL value.

Sounds like variable shadowing from a global or something to me.

This project had absolutely 0 globals. Everything was passed around via function parameters.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2479 on: June 01, 2012, 06:37:57 pm »

Nope. I have absolutely no clue how a pointer goes from non-NULL to NULL, just from being passed to a function. After the function call, it returned to its normal, non-NULL value.

Sounds like variable shadowing from a global or something to me.

This project had absolutely 0 globals. Everything was passed around via function parameters.

If it isn't a case of an overloaded name, I would guess that something is stomping on the pointer after a copy has been pushed onto the stack for the function call. Could be a write outside the range of an array or a pointer that gets pointed to the wrong place.
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.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2480 on: June 01, 2012, 10:47:05 pm »

The base converter is done! :D

What it does: Accepts a string*, an int that represents the base the string is in, an int that represents the base the string should be changed to. The string is changed.

Internally, there is one quirky thing I had to work around: when I input 4.0625, it recognized it as 4.06249999991 or something. Just 0.0625 goes to 0.0625. I did this:

Spoiler (click to show/hide)


Split the number I use to store the base10 representation of the input string into intpart and fpart, then add them together at the end. Works good. :P

The reason I posted the source up there is because I want to know what I could do to optimize that code, or better ways to do stuff.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2481 on: June 01, 2012, 11:06:57 pm »

I'm just going to jot down things as I see them.

1. Am I blind, or does CheckString not actually modify stopfunction? Also, why not just let the exception fall through CheckString to Convert?

2. Your error messages should probably be going to cerr and not cout.

3. Why are you using pointers for all your strings? References seem like they would be the more logical choice.

4. You could probably get rid of all the boolean flags with some nifty exception throwing. For example, in your catch blocks, you could add "else throw e" at the end to make the exception fall through.

5. Using brackets to enclose an if-statement inside an else-statement makes code difficult to read in my opinion. "else if(cond) { expr }" is just as valid as "else { if(cond) { expr } }". In addition, nesting those else's inside each other is not really necessary. Execution will go through all of the if- and else-if-statements until the condition matches, it finds an else without an if, or it runs out of statements to check. You can just make a long line of them without nesting and have the same result.

6. A quick way to get rid of weird floating-point arithmetic stuff like representing 4.0625 as 4.06249999... is to format the output to use the same number of decimal (or whatever base you're using) places as the input. It also gives you the bonus of using significant figures, which scientists will love. Also, string formatting is an important skill to learn. There's a lot of different ways to do this specific type, so experiment.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2482 on: June 02, 2012, 12:35:27 pm »

Does anyone here have experience embedding python into a c++ application? If so, can I ask you about a million questions?
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2483 on: June 02, 2012, 07:49:26 pm »

Well. I spent a few hours today working on MNML again.

Changes: changed the attributename/value separator to | because ; is common and could cause annoying escaping issues for a lot of data (particularly code).

Added strings that do not require most reserved characters to be escaped and also are capable of containing whitespace as data.

rewrote the grammar.

wrote a partial (and extremely hackish) bit of c# capable of creating a mnml string. It would create a  valid mnml string from an object structure if I had the validation of attributeNames, Strings and Words in the objects. I will do that, and eventually make a "pretty printer". as well as a reflection library capable of putting arbitrary c# data objects into mnml form.

MNML

Spoiler: Preface: (click to show/hide)

Spoiler: Introduction: (click to show/hide)

Spoiler: Overview: (click to show/hide)


Spoiler: Formal Grammar: (click to show/hide)



javascript implementation:
pending...

java implementation:
pending...

« Last Edit: June 02, 2012, 07:53:16 pm by Nadaka »
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.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #2484 on: June 03, 2012, 08:18:10 am »

So I finally got my "Zander" engine working with my experiments for playing around with concurrency. It works as follows:

At the lowest level you have tasks and a thread pool. The thread pool takes in tasks and executes them on one of n threads.
Tasks can have children, nexts and shared nexts.
A child is pushed onto the thread pool when the task is finished executing.
A next is pushed onto the thread pool when the task, and all of the tasks children, and all of the nexts of the tasks children, and so-on down the graph, are finished executing.
A shared next is a next which has to wait for multiple tasks to fulfil the above conditions before being enqueued.

This lets you build up huge, dynamic graphs of program flow without caring about how many threads you have, the system is guaranteed to work on any number of threads > 0. You can even use nexts and shared nexts as join points so the need for explicit low-level synchronisation (mutexes and so-on) is reduced.

The game main pushes itself onto this thread pool as a root task. It creates the graphics and audio drivers, and prepares the resource cache (which can load from passed resource sources, defaulting to the file structure but this can be changed to custom sources, with a physfs-based system for loading from archives being included in the engine). Then it starts the state manager looping on the pool.

The state manager maintains a stack of states and a list of "event managers" that have two steps: dispatch, where they send out events, and poll, where they gather information about which events they need to dispatch. States maintain their own task graph for update and draw logic, which is passed to the state manager. The state manager does the following things in a series of steps every frame, with each sub-step being executed concurrently:

Step 0:
- Delete any removed managers from the system.

Step 1:
- Check if any states exist. If not, close main.

Step 2:
- Get the draw commands from each state
- Flush the inputs from the window.
- Update audio drivers if required.
- Dispatch the event managers.

Step 3:
- Update the logic for each state that can be updated by placing their logic into one long task chain (each state can be concurrent internally, but no two states will update at the same time).
- Flush the draw commands from step 2 onto the screen, updating the display.
- Poll the event managers.

The next step is to enqueue step 0 again. Due to the nature of the thread pool, this will effectively cause the system to loop again.

It's complete overkill, but still kinda nifty to play around with :P Plus it makes debugging in a multithreaded environment easier, because I can instantly rule out or link the problem to timing issues and crashes by dropping n to 1. If it still goes wrong, it's the code. If not, it's the threading. And it makes things easier to unit test and identify problems, you can just isolate chunks of the task "graph" to test.

Now I just need to actually make something with it XD Might refactor the task system out into a more std-like syntax first and release that separately...
« Last Edit: June 03, 2012, 08:28:10 am by MorleyDev »
Logged

rutsber

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2485 on: June 06, 2012, 01:12:04 am »

Does anyone know what is wrong with this code? It's supposed to find whether a number is prime or not. It's from a book on C++ programming, so I would assume it would work.
Spoiler (click to show/hide)

The error I'm getting is:
Spoiler (click to show/hide)

Ninja'd. I was working on including this before you posted. It's supposed to find out if a given number is prime or not.
« Last Edit: June 06, 2012, 01:20:42 am by rutsber »
Logged
Gave me an idea. I'm gonna add the milkable tag to the male minotaur. MMMMmmm minotaur cheese.
A loud angry voice and instinct. "FUCK OFF URIST THIS TABLE IS MINE!"

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2486 on: June 06, 2012, 01:17:24 am »

When you say something is wrong with the code, what exactly is happening (and what is supposed to happen)?
Logged

fergus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2487 on: June 06, 2012, 01:56:17 am »

Changing
Code: [Select]
int n;
to
Code: [Select]
long n;
should work.
Logged
BY THE GODS! THIS QUOTE MADE MY SIG BOX HAVE A SCROLL BAR! HAPPY DAYS INDEED!
BY THE GODS! YOU HAVE TOO MANY SIGS!

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #2488 on: June 06, 2012, 02:59:31 pm »

Hey, it's been a while since I posted in here. I was hoping to get some advice on something.

See, I'm currently in the process of coding a dating sim (Don't ask. I'm serious, DON'T ASK) and just realized how complicated even a simple dialogue tree could get with conditional statements and such. I'm really stumped, I've never coded something quite like this before. So, what do you guys think would be the best design pattern for an arbitrarily-extensible dialogue tree? Hard-coding each individual response and stringing together a web of function calls (or goto statements, oh god) seems like it would be insane to attempt and just bad design all-around.

The only thing I can think of is to make a simple scripting language that has a couple commands for inserting character expressions, sound effects, etc. and then loading it in and parsing it in real-time as the user moves through the dialogue tree. But I need to define a set of dialogue choices for each node on the tree too, and GAH
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2489 on: June 06, 2012, 03:06:08 pm »

Yeah I know what you mean. I've never coded something as comprehensive as a dating sim, but I have done branching dialog with NPCs and stuff, and the best way is to make a simple tool or scripting language. That way all you need to do is type in the dialog and flip some switches, and you've got a fully functional conversation.

I went with the "make a tool" method so I can keep track of everything, but having a text parser so you can write everything up (special commands and all) in notepad works too.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.
Pages: 1 ... 164 165 [166] 167 168 ... 796