#You can check my snake in console in C++.

29 messages · Page 1 of 1 (latest)

worthy burrow
patent pumice
#

woah

#

that's pretty cool

#

may i suggest some code improvements?

patent pumice
#

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L30
illegal variable-length array. You can either make MAX_PLAYERS definitely known at compiletime with constexpr, or use std::vector<std::string> instead of a c-style array

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L85
not absolutely necessary but you could use C++'s <random> header

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L105
exit is not guaranteed to exist outside the std namespace

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L96
you could accept a const std::string& or std::string_view here to avoid copying the question every time

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L56
instead of .compare() you could use the != and == operators

https://github.com/tomasmark79/snake-in-console/blob/main/main.cpp#L134
you should probably avoid c-style casts, instead you can static_cast<int>(answer.length()), or make min and max of type std::size_t, or possibly allow it to cast implicitly

https://github.com/tomasmark79/snake-in-console/blob/main/src/Compiler.cpp#L27
kindof pedantic but __cplusplus represents the c++ version, which is different from the compiler version

https://github.com/tomasmark79/snake-in-console/blob/main/src/Compiler.cpp#L36
const on the return type is redundant unless you return a reference maybe?

https://github.com/tomasmark79/snake-in-console/blob/main/src/Field.cpp#L29
more redundant consts on return types

https://github.com/tomasmark79/snake-in-console/blob/main/src/Fruit.cpp#L50
i'm pretty sure this is a redundant cast to (int*), since fruitXArr is already of that type.
you could probably use a std::vector<int> here also

https://github.com/tomasmark79/snake-in-console/blob/main/src/Graphics.cpp#L170
?

https://github.com/tomasmark79/snake-in-console/blob/main/src/Process.cpp#L33-L60
could use std::unique_ptrs

worthy burrow
#

Thank you for your checks. I will try to fix all these.

patent pumice
#

if you have any questions feel free to ask

worthy burrow
patent pumice
#

;compile ```cpp
const int foo() {
return 3;
}

int main() {
int x = foo();
x = 5;
}

twin bridgeBOT
#
Compilation successful

No output.

worthy burrow
#

Ahaaaa

#

Thank you!

lone moss
#

Windows-only

worthy burrow
#

I am open for pool requests :-).

#

For adding support for Linux keys events.

lone moss
#

The console output is also windows-specific

worthy burrow
#

Hmm, then I have to try to check the code and try to get information about Windows vs Linux console compatibility.

lone moss
#

ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII escape character and a bracket character, are embedded into text. The terminal interprets these sequences as command...

worthy burrow
#

@patent pumice I have refactored several pointers to smart pointers. If you will have more recommendations how to write in modern C++ I will appreciate.

patent pumice
#

:O

#

will see

worthy burrow
#

not needly asap

worthy burrow
#

more refactors

#

I guess it is already fine with C++ rules. Only one class (graphic) I want to left with C type arrays so I don't forget how language C looks 😄

#

Still is here lot of possible optimalisations, which I will do later.