Tetris - rotate

61 views
Skip to first unread message

Symbroson Development

unread,
Aug 21, 2016, 9:08:15 AM8/21/16
to DroidScript
I am working on tetris. My problem now is rotating my array.
to see all code I attached the js-file
Here a cut of my code:

obj= ['0000','1100','0110','0000'] //one of 6 possible objects: the 'z'

//defines what to replace with what when rotate
rot=[
'0030','1031','2032','3033',
'3123','3213','3303','2302',
'1301','0300','0210','0120',
'1121','2122','2212','1211']

//if rotate button was pressed
case "Rotate":
temp=obj.slice(0)
for( i=0; i<rot.length; i++ )
obj[rot[i][0]][rot[i][1]]=temp[rot[i][2]][rot[i][3]]
break;

Tetris.js

Symbroson Development

unread,
Aug 21, 2016, 9:53:56 AM8/21/16
to DroidScript
It looks like you cant change a char of a string
s='1123'
s[0]='0'
alert(s) //'1123'
So I have to split the string into arrays...

Symbroson Development

unread,
Aug 21, 2016, 10:23:09 AM8/21/16
to DroidScript
It won't work :(
I will resume working on rotation when I'm finished with all other functions.
could anyone help me with rotation? Would be very nice 😓
Message has been deleted

Netpower8

unread,
Aug 21, 2016, 12:10:27 PM8/21/16
to DroidScript
Try this


//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

//Create a text label and add it to layout.
txt = app.CreateText( "Hello" );
txt.SetTextSize( 32 );
lay.AddChild( txt );

//Add layout to app.
app.AddLayout( lay );

alert (rotateMe(0,0,'1123'));
}

function rotateMe(pos,char,string) {
var ss='';
alert(string.length);
for (var i=0;i<string.length;i++) {
if (i==pos) ss+=char;
else ss+=string.charAt(i);
}
return ss
}

Symbroson Development

unread,
Aug 21, 2016, 12:33:23 PM8/21/16
to DroidScript
Thanks. I must only modify it for 4×4 arrays ^^
Maybe later...

Steve Garman

unread,
Aug 21, 2016, 12:33:38 PM8/21/16
to DroidScript
As a matter of interest, when discussing the TicTacToe sample privately with someone the other day, I wrote

"Some of his coding is a bit tricksy, relying on stuff like implicit coercion of a string to an array
btn.SetText(("XO")[player]);
but he has cleaned up some of the worst excesses of that."

But I now suspect this was not intended to be tricksy at all but arises from a fundamental misunderstanding of what a JavaScript string is.

It really is important to get to grips with JavaScript basics. You might want to take a step back and look at some of the JavaScript references and tutorials on the net. Especially the beginners' stuff.

For instance, because JavaScript tries much too hard to be helpful and converts values to different data types all the time, you could spend months convinced that strings are arrays.

Strings are not arrays of any type. They cannot be changed, only replaced.

Symbroson Development

unread,
Aug 21, 2016, 1:04:25 PM8/21/16
to DroidScript
Yes I know (now ^^)
I made an other system:

obj= newObject()
function newObject(){
return <objects array>[<random number>]
}

and I replaced the strings in the objects array with arrays (I made that only because strings are shorter to write), but the rotation failed anyways. As I said I'll working on that after finishing the other functions

Regards,
Alex

Steve Garman

unread,
Aug 21, 2016, 1:16:31 PM8/21/16
to DroidScript
You may find it useful to know that you can produce an array of single characters from a string using
str.split("");
The parameter is an empty string, not a space.

For example;

var arr = "1234567890".split("");
//test if it worked
if( Array.isArray(arr))
alert(JSON.stringify(arr));

Symbroson Development

unread,
Aug 21, 2016, 1:24:33 PM8/21/16
to DroidScript
Good to know :)
But I switched objects to pure array ;)
Reply all
Reply to author
Forward
0 new messages