📜 ⬆️ ⬇️

The task of the location of the figures in the game "Tetris"

I decided to throw the small task of finding and implementing the best algorithm to the Chabrasoobshchestvo. Everyone knows the game "Tetris". Imagine that a computer is playing this game for you. So, the task will be to describe the algorithm that will correctly place the figures, so that there are no empty places. I made a simple UI to test the algorithm on JSFiddle . All you need to do is implement one function. But wait, for a start I will explain the rules.

I decided not to do a complete analogue of Tetris, because you need to start small, then perhaps more people will be able to check themselves. So, simplify our Tetris:



There is one empty function in the code (more precisely, it now returns some random number) - getColumnNumberForLeftFigureSquare (w, h). You will need to implement it. w is the width of the new figure, h is the height. In total you can use 3 variables: w, h and cols. cols is an array of 10 elements. Each element is a column, the value is the occupied height of the column. The function must return an integer from 1 to 10 inclusive. This will be the number of the column into which the leftmost cell of the shape falls.
')
In total, there are 200 cells in this Tetris, i.e. You will be able to test your algorithm by the number of remaining to fill (displayed in the UI)

Make ford fiddle and send in the comments. Any attempt will be appreciated. Ideally, the solution should be as efficient and as productive as possible. Then we can improve Tetris to the original.

Once again link to JSFiddle

PS If you see errors in the UI itself - let me know.

Source: https://habr.com/ru/post/230229/


All Articles