Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 90 91 [92] 93 94 ... 796

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

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 #1365 on: February 08, 2012, 11:25:36 pm »

it cleans up code and makes it so you won't be typing the same thing over and over again.
here's a story that explains WHY you should use user defined functions it in terms a child could understand
Spoiler: Very Long Story (click to show/hide)

There are 2 different kinds of user defined functions, value returning functions and void functions,
a value returning function returns a value in place of when the function is called,  and a void function replaces the calling of the function with the statements in the function definition.


Spoiler: Void Function (click to show/hide)

Those were just 2 basic examples, I didn't feel like giong into detail with things like function signatures or go into detail about the formal and actual parameter list.  If you still need help though I can go into further detail.
« Last Edit: February 08, 2012, 11:33:25 pm by Valid_Dark »
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 #1366 on: February 08, 2012, 11:52:16 pm »

Teaching programming is fun, isn't it Valid_Dark? ;)
Spoiler (click to show/hide)
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

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 #1367 on: February 08, 2012, 11:55:44 pm »

It is, it just takes a very very very long time to type out teaching materials.
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.

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1368 on: February 08, 2012, 11:57:01 pm »

I've almost got the basic ideas down from class. Here's the last thing I did yesterday before I had to go (with lots of sometimes-contradictory help from the teacher and fellow students):
Spoiler (click to show/hide)
It ain't pretty or clean, but it works well enough. Now I'm trying to use the same ideas in a new assignment, but it's slow going. I kind of understand the point, but something about the syntax and structure of it all is hitting a brick wall in my mind.

Thanks for typing all that out though.
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 #1369 on: February 09, 2012, 12:03:43 am »

Ok guys, quick c# lesson today!
Now one of the fundamental ideals of the c# language is that things similar in concept should be similar in syntax. This is why we use those cool getters are setters that are identical in syntax to normal fields. There is also a way to make this work for some methods too that makes a static method seem non-static, when it really should be non-static.

Let's take for example a random number generator.
Spoiler (click to show/hide)
Using the standard RNG that comes with the .net framework, we can get ints, bytes and doubles. Well that is cool, but what if we wanted to get a random bool? Pretty simple!
Spoiler (click to show/hide)
But this is something we might have to do a lot in something like level gen, so it would be annoying and a little silly to repeat this line of code over and over and over. Instead it would make a lot more sense to make a function for it!
Spoiler (click to show/hide)
There we are. Nice, neat, atomic and generic. Everything we could ever need in a function, right? Well let's see how it looks next to those other functions.
Spoiler (click to show/hide)
Sort of stands out, doesn't it? It doesn't make sense for a function that is similar in concept to three others to have a different syntax. Lucky for us, c# offers a way to staple new methods onto already defined classes, using the 'this' keyword! Methods defined in this way are more limited than normal methods. For a start, these methods will not have access to a classes private attributes, this is to stop you from using it as a way to hack into the tasty interior of a class, rather than use it to keep your code neat and understandable. Secondly, these methods must be defined in a static class, to make sure that the method of the called class is not changed by the calling class through polymorphism shenanigans, because that would be conceptually stupid.

Now, let's update that code.
Spoiler (click to show/hide)
As you can see I added 'this' before the first parameter. This tells the compiler that the first parameter will be used for calling the method, rather that inside the parameters. It can only be used once, and any other parameters you want to add will need to go inside the brackets as normal.
This not a hack, it compiles exactly the same as using a normal static method, it doesn't even make the method thread safe, you still need to do that yourself. It does however make your code a lot more understandable. After all, it is damn annoying trying to remember where you put that method when it should be attached to the class.

Sirus

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

Is it just me, or does C# look a lot like Java?
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

Gatleos

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

It is, it just takes a very very very long time to type out teaching materials.
...You didn't copy over that story by hand, did you?
I've almost got the basic ideas down from class. Here's the last thing I did yesterday before I had to go (with lots of sometimes-contradictory help from the teacher and fellow students)
Maybe it's just that I had a bad experience with them, but these CS 101 courses never seem to try and teach students the big picture about programming. Instead of teaching the concepts of programming separately, they start by mashing everything together and presenting it to you in an extremely limited way, non-representative of the actual concepts they're trying to get across. Students don't know why they're doing something for an assignment, just that they're supposed to do it. Which is a really crummy technique when you're teaching something abstract like mathematics or programming.
Is it just me, or does C# look a lot like Java?
It's all part of Microsoft's campaign to assimilate the entire universe one concept at a time by copying it, improving it, then making sure it's only compatible with other Microsoft products. :P
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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1372 on: February 09, 2012, 12:22:31 am »

Is it just me, or does C# look a lot like Java?
Yes, it looks a lot like Java indeed! A simple class written in Java will often compile in c# with maybe a few changes to keywords.
This is because c++ and Java were two big influences in it's development, and c++ was one of the biggest influences in Java. They all follow similar rules that were established waaay back in c, but still, Java defiantly played a big part in c#. So much so that if Microsoft developed the framework to operate on other OS, it would be just as portable, but they don't because they are propriety little bitches ever since it worked so well for Apple. So once again, we can blame Apple for all the evil in the world.

Some people would personify c# as Java's younger brother, all dressed up in a business suit and tie, while Java go around in a hippie poncho.

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 #1373 on: February 09, 2012, 12:34:20 am »

It is, it just takes a very very very long time to type out teaching materials.
...You didn't copy over that story by hand, did you?


I didn't copy the story by hand, it was written by a friend and I copy/pasted it, but it still takes a long time to type everything else.  Which is why I stopped working on my C++ tutorial website.
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.

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1374 on: February 09, 2012, 01:51:59 am »

Can I impose on this thread to once again provide some help? I am attempting to write a program that will calculate the surface area and volume of a cylinder, based on input values for the radius and height. It wouldn't be so difficult if we weren't also required to create user-defined functions to do pretty much everything. Thus it is roughly twice as long as it needs to be.
Spoiler (click to show/hide)

Anyway, I thought I had everything set up right, but building returned a few errors and it refuses to run. Mostly involving conversions from double to float (wtf? I defined no doubles) and not having enough arguments to call printOutput back in the main function.
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

fergus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1375 on: February 09, 2012, 01:56:38 am »

Code: [Select]
void printOutput(float, float);
it's going of this, not this when determining the number and type of inputs.
Code: [Select]
void printOutput(float x)

Anyway, I thought I had everything set up right, but building returned a few errors and it refuses to run. Mostly involving conversions from double to float (wtf? I defined no doubles)
What lines are these errors on?
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!

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1376 on: February 09, 2012, 02:00:09 am »

Quote
return (((2 * 3.14) * (x * x)) + ((2 * 3.14) * (x * y)));

return (3.14 * (x * x) * y);
These two.

For the other thing, should I change it to this?
Code: [Select]
void printOutput(float x, float y)
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

Gatleos

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

Are you sure those double to float messages aren't compiler warnings? And yes, the definition of the function printOutput function needs to be changed to match the declaration above.
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

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1378 on: February 09, 2012, 02:07:20 am »

Looks like they might just be compiler warnings, actually. I got rid of one of the floats in the declaration, and it built this time (got some warnings though). It also looks like it ran, but I need to do the math on something else to make sure it's working right.

Code: [Select]
void printOutput(float);
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

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1379 on: February 09, 2012, 02:07:26 am »

@LNCP: Minecraft computers wouldn't be too much of a nightmare so long as you thought through the clock cycle well enough. Repeaters are regular enough to ensure that. An ALU in particular wouldn't be too bad at all; in a few minutes you can easily put together bitwise circuits with copy/paste PLAs like this bitwise adder, complete with a carry bit. A couple more logic gates, copy and paste the arrangement for n bits, and you have an n-bit full adder. Edit: Actually, if you do a cascading addition, you can just use 1 set with a 1 bit memory cell for the carry, though there are faster methods involving more circuitry.
Spoiler (click to show/hide)
Note that PLAs are very useful in implementing circuits; any bitwise function can be represented with them and they require pretty much no thought at all to implement. :)
« Last Edit: February 09, 2012, 02:22:33 am by alway »
Logged
Pages: 1 ... 90 91 [92] 93 94 ... 796