Browse array 1 dimension, intermediate code (3 addresses) C ++

1

Good evening, I am programming a soup of letters in c ++ with intermediate code or three addresses and then pass it to the GUI TASM assembly. What the program should do is print a one-dimensional array with the letters, and move around the array with the arrows and when you press enter on a letter, if it is one of the accepted ones, change it.

In intermediate code I can not use the coordinates (x, y) as in a two-dimensional matrix, in this case it is done by the formula f (x, y) = x + Zy; where "x" is the column, "y" is the row and "Z" is the width of the matrix.

My doubt arises, how to implement the functionality of going through the array and being an accepted letter change it color. My theory is to have three arrays, one to show the letters, another with the values of the colors and a last one with the positions that are accepted. Currently I change the colors based on an array that already has the values of colors changed.

If someone can help me, specifically to be able to go through the array and change the colors if it is accepted, very grateful

#include <windows.h>
#include <stdio.h>
#include <conio.h>

void gotoxy(int column, int line);
void drawStaticSprite(int sprite[][10]);
void drawStaticSprite3Dir();

int sleepTime = 100;
int x = 0;
int y = 0;

char sop3dir2[100]=  {'M','M','L','E','E','N','A','E','V','E',
                      'E','R','H','O','N','G','O','S','T','R',
                      'X','X','O','T','I','R','R','A','C','A',
                      'I','S','A','P','P','O','T','A','P','S',
                      'C','C','M','L','A','A','I','Z','O','T',
                      'O','A','A','U','A','N','U','L','P','U',
                      'S','O','M','B','R','E','R','O','M','P',
                      'C','N','E','A','R','R','I','I','O','O',
                      'W','O','J','E','N','O','C','P','Z','E',
                      'A','A','Z','A','A','L','N','Y','T','D'};

int Colores1[100] =  {15,14,14,14,14,14,14,14,14,14,
                      15,14,15,15,15,15,15,15,14,14,
                      15,14,14,14,14,14,14,14,14,14,
                      15,14,14,14,14,14,14,14,14,14,
                      15,14,14,14,14,14,14,14,14,14,
                      15,14,14,14,14,14,14,14,14,14,
                      15,15,15,15,15,15,15,15,14,14,
                      14,14,14,14,14,14,14,14,14,14,
                      14,14,14,14,14,14,14,14,14,14,
                      14,14,14,14,14,14,14,14,14,14};

int main(int argc, char** argv) {
int myChar;

drawStaticSprite3Dir();
do {
    myChar = getch();

    if (myChar == 224)
     {
        myChar = getch();
        }
     else
     {
            if (myChar == 72)//Fecla Arriba
        {
            y=y-1;
        }
        else if (myChar == 80)//Fecla Abajo
        {
            y=y+1;
        }
        else if (myChar == 75)//Flecha Izquierda
        {
            x=x-1;
        }
        else if (myChar == 77)//Flecha Derecha
        {
            x=x+1;
        }
        else if (myChar == 13) //Si es enter grabar posicion
        {
        //colores(y,x);

        }
}
} while (myChar != 27);
getch();
return 0;
}
void drawStaticSprite3Dir() {
int startX;
int startY;
int x;
int y;
int t1;
int t2;

startX = 0;
startY = 0;

x = 0;
l5:
if (x < 10) goto l0;
goto l1;

l0:
y = 0;
l4:
if (y < 10) goto l2;
goto l3;

l2:
t1 =  startY + y;
t2 =  startX + x;

gotoxy( t2,t1);

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Colores1[x + 
(10*y)]);

printf("%c", putchar(sop3dir2[x+(10*y)]));

y = y + 1;
goto l4;

l3:
x = x + 1;
goto l5;

l1:
return;
}

void gotoxy( int column, int line ) {
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
}
    
asked by raintrooper 21.11.2017 в 07:54
source

1 answer

1
  

My question arises, how to go through that array of a dimension, with the arrows on the keyboard, since I still do not understand very well how I should send the coordinates as I press the arrows

In a two-dimensional array you have two coordinates always , namely x e y .

What your exercise is about is to implement a bidimiensional array in a one-dimensional array. In this case, since you are going to treat it as a two-dimensional array, you will still have two dimensions: x e y . The complexity lies in how to relate both coordinates to a specific position of the array.

To do the latter, you are already giving the formula posicion = x + Z*y , where Z indicates the number of columns in the matrix. What is the raison d'être of this formula?

In a matrix the data is structured as follows:

1 2 3
4 5 6
7 8 9

And, to access the element (1,2) it is enough to make array[1][2] ... simple and easy to understand.

However, in a one-dimensional array the same data will be structured as follows:

| fila 0  | fila 1  | fila 2  |
| 1  2  3 | 4  5  6 | 7  8  9 |

Thus, to access the element with coordinates (1,2) you have to do the following transformation (remember that in C ++ the indexes start at 0):

pos = col + Z * fil = 2 + 3 * 1 = 5

| fila 0  | fila 1  | fila 2  |
| 1  2  3 | 4  5  6 | 7  8  9 |
                  ^ (1,2)

You can check then that always you're going to need two dimensions. The only thing that changes is the mechanism by which you access the positions of the array.

Now ... your program has some errors:

do {
  myChar = getch();

  if (myChar == 224) // <----
  {
    myChar = getch();
  }
  else
  {

Notice that pressing an arrow will effectively retrieve the code 224 and then the program will retrieve the code of the arrow pressed, but nothing is done with that code ... since the arrows are processed in else and, consequently, the position will never be updated.

You have to have something like this:

if (myChar == 224)
{
  myChar = getch();

  if (myChar == 72)//Fecla Arriba
  // ...

And also note that you would have to call gotoxy to update the position.

if (myChar == 224)
{
  myChar = getch();
  if (myChar == 72)//Fecla Arriba
  {
    y=y-1;
  }
  else if (myChar == 80)//Fecla Abajo
  {
    y=y+1;
  }
  else if (myChar == 75)//Flecha Izquierda
  {
    x=x-1;
  }
  else if (myChar == 77)//Flecha Derecha
  {
    x=x+1;
  }

  gotoxy(x,y);
}
else if (myChar == 13) //Si es enter grabar posicion
{
  //colores(y,x);
}

And, really ... is it necessary to use goto ? That in assembler happens that is necessary, but in C ++? In the XXI century? Sincerely ... do yourself a favor and delete all references to goto of your code. If you really like goto then assembler program ...

I do not know, I prefer to find this:

void drawStaticSprite3Dir() {
  for( int x=0; x<10; x++ )
  {
    for( int y=0; y<10; y++ )
    {
      gotoxy(x,y);
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                              Colores1[x+(10*y)]);
      putchar(sop3dir2[x+(10*y)]);
    }
  }
}

... I understand what he does to the first, to this other:

void drawStaticSprite3Dir() {
  int startX;
  int startY;
  int x;
  int y;
  int t1;
  int t2;

  startX = 0;
  startY = 0;

  x = 0;
l5:
  if (x < 10) goto l0;
  goto l1;

l0:
  y = 0;
l4:
  if (y < 10) goto l2;
  goto l3;

l2:
  t1 =  startY + y;
  t2 =  startX + x;

  gotoxy( t2,t1);

  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Colores1[x + 
      (10*y)]);

  printf("%c", putchar(sop3dir2[x+(10*y)]));

  y = y + 1;
  goto l4;

l3:
  x = x + 1;
  goto l5;

l1:
  return;
}

What about you?

    
answered by 21.11.2017 / 09:39
source