A program likes Bejeweled game

Write your question here.
Oh, hi, I have a problem:
Example, I have a matrix:
B B C E D
D E B A C
C B A D B
A A C A E

when I swap A of row 3 and C of row 4, I want to find how many character A in row 4 that is consecutive (in this example, there are four 'A', two on the left and one right) and then, I want to delete all of them. It's likes the Bejeweled game. Please help me!
We're not going to write your game for you.
Make an attempt to write it and we will help you if you get stuck.

Hint: Try implementing the matrix as a two dimensional array, then looping through each row of the array counting the number of successive cells that are the same. Mark any cell to be deleted with say "X", then make a second pass, this time searching each column for any "X"s. Remove the "X", and move any characters above the X down one position. Fill any empty positions with a new random character (A-E).
Oh,sorry, I don't mean to ask you to write this game for me, I just want you to help me about the algorithm because I can't image how it work. I'm a beginner so it's so hard for me to image, especially about how to find how many characters that is consecutive.
Last edited on
@bathach95

Here's how I would check for 3 or more and remove them. As abstractionAnon mentions, though, you still have to move all the characters above down one level, add the random characters into the spaces, and check again for the duplicates. It's probably best to use a recursive function for that. And for that, I'd be little to no help, as I'm still trying to learn it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cout << "Remove duplicates of 3 or more on board.." << endl;
	
	for(int x=0;x<4;x++)
	{
		int count = 1;
		int location = 0;
		for(int y=1;y<5;y++)
			if (board[x][y] == board[x][y-1])
			{
				count++;
				location = y; // location will equal the last array location of duplicates
			}
			if (count >2)
			{
				cout << "Found " << count << " in a row.." << endl;
				for(int remove = location; remove >= 0; --remove)
				{
					board[x][remove] = ' '; // Removes the duplicates
				}
			}
		cout << endl;

// Display the board afterward to show removed characters 
	}
Last edited on
Oh, thank you very much, @whitenite1 :D :D
@whitenite1:
I see that there are too many ways to score, such as:
B B B E D
D BC A C
C BC D B
A AC A E
F C E C F
or
B B D E D
D C C C A
C B C D B
A A C A E
F C E C F

so the code will be very long if we list all and I have a problem that when I score, the same characters will be replace with others and what happen if they make a situation that can be scored? such as:

D C C C A
C B C D B
D D C A E
F C E C F
--->
example I replace randomly:
D B C D A
C B B D B
A A A A E
F C E C F
and now there are four 'A' on row 3.

p/s: I'm from Viet Nam and my English is not good so I hope that you can understand.
Last edited on
@bathach95

You don't just fill in the empty spaces, you would first move the character above the space down one, to fill in the empty space. You would start from the bottom, and check if it;s empty. If yes, then take the character directly above, and copy it into that space. Then fill the space that you took the character from, and make it a space. That's where the recursion would come into play. Set a bool variable to false. A zero. If you are able to move a character down, then set a bool variable to true. A 1. After going through all the rows of the array, you would call the same function again, IF the bool variable is true. If no more moves, you are returned to the calling function, or main, if that is where you called the function. Hopefully, someone here can show you a good way to to do it. As I mentioned previously, I'm still trying to learn it. I made a Minesweeper game, and have been unable to use a recursion function to remove all the spaces up to the edges of the mines, as of yet. So I just remove the blocks around the location I'm checking.
I write this code base on your code to find and delete which row have more than 3 identical characters, but it does not work. I can't find what's wrong with it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
or(int j=0;j<5;j++)
	{
		int count1= 1;
		int location1 = 0;
		for(int i=4;i>=0;i--)
			if (gameboard[i][j] == gameboard[i-1][j])
			{
				count1++;
				location1 = i-1; // location will equal the last array location of duplicates
			}
			if (count1 >2)
			{
				
					for (int row=location1;row>=count1;row++)
					gameboard[row][j] = ' '; // Replace the duplicates
				

				
			}
	}
@bathach95

It looks to me that you are counting columns, not rows. The first set of brackets in an array is for the set, and the second for the numbers in that set. So, when you declare an array as int array[4][5], let's say, you are declaring that the array has 4 sets of numbers,, that go from 0 to 4, or 5 numbers. Also, your for loop of i, you have decreasing, but you use an increasing loop, row, for removing the duplicates. Lastly, did you name the Bejeweled board array, to be gameboard, like I have in my code? If not, you'll have to rename the variable gameboard, to whatever you made in your actual code.
yes, columns, not rows. I'm confused
Can we use BFS (Breadth First Search) algorithm in this game ? I see that some people use it in game Lines 2008, I think two games are the same. If you now this algorithm, please explain it to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cout << "Remove duplicates of 3 or more on board.." << endl;
// Removes duplicates from rows	
for(int x=0;x<4;x++)
{
	int count = 1;
	 int location = 0;
	 for(int y=1;y<5;y++)
		 if (board[x][y] == board[x][y-1])
		 {
		 	count++;
			location = y; // location will equal the last array location of duplicates
		}
		 if (count >2)
		 {
			 for(int remove = location; remove >= 0; --remove)
			 {
				 board[x][remove] = ' '; // Removes the duplicates
			 }
		}
}

// Removes duplicates from columns
for(int x=4;x>0;x--)
{
	 int count = 1;
	int location = 0;
	 for(int y=0;y<5;y++)
        // Check if 3 or more duplicates in column BEFORE dropping characters down
       //to fill empty spaces
		if (board[x][y] == board[x-1][y] && board[x][y] != ' ')
		 {
	 		count++;
			 location = x; // location will equal the last array location of duplicates
	 	}
		if (count >2)
		{
			for(int remove = count; remove >= 0; remove--)
			{
				board[remove][y] = ' '; // Removes the duplicates
			}
		}
// Display the board afterward to show removed characters 
}


This should work, though it is not been fully tested, but the idea is there.
Last edited on
If the algorithm finds 2 duplicates, it would only remove the second one.
Topic archived. No new replies allowed.