Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / how to make a 'flood it' game?

Author
Message
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 3rd Sep 2011 00:03
how to make a 'flood it' game?
this question keeps me busy for the past days.

for who does not know the game:
you begin with like 10x10 squares filled with random colors, and you must try to make it al the same colors, by pressing the color buttons below. if you still dont know the game google on 'flood it'

thinking of making an 2d array filled with zero's and go through it as i need to change it to another number or something...

i really dont know how to program the game.

can someone give me a push in the right direction?

much appreciated!
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 3rd Sep 2011 01:11 Edited at: 3rd Sep 2011 01:25
http://floodit.appspot.com/

web version of flood-it for reference. Basic idea is to flood the screen with one color by changing the color of the flood (starting in the upper most left hand color). Upon color change, the adjacent squares that are the same color as the flood now become part of the the flood. Try to flood the screen in fewest possible moves.

For simplicity sake I would agree on driving the logic of the game with a 2D array. I can see where a more advanced data structure could be used in conjunction wit a recursive algorithm for the flood fill. The visuals can be represented by simple 2d images/sprites or 3D plains/cubes off of the same logic system. What in particular are you having trouble with?

Your signature has been erased by a mod please reduce it to 600 x 120.
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 3rd Sep 2011 01:16
hi,the part with how to check for the connected same colors.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 3rd Sep 2011 01:30
There would be 1 of two techniques used.

A brute force solution or an elegant recursive solution. Since the brute force is easy to conceptualize, I will start with pseudocode for that.

A Recursive solution for flood fill is much simpler and cleaner, but harder (at least for me) to get right.. on first attempt.

So give me a few minutes and I'll eventually show some pseudo-code for both, and their respective dbpro code. I will honestly have to dig up a recursive algorithm solution,, and translate it to dbpro. It's the type of code that makes sense when you see it,, but coming up with the recursive solution itself is not always so straight foward in thinking.

Your signature has been erased by a mod please reduce it to 600 x 120.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 3rd Sep 2011 01:37 Edited at: 6th Sep 2011 05:05
I am replacing my long babble with this for now. It gets to the point I was going to make "on-the-fly", minus the logical errors I was making along the way.

http://en.wikipedia.org/wiki/Flood_fill

The Brute Force Solution I mentioned is equivalent to "Fixed memory method (right-hand fill method)" in the wikipedia article.

So while I am coding up a base mock-up. Your time would be better spent getting your head around this than reading my poorly worded pseudo-algorithm. This is in essence what you want. When looking at the recursive solution... you'll understand perhaps why I said that a more advanced data structure using queues and stacks with a 2d array or a dual linked list is in order. This type of problem is elegantly solved with a recursive call rather than a brute force technique. So soak some of this in.

Update
Here is the beginning of the mock-up. Hope you don't mind that I started from the beginning. This just creates a random 2D array for the game and displays it. Feel free to change it, or use wherever you were up to. I just needed it to demonstrate the Flood_Fill. I kept the code really basic. Next I'll define the block types, and create the nodes for the fill.



Your signature has been erased by a mod please reduce it to 600 x 120.
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 4th Sep 2011 00:50
hmm, its more difficult than i thought it would be with the algorithm and stuff. iam really curious to your next part.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Sep 2011 13:47
Floodfill sounds complicated, but it's actually fairly easy to implement as long as your search-space (the game grid) isn't too large, and performance isn't an issue (which for a small grid like this is not an issue)

Adding floodfill to z's code is actually fairly easy.

First though, there's a decision to be made on how to stop the floodfill going out of bounds, i.e. off any edge of the array. The choices are:
- put tests within the floodfill to stop x being less than 1 or greater than the grid width, with similar checks for y
- make the grid 1 size larger in each direction and filled with a value that can never be painted over.

With z's code already being 1 larger at the top and left (he's using the array as if it's 1-based), it's simpler to go with adding an extra row and column on the bottom and right - change the DIM to the following:

As the board is being filled from 1 to BoardSizeX and 1 to BoardSizeY, this will leave the x & y at 0 as zero, and the x & y at BoardSizeX+1 and BoardSizeY+1 at zero. Seeing as we fill with numbers from 1 to 6 and never 0, this gives us our unpaintable border.

Next, the floodfill:

As you can see, this is a direct implementation of the simplest algorithm on the wiki page - x and y together specify the node.

Next, we just want to fill with different colours, as selected by the player. I've gone with a simple key-based method that you'll need to replace with your preferred method. Put this code after the CLS command in your code:


That's it. Run the code, hit the number keys and watch it just work.

Complete code:


T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 4th Sep 2011 17:31
indeed its much simpeler than it looks. thnx for your example.

i have a related question (i think its related):

how does work a redstone simulator like in minecraft does?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Sep 2011 17:52
No idea - never heard of it until I googled it, and TBH, I'm not planning on any further research either

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 5th Sep 2011 23:09
Just letting you know I'm back to working on this today. I had to scrap the non-recursive solution for now. So i'm converting a a 4-way recursive algorithm to work with my base. Still encountering some issues, But I hope to hammer it out soon.

Your signature has been erased by a mod please reduce it to 600 x 120.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 6th Sep 2011 00:45 Edited at: 6th Sep 2011 01:10
EDIT: @IanM, Didn't see your response until now. Yeah. I was able to get the recursive flood to work. I wanted to show a non-recursive solution which should take me just implementing my own stack, but I must have been goofing up somewhere. Needless to say, the recursive algorithm is easier to understand as is.

@T4r4ntul4, I hope you don't mind that I took the game this far along. There are plenty of features you can add to this base to make the game more compelling.

***Select New COLORS with the MOUSE

Ok I'm almost done. I need to do...

- A bit more error checking (so far it seems to work).
- Increment Turns only when a New Color is chosen
- Check the array for all equal values to determine EndGame
- Ckean up and finish documenting the code
- Explain how the 4 way recursive Flood-Fill works

Here is the Game thus Far


Your signature has been erased by a mod please reduce it to 600 x 120.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 6th Sep 2011 02:27 Edited at: 6th Sep 2011 05:15
Here's the complete game



Here's a list of some things I think you could improve upon.

* Replace the 2D blocks with better quality graphics. ie(Nice glossy translucent blocks, glossy 3D translucent blocks.
* Better interface
* A way to reset/quit the current board
* An undo option
* Some sound
* A highscore board, with semi-random win/lose msgs depending on how many moves a completion took
* Some particle fx for the Flood_Fill
* Perhaps Design board/levels (rather than just completely random boards) and change the Max Turns based on the level
* Make the GameBoard Bigger, and add some scoring mechanism Powerups... eg. If you color so many new blocks on a single fill, you get a X's bonus or perhaps a powerup block that lets you change all of a certain color on the board to a chosen color. Let the player choose where to start the Flood Fill, rather than it being generated at the top left.
* make a shuffle the gameboard option

Well that's just some ideas

Your signature has been erased by a mod please reduce it to 600 x 120.
Bulsatar
13
Years of Service
User Offline
Joined: 19th Apr 2011
Location:
Posted: 6th Sep 2011 04:24 Edited at: 6th Sep 2011 04:34
I saw this and it sounded like fun so I made a version too
Have to admit, zenassem, I was struggling with the flood part as I could only get the stupid thing to cycle 3 or 4 times and then it wouldn't go anymore. I was thinking along the same lines that you had, I setup the pairing a little differently and went with if statements. Then I modified what you did and got it working great

With this one you can choose the number of colors and the grid size up to a certain level (otherwise it runs too slow refreshing all the time...). No limit on tries, but it does keep track of how many you do.


*** If anyone has any ideas on how to get a stupid time counter to "refresh" without overlapping itself and NOT redrawing the whole screen....I am all ears!!! Then the board could be gigantic with no slow down

Oh, and if anyone wants to know, this is a sample of what I tried differently from zenassem's recursive function. Previously, I was holding the oldvalue in the udt (currently changed) and set the first block directly and tried to get the rest to follow suit...didn't work...grrr...


******* doh, forgot a sync in the checkwin() function...
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 6th Sep 2011 04:42
@Bulsatar, I really like your version. Nice work! At first I wanted to show a non-recursive solution as well, but it proved more difficult than I anticipated. I had some bad logic in my original recursive fill conversion from a C implementation that kept me busy for a while. Sounds along the lines of what you ran into, as I too was changing the first block outside the function call. I'll look at your code and your question later/tomorrow.

Your signature has been erased by a mod please reduce it to 600 x 120.
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 7th Sep 2011 22:57
sorry for the late response.

thank you for making it clear how it works! i really appreciated it. now i know it i can make my own version of it. ( you will be on the credit list =) )
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th Sep 2011 14:43
nice challenge.
I played it a couple of times, you could make a Human Vs CPU version of this, rather than a set number of plays.

If I'm right, the logic to winning is to pick the colour that reveals the most currently inaccessible blocks. This would be easy to implement, and you could set levels by changing the depth to which the CPU looks.

Login to post a reply

Server time is: 2024-11-22 17:05:05
Your offset time is: 2024-11-22 17:05:05