Tag Archives: pygame

Debugging and Finalizing- Part 2

To finalize and remove any discrepancies in our code, it is best to get rid of any unnecessary code. For example, to test the solving function, I had a test board with fixed values. One important rule is to never hardcode or fix values. This is fine for testing; however, we want the user to have full control to ensure future usability.

We also want to shorten and optimize our code. From the beginning, I emphasized that functions were the best way to code. It is much faster to debug specific areas in our code. We can also simplify things by utilizing object-oriented programming. We can have a functions file and the main driver on another main file. This will make our code easier to test, read, and optimize. Since we have a better understanding of our code and the project, many functions can be simplified. It is good practice to take breaks and step away during this step. It is easy to later question why you made some functions more complicated than they should be.

Now that our code is clean and debugged, we can add a Graphical User Interface. We are using a library called PyGame, widely used in Python for small games and early game development. A library adds lots of predefined functions that we can utilize. First, we will draw our board using simple statements in PyGame. Since we have already determined what the user can do, we have a roadmap. We will use the same functions, but modify them for our board. We must create a sketch, draw, select, place, clear, and an update function. We can also create a nice GUI update to show our backtracking algorithm. PyGame has useful draw and event functions for keyboard presses and formatting. The two screenshots are of my previous code and the updated GUI code with PyGame.

The left screenshot is my outcome. I allowed the user to play and have the solving algorithm run if they choose. I created an executable and setup wizard as well so others can download and install the game using a simple plugin installed similar to PyGame called Cx_Freeze. With this, I can change the logo of the executable, create a setup wizard, and so much more. I hope you enjoyed coding this project with me and implemented new functionalities to your liking.

Written By: P. Friedman