Multidimentional Arrays

Hello,

My name is Adam and I have been a programing student for 4 months now. I have been tasked with writing code for a tetris clone and I need some direction. I need to be able to create and manipulate the shapes in a multidimensional array.I have already created the board/bucket for the shapes to fall in, but I'm having a hard time conceptualizing how write the code for the shapes. I'm not looking for answers on this and I do want to figure this out, but because of the time limit I thought I would post here to see if I could get some suggestions. I've reached my limit in what my text can teach me about this and while I do go to the bookstore to read up on C++, it is an hour away and I can't get there on a daily basis. Any direction/help given will be appreciated.

Thanks! =)

Edit:

Current goal:
Give the box a starting spot in the bucket.

Here is my current code for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

const char BOARDER1 = '|';
const char BOARDER2 = 'O';
const char BLOCK = 'X';
const char EMPTY = ' ';

const int ROWS = 40;
const int COLUMNS = 23;
char bucket[ROWS][COLUMNS];

void display_board(char bucket[][23]);

int main()
{
    const int s_rows = 4;
    const int s_columns = 4;
    char box[s_rows][s_columns] = { {'X','X',' ',' '},
                                    {'X','X',' ',' '},
                                    {' ',' ',' ',' '} };
    
    display_board(bucket);
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

void display_board(char bucket[][23])
{
   for (int i = 0; i < 24; ++i)
       bucket[39][i] = BOARDER2;
   
   for (int i = 0; i < 38; ++i)
       bucket[i][0] = BOARDER1;
       
   for (int i = 0; i < 38; ++i)
       bucket[i][22] = BOARDER1;
       
   for (int i = 0; i < ROWS; ++i)
   {
       for (int j = 0; j < COLUMNS; ++j)
       cout << bucket[i][j];
       cout << endl;
   }
};
Last edited on
Each shape has a specific space. For example, here is a piece rotated in all four dimensions:
┌─┬─┬─┐   ┌─┬─┬─┐   ┌─┬─┬─┐   ┌─┬─┬─┐   
│ │X│ │   │ │X│ │   │ │X│ │   │ │ │ │   
├─┼─┼─┤   ├─┼─┼─┤   ├─┼─┼─┤   ├─┼─┼─┤   
│ │X│X│   │X│X│X│   │X│X│ │   │X│X│X│   
├─┼─┼─┤   ├─┼─┼─┤   ├─┼─┼─┤   ├─┼─┼─┤   
│ │X│ │   │ │ │ │   │ │X│ │   │ │X│ │   
└─┴─┴─┘   └─┴─┴─┘   └─┴─┴─┘   └─┴─┴─┘   

For it, you just need an array of 3 by 3 with elements marking BLOCK or SPACE.

Then, when you wish to place the piece on the board, all you need to do is check that the BLOCK does not intersect with any edges or other BLOCK pieces on the board.

(For an extra hint, there may be multiple BLOCK elements for different colors, but you only need one SPACE element value.)

Hope this helps.
Yes it does help and thanks for the post. I'm sure I'll be posting more questions soon, but what you gave me is definitely good food for thought!
I've had only a little time to tinker with my code this past weekend, but this is something I wrote to draw the box shpae:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const char BLOCK = 'X';
const char EMPTY = ' ';

struct shape
{
       char form[4][4];
}box;

void create_box(shape box)
{
    box.form[2][0] = BLOCK;
    box.form[3][0] = BLOCK;
    box.form[2][1] = BLOCK;
    box.form[3][1] = BLOCK;
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
        if (box.form[i][j] != BLOCK)
            box.form[i][j] = EMPTY;
    }
};


I have come to realize that writing a tetris clone is still a little overwhelming for me, but I'm taking on the challenge. (I have only recently been introduced to Data Structures and Classes.) Right now I am trying to keep things simple, build upon what I can create and hopefully have something to turn in. This is why I have decided to only be concerned about getting working code for the box shape for right now. Now to my questions. =)

1. Would it make more sense to use a class for my shapes instead of a multiple structures as I create more shapes?

2. The code that I wrote inside of void create_box(shape box) to draw the box, is that good technique? I want to be critiqued on my code writing because I do not have any experienced code to compare mine to that I can understand (even most levels of basic code is still a little out of my reach at times.)

Thanks.
Don't forget colors!

┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│ │O│ │ │   │ │O│ │ │   │ │ │ │ │   │ │O│ │ │ 'O' = Orange
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│O│O│O│ │   │ │O│O│ │   │O│O│O│ │   │O│O│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │O│ │ │   │ │O│ │ │   │ │O│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│C│C│ │ │   │ │C│ │ │   │C│C│ │ │   │ │C│ │ │ 'C' = cyan
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │C│C│ │   │C│C│ │ │   │ │C│C│ │   │C│C│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │C│ │ │ │   │ │ │ │ │   │C│ │ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│ │M│M│ │   │M│ │ │ │   │ │M│M│ │   │M│ │ │ │ 'M' = Magenta
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│M│M│ │ │   │M│M│ │ │   │M│M│ │ │   │M│M│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │M│ │ │   │ │ │ │ │   │ │M│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│R│R│ │ │   │R│R│ │ │   │R│R│ │ │   │R│R│ │ │ 'R' = Red
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│R│R│ │ │   │R│R│ │ │   │R│R│ │ │   │R│R│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│Y│ │ │ │   │ │Y│Y│ │   │ │ │ │ │   │ │Y│ │ │ 'Y' = Yellow
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│Y│Y│Y│ │   │ │Y│ │ │   │Y│Y│Y│ │   │ │Y│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │Y│ │ │   │ │ │Y│ │   │Y│Y│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│ │ │G│ │   │ │G│ │ │   │ │ │ │ │   │G│G│ │ │ 'G' = Green
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│G│G│G│ │   │ │G│ │ │   │G│G│G│ │   │ │G│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │G│G│ │   │G│ │ │ │   │ │G│ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │ │ │ │   │ │ │ │ │   │ │ │ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘
┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐   ┌─┬─┬─┬─┐
│ │B│ │ │   │ │ │ │ │   │ │B│ │ │   │ │ │ │ │ 'B' = Blue
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │B│ │ │   │B│B│B│B│   │ │B│ │ │   │B│B│B│B│
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │B│ │ │   │ │ │ │ │   │ │B│ │ │   │ │ │ │ │
├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤   ├─┼─┼─┼─┤
│ │B│ │ │   │ │ │ │ │   │ │B│ │ │   │ │ │ │ │
└─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘   └─┴─┴─┴─┘

Having a 4x4 array is a good idea. Notice how I defined all four orientations for each shape? This helps you when rotating to avoid problems. I have defined them so that the L and T shapes have a "center" about which they rotate, but the others stick to the UL corner -- this is the way many tetris clones work... You may wish to have a more advanced rotation?

Note that this is just a suggestion. You do not need to define your shapes like this!
The point is that the shapes are static. You do not need a function to create them. Just hard-code the shapes into your program, with an array of arrays. For example, something like the following would work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef char shape_t[4][5];

typedef shape_t shape_list_t[4];

shape_list_t shapes[] =
  {
    // T shape
    { { " O  ",
        "OOO ",
        "    ",
        "    " },
      { " O  ",
        " OO ",
        " O  ",
        "    " },
      { "    ",
        "OOO ",
        " O  ",
        "    " },
      { " O  ",
        "OO  ",
        " O  ",
        "    " } },
    // Z shape
    { { "CC  ",
        " CC ",
        "    ",
        "    " },
    ...
  };

All you need now for each shape currently in play is two pieces of information:
- the index of the shape (0..6)
- the rotation of the shape (0..3)

Remember that when moving and rotating the shape, it must not intersect either the walls of the pit or existing data in the pit.

I hope this is helpful.
This looks like a fun project!
@Duoas
I'll digest that and try to get that in over the next couple of days.

@moorecm
Minus my newness to C++, it is fun. :)
Topic archived. No new replies allowed.