Hi guys I'm creating a simple snake console game and I have run into trouble while trying to build the wall for the right side and top and bottom
the left side seems to have worked great when I hit the wall it tells me invalid
but when I hit the right side wall my character overwrites the wall on the side,
I want an invalid to print if they hit the wall on the right side and also to and left
this is what I have so far,if you run it you can see why this isn't working
I changed the logic in the inBounds function a little but still getting the same problem but this time when I go out of bounds the character goes to the next line instead
here is the game now to be honest I'm not impressed with it but it does work in my opinion the code is pretty sloppy,
anyway here is the code now I need to implement a way to keep moving up and down and set the speed in whitch it moves up and down(not sure how this will be done)
working code is good, clean broken code is less good. Better is clean working code, of course, but one thing at a time.
consider...
for(int i = 0; i < 20; i++){ // make left side wall
for(int j = 0; j < 1;j++){
board[i][j] = '|';
}
}
if you don't plan on making the boards differently, this is too much..
for(int i = 0; i < 20; i++){ // make left and right side wall
{
board[i][0] = '|';
board[i][19] = '|';
}
and even more amusing, you can do top and bottom with one liners:
memset(&(board[0][0]), '_', 20); //top wall
memset(&(board[19][0]), '_', 20); //bottom wall
//if I got that right, memset should be used very, very carefully, some say not at all...
5 lines replaces almost 50, and it is more efficient to boot.
depending on what the game is supposed to do, sleep(some time) or a timer routine can govern input / rate of execution.
Line 2: Why are you including <stdlib.h> when you also include <cstdlib> ?
Line 8-10: Globals should be avoided.
Line 8 + many more places: You should define the size of your board using consts. If you ever want to change the size of your board, you have many places to change. Using consts properly, you would have only a single place to change.
and very true I've used the memset function before it's a c function I think that would be a neat idea to be honest would save quite a bit of code
I never knew that so stdlib.h includes cstdlib or vice versa?
and yes I hear global variables should be avoided at all costs I have not read into it yet but will do
also sleep function would be a very good idea I decided to check another persons snake c++ console game and the idea he came up with,he has a video on youtube anyway I followed his video and actually followed his idea a little bit(just for movement of the snake) is this good or bad practice?
sleep will be fine for a text based game. It probably is not the best for a graphical game with lots of complex code, but here, its ok. Its really meant to sleep worker threads, not the main thread, but I have a thing for simple solutions for simple problems :)