0

So in my class we are working a simple Tetris like applet in Java. Blocks fall from the top of the screen into 6 different columns and each different column is a stack. In order to score points you must match 3 or more blocks of the same color directly next to each other horizontally on the very top of the stacks. So only the top of the stacks can match. If you match 3 or more blocks, they must be removed (aka pop()'d off). You are allowed to switch blocks with the adjacent blocks to either the right or left (thus generating larger combos). However that is my issue. My issue involves checking whether or not 3 or more blocks (that are adjacent to each other) are matching in color. What I have to keep in mind is the possibility that the number of columns may increase. I am not saying it will increase when the program runs, it's just I have to practice proper technique and make sure I am approaching this issue with that as a possibility. With that said there is a member variable that represents the number of columns, aptly named numCols.

So if anyone could give me a push in the right direction to solving this problem please. Again, the problem: being able to write a method removeCombos() that checks for 3 or more blocks of the same color in a row horizontally (across the top of the stacks) and remove them. I want to write the code myself that's why I have not provided any code here. Unless there is something you need to see then I could provide some, but it's working with basic stack class and its generic methods, pop(), peek(), contains(), isEmpty(), as well as an iterator class within the stack class.

If I have been unclear in any way I apologize in advance and please ask me to clarify where needed. Thank you very much.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
Mike
  • 1,307
  • 3
  • 17
  • 29
  • The word "stack" has several meanings, one as a concept of the game, where blocks are stacked on each other, and there is the data structure stack, and the Java class Stack that implements it. Is it a requirement that you use Java Stack class? – Meier Oct 04 '12 at 22:59

2 Answers2

1

What you want to do is have your Stacks as an array:

protected Stack[] stacks;

protected removeCombos()
{
    int color;
    for(int i = 0; i < stacks.length; ++i)
    {
        color = stacks[i].peek();
        for(int j = 1; (i+j) < stacks.length; ++j)
            if(stacks[i+j].peek() != color)
                break;

        // j is now the number of contiguous blocks of the same color.
    }
}
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • What is the int col represent? – Mike Oct 04 '12 at 22:58
  • Ah yes, cols and colors, my bad, fixed. – CrazyCasta Oct 04 '12 at 23:00
  • I don't see how this checks for 3 or more adjacent blocks being the same color? Also I'd need to pop() off the blocks if they are the same color. – Mike Oct 04 '12 at 23:04
  • Also numCols is the variable represents the length of the stacks in my code. – Mike Oct 04 '12 at 23:06
  • Yes, I was leaving the popping off part as an exercise for you. I was missing a .peek() in the for loop. Does that help? – CrazyCasta Oct 04 '12 at 23:06
  • Keeping numCols would be redundant if you use an array of stacks (which is what you need to support changing the number of columns). – CrazyCasta Oct 04 '12 at 23:07
  • So what I meant to say is that the number of columns does NOT change but I need to take that into consideration. Pretty much my professor wants us to use good style and not attack it with brute force and use stacks[0], stacks[1], stacks[2]...until the end of the array you know? Like he wants us to write the method so that the number of columns CAN be changed without messing up the rest of the code. Hope I was clearer now :P – Mike Oct 04 '12 at 23:09
  • Right, my point is, if you're using an array, the numCols information will be the same as the stacks.length value (and therefore redundant, which makes it bad practice to include numCols). – CrazyCasta Oct 04 '12 at 23:12
0

I think that an array of stacks make this task more difficult. If we have an example:

.....
..X..
.XOX.
XOXOX

Then on top of each stack, we have Color X. But as I know Tetris, they should not be removed.

So you need some kind if data structure where you can access the rows more directly. Possible solutions are 1. an array of arrays 2. or an array of vectors.

Vectors makes it easier to remove element in the middle, but on the other side you need than to handle when a vector is not filled up to the row you are examine.

Meier
  • 3,858
  • 1
  • 17
  • 46