Jump to content

Two Dimmensionl arrays(c++)

LEXERA

Hi I'm trying to make the 2048 game using two dimensional arrays. I need some with help with a question.

basically im trying to figure out to combine the numbers, while that sound very vague I think an example would be better.

if I wanted to add upwards:

[2][4][8][16]

 ^   ^  ^   ^

 |    |   |    |

[2][4][8][16].

basically how do I add rows in multi-dimensional arrays.

I've thought of using simple inefficient if statements but the logic would take forever to write.

I'm thinking there's a more simple way of adding row 2 to row 1.

Btw, I'm having the computer make it's own moves with a randomizer based system. It basically pics a number between 1-4 and if the number is equal to 1 it goes upwards.

Link to comment
Share on other sites

Link to post
Share on other sites

and I forgot to mention, this game isn't just to 2048.

It's actually to any number higher that s^32.

it's a project my teacher gave me.

Link to comment
Share on other sites

Link to post
Share on other sites

a for loop works, but a while loop makes more sense.

Link to comment
Share on other sites

Link to post
Share on other sites

I currently don't have Visual Studio installed, but I will try :)

Link to comment
Share on other sites

Link to post
Share on other sites

Sure no problem, thanks for responding though! i appreciate it!

Link to comment
Share on other sites

Link to post
Share on other sites

Here is my progress so far..

 

int gridSize;

int[gridSize][gridSize] grid;

bool moveUp = true;

 

for(int i = 0; i < gridSize; i++)
{

for(int i = 0; i < gridSize; i++)
{

   if(moveUp)

   {

       for(int j = 0; j < gridSize; i++)
       { 

         

       {

   }

   else

   {

       for(int j = 0; j < gridSize; i++)
       { 

          

       {

   }

}
}

Link to comment
Share on other sites

Link to post
Share on other sites

Grr :P you should go on stack overflow, where it is meant for coding

 

I am continuning to work on it... I just realized that I need to do a null check.

Link to comment
Share on other sites

Link to post
Share on other sites

Lol, sure I'll try

but this is the code I got up to.

int main()
{
    class GameConfig{

        public : int Size[4][4] = {};
        void Rpos(int x, int y){
            //begins with a random position*//
         x = rand() % 15;
         y = rand() % 15;
        if (x == y)
            {
            x - 1;
        }

        Size[x][y];
    }
        void Play(){
            int trns = 1;
            while(trns == 1)
            {
            int Dir = rand() %  4 + 1;
            }
            if()

        }
}game;

    return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, LEXERA said:

Lol, sure I'll try

but this is the code I got up to.

You could use a function such as

 

void CheckDown(int posX, int posY)
{

   for(int i = posY; i < 4; i++)

   {

      if(grid[posX] == grid[posX][posY])
      {

          grid[posX][posY] = -1;

          grid[posX] *= 2;

      }

   }
}

 

I am also a student :)

Link to comment
Share on other sites

Link to post
Share on other sites

The format of the code is conflicting to the forum's format

Link to comment
Share on other sites

Link to post
Share on other sites

I could tell, when you didn't yell at me for my indenting.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, LEXERA said:

I could tell, when you didn't yell at me for my indenting.

That would be hypocritical :P

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LEXERA said:

but your code is genius!!!

Thanks :). Does it work though?

Link to comment
Share on other sites

Link to post
Share on other sites

I havent implenented it yet but I don't think it will fit in exactly.

I might have to change the code a bit, but the concept definitely helps.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LEXERA said:

I havent implenented it yet but I don't think it will fit in exactly.

I might have to change the code a bit, but the concept definitely helps.

There is probably a much more efficient way to do this with vectors and stuff, but I'm glad to help :)

Link to comment
Share on other sites

Link to post
Share on other sites

yeah you're so right. Well I have to go to bed now, it's getting real late where I am.Thanks for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, LEXERA said:

Lol, sure I'll try

but this is the code I got up to.

int main()
{
    class GameConfig{

        public : int Size[4][4] = {};
        void Rpos(int x, int y){
            //begins with a random position*//
         x = rand() % 15;
         y = rand() % 15;
        if (x == y)
            {
            x - 1;
        }

        Size[x][y];
    }
        void Play(){
            int trns = 1;
            while(trns == 1)
            {
            int Dir = rand() %  4 + 1;
            }
            if()

        }
}game;

    return 0;
}

Sorry, but that code makes no sense at all :(

 

Also, what do you mean with "add rows"...

 

Do you mean adding a new row, making the array larger...

Or do you mean mathematically adding the values of a row with the values of another row?

 

Link to comment
Share on other sites

Link to post
Share on other sites

@LEXERA, please excuse any typos and formatting I'm on a mobile phone. 

 

Id start with psuedo code which you sort of did. Using a 2D array as a grid would help. Using a 4x4 grid indexing 0-3 where 3 is the greatest x and y value the grid would loop like this:

 

3

2

1

0

   0 1 2 3

 

Using a switch statement you could write a flow of movement or physics. Sticking with 1 for up, 2,3,4 being the other directions you could do something like this:

 

Int N = grid size; //4

Case 1: 

For i =3; I>=0; I--//used for x axis coordinated

- for J=3;j>=0; j-- //used for y axis coordinates

-- if slot [j] == 0 && [N - J] != 0

--- [j] = [n - j]

 

now there will be problems with this exact formula as it does skip a few numbers but if you work on that equation a bit, it should work. 

It may fully non-functional. I thought about moving the pieces with the array used to flip, or sort an array (n - I - 1 or something like that.) and tried using conditionals to move things around. 

Link to comment
Share on other sites

Link to post
Share on other sites

I found an algorithm that seems to work, but I coded this in Python so you'll have to figure out how to code it in C++ with the generalized form of this algorithm:

 

The idea is to check in the opposite direction of the move you want and go through the combinations. This means you have at the minimum cNr([# of columns], 2) * (# of rows) comparisons to make. So for a 4x4 grid, you have 24 comparisons (cNr(4,2) = 6 * 4 = 24)

 

The algorithm is as follows (using arrays or whatever starting at 1, not 0):

  1. Let first_number equal item(n)
  2. For x from n-1 to 1
    • Let second_number equal item(x)
    • If first_number == 0 or first_number == second_number
      • Add item(x) to item(n)
      • Item(n) = 0
    • If first_number != 0 and second_number != 0
      • Go to Step 3
    • Else
      • Update first_number to equal item(x)
  3. Go back to step 1 for item(n-1), repeat until you get to item(1)

 

This works in one direction. The opposite direction is basically the opposite as far as how to traverse the grid. :3

 

So as an example, let's say we have a row of [4, 4, 2, 0] and we're moving right.

  • first_number = item(4) = 0
    • second_number = item(3) = 2
    • Since first_number == 0:
      • item(4) + second_number = 2
      • item(3) = 0
    • first_number = Item(4) = 2
    • second_number = item(2) = 4
    • Since first_number is not 0 and first_number does not equal second_number, continue on.
    • Since both first_number and second_number, are not 0, skip checking the rest of the numbers against first_number
  • Row looks like [4,4,0,2]
  • first_number = item(3) = 0
    • second_number = item(2) = 4
    • Since first_number == 0:
      • item(3) + second_number = 4
      • item(2) = 0
    • first_number = Item(3) = 4
    • second_number = item(1) = 4
    • Since first_number does equal second_number
      • item(3) + second_number =  8
      • item(1) = 0
    • first_number = Item(3) = 8
    • Since both first_number and second_number, are not 0, skip checking the rest of the numbers.
  • Row looks like [0, 0, 8, 2]
  • And the rest of the comparisons move 0s.

When you do a move, you apply it to all of the rows or columns, depending on the direction of the move.

 

Good luck :3

 

EDIT: If you want to check my work, here's the output of 32 random moves, but using only 2 as the feeder number.

Spoiler


ROUND 0
0 0 0 0
0 0 0 0
0 0 0 2
0 0 0 0

Moving Up

ROUND 1
0 0 0 2
0 0 0 0
2 0 0 0
0 0 0 0

Moving Left

ROUND 2
2 0 0 0
0 0 0 2
2 0 0 0
0 0 0 0

Moving Left

ROUND 3
2 0 0 0
2 0 0 2
2 0 0 0
0 0 0 0

Moving Up

ROUND 4
4 0 0 2
2 0 0 0
0 0 0 0
2 0 0 0

Moving Right

ROUND 5
0 0 4 2
0 0 0 2
2 0 0 0
0 0 0 2

Moving Left

ROUND 6
4 2 0 0
2 0 0 0
2 0 0 2
2 0 0 0

Moving Left

ROUND 7
4 2 2 0
2 0 0 0
4 0 0 0
2 0 0 0

Moving Left

ROUND 8
4 4 0 0
2 0 0 0
4 0 2 0
2 0 0 0

Moving Up

ROUND 9
4 4 2 0
2 0 0 2
4 0 0 0
2 0 0 0

Moving Up

ROUND 10
4 4 2 2
2 0 0 0
4 0 2 0
2 0 0 0

Moving Up

ROUND 11
4 4 4 2
2 0 0 0
4 2 0 0
2 0 0 0

Moving Left

ROUND 12
8 4 2 0
2 0 0 0
4 2 0 0
2 2 0 0

Moving Right

ROUND 13
0 8 4 2
0 0 0 2
0 0 4 2
0 2 0 4

Moving Left

ROUND 14
8 4 2 0
2 2 0 0
4 2 0 0
2 4 0 0

Moving Left

ROUND 15
8 4 2 2
4 0 0 0
4 2 0 0
2 4 0 0

Moving Left

ROUND 16
8 4 4 0
4 2 0 0
4 2 0 0
2 4 0 0

Moving Left

ROUND 17
8 8 0 0
4 2 0 0
4 2 0 0
2 4 2 0

Moving Right

ROUND 18
2 0 0 16
0 0 4 2
0 0 4 2
0 2 4 2

Moving Up

ROUND 19
2 2 8 16
0 0 4 4
0 0 0 2
0 0 0 2

Moving Left

ROUND 20
4 8 16 0
8 0 0 2
2 0 0 0
2 0 0 0

Moving Right

ROUND 21
0 4 8 16
0 0 8 2
0 0 0 2
2 0 0 2

Moving Left

ROUND 22
4 8 16 0
8 2 0 0
2 0 0 0
4 0 0 2

Moving Right

ROUND 23
0 4 8 16
0 0 8 2
0 0 0 2
0 2 4 2

Moving Left

ROUND 24
4 8 16 0
8 2 0 0
2 2 0 0
2 4 2 0

Moving Left

ROUND 25
4 8 16 0
8 2 0 0
4 0 0 0
2 4 2 2

Moving Right

ROUND 26
0 4 8 16
0 0 8 2
0 0 0 4
2 2 4 4

Moving Left

ROUND 27
4 8 16 0
8 2 2 0
4 0 0 0
4 8 0 0

Moving Right

ROUND 28
0 4 8 16
0 0 8 4
0 2 0 4
0 0 4 8

Moving Right

ROUND 29
0 4 8 16
0 2 8 4
0 0 2 4
0 0 4 8

Moving Left

ROUND 30
4 8 16 0
2 8 4 0
2 4 0 0
4 8 0 2

Moving Right

ROUND 31
0 4 8 16
0 2 8 4
2 0 2 4
0 4 8 2

Moving Left
4 8 16 0
2 8 4 0
4 4 0 0
4 8 2 0

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×