While Loops, indentations
It is easier to modify a function rather than using the function directly and making changes in the code
Back after a wonderful vacation and I couldn’t wait to get started again. I actually missed my regular coding hour and wanted to get back on track again. I started with learning about python functions and how to define them and call them in our code. The functions are really useful to simplify the code, make it more readable and to debug. Additionally, they also allow us to skip writing the same code again and again. We can easily reuse our defined function in one line of code making the code easy to understand, small and requiring less manual work. Isn’t that why we want to use programming?
I came across this wonderful free resource called “Reeborg’s World”. This can be opened directly in the browser and is a free game in which we have to guide a robot “Reeborg” to its end position by programming the robot. There are a lot of different worlds in which we can take Reeborg and program our way out. The interface is really intuitive and the best thing that I found about this is that for each line of code we write, we can see how it affects the robot. Can’t make learning more interactive than this.
I started with the world “Hurdle 1” where Reeborg enters a hurdles race and we have to program it to reach the finish line. We can do this using built in functions/commands like move(), turn_left() and conditions like front_is_clear(), at_goal(), right_is_clear(), wall_in_front(), etc,. I also defined my own function called jump() to make the code smaller and so that I did not need to write the same code for jump for every jump in the hurdles race. Further, I used a for loop so the robot could keep moving ahead and jumping until it reached the finish line. It was fairly easy and I completed the programming in a few minutes. After this, I breezed through worlds “Hurdle 2” and “Hurdle 3”. In “Hurdle 2” the challenging part was that the finish line was not fixed and changed everytime. This was fixed by the while loop. In “Hurdle 3”, the challenge was the hurdles were not at a fixed length from each other and could keep changing each game. For this, I used while loop with at_goal() condition and inside the while loop, I used conditions wall_in_front() in which case Reeborg jumped and condition front_is_clear() in which case Reeborg moved forward. All these three worlds were fairly straightforward and were easily solvable.
In “Hurdle 4”, I struggled with defining my logic for the program and had to search online for solutions. “Hurdle 4” had hurdles of changeable heights and at irregular distance from each other and this changed for each game. In the beginning I tried using a if loop and for loop inside a while loop to define my logic, while using my original jump() function I had defined. This made the logic complicated and more prone to errors. After looking online, I realised it was easier to make changes to my jump() function and use the jump function directly inside a while loop with an if condition. After this realisation, I quickly made Reeborg cross the finish line. This is where I got my insight for the day.
For my project of the day, I completed the world “Maze” in which Reeborg is stuck in a dark maze with random placement of walls and we need to help Reeborg out of the maze. Here, I used if, elif and else conditions inside a while loop. The easiest way to get out of a maze is to keep following along the right edge of a maze until you get out. So that is exactly what I used in the if condition with the elif condition being to make Reeborg move straight if there’s a wall on the right and else condition being to make Reeborg turn left if there are walls on the right and ahead. This helped Reeborg get out of the maze. But when I refreshed the game with a new maze, I came across an edge case where in Reeborg moved in a square formation over and over in a forever loop because the right side of Reeborg was always empty, so it kept turning right, moving a block forward, turning right again and moving a block forward again and then turning right again and so on. You get the idea. To get out of such an edge case, I used a while loop to make Reeborg find a wall on its right first before using my if, elif and else conditions. This took care of the extreme cases and Reeborg was happy to be out of the maze.
I found Reeborg’s world to be an amazing way to get back into routine after my short vacation. You can also create your own worlds and challenge others to solve them. Hit me up if you do create something, I would love to give it a try. Overall, I will definitely recommend checking it out and having a little bit of fun, if the theory and lectures become too much.
In other news, I had the best vacation in Salzburg. We did a few museums, ate a lot of gelato, clicked tons of pictures and videos and did all the touristy things you can imagine. We also attended a solo piano concert by Grirgory Sokolov and it was just magical. If you are visiting Salzburg and need recommendations, you know who to message!