Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Programming Tetris is it okay to hardcode elements in a 2D array

This topic is 2125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am coding a game called Tetris. I want to know, is hardcoding elements in a 2D array ever okay? 

 

Also, is this approach readable to anyone? Code is in Java

 

This code is for the reversed L piece.

/*1)                    *                {1,2}
         *              *                {1,1}
         *            * *          {0,0} {1,0}  
         */            
        if(firstForm)
        {

            array[0][0] = 1;
            array[1][0] = 1;
            array[1][1] = 1;
            array[1][2] = 1;
        }
         /*2)         *            {0,1}
         *            * * *        {0,0} {1,0} {2,0}
         */          
        else if(secondForm)
        {

                        array[0][0] = 1;
                        array[0][1] = 1;
                        array[1][0] = 1;
                        array[2][0] = 1;
        }
         /*3)           * *          {0,2} {1,2}
         *              *            {0,1}
         *              *            {0,0}
         */    
        else if (thirdForm)
        {

                        array[0][0] = 1;
                        array[0][1] = 1;
                        array[0][2] = 1;
                        array[1][2] = 1;
        }
         /*4)                              
         *            * * *    {0,1} {1,1} {2,1}
         *                *                {2,0}
         */
        else if (fourthForm)
        {

                        array[0][1] = 1;
                        array[1][1] = 1;
                        array[2][0] = 1;
                        array[2][1] = 1;
        }
Edited by warnexus

Share this post


Link to post
Share on other sites
Advertisement
"OK" is a nebulous term.

Can you get the game implemented doing that? Sure. Does it matter in the long run? Not really. Are there simpler and more robust ways to solve the problem? Almost certainly.

For instance, you could store the shape exactly once, and rotate it dynamically, instead of hard-coding all the rotations.

Share this post


Link to post
Share on other sites

It's 'okay' depending on how clean your resulting code is, and how copy-pasty it is.

I feel like if you only require a few combinations, and it's very cleanly written, and each combination only requires a little bit of data, yeah go for it.

But if each piece of data requires alot of info, or you need alot of combinations, then I'd load them from file.
Tetris combinations is right on the edge of it, imo, but as a learning project, I think it'd be fine if you code it cleanly (making it very easy to read if someone other than you, or a future you two years from now, were to read the code).

Since there are only seven combinations (if you rotate it programmatically), I think hardcoding it is fine. Alternatively, you could load it from a file by parsing something like this:

....
..X.
..X.
.XX.

Share this post


Link to post
Share on other sites


However your implementation leaves much to be desired.
It should be similar to this:


static const unsigned char s_ucBar[4][4][4] = {
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
},
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
},
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
},
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
},
};

And instead of copying the array you simply reference it.

 

I wrote a clone of the gameboy Tetris in Ruby, Python, D and C++ as a language comparison project a long time ago when I was at Uni and my implementation was very similar to this.  I raplaced the music on the Ruby version with a midi of "Ruby Tuesday" :).

Share this post


Link to post
Share on other sites


However your implementation leaves much to be desired.
It should be similar to this:


static const unsigned char s_ucBar[4][4][4] = {
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
},
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
},
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
},
{
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
},
};

 

Oh wow! That does look much cleaner! Thanks for the code example!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!