# What's the equation for working out NES scoring?

Thread in 'Research & Development' started by Rory76, 2 Dec 2019.

1. I don't know why I want this but I do like simple equations and I was thinking it would be nice to know what a particular score is based on level and lines cleared only.

So the multipliers for every level are 40, 100, 300, and 1200, and that can be worked out by:

X[n]=X[n-1]*(2+2^(n-3))​

where n is the number of lines cleared and X is points from clear. For example, for 3 lines take X for 2 lines (100) and multiply by 2+2^0 and it gives 300 (excuse the notation; I'm strictly amateur with math(s)).

That's about as far as I've got though; seems like it's what they call a 'recursive sequence', but I'm no maths whizz, and don't know where to go from there - i.e., how to find the formula that only uses 40 as a starting point (or some other number) and doesn't rely on manually inputting the previous score total each time.

Last edited: 4 Dec 2019
2. Are you sure this is not just a random formula which happens randomly to generate those 4 values?

3. Nope, not sure. But I suppose because it works it could be used as part of some larger equation. And if there's another equation that works out the same thing, I haven't seen it.

I've reached a bit of a dead end, though, figuring out where to go with exponential sequences.

Last edited: 3 Dec 2019
4. At a quick glance, I'm pretty sure that difference equation is going to have a Pochhammer symbol in the solution, although I haven't actually sat down and worked it out. So it probably is not going to have a nice equation (unless you consider Pochhammer symbols nice, which they kind of are).

I'm not quite clear on what you mean by on level and lines cleared only. Are you trying to calculate the score at some arbitrary point in the game, so after 57 lines cleared for example, or are you trying to figure out how many points any particular clear is worth?

If it's just the latter, then you could just use

$(\frac{280x^3}{3}-490x^2+\frac{2630x}{3}-440)(n+1)$

where x is the number of lines cleared with that piece and n is the current level.

For example, if we're on level 9, and clear a tetris we would have x=4 and n=9, to get

$(\frac{280\times 4^3}{3}-490\times 4^2+\frac{2630\times 4}{3}-440)(9+1)=12000.$

Does this help?

Last edited: 4 Dec 2019
FreakyByte and Rory76 like this.
5. Yes! That's exactly what I was looking for. Thanks!

How on Earth did you do that??

6. You have not seen it means nothing in mathematical term. Its proven that there exists a unlimited number of formulas matching for any combination of n datapoints. And there are mathematical methods to derive those formulas like Taylors theorem or Fourier analysis.

What original NES does however is this: https://www.sangakoo.com/en/unit/functions-defined-by-parts
Where with the original NES ROM and some fixed memory offset k the function is defines as f(x) = memory[n*2+k] if n <= x < n+1. The reason for this is simply its unmatched speed when calculating the number so I do not see any reason to change this to a more complex formula. Still holds true for modern architectures when memory from k was loaded to L1 Cache..

7. I'm glad it's what you're after!! As for how I did it, first note, like you have in your OP that a single line is worth 40, a double is 100, a triple is 300, and a tetris is 1200 at level 0. Then at level 1 it's just twice as much, at level 2 is three times as much, and so on. So if we had some way to calculate the number of points for a single, double, triple, or tetris we could just multiple by n+1, where n is our level. The additional 1 is due to starting at level 0, we don't want to multiple by 0!

The polynomial was obtained by taking the four known points we have, (1, 40), (2, 100), (3, 300), (4, 1200), where the first coordinate represents the number of lines cleared, and the second coordinate represents the value obtained, then I just fit a polynomial across those four points. It requires k+1 points to define a degree k polynomial uniquely, so we were always going to end up with something that was no more than a cubic, but if we got lucky we may have ended up with a smaller degree polynomial. I'm not sure what your level of maths education is, but if you have studied some linear algebra I can go into a bit more detail about how to find this polynomial if you'd like. Now this polynomial for the inputs 1, 2, 3, and 4 will give the correct value for the lines cleared at level 0, as that was how it was designed, so all we have to do is combine this paragraph and the previous and we're done! Rory76 likes this.
8. Bravo sir. I like to dabble in math but never studied it beyond age 17, so polynomials and recursive sequences were all new to me. I did some reading but seems like I headed down the wrong track. Thanks for coming to the rescue! xyrnq likes this.
9. Hmnmm, I've got about an hour free, I'm going to try to explain it using stuff you'll hopefully have seen. I'll point out where you might be interested in reading new topics to help solve a sub problem.

So lets go over polynomials quickly, you're probably familiar with low degree polynomials, such as a line, which (at least in Australia) is typically written mx+b, where m is the gradient and b is the y intercept. We say this is a polynomial of degree 1, as the highest power of x that occurs in it has 1 as the exponent. Then you've probably seen quadratics as well, $$ax^2+bx+c$$. This is a degree 2 polynomial, as the highest power of x that occurs in it has exponent 2. We can just keep adding terms though, and in general a polynomial looks like

$\sum_{k=0}^n a_kx^k=a_0+a_1x+a_2x^+\cdots+a_nx^n$

so this is a polynomial of degree n, as the highest power of x is n.

Now as I mentioned in my last post, you need n+1 points to uniquely define a polynomial of degree n, so for a line you need two points, for a quadratic you need three points, for a cubic you need four points, and so on. There's a proof on this wikipedia page if you're interested in that: https://en.wikipedia.org/wiki/Polynomial_interpolation. Note that this does not contradict what TGGC has said about there being an infinite number of functions going through n+1 points, this is only a restriction on the number of polynomials going through our points.

The reason I decided to use a polynomial for this is mostly down to experience, I did a lot of polynomial fitting in my masters (which I dropped, so maybe I'm not the best person to advocate learning more maths :S), so it was pretty natural for me to take some sequence of numbers and consider them as points in a plane. Creating the points is a pretty natural exercise, you have 1 corresponding to 40, 2 corresponding to 100, 3 corresponding to 300, and 4 corresponding to 1200, so it's not unreasonably to imagine this sequence as a two dimensional sequence, and you get the points (1, 40), (2, 100), (3, 300), (4, 1200).

Now we can consider those on the Cartesian plane, and ask what's the smallest degree curve we can plot that intersects these four points. We now know that given we have n+1 points, we can fit a degree n polynomial to them, so seeing as we have four points, we can fit a degree 3 polynomial to these points, that is, some polynomial of the form $$f(x)=ax^3+bx^2+cx+d$$.

We also have more information we can use though, we actually know what happens for some particular x values.

When x=1, we get f(1)=40, so we can write $$f(1)=a\cdot1^3+b\cdot1^2+c\cdot1+d=a+b+c+d=40$$ which gives us one equation, but four unknowns. But we have three other x values with corresponding values for the polynomial, and hopefully you've solved simultaneous equations before, and if you have then you'll know that you need m equations to find m unknowns, and in this case, we have four unknowns, so we need four equations, which fortunately we can generate with the first x value we've already created the equation for, and the other three x values can get us up to the four equations required.

When x=2, we get f(2)=100, so we can write $$f(2)=a\cdot2^3+b\cdot2^2+c\cdot2+d=8a+4b+2c+d=100$$.

When x=3, we get f(3)=300, so we can write $$f(3)=a\cdot3^3+b\cdot3^2+c\cdot3+d=27a+9b+3c+d=300$$.

When x=4, we get f(4)=1200, so we can write $$f(4)=a\cdot4^3+b\cdot4^2+c\cdot4+d=64a+16b+4c+d=1200$$.

So now we have our four equations, and four unknowns, so we can solve these like a set of simultaneous equations, which is going to be long and tedious, which is why I initially asked if you've ever done any linear algebra, because this is the point where you deviate from maths you learn in school, you'd construct a matrix representation of this system of equations and do something called row reduction to systematically solve this system. I haven't actually read this wikipedia page, but I'm sure it's a good starting point for what I'm skipping here: https://en.wikipedia.org/wiki/System_of_linear_equations. But honestly, it doesn't matter how you solve these equations, and I didn't actually do this by hand in my original post, I just asked wolfram alpha what the solution was*.

Given that we have solved this system however we want, we should get $$a=\frac{280}{3}$$, $$b=-490$$, $$c=\frac{2630}{3}$$, and $$d=-440$$. Now we can substitute this back into our $$f(x)$$, because these are the coefficients we were trying to find. So we get the equation $f(x)=\frac{280}{3}x^3-490x^2+\frac{2630}{3}x-440.$

Feel free to ask questions if you have any, although no promises on how quickly I can respond * Even further admission, I didn't actually do most of this beyond set up the points originally, I just asked wolfram alpha what the polynomial through the four points were, and I assume it solves it like this, or doing something with interpolating polynomials at the very least. This is a pretty standard exercise to give to students who are learning how to solve linear equations using a matrix representation, so either first or second year university courses will have this type of problem. The university I attended shows it in the first year engineering mathematics course without any justification for why it works, and then again in the linear algebra course which is done in second year, with justification for why it works, and uniqueness, and I can't quite remember, but I suspect it's gone over again briefly in our third year numerical methods course when talking about fitting curves to points. I've taught the engineering course and the linear algebra course so they are much more fresh in my mind, whereas the numerical methods course I did in 2012, so the structure of the content is gone from my memory.

Last edited: 5 Dec 2019
Rory76 and CylinderKnot like this.
10. @xyrnq is finally putting that forum LaTeX plugin to good use FreakyByte and xyrnq like this.
11. The original implementation on NES also calculates values for other numbers of line clears for any number from 0 ... 255. However I think 0 is the only useful parameter where its not some random number returned and really used in the game. In fact if you lock a piece clearing 0 lines, 0 is added to you score multiple times reflecting the current level number. With the aforementioned formula you would substract 440.

Rory76 likes this.
12. @Muf It's actually really nice!! I didn't realise the plugin existed until MHL pointed it out to me though.

@TGGC unfortunately it doesn't work quite that nicely, to accommodate the point (0,0) into our list we end up with the polynomial being

$\frac{55x^4}{3}-90x^3+\frac{455x^2}{3}-40x$

and that just replaces the original polynomial. You can technically do it for the other values as well, but they are never actually used, where as it might be somewhat worth it with 0, as you do clear 0 lines with a lot of pieces.

Rory76 likes this.
13. I like the polynomial including the point (0,0); that seems somehow more complete.

I originally posed this as a question elsewhere as to what a hypothetical five-line clear would be worth. The equation in the OP returns 7200, while the two (better ones) from xyrnq give 3360 and 3800 respectively.

@TGGC Do you know what the game would actually give for a five-line clear? I guess something requiring another formula? And so on.

@xyrnq Thanks for the detailed explanation: that made perfect sense. Last edited: 5 Dec 2019
14. It will add the rom values from 9caf and 9cb0 to the score, but in a BCD format. The result on a NES cart should be that it will add either 4206 or 4046 to the score depending on the current score and wether its last two numbers are above or below 60. It also might lead to a garbled score display which is corrected when the next push down points are awarded or it substracts 10 points in the process. So it ultimately adds 4036, 4046, 4196 or 4206 depending on current score and the next push down score. This is very unlikely used to intentially reward a pentris as those memory values are also executed when the tetromino moves down.

Rory76 likes this.