Tetris on Google Sheets

Pizza and Beers

Fridays after work was when Dennis, Feynman and myself would take refuge in the savory and the cold that was pizza and beers. Amici’s in Soma, or Sliver in Berkeley if we were feeling adventurous.

I had become quite good with Apps Script, my PM Dennis, was obsessed with making internal tools with it. Almost asked him to shove it when he first asked me to code in Apps Script; but I came around because we all love Dennis.

Me, in blue, fully fronting for this corporate photo.

Our CEO at the time, used to ask engineers in interviews, how they would code Tetris in Javascript.

During one particular pizza and beers session, when I was admitting my surprise at how good Apps Script is, I was challenged: “But can you code Tetris in it?” — a joke but, could I?

The answer is yes.

Sheet as a Datastore, sheet as a CSS grid

There are four major tasks to complete to have Tetris on Google Sheets:

  1. The falling of tetrominoes(that’s what tiles are called!)
  2. The stacking of tetrominoes
  3. The controlling of tetrominoes
  4. The rotating of tetrominoes

I decided to use one sheet as my frontend, like a CSS grid; one as my memory, holding the state of my application and another as my disk, holding information on tetrominoes and such.

It probably is possible and more confusing to merge the frontend and memory.

Falling

The simple part. Color some cells, sleep, color the cells below it and erase the ones above.

Stacking

We consult the memory while stacking: is there a tile below me?

To model a memory, I used a shadow board, another sheet with the same layout as the original board. It would remember where the tetrominoes stop and mark those cells with the number 1.

Frontend (pic from later dev stage)
Memory

At each step of the tetrominoes are falling, they check to see if they should stack. They stack when there is another tetromino immediately below them. The layer below them is called the buffer. They know another tetromino has entered their buffer by checking the memory, where we keep a record of the has been.

Controlling

Another easy one. At each step, read the input, move the tetromino to left or right. I used w,a,s,d to remember my PC gaming days <3.

Rotating

The trick about rotating is that the buffer needs to change too:

Simple for just the straight tetromino. Adding the rest poses a challenge because each have their own shape and buffer requirements.

I can see a bug here, can you? answer at the end

To address this, I used a coordinate system. I put each tetromino in a 3×3 block, and referenced their cells and buffer cells, using the middle cell of the 3×3 block as origin.

Every origin on column C belongs to 0 degrees rotation, column G 90 degrees and so on. I is on row 3, J on 7, L on 11 and so on.

Then, using a map, for S for instance, we have:

...
's':{0:[[left, up, ur], [ll, down, right]], 90:[[up, right, lr], [down, d2r]], 180:[[left, up, ur], [ll, down, right]], 270:[[up, right, lr], [down, d2r]]},
...

Let’s open one up, 0:

{0:[[left, up, ur], [ll, down, right]] ...

The first array says for a 0 rotated S, excluding the origin, the cells to the left, up and upper left of the origin are colored.

The second array says for a 0 rotated S, the buffers are at lower left, down and right of the origin.

You can see the rest here if interested.

The Final Product

Added some animation for that sweet dopamine spike.

Answer to bug challenge

There actually must be buffers to the left and right of the tetrominoes too, otherwise they will exit the bounds or bleed into other tetrominoes laterally. Even though my drop cycle makes it hard to do so.

Social

Follow me on twitter

Appendix

Code: https://github.com/maininformer/Tetris/tree/master

Sheet:https://drive.google.com/drive/folders/1X_aPHyIAqY5bI9Dtc5oTLLBRxVCovXzG

12 responses to “Tetris on Google Sheets”

  1. Hey, Emin. Have you looked at using PropertiesService or CacheService to use as your key-value store? Should be much faster than read/write operations to the sheet.

    Liked by 1 person

    1. Hey Diego! This is a great idea! I have come across them, but never investigated them. Thanks you.

      Like

  2. Well, done! There are a number of improvements that could be made but this makes for a pretty solid implementation. Have you tried using clickable images for directional controls?

    Liked by 1 person

    1. Thank you very much. I have not. A couple of times sheets broke where it would not stop the script from running even after refreshing (video in the drive). So I decided to stick to one process. But it can be good improvement.

      Like

      1. Awesome work regardless. There is a vibrant Google Apps Script community on G+ (https://plus.google.com/u/0/communities/102471985047225101769) . You should check it out.

        You are just beginning to scratch the surface of what GSuite/Google Apps Script has to offer you as a developer. Also keep in mind that it is but one part of Google’s extensive Cloud platform. This rabbit hole goes far deeper that you realize.

        Liked by 1 person

  3. I requested access to the doc because the link wasn’t working, could you please accept. Kind regards, Josh

    Like

    1. Hey Josh, sorry for the super late reply, I did not see the pending comment. What you need to do is to make a copy of the sheet first and then play with it. You cannot work directly on the sheet that I shared, unfortunately, otherwise, I would have to keep putting up new sheets. Thank you for your interest!

      Like

  4. Could you try using functions (i.e. SUMIF, IF, SUM) in google sheets to create a game such as pong? You can add custom functions, so I was thinking if you created one that changes the fill of cells. It probably is a stretch, and I don’t Know much about java and html, but I do know that to use functions in sheets and when developing are the same setup.

    Like

    1. Sorry, javascript. Got that wrong.

      Like

    2. Hey Max. I am glad that you are interested. My first thought is that, as far as I know, it is not possible to create a game with only formulae.

      The reason for that is, that when a game renders, frame by frame, it is the computer, changing the color of the pixels, or in our case the cells, giving us the impression that something is moving. In google sheets, I did this by having the program run forever and sleep a second or two every time, so for us humans it is slow enough to understand. Otherwise, it will be too fast. I don’t know of a way to delay execution on formulae. Formulae also will complain if they go into an infinite loop.

      That being said, my first ever automation was with excel and later excel macros. Now I am a backend engineer. So I will encourage you to try and see if you can make a game using only formulae and what problems you face.

      You don’t really need HTML or javascript to code this game. It is apps script, which much simpler to learn than javascript. So if you do get to that point to be needing to use apps script, don’t worry too much, just give it a try. There is very good documentation on it as well: https://developers.google.com/apps-script/reference/spreadsheet/

      Like

  5. Thanks for your feedback! Also thanks for showing me that part of the google developers hub! I had no idea you could use that for sheets. It will definitely make things a lot easier for me.

    Liked by 1 person

Leave a reply to plumSemPy Cancel reply

Blog at WordPress.com.