#Frustratingly strange error that I cannot figure out trying to make a snake game.

45 messages ยท Page 1 of 1 (latest)

foggy daggerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

valid hamlet
#
void initialise_map(enum land map[SIZE][SIZE]) {
    for (int row = 0; row < SIZE; ++row) {
        for (int col = 0; col < SIZE; ++col) {
            map[row][col] = NOT_VISITED;
        }
    }
}

void print_map(enum land map[SIZE][SIZE]) {
    for (int row = 0; row < SIZE; ++row) {
        for (int col = 0; col < SIZE; ++col) {
            if (map[row][col] == NOT_VISITED) {
                printf(". ");
            } else if (map[row][col] == VISITED) {
                printf("- ");
            } else if (map[row][col] == SNAKE) {
                printf("S ");
            } else if (map[row][col] == APPLE) {
                printf("A ");
            }
        }
        printf("\n");
    }
}```
#

if you " combine" that code into one file essentially then thats my program

#

max char limit is fucking annoying

#

anyways

#

hopefully what its inteded to do should be somewhat understandabkle

#

but im getting the strangest behavouir and for the life of me i cannot figure out why

#
Please enter apple location: 2 3
Please enter snake location: 4 5
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . S . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
r
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . S . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
d
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - S . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
d
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . S . 
. . . . . . . . 
. . . . . . . . 
l
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . . . . . S . 
. . . . . . . . 
l
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . . . . S - . 
. . . . . . . . 
l
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . . . S - - . 
. . . . . . . . 
l
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . . S - - - . 
. . . . . . . . 
d
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . S - - - - . 
. . . . . . . . 
u
. . . . . . . . 
. . . . . . . . 
. . . A . . . . 
. . . . . . . . 
. . . . . - - . 
. . . . . . - . 
. . - - - - - . 
. . S . . . . . ```
#

the snake only moves

#

upon inputting a SECOND character

#

regardless of what the character

#

but i have no fuycking idea why

#

this is sooooooooooooooooooo fucking strange lmao

ember thicket
#

Don't do scanf("%c ") but rather do scanf(" %c")

valid hamlet
#

whats the difference?

ember thicket
#

The first expects a character and a whitespace (I think), the second ignores any whitespace (including newlines which is where I think the error stems from).
I do have to admit that I don't really know the difference for sure but yeah, you shouldn't ever use scanf in the first place: https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

valid hamlet
#

ahhhhh i see

#

could i have used

#

fgets

ember thicket
#

Yes

valid hamlet
#

i wasnt really sure how to

#

for example like

#

lets say the grid was bigger like 12x12

#

and i inputted 12 12

ember thicket
#

Yeah?

valid hamlet
#

i would get smth like c {'1', '2', ' ', '1', '2', '\n', '\0'} right @dire sand

ember thicket
#

Yes

valid hamlet
#

then how would i like

#

get those numbers "added" together im not really sure how to word it

ember thicket
#

You can remove any possibly trailing newline from a string s by doing:

s[strcspn(s, "\n")] = 0;
valid hamlet
#

like the digits connected into an integer

ember thicket
valid hamlet
#

if i were to do it manually?

ember thicket
#

!man strtol

foggy daggerBOT
#

strtol, strtoll, strtoq - convert a string to a long integer

Synopsis
#include <stdlib.h>

long strtol(const char *restrict nptr,
            char **restrict endptr, int base);
long long strtoll(const char *restrict nptr,
            char **restrict endptr, int base);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

strtoll():
    _ISOC99_SOURCE
        || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

valid hamlet
#

as my uni course has not really

#

explained anything ๐Ÿ’€

#

like if i were to do it with a while loop or smth

#

the only string function we did was strcopy

ember thicket
valid hamlet
#

it does ๐Ÿ™‚

ember thicket
#

Yeah, then just don't worry about it

#

Never change a running system

valid hamlet
#

!solved

foggy daggerBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity