Hello, I'm making a program that can play Bejeweled 2.
Everything is working smooth except for a glitch in the movement algorithm.

If anyone is really good at making these type of algorithms can you look at this code and/or give me some hints ?

This is the core algorithm for 3 pieces of the same color. This is also the base for 4,5 pieces and also for the sparkle piece.
I added the i and j rules recently i don't know if they are really necessary.

for(j=0;j<8;j++)
for(i=0;i<8;i++)
	if(t==0){
		if((i>=1&&j>=0&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-2][j-1].color)||
		   (j>=2&&ara[i][j].color==ara[i][j-2].color&&ara[i][j].color==ara[i][j-3].color)||
		   (i<=5&&j>=1&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+2][j-1].color)||
		   (i>=0&&i<=6&&j>=0&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i+1][j-1].color)){
			   p.x1=ara[i][j].width;
			   p.y1=800-ara[i][j].height;
			   p.x2=ara[i][j-1].width;
			   p.y2=800-ara[i][j-1].height;
			   t=1;
		}else
		if((i>=0&&j>=1&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-1][j-2].color)||
		   (i>=2&&ara[i][j].color==ara[i-2][j].color&&ara[i][j].color==ara[i-3][j].color)||
		   (i>=0&&j<=5&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i-1][j+2].color)||
		   (i&&j&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-1][j+1].color)){
			   p.x1=ara[i][j].width;
			   p.y1=800-ara[i][j].height;
			   p.x2=ara[i-1][j].width;
			   p.y2=800-ara[i-1][j].height;
			   t=1;
		}else
		if((i>=0&&j>=1&&i<=6&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+1][j-2].color)||
		   (i<=4&&ara[i][j].color==ara[i+2][j].color&&ara[i][j].color==ara[i+3][j].color)||
		   (i<=6&&j<=5&&ara[i][j].color==ara[i+1][j+1].color&&ara[i][j].color==ara[i+1][j+2].color)||
		   (i<=6&&j<=6&&j>=0&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+1][j+1].color)){
			   p.x1=ara[i][j].width;
			   p.y1=800-ara[i][j].height;
			   p.x2=ara[i+1][j].width;
			   p.y2=800-ara[i+1][j].height;
			   t=1;
		}else
		if((i>=1&&j<=6&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i-2][j+1].color)||
		   (j<=4&&ara[i][j].color==ara[i][j+2].color&&ara[i][j].color==ara[i][j+3].color)||
		   (i<=5&&j<=6&&ara[i][j].color==ara[i+1][j+1].color&&ara[i][j].color==ara[i+2][j+1].color)||
		   (i>=0&&i<=6&&j<=6&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i+1][j+1].color)){
			   p.x1=ara[i][j].width;
			   p.y1=800-ara[i][j].height;
			   p.x2=ara[i][j+1].width;
			   p.y2=800-ara[i][j+1].height;
			   t=1;
		}
	}

Recommended Answers

All 8 Replies

Try posting your code readably and someone might be able to follow it.

Indenting is a good thing, but indenting 8 characters is usually overkill, as your code shows.

Member Avatar for iamthwee

Try posting your code readably and someone might be able to follow it.

Indenting is a good thing, but indenting 8 characters is usually overkill, as your code shows.

Yup.

Indenting is a good thing, but indenting 8 characters is usually overkill, as your code shows.

My attempt to fix it failed :(
@OP: Less characters on one line please.

Glancing through your code, one thing immediately caught my eye:

if((i>=1&&j>=0&&ara[i][j].color==ara[i-1][j-1]

What happens to this piece of code when j == 0?

Alright, I fixed the i and j's and ran just this algorithm.
It seems to be working but there's still a small glitch and after a while it crashes but that's probably because of some error elsewhere. It's going to be a pain to check all that code. Should I delete the image pixel pointers once in a while ? Not that big, just 1280x800 ^^

The glitch is that it makes a mistake switching 2 colors with no effect but afterwards it gets it right. Could it be that the timing is off, should I put Sleep() longer than 100 milliseconds between switches ?

Also, is there any intelligent strategy I could use apart from mindlessly switching colors ? Some detail would be great :)

Here is the code without indenting:

for(j=0;j<8;j++)
for(i=0;i<8;i++)
if(t==0){

if((i>=2&&j>=1&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-2][j-1].color)||
(j>=3&&ara[i][j].color==ara[i][j-2].color&&ara[i][j].color==ara[i][j-3].color)||
(i<=5&&j>=2&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+2][j-1].color)||
(i>=1&&i<=6&&j>=1&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i+1][j-1].color)){
p.x1=ara[i][j].width;
p.y1=800-ara[i][j].height;
p.x2=ara[i][j-1].width;
p.y2=800-ara[i][j-1].height;
t=1;
}

else

if((i>=1&&j>=2&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-1][j-2].color)||
(i>=3&&ara[i][j].color==ara[i-2][j].color&&ara[i][j].color==ara[i-3][j].color)||
(i>=1&&j<=5&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i-1][j+2].color)||
(i>=1&&j>=1&&j<=6&&ara[i][j].color==ara[i-1][j-1].color&&ara[i][j].color==ara[i-1][j+1].color)){
p.x1=ara[i][j].width;
p.y1=800-ara[i][j].height;
p.x2=ara[i-1][j].width;
p.y2=800-ara[i-1][j].height;
t=1;
}

else
if((i>=1&&j>=2&&i<=6&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+1][j-2].color)||
(i<=4&&ara[i][j].color==ara[i+2][j].color&&ara[i][j].color==ara[i+3][j].color)||
(i<=6&&j<=5&&ara[i][j].color==ara[i+1][j+1].color&&ara[i][j].color==ara[i+1][j+2].color)||
(i<=6&&j<=6&&j>=1&&ara[i][j].color==ara[i+1][j-1].color&&ara[i][j].color==ara[i+1][j+1].color)){
 p.x1=ara[i][j].width;
p.y1=800-ara[i][j].height;
p.x2=ara[i+1][j].width;
p.y2=800-ara[i+1][j].height;
t=1;
}

else
if((i>=2&&j<=6&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i-2][j+1].color)||
(j<=4&&ara[i][j].color==ara[i][j+2].color&&ara[i][j].color==ara[i][j+3].color)||
(i<=5&&j<=6&&ara[i][j].color==ara[i+1][j+1].color&&ara[i][j].color==ara[i+2][j+1].color)||
(i>=1&&i<=6&&j<=6&&ara[i][j].color==ara[i-1][j+1].color&&ara[i][j].color==ara[i+1][j+1].color)){
p.x1=ara[i][j].width;
p.y1=800-ara[i][j].height;
p.x2=ara[i][j+1].width;
p.y2=800-ara[i][j+1].height;
t=1;
}
}

how is 'ara' declared?

how is 'ara' declared?

ara is an array of 8x8.

typedef struct
{
	unsigned char color;
	unsigned int sparkle;
	unsigned int width;
	unsigned int height;
} Array;

I'm not sure if using unsigned was the best idea here but if the compiler agrees with it then I agree as well :)

The sparkle should really be a bool here because it specifies if that color is the result of a 4 color burst. Going to change it.

ara is an array of 8x8.

I thought so.

Now what would happen with this piece of code when i reaches 7 ?

for(i=0;i<8;i++)
[....]
if( [..........] &&ara[i][j].color==ara[i+2][j-1].color)|| [........]

Uh-oh: out of bounds error....

I think you need to rethink how you want to handle this problem. Using enormous if-statements is never a very good idea. How about creating a few functions which each check their own little piece?

Yeah, the end stage is going to be turning this into functions.
Actually that's the reason I'm using the i and j to avoid any unwanted complications here. I think i checked every possibility, like when j needs to be checked at +3 then j must be <=4.
Did I miss something ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.