# Need an if statement which compares a value to 500+ different values

I need to make an if() statement which compares 1 four digit value from 100 - 3030 to 500+ values.

But I need to make it shorter

Here is what i got:

``````int value_X = 0;
int value_Y = 1;
int value_XY = 100;

void setup() {
randomSeed(A0);
}

void loop() {

value_X = random(0, 30);
value_Y = (random(0, 30) * 100);
value_XY = (value_Y + value_X);

if((value_XY >= 101 && value_XY <= 112) || (value_XY >= 116 && value_XY <= 127) || (value_XY == 201 || value_XY == 206 || value_XY == 212 || value_XY == 216 || value_XY == 222 || value_XY == 227) || (value_XY == 301 || value_XY == 306 || value_XY == 312 || value_XY == 316 || value_XY == 322 || value_XY == 327) || (value_XY == 401 || value_XY == 406 || value_XY == 412 || value_XY == 416 || value_XY == 422 || value_XY == 427) || (value_XY >= 501 && value_XY <= 527) || (value_XY == 601 || value_XY == 606 || value_XY == 609 || value_XY == 619 || value_XY == 622 || value_XY == 627) || (value_XY == 701 || value_XY == 706 || value_XY == 709 || value_XY == 719 || value_XY == 722 || value_XY == 727) || ((value_XY >= 801 && value_XY <= 806) || (value_XY >= 809 && value_XY <= 812) || (value_XY >= 816 && value_XY <= 819) || (value_XY >= 822 && value_XY <= 827)) || (value_XY == 906 || value_XY == 912 || value_XY == 916 || value_XY == 922) || (value_XY == 1006 || value_XY == 1012 || value_XY == 1016 || value_XY == 1022) || (value_XY == 1106 || (value_XY >= 1109 && value_XY <= 1119) || value_XY == 1122) || (value_XY == 1206 || value_XY == 1209 || value_XY == 1219 || value_XY == 1222) || (value_XY == 1306 || value_XY == 1309 || value_XY == 1319 || value_XY == 1322) || ((value_XY >= 1400 && value_XY <= 1409) || (value_XY >= 1419 && value_XY <= 1428))) {

// Do some stuff

}
}
``````

You've got 200+ posts - do you think you could start using code boxes, please?

I tried teletype but no box appeared..
Plus I learn slowly as I have a disability, so please don't remind me about that. It makes me feel depressed (sad)

ohh.. I just learned what the # button does, will use that for future posts.

Sorry AWOL

EDIT: Code Box is now fully correct

So, from a very small sample of the code I looked at, you're looking at test a range of +11 from a list of numbers?

So, declare an array with the start of the range in it, and iterate through that with a "for" loop and test against the array.

If not all values are +11, have two arrays, one with the start of the range and the second with the range.

(Sorry if I snapped)

Might be easier to figure out how best to compress it with a few more details - what are you trying to do?

AWOL:
So, from a very small sample of the code I looked at, you're looking at test a range of +11 from a list of numbers?

So, declare an array with the start of the range in it, and iterate through that with a "for" loop and test against the array.

If not all values are +11, have two arrays, one with the start of the range and the second with the range.

I am trying to make a list of acceptable locations for the Player, Ghosts, food, Cherry, Apple, Bannana?, ect... for a PacMan game.

Here is the Background Map:

It is an resized (34 pixel wide X 33 pixel tall) blue & black image with the rest of the screen black. (Each 1x1 pixel was resized to an 8x8 pixel)

If "value_XY" is "101" the player's PacMan will appear at the upper-left most spot you would typically see in a classic PacMan game.
If "value_XY" is changed to "102" the center of the PacMan will move right 8 pixels.(as the background map is draw to a grid, w/ each gridline drawn evey 8 pixels)

However if "value_XY" = "102" is made to equal "202" the Pacman will would move down 8 pixels, and also appear to cover up part of the blue wall. Because this is not how the game is played, "value_XY" cannot equal "202", or any other value that would cause the PacMan to appear in an undesired spot.
(only spots that would draw the PacMan on the center of the black path, and not in the ghost room/ghost box/ghost cage, and not in the large open area.) (The place where you would end up if you didn't appear on the opposite side when PacMan goes as far left/right as you can when playing the game)

An obvious brute force approach would be to store the map 1 bit per pixel and write some accessor functions to encapsulate the necessary bit twiddling to use it. Storage cost of 140 bytes.

Why not just build a simple bitmap?
About 150 bytes worth of bitmap.

Edit: Beaten to the draw again!

AWOL:
(Sorry if I snapped)

It's ok, what you said should be what I should be doing based on what you knew when you said it.

Plus I'm not sad / upset from what you said, because I knew you didn't know the all the details.
In addition you should say stuff like that to more than 90% of the people doing stuff like what I did, because very few people who can program a microcontroller have disabilities.

Also my disability is the cause to why I am able to be so fluent in (mostly) everything electronic. Even if it also causes me to learn slower than most people. It doesn't hinder how much or what I can learn, just the rate at which I learn, the rate at which I process information(slow at solving problems), the rate at which I write / type, spelling, how accurately I read, how well I remember things and how much patience I have(which will vary day to day)

OK, your approach so far is going to lead to a large unmanageable "if" statement, that is going to be difficult if you want to change the playing area.
If you reduce each row and column of the playing area to on/off, the problem becomes much simpler.
Each row of 34 cells can be represented in fewer than five bytes. Let's say five to make the arithmetic and the array declaration easier.
So, now instead of a big "if", you feed a function the values of x and y and get the function to look at your five byte wide by 33 rows deep array, and get the function to return true or false.
You have to do some simple arithmetic on x to locate the correct bit out of the five bytes, but this is very quick, and the whole thing will be easier to maintain than your "if".

Even if you wanted to make things more complex, like "cell contains power-pill", requiring more than a simple true/false result, you could use two or three bits per cell, and it would still be simpler and more compact.

AWOL:
Why not just build a simple bitmap?
About 150 bytes worth of bitmap.

Edit: Beaten to the draw again!

Except I'm using a Gameduino, and a bitmap is not supported just upto 256 (16x16pixel) sprites which in may case need to be just the ghosts, food-dots, large food dots, cherry, apple, stawberry, peach, lime, bannana, grapes, key and PacMan

Except I'm using a Gameduino, and a bitmap is not supported

The Gameduino doesn't support large "if" statements either.

The bitmap is on the Arduino, not the Gameduino.

[quote author=TECH GEEK link=topic=67297.msg494322#msg494322 date=1311346422]and a bitmap is not supported[/quote]We're not talking about a bitmap here in the sense of a BMP image sort of bitmap. We're talking about a map of bits.

``````int[] maze = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
``````

Any cell with a 1 in it is a wall, any 0 is not.

``````########
#      #
###  ###
#      #
########
``````

(don't copy the code directly, it's from my non-C++ memory, but the point applies)

AWOL:
The Gameduino doesn't support large "if" statements either.

The if statement is on my Arduino Mega 1280, and it is not being sent as a command that controls what the Gameduino does.
The if statement only controls whether or not to to send a statement from the "Gameduino Library" that draws a sprite.
(sprite = a 16x16 pixel image (see picture for a few of the sprites I will use) )

The if statement will compare a newly generated value to a list of where the sprite can be drawn.
Based on whether the newly generated value is in the list or not it will either send the "draw_sprite()" with the new location, or keep
the sprite in its current location.

The if statement is on my Arduino Mega 1280

And so is the bitmap.

``````int value_X;
int value_Y;

void setup() {
randomSeed(A0);
}

void loop() {

value_X = random(0, 30);
value_Y = (random(0, 30) * 100);

if(inMyBitMap (value_x, valueY))
``````

Does that start to make sense?

AWOL:

``````int value_X;
``````

int value_Y;

void setup() {
randomSeed(A0);
}

void loop() {

value_X = random(0, 30);
value_Y = (random(0, 30) * 100);

if(inMyBitMap (value_x, valueY))

``````

Does that start to make sense?
``````

Yes.. Yes it does.

But how should "inMyBitMap()" be coded?
Should the code for "inMyBitMap()" look like this, when the image is this 6x6-pixel grid.
Where the value 1,1 is the center of the left-most pixel on the top, the value 6,1 is the center of the right-most pixel on the top,
the value 1,6 is the center of the left-most pixel on the bottom, and the value 6,6 is the center of the right-most pixel on the bottom.

``````void inMyBitMap(int value_X, int value_Y) {

int Grid_1[] = {0,0,0,0,0,0};
int Grid_2[] = {0,1,1,1,1,1};
int Grid_3[] = {0,1,0,0,0,1};

if(value_Y == 1) {
return Grid_1[value_X];
}
else if(value_Y == 2 || value_Y == 4 || value_Y == 6) {
return Grid_2[value_X];
}
else if(value_Y == 3 || value_Y == 5) {
return Grid_3[value_X];
}
}
``````

Yes that's a start, but an "int" is a very poor way of storing one bit of information, and the language allows two dimensional arrays, so the "if (y ==1) stuff isn't needed.
Once you get bit arithemetic sorted, the whole problem shrinks down.

``````byte grid [4][4] = {{0,0,0,0},
{1,0,0,1},
{1,0,0,1},
{0,1,1,0}};
``````

(even a byte isn't a good way to store a bit, but it demonstrates a two dimensional array)

Have a look at "bitRead",and see if you think that, with a little arithmetic will help.

Like this for the same 6x6 pic above?

``````void inMyBitMap(byte value_X, byte value_Y) {

byte Grid [6][6] = {{0,0,0,0,0,0},
{0,1,1,1,1,1},
{0,1,0,0,0,1},
{0,1,1,1,1,1},
{0,1,0,0,0,1},
{0,1,1,1,1,1}};
return Grid [value_X][value_Y];    // Or should I swap [value_X] with [value_Y]???
}
``````

Yes, swap Y and X.

Conventionally, y (row) is the first subscript and x (column) is the second.

Looking good.