Thread: 2048 - Slightly Different Behaviour

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    35

    2048 - Slightly Different Behaviour

    A few days back, at school, I saw a friend of mine playing 2048, which at that time I hadn't heard of but I liked the game so I made a similar version of it that behaves almost similarly to the original game. I haven't added any achievements and that sort of stuff and neither a win condition so that game can go infinitely long unless you press the ESC
    key.

    (Made on Turbo C++)

    Here's the code (yes, it's complex in my implementation because I'm not very experienced but the original game logic might be much much simpler)

    Code:
    # include <iostream.h>
    # include <conio.h>
    # include <iomanip.h>
    # include <stdlib.h>
    # include <time.h>
    
    
    # define UP_ARROW_KEY    72
    # define DOWN_ARROW_KEY  80
    # define LEFT_ARROW_KEY  75
    # define RIGHT_ARROW_KEY 77
    # define ESCAPE_KEY      27
    
    
    int Lowest_Num(int BOX[4][4]);
    
    
    void main()
    {
        clrscr();
        randomize();
    
    
        int BOX[4][4] , CTR_VAR_1 , CTR_VAR_2 , FLAG;
        int KEYINPUT;
    
    
        for( CTR_VAR_1=0 ; CTR_VAR_1<4 ; CTR_VAR_1++ )
        {
            for( CTR_VAR_2=0 ; CTR_VAR_2<4 ; CTR_VAR_2++ )
            {
                BOX[CTR_VAR_1][CTR_VAR_2]=0;
            }
        }
    
    
        CTR_VAR_1=random(4);
        BOX[CTR_VAR_1][CTR_VAR_1]=2;
    
    
        lbl:
        if( (CTR_VAR_2=random(4))==CTR_VAR_1 )
        {
            goto lbl;
        }
        else
        {
            BOX[CTR_VAR_2][CTR_VAR_2]=2;
        }
    
    
        do
        {
            clrscr();
            cout<<endl<<endl<<endl;
            for( CTR_VAR_1=0 ; CTR_VAR_1<4 ; CTR_VAR_1++ )
            {
                for( CTR_VAR_2=0 ; CTR_VAR_2<4 ; CTR_VAR_2++ )
                {
                    cout<<setw(5)<<BOX[CTR_VAR_1][CTR_VAR_2];
                }
                cout<<endl<<endl;
            }
    
    
            KEYINPUT=getch();
    
    
            switch(KEYINPUT)
            {
                case UP_ARROW_KEY:
                {
                    for( CTR_VAR_1=1 ; CTR_VAR_1<4 ; CTR_VAR_1++ )
                    {
                        for( CTR_VAR_2=0 ; CTR_VAR_2<4; CTR_VAR_2++ )
                        {
                            switch(CTR_VAR_1)
                            {
                                case 1:
                                {
                                    if( BOX[CTR_VAR_1-1][CTR_VAR_2]==0 || BOX[CTR_VAR_1-1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 2:
                                {
                                    if( BOX[CTR_VAR_1-1][CTR_VAR_2]==0 || BOX[CTR_VAR_1-1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1-2][CTR_VAR_2]==0 || BOX[CTR_VAR_1-2][CTR_VAR_2]==BOX[CTR_VAR_1-1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-2][CTR_VAR_2]+=BOX[CTR_VAR_1-1][CTR_VAR_2];
                                        BOX[CTR_VAR_1-1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 3:
                                {
                                    if( BOX[CTR_VAR_1-1][CTR_VAR_2]==0 || BOX[CTR_VAR_1-1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1-2][CTR_VAR_2]==0 || BOX[CTR_VAR_1-2][CTR_VAR_2]==BOX[CTR_VAR_1-1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-2][CTR_VAR_2]+=BOX[CTR_VAR_1-1][CTR_VAR_2];
                                        BOX[CTR_VAR_1-1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1-3][CTR_VAR_2]==0 || BOX[CTR_VAR_1-3][CTR_VAR_2]==BOX[CTR_VAR_1-2][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1-3][CTR_VAR_2]+=BOX[CTR_VAR_1-2][CTR_VAR_2];
                                        BOX[CTR_VAR_1-2][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                default: break;
                            }
                        }
                    }
                }
                break;
    
    
                case DOWN_ARROW_KEY:
                {
                    for( CTR_VAR_1=2 ; CTR_VAR_1>=0 ; CTR_VAR_1-- )
                    {
                        for( CTR_VAR_2=0 ; CTR_VAR_2<4; CTR_VAR_2++ )
                        {
                            switch(CTR_VAR_1)
                            {
                                case 2:
                                {
                                    if( BOX[CTR_VAR_1+1][CTR_VAR_2]==0 || BOX[CTR_VAR_1+1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 1:
                                {
                                    if( BOX[CTR_VAR_1+1][CTR_VAR_2]==0 || BOX[CTR_VAR_1+1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1+2][CTR_VAR_2]==0 || BOX[CTR_VAR_1+2][CTR_VAR_2]==BOX[CTR_VAR_1+1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+2][CTR_VAR_2]+=BOX[CTR_VAR_1+1][CTR_VAR_2];
                                        BOX[CTR_VAR_1+1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 0:
                                {
                                    if( BOX[CTR_VAR_1+1][CTR_VAR_2]==0 || BOX[CTR_VAR_1+1][CTR_VAR_2]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+1][CTR_VAR_2]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1+2][CTR_VAR_2]==0 || BOX[CTR_VAR_1+2][CTR_VAR_2]==BOX[CTR_VAR_1+1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+2][CTR_VAR_2]+=BOX[CTR_VAR_1+1][CTR_VAR_2];
                                        BOX[CTR_VAR_1+1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1+3][CTR_VAR_2]==0 || BOX[CTR_VAR_1+3][CTR_VAR_2]==BOX[CTR_VAR_1+2][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1+3][CTR_VAR_2]+=BOX[CTR_VAR_1+2][CTR_VAR_2];
                                        BOX[CTR_VAR_1+2][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                default: break;
                            }
                        }
                    }
                }
                break;
    
    
                case RIGHT_ARROW_KEY:
                {
                    for( CTR_VAR_2=2 ; CTR_VAR_2>=0 ; CTR_VAR_2-- )
                    {
                        for( CTR_VAR_1=0 ; CTR_VAR_1<4; CTR_VAR_1++ )
                        {
                            switch(CTR_VAR_2)
                            {
                                case 2:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2+1]==0 || BOX[CTR_VAR_1][CTR_VAR_2+1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 1:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2+1]==0 || BOX[CTR_VAR_1][CTR_VAR_2+1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2+2]==0 || BOX[CTR_VAR_1][CTR_VAR_2+2]==BOX[CTR_VAR_1][CTR_VAR_2+1] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+2]+=BOX[CTR_VAR_1][CTR_VAR_2+1];
                                        BOX[CTR_VAR_1][CTR_VAR_2+1]=0;
                                    }
                                }
                                break;
    
    
                                case 0:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2+1]==0 || BOX[CTR_VAR_1][CTR_VAR_2+1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2+2]==0 || BOX[CTR_VAR_1][CTR_VAR_2+2]==BOX[CTR_VAR_1][CTR_VAR_2+1] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+2]+=BOX[CTR_VAR_1][CTR_VAR_2+1];
                                        BOX[CTR_VAR_1][CTR_VAR_2+1]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2+3]==0 || BOX[CTR_VAR_1][CTR_VAR_2+3]==BOX[CTR_VAR_1][CTR_VAR_2+2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2+3]+=BOX[CTR_VAR_1][CTR_VAR_2+2];
                                        BOX[CTR_VAR_1][CTR_VAR_2+2]=0;
                                    }
                                }
                                break;
    
    
                                default: break;
                            }
                        }
                    }
                }
                break;
    
    
                case LEFT_ARROW_KEY:
                {
                    for( CTR_VAR_2=1 ; CTR_VAR_2<4 ; CTR_VAR_2++ )
                    {
                        for( CTR_VAR_1=0 ; CTR_VAR_1<4; CTR_VAR_1++ )
                        {
                            switch(CTR_VAR_2)
                            {
                                case 1:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2-1]==0 || BOX[CTR_VAR_1][CTR_VAR_2-1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
                                }
                                break;
    
    
                                case 2:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2-1]==0 || BOX[CTR_VAR_1][CTR_VAR_2-1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2-2]==0 || BOX[CTR_VAR_1][CTR_VAR_2-2]==BOX[CTR_VAR_1][CTR_VAR_2-1] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-2]+=BOX[CTR_VAR_1][CTR_VAR_2-1];
                                        BOX[CTR_VAR_1][CTR_VAR_2-1]=0;
                                    }
                                }
                                break;
    
    
                                case 3:
                                {
                                    if( BOX[CTR_VAR_1][CTR_VAR_2-1]==0 || BOX[CTR_VAR_1][CTR_VAR_2-1]==BOX[CTR_VAR_1][CTR_VAR_2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-1]+=BOX[CTR_VAR_1][CTR_VAR_2];
                                        BOX[CTR_VAR_1][CTR_VAR_2]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2-2]==0 || BOX[CTR_VAR_1][CTR_VAR_2-2]==BOX[CTR_VAR_1][CTR_VAR_2-1] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-2]+=BOX[CTR_VAR_1][CTR_VAR_2-1];
                                        BOX[CTR_VAR_1][CTR_VAR_2-1]=0;
                                    }
    
    
                                    if ( BOX[CTR_VAR_1][CTR_VAR_2-3]==0 || BOX[CTR_VAR_1][CTR_VAR_2-3]==BOX[CTR_VAR_1][CTR_VAR_2-2] )
                                    {
                                        BOX[CTR_VAR_1][CTR_VAR_2-3]+=BOX[CTR_VAR_1][CTR_VAR_2-2];
                                        BOX[CTR_VAR_1][CTR_VAR_2-2]=0;
                                    }
                                }
                                break;
    
    
                                default: break;
                            }
                        }
                    }
                }
                break;
    
    
                default: break;
            }
    
    
            if( KEYINPUT==UP_ARROW_KEY || KEYINPUT==DOWN_ARROW_KEY || KEYINPUT==LEFT_ARROW_KEY || KEYINPUT==RIGHT_ARROW_KEY )
            {
                do
                {
                    FLAG=0;
                    CTR_VAR_1=random(4);
                    CTR_VAR_2=random(4);
    
    
                    if( Lowest_Num(BOX)==BOX[CTR_VAR_1][CTR_VAR_2] )
                    { FLAG=1; }
    
    
                }while( FLAG!=1 );
            }
    
    
            BOX[CTR_VAR_1][CTR_VAR_2]=2;
    
    
        }while( KEYINPUT!=ESCAPE_KEY);
    
    
        getch();
    }
    
    
    int Lowest_Num(int BOX[4][4])
    {
        int MIN;
    
    
        for( int i=0 ; i<4 ; i++ )
        {
            for( int j=0 ; j<4 ; j++ )
            {
                if( BOX[i][j]<=MIN)
                {
                    MIN=BOX[i][j];
                }
            }
        }
    
    
        return MIN;
    }
    I decided to post this here because me and my friends are beginners and have been practising C++ for about 1+ years but when I made this, they were all awestruck but honestly it was really easy. It was just messing around with arrays and key inputs. Another reason for posting it is because there might be someone somewhere who might look up google for reference and this is could be of some help

    (Also, I didn't type all of it because I'm not retarded. It's just simple copy-pasting the same stuff and changing the variables once you figure out the logic)
    Last edited by Zeus_; 4 Weeks Ago at 08:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,378
    > It's just simple copy-pasting the same stuff and changing the variables once you figure out the logic)
    Yeah, before you reached for ctrl-v, you should have been thinking "How do I make this a function?".

    You've copy/pasted yourself to a main that's nearly 400 lines long!.

    > void main()
    main returns int - period.
    Ignore the fact that your OBSOLETE compiler seems to accept it.
    At least do yourself a favour and recognise that it's obsolete and don't take on all it's bad habits.

    > lbl:
    > if( (CTR_VAR_2=random(4))==CTR_VAR_1 )
    > goto lbl;
    Ugh!!!!
    Never use goto to jump backwards in the code.
    Use a while loop or something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    35
    I know I was dumb as hell when I copy pasted it. I definitely thought of making a function but that was after I finished copy-pasting all of it so I never actually returned to rewriting it. Although, I will do it tonight as you suggested it anyway.

    As for the goto part, uh...my...net errr ..stopped... working...?!?

    > main returns int

    No it doesn't necessarily. Not if you were using a compiler made in the 90s ))
    I do use int everywhere else like when I'm using C::B but I just resort to using void in Turbo which I know is a bad practice so as you said, I'll stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-23-2017, 09:50 AM
  2. slightly stuck with C code
    By suspense in forum C Programming
    Replies: 6
    Last Post: 05-11-2016, 09:39 AM
  3. Slightly Disoriented.
    By CrazyNorman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-11-2005, 12:24 PM
  4. Slightly depraved humor
    By misplaced in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-29-2005, 01:34 PM
  5. a slightly different triangle
    By sweetly in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2003, 01:46 AM

Tags for this Thread