Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2 3   Go Down

Author Topic: Programming Q & A Round II  (Read 8492 times)

0 Members and 1 Guest are viewing this topic.
Programming Q & A Round II
« on: February 11, 2011, 05:34:14 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
As a tutor in the field, I am currently sick of the state of today's programmers.  It makes me cringe when I see graduate students typing things like:

Code: [Select]
string = "name";

list.add(string);

As such I'm continuing Infini's programming Q & A in an attempt to save the burning shipwreck that are today's programmers!  As with last time, please post relevant information (source code, errors etc).  Keep in mind this is not a "How to build an engine" thread :)
Logged



i love big weenies and i cannot lie
Re: Programming Q & A Round II
« Reply #1 on: February 11, 2011, 10:22:54 am »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Very well. Here is a short (possibly dumb XD) gamemaker question.

Code: [Select]
if (x= 1 or x = 2 or x = 20) {do something};
Can this be written down in a simpler/more elegant manor? In SQL I believe you can check x against the collection {1;2;20} with one command. I'm not sure wether this is possible in other languages though, like in this case gamemaker.
Logged
Re: Programming Q & A Round II
« Reply #2 on: February 11, 2011, 12:22:19 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Doesn't something like this work:
if (x=1|2|20) {do something};
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Programming Q & A Round II
« Reply #3 on: February 11, 2011, 12:30:46 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
No, doesn't seem to work. Condition 1 (x=1) is recognized, but not the rest.
This is the type of anwser I'm looking for though.
Logged
Re: Programming Q & A Round II
« Reply #4 on: February 11, 2011, 12:41:35 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Posts: 61
It seems to work like this:
if (x==1||2||3) {do something};
So basically Darklight's version with double |s.

Attached is the GM7 source.
Logged
Re: Programming Q & A Round II
« Reply #5 on: February 11, 2011, 12:52:47 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Thank you. I'll look at it in a minute. And what about the AND command?
Logged
Re: Programming Q & A Round II
« Reply #6 on: February 11, 2011, 02:10:24 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
It seems to work like this:
if (x==1||2||3) {do something};
So basically Darklight's version with double |s.

Attached is the GM7 source.
Yeah, I should have been using doubles :P just my bad coding habits thanks to gamemaker since it doesn't force you to use == for comparisions.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Programming Q & A Round II
« Reply #7 on: February 11, 2011, 04:07:06 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
For and you can use &&.
Logged



i love big weenies and i cannot lie

Xiphirx

wat
Re: Programming Q & A Round II
« Reply #8 on: February 11, 2011, 04:53:06 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
How to engine? :(
Logged
  • For The Swarm
Re: Programming Q & A Round II
« Reply #9 on: February 11, 2011, 06:04:22 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
It seems to work like this:
if (x==1||2||3) {do something};
So basically Darklight's version with double |s.

Attached is the GM7 source.

Wait that actually works in Game Maker? lol
I'm not really sure if you can pull that shortcut elsewhere.

I could be wrong, but in more higher-level programming languages you'd see that as:
Code: [Select]
if (x == 1 || x == 2 || x == 3)
{
    .....
}
Logged
  • https://colbydude.com
Re: Programming Q & A Round II
« Reply #10 on: February 11, 2011, 06:28:00 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Very well. Here is a short (possibly dumb XD) gamemaker question.

Code: [Select]
if (x= 1 or x = 2 or x = 20) {do something};
Can this be written down in a simpler/more elegant manor? In SQL I believe you can check x against the collection {1;2;20} with one command. I'm not sure wether this is possible in other languages though, like in this case gamemaker.
No the only way to do it is by
Code: [Select]
if(x == 1 || x == 2 || x == 20) { do something; }
or you could do:
Code: [Select]
switch(x)
{
case 1:
case 2:
case 20:
    do something;
    break;
}

It seems to work like this:
if (x==1||2||3) {do something};
So basically Darklight's version with double |s.

Attached is the GM7 source.
Yeah, I should have been using doubles :P just my bad coding habits thanks to gamemaker since it doesn't force you to use == for comparisions.
No that does not work. Your example is also really faulty in that you have if(x == 1 || 2 || 3 || 4), thus in your thought it only would not draw something when it is 0. Thus x = floor(random(4)). But in your example it always draws a circle at each press of the enter key. This is because the expresion x == 1 || 2 || 3 || 4 always evaluates to true. Even when I remove 2 and 3. In short your answer is wrong.

1) GM does not really have boolean values. Anything of 1 and higher is true and anything of 0 and lower is false.

2) the '||' operator is a binary logic operator, not a list operator. And the emphasis is on binary, which takes 2 boolean expresions.

Now lets examine the expresion 1 || 2 || 20. I wil put '(' and ')' to show each indivdual boolean expression and slowly evaluate the expresion from left to right. And remember 1 and higher equals true.

Step 1: ( ( ( 1 )  || ( 2 )  ) || ( 20 ) )
Step 2: ( ( true  || ( 2 ) )   || ( 20 ) )
Step 3: ( ( true  || true )   || ( 20 ) )
Step 4: (         true           || ( 20 ) )
Step 5: (         true           ||  true  )
Step 6:                  true

Now that you have resolved the logical operator, GM will continue with the comparison operator '=='. And something equals 'true' evaluates to true. Thus when x is 4 it still evaluates to true. See the attachement for a GM7 file that shows that it goes completely wrong.
« Last Edit: February 11, 2011, 06:30:48 pm by Niek »
Logged
Re: Programming Q & A Round II
« Reply #11 on: February 11, 2011, 06:31:04 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
You just beat me to it. It doesn't work for string comparisons.
Logged
Re: Programming Q & A Round II
« Reply #12 on: February 11, 2011, 06:34:12 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
You just beat me to it. It doesn't work for string comparisons.
neither for numbers, it just does not work.

MG, why don't you educate everyone on the proper use of if() statements. I see way to often that consecutive if statements are used, which are mutual exclusive without the use of 'else'. GM is the same as Java, C, C++ and C#.

Also the use of operators and the precedence order.
« Last Edit: February 11, 2011, 06:57:56 pm by Niek »
Logged
Re: Programming Q & A Round II
« Reply #13 on: February 11, 2011, 09:17:22 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Ok then.

There are 3 types of decision constructs.  If then else, if else if, and the switch (select if you're using VB).  All 3 of these will get you the same results, it's just different ways of doing it.

If then else:

Code: [Select]
if (expression)
{
     statement;
}

Lets examine this: If the expression is true...we do the statement.  This can also be written as:

Code: [Select]
if (expression == true)
{
    statement;
}

Notice the second equals.  This is the comparison operator and it is imperative that you understand this for languages outside of GM and VB.  This operator will return either a value of true or false, depending on the two values being compared.

You can also say expression == false, or !expression.

Extending this further, we can add an else:

Code: [Select]
if (expression)
{
    statement;
}
else
{
   statement;
}

What this will do is if the expression is evaluated to false, it will skip the first statement and move to the second.  Logically, this is saying, if this is true do this...otherwise, do that.

If else if

The if else if takes the previous statement further and does this:

Code: [Select]
if (expression1)
{
   statement;
}
else if (expression2)
{
   statement;
}
else
{
   statement;
}

This allows you to check multiple expressions in one construct.  Similar to how the if then else went to the else when the expression was false, this will follow through each expression until it finds one that is true, and then executes the related statements.  The else at the bottom is optional, and if it's there it will run if none of the other expressions are true.

Switch/Select

This one is a little different.  It has some silly functionality depending on the language.  First, you can only check one variable against your expressions.  Second, depending on your language, you cannot use >, <, >=, or <= in your expressions.  This limitation is present in C/C++, Java and C#.  The switch is structured as follows:

Code: [Select]
switch (variable)
{
   case x:
      statement;
      break;

   case y:
     statement;
     break;

   default:
     statement;
     break;
}

Notice the cases.  Those are your expressions.  This will take the variable that you have specified and check it against each case.  (i.e. if variable equals x, then case x will execute).  When it finds Should a case not be found that matches, the default case will run, similar to the else expression.  

Please note that the Switch and else if are the same thing in different forms with slightly different functionality.  Sometimes you will be required to use one over the other, however (such as the case where you need to use a >= for example).

Keep in mind in the switch and else if, when a true expression is found, the rest of the expression following are skipped.

Now, onto boolean logic...

We have 2 binary operators.  And, or, represented as && and || respectively.  There is also ! which represents not.

In the case of an And statement, both expressions MUST be true for the expression to evaluate true.

if (5 == 5 && 6 == 2)

This is false, because 6 != 2.

if ( 5 == 5 && 5 == 5 )

This however, is true.

As for the or, only one expression has to be true.

if ( 5 == 5 || 5 == 2)

this is true.

if ( 5 == 2 || 5 == 5)

This is also true.

if ( 5 == 2 || 6 == 3)

This is not true.

Also keep in mind if the first expression in an or is true, then the program will skip evaluating the second expression (there's no point, you already know it's true).

Now, lets throw the not in there...

if ( 5 != 2 && 6 != 2)

This is true, because 5 is not equal to 2 and 6 is not equal to 2, which we specified with the !.

I need to run out now, if anyone feels they need to continue this, please feel free to do so.  I'll leave some practice logic here for you guys...

Evaluate each:


(true || false)
((false || true) && true)
((false  & false) || (true && true))
((true || false) && (false && false)) || (true && false)
((5 >= 3) || (3 == 2)) && (2 <= 7 && 4 != 5)
« Last Edit: February 11, 2011, 09:19:53 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: Programming Q & A Round II
« Reply #14 on: February 12, 2011, 06:01:25 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
A very good explanation, there are only a few comments I have to make.

Switch/Select
...
Please note that the Switch and else if are the same thing in different forms with slightly different functionality.  Sometimes you will be required to use one over the other, however (such as the case where you need to use a >= for example).
This is not entirely true. But this is a bit more advanced stuff.

This is only true when each case is ended by the break statement. If there is no break ending a case, the switch will continue to execute the code of the next case until it either finds a break or reaches the switch's closing bracket. It allows you to group cases. Or if one case uses the exact same code as another but precedes it with a little other code you can cascade it. In effect the case only tells where in the code block you have to start and the break statement and closing bracket tells you when to stop.

Example of grouping cases
Code: [Select]
switch (variable)
{
    case a:
    case b:
    case c:
        statement;
        break;
    case x:
    case y:
        statement;
        break;
    default:
        statement;
        break;
}

Example of cascading cases. Note that the break is not required in the last statement due to it being followed by the closing bracket.
Code: [Select]
switch (variable)
{
    case x:
        statement;
    case y:
        statement;
        break;
    default:
        statement;
}


Keep in mind in the switch and else if, when a true expression is found, the rest of the expression following are skipped.
This is true in all languages except GM. Game Maker always evaluates the complete expresion. Thus when a false is found with a && operator and when a true is found with the || operator, it still continues evaluating instead of breaking and it. Game Maker is complete idiot in that.
Logged
Re: Programming Q & A Round II
« Reply #15 on: February 12, 2011, 04:24:24 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
I didn't really want to get into compounding cases, as it's a bit out of scope for an intro lesson (The CS101 class at my campus typically doesn't get into that until they've worked with switches with 2 or 3 assignments first), but regardless, thanks for the addition!
Logged



i love big weenies and i cannot lie
Re: Programming Q & A Round II
« Reply #16 on: February 12, 2011, 05:57:08 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Sorry about that, but I have seen too many newbies forget the importance of 'break' with a switch construction.
Logged
Re: Programming Q & A Round II
« Reply #17 on: February 12, 2011, 09:26:47 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
yes, break it important, and I did forget to mention that.
Logged



i love big weenies and i cannot lie
Re: Programming Q & A Round II
« Reply #18 on: February 12, 2011, 09:36:42 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
General question not pertaining to any particular language:
How would I start to go about making a tile engine?  Something that loads tiles based upon whatever(text file or other).
It is something I still am having trouble even concepting.  Sorry, I'm a newb.

I would like an answer more directed at C#/XNA or C++ but it can be a generalized answer.

Not wanting a "how to" just where to being or how to start it.
Logged
  • Super Fan Gamers!

Xiphirx

wat
Re: Programming Q & A Round II
« Reply #19 on: February 12, 2011, 09:53:59 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
General question not pertaining to any particular language:
How would I start to go about making a tile engine?  Something that loads tiles based upon whatever(text file or other).
It is something I still am having trouble even concepting.  Sorry, I'm a newb.

I would like an answer more directed at C#/XNA or C++ but it can be a generalized answer.

Not wanting a "how to" just where to being or how to start it.

First you may want to start thinking of a file format for your map files.
Logged
  • For The Swarm
Pages: [1] 2 3   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.075 seconds with 75 queries.

anything