Kollisionsabfrage gesucht. Meine alter funktioniert nicht =(



  • Hallo.
    Ich habe hier einen kleinen Ausschnitt aus meinen Programm.

    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    
    int locate(y,x)
    {
        HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD xy;
        xy.Y = y;
        xy.X = x;
        SetConsoleCursorPosition(stdOut,xy);
        return 0;
    }
    
    struct feindstruktur
    {
           int x;
           int y;
    
           struct feindstruktur *next;
    };
    struct feindstruktur *feindnull = NULL;
    struct feindstruktur *feind;
    
    void add(int y, int x)
    {
         struct feindstruktur *mann = (struct feindstruktur *) malloc(sizeof(struct feindstruktur));
    
         mann->x = x;             
         mann->y = y;
    
         mann->next = feindnull;  
         feindnull = mann;
    }
    
    int main()
    {
        int i;
    
        srand(time(0));
    
        for(i=0;i<90;i++)        
        {
            add(rand()%24,rand()%79); 
            // Hier werden 90 zufällig platzierte "." erzeugt.
        } 
    
        do
        {
            system("cls");
    
            feind = feindnull;
            while(feind)
            {
                        locate(feind->y, feind->x); printf(".");  
                        // Mit locate(y,x); wird der Cursor an eine
                        // zufällige Position des Bildschirms gesetzt.
                        // Anschließend wird der Punkt gemalt.
    
                        if(feind->x > 40) feind->x--;
                        if(feind->x < 40) feind->x++;
                        if(feind->y > 12) feind->y--;
                        if(feind->y < 12) feind->y++;
                        // HIer wird jeder Punkt einen Schritt näher 
                        // zur Mitte gesetzt.
    
                        feind = feind->next;
            }
    
            locate(12,40);printf("%c",1);
    
        }
        while(!kbhit());   
    }
    

    Alle 90 Punkte soll in die Mitte.
    Aber sie sollen alle kollidieren und nicht übereinander laufen....
    Wie kann ich das realisieren??

    Falls jemand mir Denkanstöße geben kann, oder ein kleines beispiel oben reinschreiben kann, wäre ich ihm sehr dankbar.

    Gruß michael



  • nicht getestet:

    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    
    int locate(int x, int y)
    {
        HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD xy;
        xy.Y = y;
        xy.X = x;
        SetConsoleCursorPosition(stdOut,xy);
        return 0;
    }
    
    struct feindstruktur
    {
           int x;
           int y;
    
           struct feindstruktur *next;
    };
    struct feindstruktur *feindnull = NULL;
    struct feindstruktur *feind;
    
    void add(int x, int y)
    {
         struct feindstruktur *mann = (struct feindstruktur *) malloc(sizeof(struct feindstruktur));
    
         mann->x = x;             
         mann->y = y;
    
         mann->next = feindnull;  
         feindnull = mann;
    }
    
    int main()
    {
        int i;
    
        srand(time(0));
    
        for(i=0;i<90;i++)        
        {
            add(rand()%79,rand()%24); 
            // Hier werden 90 zufällig platzierte "." erzeugt.
        } 
    
        do
        {
            system("cls");
    
            feind = feindnull;
            while(feind)
            {
                        locate(feind->x, feind->y); printf(".");  
                        // Mit locate(y,x); wird der Cursor an eine
                        // zufällige Position des Bildschirms gesetzt.
                        // Anschließend wird der Punkt gemalt.
    
                        int nx = feind->x;
                        int ny = feind->y;
                        if(nx > 40) nx--;
                        if(nx < 40) nx++;
                        if(ny > 12) ny--;
                        if(ny < 12) ny++;
    
                        bool hit = false;
                        for (struct feindstruktur *temp = feindnull; *temp; temp = temp->next;
                            if (temp->x == nx && temp->y == ny)
                            {
                                hit = true;
                                break;
                            }
    
                        if (!hit)
                        {
                            feind->x = nx;
                            feind->y = ny;
                        }
                        // HIer wird jeder Punkt einen Schritt näher 
                        // zur Mitte gesetzt.
    
                        feind = feind->next;
            }
    
            locate(40 ,12);
            printf("%c",1);
    
        }
        while(!kbhit());   
    
        while (feindnull)
        {
    	struct feindstruktur *temp = feindnull;
    	feindnull = feindnull->next;
    	free(temp);
        }
    }
    


  • mhh...sorry ging leier nicht so wie du das hattest.Ich hate den CODE mal geändert so das er funktioniert. aber leider passiert nichts.... Trotzdem danke für deiner Teilnahme an meinen Problem. Jetzt weis ich wenigstens wie ich den Speicher wieder freigeb 🙂


Anmelden zum Antworten