Greetings Steemians! :)
As a project for my very first CS class I made the classic game Flood-It using Python and a Draw module (that uses tkinter and allows for some basic drawings and animation).
The starting board looks like this:
The board is a 15x15 grid and each tile is randomly assigned a color at the start of the game, using the following code:
The color a tile is assigned is saved so that a 15x15 2D-list of every tile’s color can be created. This allows us to easily access and change the color of a tile later on during gameplay.
To allow the player to actually ‘flood-it’, I used the flood-fill algorithm, shown below:
After the player chooses a color from the color palette (the options are to the left of the flood-it board), this function is called like so: colorCheck(0, 0, colors_grid[0][0], color_chosen).
This tells the program to run the function starting with the top-left tile (tile[0][0]) and to check for the color the player chose.
If the tile is currently the ‘oldColor’, then the program will change it to the ‘newColor’ that the player chose and then proceed to check the tiles above, below, to the right, and to the left of that tile (if they exist). So after checking tile[0][0], the program will run the same function on tile[1][0] and tile[0][1]. If those tiles are the old color, they will be changed to the new color and then the function will check all of their surrounding tiles, and so on. The function does not do this if the tile in question is not the old color OR if it is already the new color. In that case, the program exits the function. The tile’s color is not changed and its surrounding tiles are not checked.
The game comes out to only 277 lines of code, most of which was very simple (even for a beginner like me). The recursive algorithm took the most time for me to get working properly by far, and it was incredibly frustrating at times, but that made it all the more worth it. I learned so much during the process and all-in-all it was incredibly rewarding, so thank you for letting me share my very first project with you! :D
OMG - That is insane! Brilliant stuff! Would love to play it. Welcome!!!
Thank you!!!
I'd love to show it to you some time
:)