Author Topic: Tetris in TI basic  (Read 20008 times)

0 Members and 3 Guests are viewing this topic.

Offline theUnnamed

  • LV3 Member (Next: 100)
  • ***
  • Posts: 63
  • Rating: +3/-3
    • View Profile
Re: Tetris in TI basic
« Reply #30 on: April 17, 2010, 06:51:24 pm »
looking at your code It is far more brute force than my design which tries to minimize interpreter parse time by employing lists as much as possible. I also keep an index of the board in a matrix in the background which is what takes up the most memory because i found that it was very slow to test the pixels on the screen.  I found a nifty way of making line elimination detection really fast.  and my original version made an effort to reduce flicker of the blocks by pre-calculating the shift and only changing the pixels that absolutely had to be changed. As you can tell my design is far more complicated then yours.  I guess I got so wrapped up in optimization that I neglected to take the simple approach. I don't understand is how you are doing a few things:
controlling the speed of block falls
removing lines
generating block patterns

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Tetris in TI basic
« Reply #31 on: April 17, 2010, 07:13:41 pm »
Yeah, i find that Lists tend to be a lot slower because then you need to use loops to iterate through the 4 elements of the piece, and while the interpreter might be slow, running a loop if definetaly slower than expanding it all the way out.  For example:

Code: [Select]
Pt-On(0,0
Pt-On(1,0
...
Pt-On(94,0

is always going to be faster than

Code: [Select]
For(F,0,95
Pt-On(F,0
End

because even though it might look like you are cutting down on parser time, the OS is still parsing Pt-On(F,0 96 times, and now you are both using the variable F, but also having to go through the loop 96 times.

As for controling the speed of the falling, i use this piece of nifty code:

Code: [Select]
C(C!=V)+1->C
Which when looped over and over, will make C loop from 1 to V-1 over and over.  When C=1 i move the piece down, and so changing V changes the speed at which the piece falls.  Or more accurately, V is the number of frames between drops of the piece.

For removing lines, I retrieve the Y values of the piece that just dropped (since those are the only ones that could possible clear lines) and i loop through them top to bottom.  I loop across the entire screen, and if every block is filled, i move onto the clear line set of code.  The clear line set of code copies lines from the line above, to the current line, and moves upward until there are no more blocks to be copied.  After the program does this for all 4 possible Y values, the code exits.

Generating block patterns is easy, I merely store all possible block patterns in the beginning of the program to 2 lists (the storing is a bit weird because i was trying to cut down on memory) and they are accessed with Random numbers.

This Tetris clone is not perfect, but the main loop is so minimal that i see very little ways for it to be sped up.  One way would to be to do as you suggested and only switch the pixels that change, although calculating which pixels is often going to take longer than just turning them off to begin with.  Right now the main loop consists of only this:

Turning on the piece
Incrementing the counter
Getting the Keypress
turning Off the piece (not always done
Moving the piece
Checking for collision

And as every single one of these pieces of code is needed, the only way i can see to make it faster is to improve on the methods used.  However, there might be an alternative method that i am overlooking, often times thinking out of the box can yield impressive results, with creative code :) Tetris in Ti Basic is definetaly one of the greatest challenge for a Basic programmer, as is many real time puzzle/action games

Offline theUnnamed

  • LV3 Member (Next: 100)
  • ***
  • Posts: 63
  • Rating: +3/-3
    • View Profile
Re: Tetris in TI basic
« Reply #32 on: April 17, 2010, 08:37:38 pm »
I do realize that Iterating a loop takes time but I do not think you realize how for loop iteration is done in TI-basic or how much faster variable resolution is then number resolution.  In TI basic the varibles a-z and Theda are indexed using an array and the calculator doesn't have to do anything more then *(arry_ptr+var_name-'a') to retrieve the value. where as the resolution of a number is a far more complex task because It has to do a conversion form a char array to a float which If you've ever implemented that in c is not pretty and a rather complex loop even when optimized. additionally for loop end values,increments and a pointer to the line immediately following the for( statement is cashed on the stack making the incrementing and checking of for loop conditions a trivial amount of time because no interpretation is ever done if you need proof of this fact try this:
Code: [Select]
:1->X
:0
:For(X,1,100,X
:Ans+1
:End
:Ans
if the interpreter reinterpreted this loop each time conditions were checked the loop would run only 8 times but instead it runs 100. therefore it must cashe the value of the inc as of the time the loop starts and use that.  This also explains the weird discrepancy in behavior of the for and while loops but I digress.

here is my strategy for detecting coalitions, row eliminations and full row detection a matrix of size board width+2 by board height+1 this seems like a crazy waste of memory but makes collision detection, row detection and row moving faster.
first step is the pregame setup
you set up your matrix like this (for your 10X16 board)
Code: [Select]
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1,1

now checking for a collision become a single short line (assume L1 to be x coordinates and L2 to be y coordinates)
Code: [Select]
0=sum(seq([B](L[sub]1[/sub](i),L[sub]2[/sub](i)),i,1,4there is no argument that this will woln't execute faster then your check condition because it runs far faster (like half the time if i remember correct)
once the final placement of the block is determined do this:
Code: [Select]
:For(I,1,4
:L1(i->X
:L2->Y
:1->[B](Y,X
:[B](Y,X-1->A
:[B](Y,X+1
:Ans+A+1->[B](Y,X+Ans
:Ans->[B](Y,X-A
:End
:clrlist L3
:For(Y,min(L2),max(L2
:If 12=[B](Y,1
:Then
:Y->L3(1+dim(L3
:mRow(0,[B],Y
:1->[B](Y,1
:Ans->[B](Y,30
:End
:End
L3 now contains the rows that must be eliminated and they be have been reset to [1,0,0,0,0,0,0,0,0,0,0,1]
you can then use row the row swap command to move the line down. then you update the screen but I forget how I did that quickly right now so I'll get back to you when I remember.
YOU CANNOT TURN THE MATRIX  to make the index order constant with that of the pixel drawing proceedure or you lose the fast row elimination.
« Last Edit: April 17, 2010, 09:54:31 pm by theUnnamed »

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Tetris in TI basic
« Reply #33 on: April 17, 2010, 09:32:56 pm »
This is all really interesting. Learning lots.

@theUnnamed
Just a little tip is that BBCode doesn't work inside of the [code][/code] command, such as your subscript commands. Just thought I'd let ya know :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline theUnnamed

  • LV3 Member (Next: 100)
  • ***
  • Posts: 63
  • Rating: +3/-3
    • View Profile
Re: Tetris in TI basic
« Reply #34 on: April 17, 2010, 09:35:59 pm »
@theUnnamed
Just a little tip is that BBCode doesn't work inside of the [code][/code] command, such as your subscript commands. Just thought I'd let ya know :)
I realize this now

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Tetris in TI basic
« Reply #35 on: April 17, 2010, 09:54:16 pm »
In TI basic the varibles a-z and Theda are indexed using an array and the calculator doesn't have to do anything more then *(arry_ptr+var_name-'a') to retrieve the value. where as the resolution of a number is a far more complex task because It has to do a conversion form a char array to a float which If you've ever implemented that in c is not pretty and a rather complex loop even when optimized.

While it makes sense in theory, the TiOS is fairly weird, try these programs: (Note you need an 84)

Code: [Select]
StartTimer->A
For(F,0,9001
123+456
End
Output(1,1,CheckTimer(A)/F

Code: [Select]
StartTimer->A
For(F,0,9001
C+C
End
Output(1,1,CheckTimer(A)/F

This is my old code tester program, it tells you how many seconds it takes for any given piece of code to execute.  It is very useful for comparing two pieces of code.  It works by starting the timer, running the code for over 9000 iterations (more if needed) and then finding the ratio.  The first program evaluates to about .00279 Seconds, and the second evaluates to .00365 seconds.  Of course this isnt completely accurate because of the presence of the for loop, but it does give a good way to compare, and for some strange reason Variables take longer then Numbers o.O and accessing Lists takes longer than both variables or numbers, at

As for matrices, while they do make for line testing to be quite faster, everything else becomes quite slower.  Reading from a matrix of that size takes about .00411 seconds, while using a PixelTest takes .00322 seconds.  As for storage, storing to a matrix takes .00444 seconds, and storing to the screen... you already did it when you updated the screen ;) so it takes no extra time.  I focused on getting the main loop as fast as possible, and since it needs at least some form of collision, i decided for pixel-tests instead of matrices.


Offline theUnnamed

  • LV3 Member (Next: 100)
  • ***
  • Posts: 63
  • Rating: +3/-3
    • View Profile
Re: Tetris in TI basic
« Reply #36 on: April 17, 2010, 10:31:04 pm »
in the older version of the OS (by older I mean really old) I got better performance with the matrix method.

also your testing procedure is flawed you forget that variables can store complex numbers so your "access time" difference is not dew to the difference in access time but dew to the fact that it has to do a complex number addition instead of an  real number addition. which in fact takes longer because its doing 2 floating point adds which on the z80 are the slow step of that line of code.

I have no Idea how to test this and I currently am using a N-spire emulation for my 84 so I can not truly accurately test this because the emulator runs on an OS that is not nearly as constant in runtime as the real 84+ is also you should try changing the top line to
Code: [Select]
:starttmr->A
:While not(checkTmr(A
:End[
and the last line to
Code: [Select]
:CheckTmr(A+1)/Fthis allows for a more consistent start time within the clock cycle.
and yes matrix access is slower...
in a loop because of parse time of the index but when in a seq( command it's just as fast if not faster because the interpreter only parses it once and then executes the entire seq( statement.
« Last Edit: April 18, 2010, 09:09:31 pm by theUnnamed »

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Tetris in TI basic
« Reply #37 on: April 17, 2010, 11:09:19 pm »
But wait, I was Using real variables, not immaginary ones.  The OS does know the difference, and even groups them diffeently in the memory menu.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Tetris in TI basic
« Reply #38 on: April 17, 2010, 11:12:44 pm »
To check the difference, does it takes more processing time, though?

Also I am not too sure if speed tests done on a TI-Nspire can really be taken for granted, since this calc, in 84+ mode, doesn't even emulate the original speed correctly. Some stuff will be slower and other stuff faster and it varies from an OS to another. OS 2.0, for instance, is considerably faster with ASM.
« Last Edit: April 17, 2010, 11:13:57 pm by DJ Omnimaga »

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Tetris in TI basic
« Reply #39 on: April 18, 2010, 01:09:21 am »
Okay i switched the code to this instead to get rid of any discrepancy:

Code: [Select]
StartTmr->A
Repeat checkTmr(A
End
For(F,0,9001
C
End
Output(1,1,checkTmr(A)/F

and

Code: [Select]
StartTmr->A
Repeat checkTmr(A
End
For(F,0,9001
123
End
Output(1,1,checkTmr(A)/F

and i got .002888 seconds for the first code and .002332 seconds for the second code.  Somehow, even merely storing the result to Ans is slower when accessing a variable.


And as for matrices, i understand how they are so much faster for line clearing, but they are not faster when being accessed for collision, even if put into a seq() command.  And i would much rather have fast gameplay and a half second slower line clearing than slower gameplay and blazing fast line clearing.
 

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Tetris in TI basic
« Reply #40 on: April 18, 2010, 01:31:51 am »
i agree with builderboy on the last one. Most Tetris clones on ticalc are too slow to be any fun and challenging, this one should be as fast as possible during falling pieces. We don't mind if line erasing is slower.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Tetris in TI basic
« Reply #41 on: April 18, 2010, 11:28:17 am »
Wow, this is a really cool challenge!  Good luck you two! :D

As for speed, I'm pretty sure the methods Builderboy is employing are almost as fast as if could ever get. ;D

I can't wait to see what you come up with, TheUnnamed. :)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Tetris in TI basic
« Reply #42 on: April 18, 2010, 11:30:35 am »
As for speed, I'm pretty sure the methods Builderboy is employing are almost as fast as if could ever get. ;D
True, I personally have no doubts for his skills. Just having seen his previous work in the TI community is enough to convince me his optimizing skills are practically as good as Weregoose's, in overall. Plus he has done calc stuff for quite a while already.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Tetris in TI basic
« Reply #43 on: April 18, 2010, 01:53:14 pm »
Thanks guys :)

Plus he has done calc stuff for quite a while already.
Tomorrow I will have been with Omnimaga for a year :O

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Tetris in TI basic
« Reply #44 on: April 18, 2010, 02:04:39 pm »
Nice. :)

Builderboy, I just tried your version, and it works Very well!  Great job!  Tetris in TI Basic is really hard to make look so good, and you've done a wonderful job! ;D

When I play, I always use Up for Rotate, so in your code I changed it so there are some 2=abs(K-23 where there was K=21. ;D

I think you should make the pieces contact the walls also, and instead of moving the walls, I'd make them 2 pixels thick, so it touches and you can pxlTest. :D

Overall, Wonderful job!  This is an incredible program! ;D