• Create Account

#Actualslicer4ever

Posted 06 August 2013 - 02:56 PM

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?
Ex:

void CheckKeyboard(int k, int *p, int i){	if(keyboard_check(k))		*p += i;}...const int i = 5;int x, y;CheckKeyboard(vk_left,  &x, -i);CheckKeyboard(vk_right, &x,  i);CheckKeyboard(vk_up,    &y, -i);CheckKeyboard(vk_down,  &y,  i);
I Should have used a better function name than CheckKeyboard but none came to mind...

Just get rid of the if's and use the result of keyboard_check directly:
x += (int)keyboard_check(vk_right);
X -= (int)keyboard_check(vk_left);
y += (int)keyboard_check(vk_down);
y -= (int)keyboard_check(vk_up);

and bam, no more indentation/bracket positioning problems. =-)
If the language your using uses "1", "0", and "-1" for boolean that might work ( if you can deal with the "-1" ) ,  HOWEVER the increments the OP used are by 5, which throws a wrench in your example.

I did that from my phone, i meant to wrap the values by a *5 to do what the OP did, but we drove to a place that doesn't have service when i was trying to edit it. I also subtract to do the negatives.

Also, what sane language that allows bool to int casting doesn't represent the bool as 1 or 0?

edit: editted code that i was trying to change before i lost service:

x += ((int)keyboard_check(vk_right))*5;
X -= ((int)keyboard_check(vk_left))*5;
y += ((int)keyboard_check(vk_down))*5;
y -= ((int)keyboard_check(vk_up))*5;


#1slicer4ever

Posted 06 August 2013 - 02:02 PM

Since the 4 ifs are nearly identical, why not wrap this up in a function or something?
Ex:

void CheckKeyboard(int k, int *p, int i){	if(keyboard_check(k))		*p += i;}...const int i = 5;int x, y;CheckKeyboard(vk_left,  &x, -i);CheckKeyboard(vk_right, &x,  i);CheckKeyboard(vk_up,    &y, -i);CheckKeyboard(vk_down,  &y,  i);
I Should have used a better function name than CheckKeyboard but none came to mind...

Just get rid of the if's and use the result of keyboard_check directly:
x += (int)keyboard_check(vk_right);
X -= (int)keyboard_check(vk_left);
y += (int)keyboard_check(vk_down);
y -= (int)keyboard_check(vk_up);

and bam, no more indentation/bracket positioning problems. =-)
If the language your using uses "1", "0", and "-1" for boolean that might work ( if you can deal with the "-1" ) ,  HOWEVER the increments the OP used are by 5, which throws a wrench in your example.

I did that from my phone, i meant to wrap the values by a *5 to do what the OP did, but we drove to a place that doesn't have service when i was trying to edit it. I also subtract to do the negatives.

Also, what sane language that allows bool to int casting doesn't represent the bool as 1 or 0?

PARTNERS