Flood-It with Python

in #technology6 years ago (edited)

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:
Screen Shot 2018-01-19 at 10.36.33 AM.png

The board is a 15x15 grid and each tile is randomly assigned a color at the start of the game, using the following code:
Screen Shot 2018-01-19 at 10.37.37 AM.png

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:
Screen Shot 2018-01-19 at 10.37.43 AM.png

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.

Screen Shot 2018-01-19 at 10.37.02 AM.png

Screen Shot 2018-01-19 at 10.37.20 AM.png

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

Sort:  
Loading...

OMG - That is insane! Brilliant stuff! Would love to play it. Welcome!!!

Thank you!!!
I'd love to show it to you some time
:)

Congratulations @devohier, you have decided to take the next big step with your first post! The Steem Network Team wishes you a great time among this awesome community.


Thumbs up for Steem Network´s strategy

The proven road to boost your personal success in this amazing Steem Network

Do you already know that awesome content will get great profits by following these simple steps, that have been worked out by experts?

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.031
BTC 68398.12
ETH 3837.26
USDT 1.00
SBD 3.65