0

I can't seem to make the nodes follow the previous node, the nodes only follow the head which is the start node, how can I make the node follow the previous node and make a snake-like movement, please help. For now, in the output screen, the snake doesn't bend when direction keys are pressed, all of the nodes align in the direction all at once.

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

int point_x = 0, point_y = 0;

struct snake
{
    int x, y;
    struct snake *next;
    struct snake *prev;
} *ptr, *temp, *start;

void create()
{
    ptr = (struct snake *)malloc(sizeof(struct snake));
    ptr->next = NULL;

    if (start == NULL)
    {
        start = ptr;
    }
    else
    {
        for (temp = start; temp->next != NULL; temp = temp->next)
            ;
        temp->next = ptr;
        ptr->prev = temp;
    }
}
void gotoxy(int x, int y)
{
    COORD CRD;
    CRD.X = x;
    CRD.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CRD);
}

void generatePoint()
{
    point_x = rand() % 120;
    point_y = rand() % 30;
}

int main()
{
    /*-------------------------HIDING CURSOR------------------------*/
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
    /*-------------------------HIDING CURSOR------------------------*/

    start = NULL;

    int points = 0;
    char ch1;
    create();
    start->x = 60;
    start->y = 15;
    gotoxy(start->x, start->y);
    printf("%c", (char)254);

    generatePoint();
    gotoxy(point_x, point_y);
    printf("\033[32m%c\033[0m", (char)254);

    while ((ch1 = _getch()) != 27)
    {
        if (ch1 == 'w')
        {
            while (_kbhit())
                _getch();

            while (start->y > 0)
            {

                system("cls");
                start->y = start->y - 1;
                gotoxy(start->x, start->y);
                printf("%c", (char)254);

                if (points > 0)
                {

                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }

                if (start->x == point_x && start->y == point_y)
                {
                    points++;
                    generatePoint();
                    create();
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }
                else
                {
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }
                if (points > 0)
                {

                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y) + 1;
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }

                gotoxy(110, 0);
                printf("\033[34mPoints: %d\033[0m", points);

                Sleep(90);

                if (_kbhit())
                {
                    break;
                }

                if (start->y == 0)
                {
                    system("cls");
                    gotoxy(55, 15);
                    printf("\033[31mGame over!\033[0m");
                    system("pause>nul");
                    exit(0);
                }
            }
        }
        else if (ch1 == 'a')
        {
            while (_kbhit())
                _getch();

            while (start->x > 0)
            {

                system("cls");
                start->x = start->x - 1;
                gotoxy(start->x, start->y);
                printf("%c", (char)254);
                if (points > 0)
                {

                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }
                if (start->x == point_x && start->y == point_y)
                {
                    points++;
                    generatePoint();
                    create();
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }
                else
                {
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }

                if (points > 0)
                {
                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x) + 1;
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }

                gotoxy(110, 0);
                printf("\033[34mPoints: %d\033[0m", points);

                Sleep(90);

                if (_kbhit())
                {
                    break;
                }

                if (start->y == 0)
                {
                    system("cls");
                    gotoxy(55, 15);
                    printf("\033[31mGame over!\033[0m");
                    system("pause>nul");
                    exit(0);
                }
            }
        }
        else if (ch1 == 's')
        {
            while (_kbhit())
                _getch();

            while (start->y < 30)
            {

                system("cls");
                start->y = start->y + 1;
                gotoxy(start->x, start->y);
                printf("%c", (char)254);
                if (points > 0)
                {

                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }
                if (start->x == point_x && start->y == point_y)
                {
                    points++;
                    generatePoint();
                    create();
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }
                else
                {
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }

                if (points > 0)
                {
                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {
                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y) - 1;
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }

                gotoxy(110, 0);
                printf("\033[34mPoints: %d\033[0m", points);

                Sleep(90);

                if (_kbhit())
                {
                    break;
                }

                if (start->y == 0)
                {
                    system("cls");
                    gotoxy(55, 15);
                    printf("\033[31mGame over!\033[0m");
                    system("pause>nul");
                    exit(0);
                }
            }
        }
        else if (ch1 == 'd')
        {
            while (_kbhit())
                _getch();

            while (start->x < 119)
            {
                system("cls");
                start->x = start->x + 1;
                gotoxy(start->x, start->y);
                printf("%c", (char)254);
                if (points > 0)
                {

                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x);
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }
                if (start->x == point_x && start->y == point_y)
                {
                    points++;
                    generatePoint();
                    create();
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }
                else
                {
                    gotoxy(point_x, point_y);
                    printf("\033[32m%c\033[0m", (char)254);
                }

                if (points > 0)
                {
                    for (temp = (start->next); temp != NULL; temp = temp->next)
                    {

                        temp->x = ((temp->prev)->x) - 1;
                        temp->y = ((temp->prev)->y);
                        gotoxy(temp->x, temp->y);
                        printf("%c", (char)254);
                    }
                }

                gotoxy(110, 0);
                printf("\033[34mPoints: %d\033[0m", points);

                Sleep(90);

                if (_kbhit())
                {
                    break;
                }

                if (start->y == 0)
                {
                    system("cls");
                    gotoxy(55, 15);
                    printf("\033[31mGame over!\033[0m");
                    system("pause>nul");
                    exit(0);
                }
            }
        }
        else
        {
            continue;
        }
    }
}`

I tried to make a snake game in c with linked list, but i cant seem to make it work

  • don't cast pointer from malloc: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. Furthermore, use local variables unless you _need_ a global (e.g. `*temp`, `*ptr`). – nicksheen Apr 08 '23 at 10:54
  • also yourtcode to navigate seems to be pretty redundand. You could put that behavior into a function and reduce the complexity/amount of code. Maybe the error will become obvious. – nicksheen Apr 08 '23 at 11:18
  • Debugger........ – Martin James Apr 08 '23 at 12:42

2 Answers2

0

At least this problem:

ptr->prev = NULL; missing

In head node case.

if (start == NULL)
{
    ptr->prev = NULL; // add
    start = ptr;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

I changed a bit of the code where I need to update the node with their previous node's coordinates

if (points > 0)
{
    for (temp = last; temp != start; temp = temp->prev)
    {

        temp->x = ((temp->prev)->x);
        temp->y = ((temp->prev)->y);
        gotoxy(temp->x, temp->y);
        printf("%c", (char)254);
    }
}
Alexandru Rusu
  • 569
  • 1
  • 5
  • 21