Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
PLEASE HELP!!!

This code is supposed to run like the basic snake game (except that I haven't coded the game-over conditions yet). But what is happening is that the body length doesn't increase more than 1 (i.e., the 1st time the snake eats food, its length increases, but it doesn't increase even a bit after that, no matter how much food the snake eats.
In the below code, the comment out part is the 1 I was using to find the mistake.

If you want some detail on a particular part of the code, tell me, I'll comment regarding that.

What I have tried:

<pre>#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int height = 21, width = 51, x, y, xf, yf, move, go, score = 0, body = 0;
char a;

void game(int *ptrx, int *ptry)
{
    int prevx, prevy, prev2x, prev2y;

    prevx = *(ptrx + 0);
    prevy = *(ptry + 0);
    *(ptrx + 0) = x;
    *(ptry + 0) = y;

    for (int i = 1; i <= body; i++)
    {
        prev2x = *(ptrx + i);
        prev2y = *(ptry + i);
        *(ptrx + i) = prevx;
        *(ptry + i) = prevy;
        prevx = prev2x;
        prevy = prev2y;

        // printf("\n\n\nPTRX [%d] = %d\n",i,*(ptry+i));
        // printf("PTRY [%d] = %d\n\n",i,*(ptry+i));
        // printf("PREVX = %d",prevx);
        // printf("\nPREVY = %d\n",prevy);
        // printf("\nPREV2X = %d",prev2x);
        // printf("\nPREV2Y = %d",prev2y);
    }

    if (move == 1)
    {
        y--;
    }
    else if (move == 2)
    {
        x--;
    }
    else if (move == 3)
    {
        y++;
    }
    else if (move == 4)
    {
        x++;
    }

    if (x == 51)
    {
        x = 1;
    }
    else if (x == 0)
    {
        x = 50;
    }
    if (y == 21)
    {
        y = 1;
    }
    else if (y == 0)
    {
        y = 20;
    }

    if (x == xf && y == yf)
    {
    label1:
        srand(time(NULL));
        xf = rand() % 51;
        if (xf == 0)
        {
            goto label1;
        }

    label2:
        srand(time(NULL));
        yf = rand() % 21;
        if (yf == 0)
        {
            goto label2;
        }

        score += 10;
        body++;
    }
}

void controls()
{
    if (kbhit())
    {
        a = getch();

        if (a == 'w')
        {
            move = 1;
        }
        else if (a == 'a')
        {
            move = 2;
        }
        else if (a == 's')
        {
            move = 3;
        }
        else if (a == 'd')
        {
            move = 4;
        }
    }
}

void setup()
{
    x = 35;
    y = 15;

    xf = 29;
    yf = 11;
}

void disp(int *ptrx, int *ptry)
{
    system("cls");
    int i, j, k, c;
    for (i = 0; i <= height; i++)
    {
        for (j = 0; j <= width; j++)
        {
            if ((i == 0 && j == 0) || (i == height && j == width))
            {
                printf("/");
            }
            else if ((i == 0 && j == width) || (i == height && j == 0))
            {
                printf("\\");
            }
            else if ((i == 0 && j <= width) || (i == height && j <= width))
            {
                printf("*");
            }
            else if ((j == 0 && i <= height) || (j == width && i <= height))
            {
                printf("|");
            }
            else if ((height > i > 0) && (width > j > 0))
            {
                if (i == y && j == x)
                {
                    printf("7");
                }
                else if (i == yf && j == xf)
                {
                    printf("o");
                }
                else
                {
                    c = 0;
                    for (k = 0; k <= body; k++)
                    {
                        if (i == *(ptry + k) && j == *(ptrx + k))
                        {
                            printf("0");
                            c = 1;
                        }
                    }
                    if (c == 0)
                    {
                        printf(" ");
                    }
                }
            }
        }
        printf("\n");
    }
}

int main()
{
    setup();
    while (go == 0)
    {
        int *ptrx;
        int *ptry;
        ptrx = (int *)calloc(body + 1, sizeof(int));
        ptry = (int *)calloc(body + 1, sizeof(int));
        controls();
        game(ptrx, ptry);
        // _sleep(750);
         disp(ptrx, ptry);
        // printf("\n\nBODY = %d", body);
        //     printf("\nY =        %d", y);
        //     printf("\nX =        %d", x);
        // for (int i = 0; i < body; i++)
        // {
        //     printf("\nPTRY [%d] = %d", i, ptry[i]);
        //     printf("\nPTRX [%d] = %d", i, ptrx[i]);
        // }

        free(ptrx);
        free(ptry);
        _sleep(750);
    }
    return 0;
}
Posted
Updated 11-Sep-22 12:32pm
v2
Comments
Greg Utas 12-Sep-22 8:20am    
I, and probably many others, don't even know what the "snake game" is. Second, this is a fair bit of code, and you're asking us to debug it for you. Once you're writing software of this size, you absolutely must learn to use a debugger. It's almost inevitable that code this size will have bugs, and by far the fastest way to find them is to step through the code with a debugger, line by line, to see where its behavior differs from what you expected.

1 solution

In my cursory look at your code, I see a few issues. The first is the use of so many global variables. I understand they are needed on occasion but at the least, given them descriptive names that are longer than two letters. When I use a global variable I also give it a capitalized first letter and I do this only for global variables so it is easy to distinguish them.

The second thing is your use of the function srand. The pseudo-random number generator should be seeded only one time so that call should be moved into the main function as one of the first things it does. The calls to rand are OK as they are.

I also wonder about how you use the integer arrays. You are re-allocating them and initializing them with values of zero every time through the loop. Are you sure they get set correctly? It appears to me that prevx and prevy are always set to the values of the first items but those are always zero. Is that really the behavior you want?

If I were you I would allocate those two arrays just once and their size would be the maximum they could ever be. Then a length value should be maintained and used which you already have - body. This way, the data is persistent and never cleared. Your logic will need to change to simulate movement but I think this is a better way to go with the code.

One more thing - the literal values 21 and 51 should appear only once. Since this is C, I would add those two as definitions and then use the definitions instead of those numbers. Here is an example :
C
#define HEIGHT 51
#define WIDTH  21
and then
C
xf = rand() % WIDTH;
yf = rand() % HEIGHT;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900