Public Group

# Console Game [C++]

This topic is 3045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been learning C++ for about 9 months and I want to start making a good console game. I'm thinking something along the lines of snake but I'm not so sure where to begin. I think I should have a board (2-d array?) with "@" as the walls. I should have a function for updating the board. I'll have "*" as the snake and "+" for the bits of food. Now for my problem. How do I handle user input given that it uses the arrow keys? I'm looking for all sorts of help here, I'll put up some code when I have it!!!

##### Share on other sites
I think the conio.h functions might help with the input. I found this thread which might be helpful.

##### Share on other sites
Having some trouble (already, Not Good!)
What I want to do is create a board, char board[20][20]; and fill it entirely.
If it's a border, I want it to be an '@' and everything else, a space.

I don't really want to hard-code it because it won't teach me anything and there should be a much easier way to do it. Is for loops for every single row the only way?

Also, How do I display a full array? Do I have to go through all that with the for loops for every row again?
Thanks!

Also, for readibility, should I use const chars for borders and spaces?

##### Share on other sites
How are you doing it at the moment?

##### Share on other sites
It's very easy to implement: use a paper and a pen to figure it out, you won't learn, if you don't try to solve the very first complicated problem you encounter by yourself.

All these indexing/grid/hex-grid stuff are always very hard (work) to implement. You have to go through it for yourself, or you will never be able to do it.

I started to program a hexagonal-grid minesweeper 2 days ago, displaying the grid alone took me 2 hours to figure out. Panning an zooming the grid an other 2 hours (and maybe 5 sheets of A4 paper). Not to mention looping through the "neighbor" fields: about 3 hours. Despite that I had done a lot of grid stuff before this. And even a regular square grid minesweeper.

##### Share on other sites
I'm not! I just got to populating the borders and I thought there has to be a better way to do this! I was using for loops as in:

for (int i=0; i<19; i++){   board[0]='@';  //populate x axis (border)}for (int i=0; i<19; i++)  //populate y axis (border){   board[0]='@' <<"\n";  //should I be using endl or is there any difference?}

It's not that I can't do it already, it's just that there should be a way to do it thats faster...

##### Share on other sites
Quote:
 I thought there has to be a better way to do this!
Better tends to be a meaningless term in programming. Define better. If I gave you an alternative solution, what metrics would you use to determine its quality relative to your current approach?

Quote:
 it's just that there should be a way to do it thats faster...
Faster in what way? (Define faster) And why do you believe what you have is noticeably slow?

##### Share on other sites
Don't get stuck in these things. If it works, it works, that's the most important. It's meaningless to start optimizing a code that simple (and that is called once in the whole game, since it's just initialization). Who cares if it's not elegant? It will be better next time. That problem has nothing to do with syntax, or the structure of the language or something, it's worthless (= you won't benefit from it) to spend much time with it.

##### Share on other sites
Ok, Have the board displaying correctly, just needed to think things through first.
I just realised how big this code is going to get, should I learn how to use header files and the like?
I have an idea for the snake's movement, to have a function getting the direction (how?) and then another function to see what will happen if the snake moves there, if it's legal, move the snake and if it's illegal stop the game and show the finished screen with the dead snake's last position. If you understand what I mean, is that how I should do it or is there a better way?
Thanks

##### Share on other sites
Quote:
 I just realised how big this code is going to get, should I learn how to use header files and the like?
You've been learning C++ for 9 months. You have to confront how C++ handles organization of code at some point.

Quote:
 If you understand what I mean, is that how I should do it or is there a better way?
Maybe there is a better way, maybe there isn't. But the point is that you learn. If we feed you information, you don't learn.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999589
×