One thing you can do to improve your code logic ability is to do Sudoku puzzles. Sudoku puzzles require you to consider many possibilities and think 1 step ahead.
Game logic is driven by 'if' statements and comparison operators (and, or, not, equals).
Another way to achieve better logic is to explain it in the simplest terms possible. The easiest code is often the most useful.
Imagine a level system in a game. You have experience points, levels, level requirements, and have to tell the game that if the points reach the amount of points you need to gain a level, reset the points and add one to the level, but you also need to preserve the points gained above that level requirement.
So if you are level 1 and you need 10 exp to reach level 2, then add 1 to your level and reset the required exp.
int level = 1, exp = 0;
void Main()
{
if (level == 1 && exp > 10)
{
level = 2;
exp = 0;
}
}
That's really all there is to thinking how code logic will affect your game. The above code will do what it it says, but it does need refining because we need the game to be able to know how much exp and levels the player could possibly have at any given time without having to be there to change it for them manually while they play the game.
My code for this system is so simple and clean I impressed myself when I figured it out. I thought it would be more complicated. Its not totally finished, but it achieves what we want for a progressive all-knowing level system:
//exp is active exp points, totalExp is your total exp, and
//tnl (meaning to next level) is the amount of exp you need to level up
// we set the tnl the first time and let the game do the rest
int exp, totalExp = 0, tnl = 10, level = 1, scaleFactor = 2;
if ([something happens]) { exp += 5; totalExp += exp; }
if (totalExp >= tnl) { level++; tnl += totalExp / scaleFactor; }
The first line checks if something has happened. If it has, you get 5 exp and that exp is then added to your total
The second line is checking if the total exp you have equals the required exp to level up. If it has, add 1 to your current level.
THe next part is something of my own design. There are MANY ways this could go at this point, but this way is just about as simple as it gets, and it works surprisingly well. Lets say I have 50 levels in my game. I would have to define each level's required exp individually if I wanted them to be static (always the same).
So level 1, tnl = 10, level 2, tnl = 20, level 3, tnl = 35, and so on. This creates a problem later on if you want to change the level up amount or you have to balance the exp you are getting with how much you need per level (a difficulty scale factor). This is why I made this simple equation.
The game takes how much exp you have and divides it by the scale factor. In this case, after you [did something] 2 times, your exp would equal your tnl and you would level up. Since your total exp is 10 at this point, the new tnl would be 10 + 10 / 2, or 15. Now you have to [do something] 3 times to level up, creating a slight increase in how much effort needs to be put into the game to continue leveling. When you hit level 3 the equation would read, tnl = 15 + 15 / 2. since 15 / 2 is 7.5, but the variable is an integer data type, it would be rounded to 8, making your new tnl 15 + 8 = 23. You now have to [do something] 5 times in order to reach level 4. This also works if you are playing through the game and collect more exp from harder enemies or challenges.
The point I'm making is that I had to think about this and go through all these mental steps and checklists to arrive at the results I wanted for my game, and that would work every time.
Hope this helped somehow!
-Sorv