Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 117 118 [119] 120 121 ... 796

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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1770 on: February 27, 2012, 08:44:21 pm »

So if z had a value of say, 5, the loop would run 5 times before breaking off on the sixth run.
That is correct, yes.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1771 on: February 27, 2012, 09:32:15 pm »

So today I had some free time at school, decided to start laying the groundwork for a dungeon crawler...
and it turns out that C doesn't have a String variable. WHAT.

According to the textbook (some 300-400 pages ahead of what we're currently working on), the way you work around it is with a modified char variable that stores the fake!String as a bunch of separate characters. Needless to say, I am not getting it to work properly yet.

Indeed. It's not really a modified char variable. It's an array of chars.

Code: (strings.c) [Select]
#include <string.h>

int main() {
char name1[] = {'M', 'e', 'g', 'o', '\0'}; // Defining the C-string using the typical way to define an array.
                        // Note the \0 at the end, it is essential.

char name2[] = "Mego"; // Defining the C-string the special way that char arrays can be defined.
// No trailing \0, because that is added for us.

char* name3 = "Mego"; // Taking advantage of array/pointer duality in C.

if(!(strcmp(name1, name2) || strcmp(name2, name3)))
return -1; // name1, name2, and name3 all contain the same value.
// strcmp is a function that takes two char arrays as arguments.
// If the arrays are identical, it returns 0.
// Else, it returns a value that indicates which array has the greater value when characters first differ.

printf("%d, %d, %d\n", strlen(name1), strlen(name2), strlen(name3));
// strlen returns the length of the char array passed as an argument, minus the trailing \0 character.

char name4[14];

strcat(name4, name1); // Concatenate all the strings into one array, name4.
strcat(name4, name2);
strcat(name4, name3);

printf("%s\n", name4);

return 0;
}

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1772 on: February 27, 2012, 09:43:50 pm »

Alright, I managed to get the program to read a name and return it properly, which is better than earlier. I still think it's bs that there isn't an actual String function, but whatcha gonna do?
Code: [Select]
int newChar()
{
//Local Declarations
char name[9];

printf("\n First, what is thy name?");
printf("\n Your name is...");
scanf("%8s", &name);

printf("\n Welcome, %s. I have been expecting you.", &name);

return;

}
Part of the function for creating a new character. I'm thinking this will be a semi-roguelike; permanent death ftw.
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1773 on: February 27, 2012, 09:46:31 pm »

Strings themselves are just nice wrappers that go around arrays of chars (Most of the time, there are buffered strings that work more like a linked list) however, c doesn't have classes, so that all becomes a little more tricky...

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1774 on: February 28, 2012, 04:12:27 am »

I must admit I'm a little amused right now... Malloc, perhaps you should go study programming a little with the professionals before you critique a design patten, especially in the way you have described.

I don't know that you find amusing, please elaborate?

Also, when arguing, no need to get personal. This sort of ad hominum argumentation just makes you look mean.
All I tried to point out was you could get by just fine without singletons.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1775 on: February 28, 2012, 04:20:01 am »

Pointing out that you lack tertiary training isn't exactly a personal attack... Anyway, if you really think that just because you don't need to use that design patten is a good reason to not use it, then fine, do as you like and best luck, but don't go strutting around and not expecting to be challenged.

The point still remains that singleton helps in separation of logic, thus allowing you to keep other classes agnostic as to what is going on inside your little black box.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1776 on: February 28, 2012, 04:32:18 am »

I'd have to agree with malloc that the comment is indeed mean. Concluding from someones opinion that he lacks training isn't nice, even when you're right. And it is an ad-hominem, since it's about his training when we were talking about singletons. The fact Our experience that they work better in a real-world scenario than in the theoretical "perfect design" scenario can be discussed without commenting on his hypothetical lack of work experience.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1777 on: February 28, 2012, 04:46:08 pm »

Pointing out that you lack tertiary training isn't exactly a personal attack...

No it probably is not, but coming up with a convincing argument on why I am so wrong to have my opinion would probably be better, instead of saying I must lack experience because I don't share your opinion.
Besides, how would you even know how much training I have had? I admit that I am nothing but a CS student, and that my experience developing a major code bases is limited to a few projects, I done alone and with some friends. (Yeah, it involves a game engine, something all CS students do at some point. A basic web server, a university assignment. A very slow ray tracer, I actually used a singleton in this one for resource management, but that was before I realized there is no real need for them, so shame on me and my double standards)

But according to your profile you're 20, which limits how much seriously real world software development experience you've had yourself (see what I did there?), so who are you to judge my experience?


Anyway, I actually have a question.
I have two different methods for computing a value, one involving extra logical comparisons and one involving extra floating point arithmetic. Funny thing is, on my AMD machine, the extra arithmetic one seems to be performing faster. While on my intel machine the extra comparison based one seems to be performing faster.
It makes me wonder what other parts of my code will perform differently across processors. If there any rule of thumb for what CPU's does what best, or is it random? Does anyone have any experience with it?
« Last Edit: February 28, 2012, 07:48:18 pm by malloc »
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1778 on: February 28, 2012, 05:03:17 pm »

Pointing out that you lack tertiary training isn't exactly a personal attack...
...

Anyway, I actually have a question.
I have two different methods for computing a value, one involving extra logical comparisons and one involving extra floating point arithmetic. Funny thing is, on my AMD machine, the extra arithmetic one seems to be performing faster. While on my intel machine the extra comparison based one seems to be performing faster.
It makes me wonder what other parts of my code will perform differently across processors. If there any rule of thumb for what CPU's does what best, or is it random? Does anyone have any experience with it?

The answer to your question is deep magic. I do have experience with it, but its rusty experience and probably not entirely accurate anymore.
It depends on very particular qualities of the chip and how your compiler optimizes its code. In your case, I believe that INTEL has better branch prediction in its pipeline than AMD, so the logical comparisons are more efficient on intel hardware.
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.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1779 on: February 28, 2012, 05:18:32 pm »

Pointing out that you lack tertiary training isn't exactly a personal attack...
...

Anyway, I actually have a question.
I have two different methods for computing a value, one involving extra logical comparisons and one involving extra floating point arithmetic. Funny thing is, on my AMD machine, the extra arithmetic one seems to be performing faster. While on my intel machine the extra comparison based one seems to be performing faster.
It makes me wonder what other parts of my code will perform differently across processors. If there any rule of thumb for what CPU's does what best, or is it random? Does anyone have any experience with it?

The answer to your question is deep magic. I do have experience with it, but its rusty experience and probably not entirely accurate anymore.
It depends on very particular qualities of the chip and how your compiler optimizes its code. In your case, I believe that INTEL has better branch prediction in its pipeline than AMD, so the logical comparisons are more efficient on intel hardware.
It's a combination of hardware architecture, the closely linked topic of the Instruction Set Architecture (ISA) being used by it, as well as the microarchitecture controlling the flow within the instructions. Essentially it's a result of that magical place in the computer hierarchy where hardware and software meet.
« Last Edit: February 28, 2012, 05:20:07 pm by alway »
Logged

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1780 on: February 28, 2012, 05:24:01 pm »

I see. This brings me to my next question, having these two methods, it seems it could be smart to actually optimize these things as you catch them, especially for performance critical code. But is there a clean way to do this? Having two codebases and compiling two different versions of your application seems stupid overly complicated. I would like to keep compiler optimization such as inlining, so virtual functions and function pointers probably won't be an option either.
So, I guess I am left with templating?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1781 on: February 28, 2012, 05:32:46 pm »

I see. This brings me to my next question, having these two methods, it seems it could be smart to actually optimize these things as you catch them, especially for performance critical code. But is there a clean way to do this? Having two codebases and compiling two different versions of your application seems stupid overly complicated. I would like to keep compiler optimization such as inlining, so virtual functions and function pointers probably won't be an option either.
So, I guess I am left with templating?
If the language you're using supports it, you could use preprocessor macros and have a single codebase that compiles differently according to the target hardware.
Also note that the language used becomes of importance in this case. For example, it may be worthwhile to write parts of your function in in-line assembly in such a case.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1782 on: February 28, 2012, 11:57:58 pm »

ok, I need some schooling,  so far I've only been working in main, made programs that were thousands of lines long, but was all in main,  Recently I was talking to someone about SDL, and they linked me a tutorial they were having trouble with
It was an easy tutorial about making a star field that had random stars going random speeds, but when I downloaded the example code,  It was split among 10 different C file / header files, and I wasn't able to compile it.
what / when is the proper way / time to get out of main?  And why cant I compile this 10 file program?
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #1783 on: February 29, 2012, 12:03:39 am »

To be honest, that Robo-Juggler project was probably the right time for you to get out of main. That is, once you need to start using classes.

What is preventing the code from compiling? Is everything included in your project? Are you getting a linker error? Undefined variable errors?
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

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1784 on: February 29, 2012, 12:20:26 am »

Does anyone have an idiot's guide to OOP? I keep reading the book and watching youtube tutorials and I just don't get it.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting
Pages: 1 ... 117 118 [119] 120 121 ... 796