# Unity The Quest for Tetris... Part 2

## Recommended Posts

Hello all, Continuing from part 1, I have even more problems with my code. (Ok, so only one right now, but I'm pretty sure that it's messing everything else up) PROBLEM 3: My line-removing code doesn't work correctly. It funks out and makes weird patterns. So I'm placing the brick and then it cleared all the lines on top. Then I placed it again and it made weird lines which went away when my brick hit it. Many thanks in advance for the help. adam_o UPDATED CODE EDIT 1: Updated code, updated Problem 3. [Edited by - adam_o on June 9, 2007 9:02:31 PM]

##### Share on other sites
Sounds like an off-by-one error. Remember that C++ arrays start at 0 and go to n-1.

##### Share on other sites
Quote:
 Original post by RavuyaSounds like an off-by-one error. Remember that C++ arrays start at 0 and go to n-1.

I don't think it's off-by-one, because that kicks up the debugger... I had that happen between Problem 2 and Problem 3, but I was able to get the help on that one... nice try...

##### Share on other sites
Quote:
Quote:
 Original post by RavuyaSounds like an off-by-one error. Remember that C++ arrays start at 0 and go to n-1.

I don't think it's off-by-one, because that kicks up the debugger... I had that happen between Problem 2 and Problem 3, but I was able to get the help on that one... nice try...

Not quite. Off-by-one errors are simply miscalculations of location by one, because you forgot to compensate for a starting point or such. First, this is not guaranteed to cause an error, if you overrun an array, so it may or may not crash/invoke debugging. This is not something you can rely on. Second, the off-by-one error may not even be causing you to overrun the array, which would not even generate a potential for an overrun, so a crash/debug wouldn't even happen. It is merely a logic off-by-one.

If you're code is removing a line above the one you want to remove, you probably do, in fact, have an off-by-one error. Check you're code for calculating which row to remove. In a C++ array, the second item is index 1.

##### Share on other sites
I looked at your code and I'm not sure where the problem is, but I wanted to say 3 things:

1) You should create helper functions that operate on a whole row (check if it's full, clear it, ...). This will make functions like check_clear_row() much cleaner (and less error-prone!).

2) In the loop, why is the first index of the array always 0 on the right?

3) Why did you choose to have the the array as -
arr_tetris_screen[Width][Height]? Arrays are row major so switching between the width and the height will make the code more intuitive.

These are just quick suggestions, sorry if I misunderstood your code (regarding 2 and 3).

##### Share on other sites
Quote:
 Original post by DaishimNot quite. Off-by-one errors are simply miscalculations of location by one, because you forgot to compensate for a starting point or such. First, this is not guaranteed to cause an error, if you overrun an array, so it may or may not crash/invoke debugging. This is not something you can rely on. Second, the off-by-one error may not even be causing you to overrun the array, which would not even generate a potential for an overrun, so a crash/debug wouldn't even happen. It is merely a logic off-by-one.If you're code is removing a line above the one you want to remove, you probably do, in fact, have an off-by-one error. Check you're code for calculating which row to remove. In a C++ array, the second item is index 1.

I understand this. It doesn't just remove the line above though, it removes every line.
Quote:
 Original post by Gage64I looked at your code and I'm not sure where the problem is, but I wanted to say 3 things:1) You should create helper functions that operate on a whole row (check if it's full, clear it, ...). This will make functions like check_clear_row() much cleaner (and less error-prone!).

That sounds good. I'll add that to the code.
Quote:
 2) In the loop, why is the first index of the array always 0 on the right?

The first one of the set is always 0, because that's the first one I decided to remove. I didn't really decide to do it in any particular order, just that it's easier if I just do it from low to high to remember. I didn't entirely understand the question, so that's what I hope you're asking.
Quote:
 3) Why did you choose to have the the array as -arr_tetris_screen[Width][Height]? Arrays are row major so switching between the width and the height will make the code more intuitive.

I didn't know! Interesting... I guess width by height is just easier for me, because that way it goes along with the (x,y) coordinate system. It's just the way I tought myself, I guess.

##### Share on other sites
Ok, so I'm really confused about what to do... could somebody show what some pseudocode for clearing lines would look like?

##### Share on other sites
I'm not sure if this will work, but try it anyways.
void check_clear_row(){	int row;	// Scan through all the rows	for(row = 0; row < SCREENH - 1; row++)	{			int i;			BOOL isFull = TRUE;			// Does this row need to be cleared?			for(i = 0; i < 8; i++)			{				if(arr_tetris_screen[i][row] == NO_TILE) isFull = FALSE;			}			// Clear row			if(isFull)			{				int currow;				// Scan through all rows above this one, starting from this one.				for(currow = row; currow > 0; currow--)				{					// Replace this row by the one right above it					for(i = 0; i < 8; i++)					{						arr_tetris_screen[i][currow] = arr_tetris_screen[i][currow - 1];					}				}			}	}		translate();}

##### Share on other sites
Quote:
 Original post by AcePilotI'm not sure if this will work, but try it anyways.*** Source Snippet Removed ***

It didn't work, thanks for trying. I located the spot in the code where the code whas wrong- and it was an off by one code error. WHOOPS! LOL New code attached, looking for new problems.

##### Share on other sites
I found a new problem... see above, I wrote a ton on it... dangit I thought it worked, but aparently, it didn't.

##### Share on other sites
After a much needed break from my project, I have come back afresh, ready to try and re-tackle this problem. I have noticed that if I drop the brick that is above the layer(s) being cleared on the very left, then it clears it differently than the others... hopefully you will understand what I mean by the pictures... I was thinking of doing something where I create a new array for storing all of the values except for those of lines that need to be cleared, then moving the lines down after the fact. Does that sound like it would work?
left before, left after, left after-after
not left before, not left after

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614
• ### Similar Content

• By trjh2k2
I've never really been a "Unity guy", since all of my game-dev learning happened in C++, and in other engines, but I recently discovered the "complete projects" section in the asset store.  It's full up on projects you can buy that are billed as "ready to customize and release", with full ad integration.  Some of them claim to be for educational purposes, but why would you include a complete, polished, full featured game with ads as an educational example?
This leads me to the question of why this goes by unchallenged?  Does Unity and the environment of the Unity Store actively encourage this style of game development?  Is the problem of asset flipping our own fault?  I don't mean this as a "we should make Unity shut this down" kind of thread, but rather just to examine whether or not the environment of being able to just buy whole games or pieces of games is something that damages the industry.  I get why Unity would allow it, and I'm sure it's a working business model for some people- and maybe some people DO actually just use these to learn from, but I'm not that naive as to think that there aren't people who recognize this as one of the shortest paths to putting a game on the market so they can cash in.
Thoughts?
• By StefanJo
Hi everyone!
I have just released a project called Line Driving to the Unity Asset Store.
Line Driving is complete project ready for release for both Mobile and PC.
It is a game where you have to draw your own road for a car using mouse controls or touch input.
Features:
1.Simple to understand,detailed commented scripts
2.Well-written documentation
5.Easy to customize
and much more…
Support email: sjovanovic0831@gmail.com

• Got the UI Inventory screen working now.
Fully keyboard and mouse driven, this is where you manage all your items split up into separate 'tabs'.
This is the 'Fossil' management area.

• Hey, I'm looking for some people that can help me with a game that I'm making that is a 3d platformer.
I mostly need someone for character design and animation,  I can do some coding but if you can do coding as well you're still welcome because I'm still not the greatest at coding just yet.
if you need to contact me add me on discord @ Kurupted#1206

• Hi,guys.I m working on a Fantasy RPG.Currently i m work alone on the project.I really need someone can make UI stuff.If someone can handle it please feel free to contact me on email: 270514974@libero.it.
Thank you guys and sorry for my english.

• 13
• 12
• 10
• 12
• 22