Answer the question
In order to leave comments, you need to log in
How to comprehend the solution of problems based on the communication of functions?
Greetings
, I sat for an hour and a half and thought about solving the following problem: The game starts with the number 0. During the move, you can add any number from 1 to 10 to the existing number. The one who receives the number 100 wins. It seems that everything is simple, I wrote several functions (selecting the first computer move, human move, check for the number of possible points to enter, etc.) and everything is ready, but there is such dissonance in thoughts that it comes to a stupor. I can’t figure out how to arrange the functions correctly and organize their communication correctly, either write my own function for every slightest action, or somehow reduce the number of functions by adding more features to each and the like.
There is not enough experience, understanding of the process itself, even writing in pseudocode cannot be comprehended in this case to the very end. In general, it is understandable, but the details slip away. I read a lot of literature, but it mainly provides simple examples based on the communication of a couple of simple functions.
What would you advise in this case for a better entry into the understanding of solving complex problems?
Answer the question
In order to leave comments, you need to log in
First, a full verbal description.
Then a block diagram according to the verbal description.
Then the pseudocode on the block diagram.
Then pseudo code.
You do all the steps on paper or whatever, until they begin to be performed in the imagination. That is, if you write pseudocode, then there should already be a clear block diagram in your head. If you draw a flowchart, then there should be a complete verbal description of the algorithm in your head. If you write code right away, then everything should be in your head.
And now your pseudo-code does not represent anything, therefore, the code for it turns out to be nothing.
Functions are a tool used to write maintainable code, their main task is to save the programmer from having to write the same thing several times.
If you apply this definition to your task, it becomes clear that you do not need functions at all, forget about them for now.
The process in general is as follows:
Approximately such an algorithm is described by Martin in "Perfect Code", if my memory serves me. Read it, good book. Ideally, it is worth adding more testing to this process, but again, this is if the head does not swell without it.
This allows at each stage to focus on what is important at the moment and not to fill your head with irrelevant things. There is an elephant in pieces, that's all.
If it's a game - even the simplest one - then try using a game loop.
The entire gameplay is one cycle. In some variable, the value of whose move is now stored.
There is a check in the loop:
If the player’s move, then we ask him for a value, check it, and update the game situation (in our case, the adder
) up to 10.
At the end of the game loop, we check if the adder = 100, then the player who moved now won, and if < 100, then we change the variable "whose move" and go to the next step of the loop!
It is already possible to extract some functions from this algorithm. In general, learn OOP right away. :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question