2d Matrix erstellen mit gotoXY



  • Hallo, ich versuche die ganaze zeit folgendes:

    OOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOO

    auszugeben.

    hierbei hab ich eine function erstellet die ein "Block"
    mithilfe der einer for schleife erstellt

    hier der code:

    ...
    
    int x_pos = 10
    int y_pos = 10
    
    void gotoxy(int x,int y)
    {
        printf("%c[%d;%df",0x1B,y,x);
    }
    
    ..
    
    void draw_test(int x_pos, int y_pos)
    {
     int i1,i2;
    
     for(i1=0 i1<x_pos i1++)
     {
       for(i2=0 i2<y_pos i2++)
       {
         printf(".");
         gotoxy(x_pos,y_pos);
       }
     }
    }
    
    ...
    

    als ausgabe kommt
    .

    -hier dier cursor

    .........

    was mache ich falsch ?



  • Sag mir, was die Schleife machen soll und was das gotoxy (und warum nach der ausgabe).



  • test_linux schrieb:

    hier der code:

    Zeig den echten Codetm 🙄



  • habs:

    for (i1=0; i1<x_input; i1++) 
     {
      for (i2=0; i2<y_input; i2++) 
      {
       printf(".");
      }
      printf("\n");
     }
    


  • so hier mein ganzer code:

    #include <stdio.h>
    #include <stdlib.h>
    
    #include<windows.h>
    
    int x_input;
    int y_input;
    int z_input;
    
    void input();
    void gotoxy(int x, int y);
    
    //Windows - use windows.h
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    //LINUX - use stdlib.h
    /*
    void gotoxy(int x,int y)
    {
     printf("%c[%d;%df",0x1B,y,x);
    }
    */
    
    void create_map()
    {
     int i1, i2;
    
     for (i1=0; i1<y_input; i1++)
     {
      for (i2=0; i2<x_input; i2++)
      {
       printf(".");
      }
      printf("\n");
     }
    }
    
    void draw_z()
    {
     int i3;
    
     for (i3=0; i3<z_input; i3++)
     {
      printf(".");
     }
    
    }
    
    void input()
    {
      printf ("Länge: ");
      scanf ("%d", &x_input);
    
      printf ("Breite: ");
      scanf ("%d", &y_input);
    
      printf ("Höhe: ");
      scanf ("%d", &z_input);
    
      system("cls");
      //system("clear");   - Linux
    
      create_map();
    
      printf("x pos: %d \n",x_input);
      printf("y pos: %d \n",y_input);
      printf("z pos: %d \n",z_input);
    
      gotoxy(35,7);
      draw_z();
      gotoxy(35,9);
      system("PAUSE");
    }
    
    int main(int argc, char *argv[])
    {
     input();
     return 0;
    }
    


  • *schauder*

    #include <stdio.h>
    #include <stdlib.h>
    
    #include <windows.h>
    
    void gotoxy( SHORT x, SHORT y )
    {
    	COORD pos;
    	pos.X = x;
    	pos.Y = y;
    	SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), pos );
    }
    
    void clrscr()
    {	
    	HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	DWORD length;
    	COORD origin = { 0 };
    	DWORD written;
    
    	GetConsoleScreenBufferInfo( std_output, &csbi );
    
    	length  = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    	length *= csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    
    	FillConsoleOutputAttribute( std_output, csbi.wAttributes, length, origin, &written );
    	FillConsoleOutputCharacterA( std_output, ' ', length, origin, &written );
    
    	gotoxy( 0, 0 );
    }
    
    void pause( void )
    {
    	char buffer;
    	DWORD read;
    	HANDLE std_input = GetStdHandle( STD_INPUT_HANDLE );
    	DWORD input_mode;
    	puts( "Press any key to continue . . ." );
    	GetConsoleMode( std_input, &input_mode );
    	SetConsoleMode( std_input, ENABLE_PROCESSED_INPUT );
    	FlushConsoleInputBuffer( std_input );	
    	ReadConsoleA( std_input, &buffer, 1, &read, NULL );
    	SetConsoleMode( std_input, input_mode );
    }
    
    typedef struct map_tag {
    	unsigned x, y, z;
    } map_t;
    
    void map_draw( map_t const * map )
    {
    	unsigned y, x, z;
    	for( y = 0; y < map->y; ++y, putchar( '\n' ) )
    		for( x = 0; x < map->x; ++x )
    			putchar( '.' );
    
    	printf("\nx: %u\ny: %u\nz: %u\n", map->x, map->y, map->z );
    
    	gotoxy( 35, 7 );
    	for( z = 0; z < map->z; ++z )
    		putchar( '.' );
    }
    
    map_t map_input( void )
    {
    	map_t map;
    	printf( "length: " );
    	scanf ( "%u", &map.x ); 
    	printf( "width:  " );
    	scanf ( "%u", &map.y ); 
    	printf( "height: " );
    	scanf ( "%u", &map.z);
    	return map;
    }
    
    int main( void )
    {
    	map_t map = map_input();
    	clrscr();
    	map_draw( &map );
    	gotoxy( 35, 9 );
    	pause();
    }
    

Log in to reply