Advertisement

Tetris Help!

Started by November 30, 2014 03:24 AM
2 comments, last by MyLastGamble 10 years, 1 month ago

So I'm writing a Tetris game, mostly for practice, and I have come to a wall. Let me start by saying I'm not using any frameworks, I'm doing this all in C# using Visual Studio and I know I'm probably not doing this the best way however i am determined to get this finished before I start doing more research and using a framework.

So my Form has a panel in it that I have put a picture box for every possible Tetris block. They all start with the visibility flag set to false, and I have two arrays I have created, one that is for the gameMap and the other is for the current tile in play, as follows

int[,] gameMap = new int[,] {

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 } }

int[,] currentTile = new int [,] {

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0} }

This has been slightly simplified, but you get the idea. I have an array index for every picture box and I have a timer running that is constantly checking the gameMap and currentTile arrays to see if any of the positions are set to 1 and if they are then I set the tile that maps to this index to visible = true;

The problem I am having is dropping the tiles. I have set up another timer to run in the background and drop the till in the current tile array, and this is where I am having an issue. I had written a line of code that is a for loop inside of a for loop looking for an index set to 1 and it sets this to 0 and sets the index below it to 1(ie if its currentTile [0,5], it sets this to 0 and sets currentTile[1,5] to 1). I quickly figured out that this runs through the whole loop and ends up sending the tile to the bottom of the board at the end of the for loop.

I had tried a few more ideas to get it to drop the 1's in the array down 1 level, but nothing I have tried has worked. Does anyone have any ideas, or will it just not work this way? I don't want to try this any other way at the moment until I get a little more comfortable with the ins and outs of classes. Any help would be appreciated!

You have the right idea -- read up on sorting algorithms. The infamous bubble sort could solve the problem.

Advertisement

This is not directly a sorting problem. When copying rectangular areas (Edit: inplace), the loop direction has to account for the copy direction. If you want to move your tiles one row down, the loop should start at the last row and count backwards.

PS: If you really need sorting, don't implement a bubble sort, use the readily availble sorting functions (Array.Sort, List<T>.Sort or LINQ's OrderBy).


If you want to move your tiles one row down, the loop should start at the last row and count backwards.

Yes! I should have stuck with the KISS principle. Thanks! Stuff like this is exactly why I'm practicing and forcing myself to take on these problems. The more I practice and work on projects, the better I will get at not making such easy mistakes.


If you really need sorting, don't implement a bubble sort, use the readily availble sorting functions (Array.Sort, List.Sort or LINQ's OrderBy).

I do not feel the need for a sort for this game, I cannot think of any benefit it will have to how I have this implemented. And I do know, and have used, the Array.Sort method before.

Thanks again for the help!

This topic is closed to new replies.

Advertisement