# My roommate is back add it!

My roommate's computer desk has a paper on it with these 3 weird grids and drawings. I know that he is trying to learn to write computer code and he likes making old video games as a way to learn. I'm an experienced game designer so sometimes I like to look at his notes and see if I can figure out what he is working on. This one really has me stumped, though. So I come to you, dear Puzzling StackExchange, for your insights!

|  0 | +1 | +2 | +3 |
---------------------
| -1 |  0 | +1 | +2 |
----------o----------
| -2 | -1 |  0 | +1 |
---------------------
| -3 | -2 | -1 |  0 |


He had this written in a 4x4 grid with a scribbled dot in the center.

Then there was another grid:

| +3 | +2 | +1 |  0 |
---------------------
| +2 | +1 |  0 | -1 |
----------o----------
| +1 |  0 | -1 | -2 |
---------------------
|  0 | -1 | -2 | -3 |


and a final grid that seemed to link them.

| +3 | +1 | -1 | -3 |
---------------------
| +3 | +1 | -1 | -3 |
----------o----------
| +3 | +1 | -1 | -3 |
---------------------
| +3 | +1 | -1 | -3 |


There are a couple of functions/equations here, too, but I'll post those as spoilers if no one gets this in a week.

What is the game he is working on? What are the three grids for?

A partial answer to the bonus question has been given below (3 functions)

Bonus question: What are the 2 equations?

Note: For the equations there could certainly be some variation in correct answers. Maybe you'll come up with 1 function that is as good as both functions, or maybe you'll find three useful functions. The bonus question is essentially just, "how do these grids aid in writing a function for this game?"

Update 2/4/17

As promised, here are the functions that he wrote down by the matrices.

(g') = c - r
(g'') = (g''') + (g')
(g''') = 3 + (c * 2)


Hints

CW

Grid 1: Rows. Grid 2: Columns. Grid 3: Difference

Help with the equations: (Helpfulness level: 5)

g' = grid 1

g'' = grid 2

g''' = grid 3

Ryan:

Correct, 180 degrees isn't a move in this game, so that's not the meaning of that grid. And the way I look at it, the third grid doesn't really rotate it at all, but rather mirror it in the vertical axis ... although that is not actually really what the third grid is for either. The 3rd grid is explained by the (g''')

Asteria:

I think the thing you need to look at is the titles of the grids. What is the number in each cell in your calculation? +3 means what in the first grid? Second Grid? 3rd Grid? They don't all mean the same thing :) You guys are getting close!

• Flow graph path finding? The Zeros could be the path, and the numbers correlate to how far left / right an object is off of the path to get back on path? Feb 3, 2017 at 21:44
• @keevee good guess but not this. Feb 4, 2017 at 5:48

Is it…

Tetris?

You can use the grids to

rotate a tetromino clockwise

I’m not particularly confident in this answer, though, because

you can use those grids to rotate anything, and the third grid would rotate a piece 180°, which isn’t a valid move in Tetris as far as I’m aware.

• Now we are getting somewhere!!! Your last comment is correct, meaning you're missing something. The moves in this game are the same as the original. Feb 6, 2017 at 20:38
• One reason your first answer is more correct than many other possible things is that these are 4x4 grids. So there is a limit to what they could work for graphically represented, although the equations would indeed work for any size "thing" Feb 7, 2017 at 23:37
• @NealDavis: Er, to clarify, because the hints and other answer kind of suggest otherwise: it’s not Tetris, right?
– Ry-
Feb 10, 2017 at 4:18
• sorry. It IS Tetris. I wasn't trying to be misleading. Feb 10, 2017 at 4:18
• @NealDavis: You overlay the piece on the grid and add the numbers corresponding to each cell to the cell’s row (for the first grid) and column (for the second), right? I thought that kind of went without saying, sorry. I guess the third grid helps because a rotation is a mirroring across one axis and then the other…? Confusion might be coming from the fact that every operation can be derived from every other operation so it’s not clear which ones are there to be intermediates or something.
– Ry-
Feb 10, 2017 at 5:23

This looks like Pong or a ping-pong-esque game to me. The numbers are to identify the font size of the ball as it moves to the front right or front left from the 1st person, and then the connecting grid is for when it moves center front and back, but rotated 90 degrees.

Following the last part of Ryan's answer, I think

the first grid is the initial state of a tetromino, and the second grid is the final state. The third grid rotates the piece 270° (three times clockwise). You can tell because the rows in the first grid correspond to the columns of the second grid.

As for the functions, they are for

computing the third grid which rotates a piece clockwise once (g'), twice (g''), and three times (g'''). I've worked out matrices used for rotating the first grid clockwise once and twice. Indeed, the matrix for rotating it twice is equal to the sum of the matrices that rotate it once and three times.

• Fascinating! You and Ryan have found something I didn't see! I'll post a couple of comments in my post as spoilers relating to your answer. Feb 9, 2017 at 7:02

Just a guess but...

Is he making some code for the game battleships?
This could be some AI to decide the best/worse moves to find each ships.
The closer you are to a ship, the higher the numbers. The last grid combines the probabilities of all the enemy ships and is used to decided the best next move depending on the AI difficulty level.

• I like this guess. I won't say how close you are, but it's a "miss". Jan 30, 2017 at 15:53

For anyone interested, the script that I ended up with is here:

        function rotateRight():void{
for (var i:int = 0; i < p.numChildren; i++){
var b:Block = p.getChildAt(i) as Block;
innerLoop: for (var j:int = -3; j <= 3; j++){
if (b.rowLocal == b.columnLocal + j){
b.columnLocal += j + (3 - b.rowLocal * 2);
b.rowLocal -= j;
b.updateText();
break innerLoop;
}
}
}
}


For each block in a tetromino piece, you take the row and colum that it is in in relation to the tetrominos center point (indicated by the dot in the middle of the grids) and you can plug them into this function, and get the updated column and row about its center.

The first grid has values that tell how many rows to go down and the second grid shows how many columns to go right. That's all well and good, and a few people were sniffing around that but finally Ryan deduced the correct game, and the function of the grids. The part that remained unsolved was the purpose of the mysterious 3rd grid. I shall explain:

Let's use an example. Let's say the "L" tetromino is positioned like so:

|    |    |    |    |
---------------------
|    |    |XXXX|    |
----------o----------
|XXXX|XXXX|XXXX|    |
---------------------
|    |    |    |    |


This (taken from the first grid) shows us how many rows downward each block should be shifted.

|    |    |    |    |
---------------------
|    |    |  1 |    |
----------o----------
| -2 | -1 |  0 |    |
---------------------
|    |    |    |    |


Let's just focus on one block:

|    |    |    |    |
---------------------
|    |    |    |    |
----------o----------
|    |    |  0 |    |
---------------------
|    |    |    |    |


What formula could I use to determine where that piece will end up after one CW rotation? All I know is it is at row 3 and column 3. What equation will result in a row 3 column 2? Obviously subtracting the column number by 1 gets me there... But look at the next block:

|    |    |    |    |
---------------------
|    |    |    |    |
----------o----------
|    | -1 |    |    |
---------------------
|    |    |    |    |


This block starts at row 3 column 2. If I apply the same formula, it just moves to the left... I need this one to move up and not to the left at all: subtract 1 from the row number and do nothing to the column number. So these formulas don't work independent of starting position. So unless I wanted to write a different formula for each cell, this wouldn't work (one could argue that with only 16 cells, that's not such a bad idea, or even better, just hard code the pieces in the four rotations).

So what formula would work for any block in the grid and any position?

The reason my initial attempt at finding a formula failed was because the amount that a block moves to the left or down or up or wherever is dependent on its row AND its column. In one column, being in the top row means a CW rotation will move that block down a lot, but in a different column, the opposite is true.

But what I finally noticed was that the rate at which that difference occurs was LINEAR, and orthogonal to the rows and columns! So I plotted the DIFFERENCE in the column shift and row shift in one CW rotation...

The answer became clear when I subtracted the grid showing rows values from the grid showing the columns values. The reason this is essentially the same thing as what Ryan said about the second grid being a rotated one is that the rows become columns (in a sense) when they are rotated, so it works out that they look the same. The result was surprisingly orderly. If you take the first top left cell from the first grid and subtract it from the second grid you get +3, etc, leading to this grid:

| +3 | +1 | -1 | -3 |
---------------------
| +3 | +1 | -1 | -3 |
----------o----------
| +3 | +1 | -1 | -3 |
---------------------
| +3 | +1 | -1 | -3 |


This is surprising to me! The number descend by 2s going to the right! Weird.

Now, let's go back to the example from before with the "L" shape:

|    |    |    |    |
---------------------
|    |    |  1 |    |
----------o----------
| -2 | -1 |  0 |    |
---------------------
|    |    |    |    |


We can now write a function that results in the new row and column for each block. I start with the known information: the starting position of a single block:

(oldColumn, oldRow) = (3,3)


And then to get the grid_3_value:

grid_3_value = -3 + (2 * (oldColumn-1))


So, starting with only the oldColumn and oldRow (3,3), I can figure out the colDelta and rowDelta (-1,0) like this:

colDelta = grid_3_value + (3 - oldRow * 2)


So I can plug that value into the colDelta along with my known row. Now to get my rowDelta:

rowDelta = -(grid_3_value)


now just add the rowDelta and colDelta to oldRow and oldColumn, respectively, and you get newRow and newColumn!

The thing that is so beautiful to me about this is that by subtracting the first two grids (row shift and column shift) we get a grid that changes only in one direction which is what allows for such simple calculations.

I wish someone could explain why this works this way. I stumbled upon it and wanted to share it, but I'm far from understanding it fully.

Thanks for taking some of your time with this!

Cheers!

• Note the original Tetris doesn’t rotate all pieces in a 4×4 grid, just the I and O: gamedev.stackexchange.com/questions/17974/…
– Ry-
Feb 10, 2017 at 15:51
• Interesting. I thought I was on the right track. Thanks for that! Feb 10, 2017 at 16:36