r/cpp_questions • u/Xasozine • 3d ago
OPEN I understand c++ syntaxes, but I still can't code
I've watched multiple yt tutorials on coding in c++, and for the syntaxes I understand it; I understand things like variables, pointers, header files, syntaxes like cin and cout.
However, I still struggle to know how to code in general. I wanted to code a game on snake, yes the one many used to play on a nokia, but I find myself confused and unfamiliar with what to write.
I don't want to use AI, not even for helping me. So can someone explain to me how you dealt with the issue
2
Upvotes
19
u/chakani 3d ago
Start with something simple. Just sit down and write code. Compile, run, test. Try new stuff. Be patient and tenacious.
7
u/the_poope 3d ago
Practice, practice, practice. Not for hours, not for days, not for months, but for years!
There's no cheat code or shortcut. You just need to put in waaaaay more time than you think.
3
u/smozoma 3d ago
Start with text-based programs. A Snake game is jumping in too far.
Try starting with Tic-Tac-Toe. cout (or one of the newer
printcommands) to draw the board and alternate asking the X and O players to enter a row and a column.Also, I don't think youtube videos are the best way to learn programming... Try reading, like https://learncpp.com.
3
u/i_am_someone_or_am_i 3d ago
You shouldn't jump from watching tutorials on youtube to write snake. Write more simple projects first
2
u/imradzi 1d ago
programming is not about knowing the syntax, but knowing the problem and what's the step to make it work. As for the snake game, it's knowing how it work, what kind of data you have to maintain, how it is presented on screen and the handling user input and responses.
24
u/Draynrha 3d ago
Despite looking simple, snake is a good challenge for beginners because it requires different systems to make the game. I'd say before coding, you need to have an understanding of your goal. And then you need to break it into steps.
Before doing snake, a more simple game to tackle is Guess the Number. The goal is simple: the code generates a random-ish number between 1-1000 and your goal is to guess it using the least amount of attempts. Each time you guess, the code tells you if the number is greater or smaller than your guess until you reach the correct number.
Even this smaller game requires you to keep track of multiple elements like the number to guess, the number of attempts made and it makes use of some kind of logic to determine if the user inputs are valid guesses.
2
u/drbitboy 3d ago
Start with your data model.
Tl;DR
Unless you are writing device drivers, your computer programs modify bits in memory, and those bits in memory are a model of something in the real world, or in a virtual world in the case of a game. Programs do not talk directly to hardware, measure electrical signals, or anything like that.
So the first step of writing any computer program is designing the data model I.e. where the bits are that the program will modify.
If I remember snake, it comprises a 2D Cartesian grid of discrete locations, and every location has a state: open; a piece of the snake; the next kibble to be consumed. Also, each piece of the snake needs to know where the adjacent piece(s) of the snake are, so the new head location after the next move can be added to the previous head, and the tail location can be cleared an the tail updated.
What data C++ structures or classes can be used to model that grid and the pieces of the snake in the game's "world?"
Once you have your data model, writing the code to manipulate those data is easier; in some cases it all but writes itself.
Also, keep an agile mindset, and don't be reluctant to refractor the data model (and the code) when you need to model new states or behavior you did not think of originally.
7
u/FlailingDuck 3d ago
Coding is problem solving. Learning syntax is memorization. Entirely different things.
Often I think people struggle, because they bite off more than they can chew. If you don't know how to make snake, then you're probably biting too much.
Can you make, tic-tac-toe? How would you go about it. Think about how you play it on paper. If you had squared paper, how would you draw the tic-tac-toe board. On the lines, between the boxes (or cells).
How many cells do you need? 9.
In a particular arrangement? 3x3. What C++ things have I learnt about that can make a number of cells in a 2d grid 3 by 3.
What can be in each cell? empty, X or O. How can I denote that.
If I have that "data" of a 3x3 grid. How can I display it? On the console. Maybe I can just print it. Maybe I can clear the screen betwee each print.
How do I select the next cell? Can a cell be in 'selecting' mode. Can keyboard keys move me between cells.
How do I place my mark in that cell.
How do I check for a win condition.
....
Each question needs new code to solve it.
Snake its probably a few levels more complex than just tic-tac-toe. But starting with something smaller, and the questions and answers to snake start to become familiar.
6
u/tangerinelion 3d ago
That's because programming isn't a syntax quiz, it's literally about understanding what needs to happen to accomplish what you want. Knowing the syntax for using a member function pointer doesn't help you understand how to traverse a DAG.
6
u/neppo95 3d ago •
Unironically I just had a discussion elsewhere where people tried to convince me writing code was the main part of an engineers job, I should point them here ;)
Knowing syntax, how a language works, being able to read it and even being able to write it is just a part of creating something. The biggest part (depending on what kind of software you create) is in the work before that first line of code is created. Now we're talking snake here, not the apollo rocket, so it isn't that deep but the point stands. When you want to create something, even something relatively simple as snake, you have to ask yourself what you are making and how it works. What happens when you run your program? What do you see? What do you want it to do? If you lookup user stories, that's kind of what I'm aiming at here. Even something basic as snake has them, maybe they're in your head, on a notepad, or elsewhere, but they're somewhere. After you got that, try picking one of them and making just that one thing. To put this into an example for snake:
Now in this case going from basically zero, you'd probably have even simpler ones like "Draw snake". So how do you draw a snake? You need to know its position (x/y), so you need to store those somewhere. You need to know how big it is, so you need to store its size probably. Literally, building from the ground up thinking: What do I need to complete this one task.
Now you can do that as you go along in code, but like I said at the beginning, most work usually is before you write code. Take above example, you got its position, you got its size but hang on.... Why is the snake just a straight line? You stored its size, you stored its position, but you did not account for the fact that each point of the snake needs its own position stored and the size simply becomes the size of that group of elements. Were you to plan this out before you write code (which you probably wouldn't for a small project this size) then you would have probably caught that.
1
u/Toucan2000 3d ago
I know you said you don't want to use AI. If that's because you don't want it doing the work for you, I totally understand, but you can still use it to teach you. Check out this GitHub project Learn Harness
1
u/ButterscotchNo670 3d ago
maybe you should watch a yt video about coding a snake
1
u/dnult 3d ago
Start with a simple component of your game - perhaps a score keeper, or simple rules engine. Get it working and tested, then move on to the next thing.
It takes experience to be good, and experience comes from making mistakes. Starting simple makes it easy to recover and move on.
1
u/SmokeMuch7356 3d ago
You learn how to write code by writing code. Programming is a skill, and like any other skill requires non-trivial amounts of practice.
Start small. Write toy programs that do one thing and one thing only - write a hardcoded greeting, compute a hardcoded output, etc.
Once you get that down, start adding inputs, play with different types.
Then add simple decisions (
if/else,switch) and repetitions (for,while).You just have to write a lot of code before it really starts to click.
1
u/Dazzling_Music_2411 3d ago
What are you using for a GUI? Some graphical library like Qt or whatever, or are you using ncurses? O/S?
1
u/ButchDeanCA 3d ago
Firstly you need to understand this difference: memorizing is not understanding. You are memorizing simple syntax that is exposed as such when you try to make something.
As others have said make something simple, you certainly are not ready to make games like Snake (I did that over 20 years ago and it is a challenge).
One step at a time.
1
u/manni66 3d ago
That's the problem. Use learncpp.com or a book, and do the exercises.
1
u/serafean 3d ago
First I drew a block on the screen, then I made it move by itself in a single direction, then I added directional controls, then I added collision with borders, then I added generation of goodies, then eating (collision + snake grow), then collision with self.
Divide & conquer the problem, that's what programming is.
1
u/ContraryConman 3d ago
Think about learning a natural language. It is possible to know a language well enough to understand it when it is spoken, while not really being able to speak yourself. Reading and writing are similar, where some people can read a language but can't write it, and both skills have nothing to do with speaking.
Programming languages are actual languages, so a lot of the same learning rules apply. There are actually multiple skills combined here:
If you only have the middle skill and you haven't built the other two, you're not going to be able to program in C++. As you practice, it's not good enough to be able to understand, you need to push yourself to actually write more code. Is snake is too hard to write, pick something easier and go from there
1
u/StickyDeltaStrike 3d ago
Make a snake game in ascii first in a terminal maybe?
1
u/Thin_Tradition8467 2d ago
https://youtu.be/AaGK-fj-BAM?si=hUHgedDThD5FnQJm
While i was learning coding C# unity. I often watch videos like these in my spare time. It helped me a lot to understand what you are looking for.
If you ask me, good coder is the one who knows arrays well. Snake is best practice for beginners.
1
u/tcpukl 3d ago
Practice basics.
Like print out 10 numbers.
Then write the high low game guessing game.
Then it's algorithms.
0
u/ClockMoney4525 3d ago
?????
why dont you want to use AI
just ask it the questions you seek it will be way more efficient than on reddit
if you dont want it to give you code just say so and it wont