Re: [Flashcoders] Advice on creating random grid of pairs for a game
Jobe Makar's book about Flash Games Demystified has a wordsearch that shuffles words in a grid - it will keep shuffling upto a point, then decide that it is getting nowhere and try again. There may be some examples of this sort of thing online - I have the book at home if not :) Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Advice on creating random grid of pairs for a game
I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Advice on creating random grid of pairs for a game
Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
oops... I just woke up when I wrote it, and I didn't read the full post. I thought you were making a memory game for some reason. Paul Steven wrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
Paul, Maybe the problem is actually that difficult - there may be no easy solutions to the calculation, and it might indeed take hours to find a valid solution. But anyway - here's a thought, in case it helps. Have you considered, for each pair: - putting the pair on adjacent empty squares. - moving one (or both!) away from the other until it hits an obstacle or a 'used' point (see next line). Therefore there's a clear path between them. - marking the 'clear' path between them as 'used' Rinse and repeat. Don't know if that's helpful, but you never know. That should eliminate the 'pick all possible squares and pathfind' part of it, I think. Ian On Wed, Mar 11, 2009 at 4:59 PM, Paul Steven paul_ste...@btinternet.com wrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list
Re: [Flashcoders] Advice on creating random grid of pairs for a game
My first thought for your algorithm is to essentially follow the solution path that your user might end up taking. Your user will be clearing areas out, opening them up. Your algorithm could start with an empty grid and fill in around a starting seed, growing the tile array outward. 1) empty grid 2) generate random sequence of pairs with the blank embedded in it, such as 4,4,27,27,1,1,36,blank,36, etc 3) choose random location for first tile, and place it there, and take it off the pair queue. 4) choose an unfilled location which is 4-connected to any filled location, and put the next tile from the queue in it. 5) repeat 4) until the queue is empty. In this system, putting the blank in a location counts as filling that location, so the value for blank needs to be different than the initial values in the grid. So for example, if -1 means uninitialized and 0 means blank: -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,01 then 36,-1,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 36,27,01 This guarantees that a solution exists, because the algorithm essentially traces out the solution as it generates the level. This presumes that players are not allowed to move tiles around yet leave them on the board while solving the puzzle - it assumes that tiles just disappear from the grid when the user selects a pair which has a clear path between it, and that's the only interaction allowed. If they can slide them around, my algorithm will still produce solve-able puzzles, but they won't be as difficult as they could be, I think. :) Interesting problem! Dave On Wed, Mar 11, 2009 at 6:09 AM, Glen Pike g...@engineeredarts.co.ukwrote: Jobe Makar's book about Flash Games Demystified has a wordsearch that shuffles words in a grid - it will keep shuffling upto a point, then decide that it is getting nowhere and try again. There may be some examples of this sort of thing online - I have the book at home if not :) Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
Hi, Paul, Can you explain what you mean by a clear path. For example, when I look at this example below: 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 I don't see the clear path between 1 - 1. Seems like it jumps from one square to another. So is a clear path just a straight or diagonal line between the next number in the sequence that is unobstructed by a non-used pair, or what? -jonathan On Wed, Mar 11, 2009 at 12:59 PM, Paul Steven paul_ste...@btinternet.comwrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___
Re: [Flashcoders] Advice on creating random grid of pairs for a game
A simpler way to ask would be if 1,2,2,1 is also a clear path (provided 2 has been removed). On Wed, Mar 11, 2009 at 1:43 PM, jonathan howe jonathangh...@gmail.comwrote: Hi, Paul, Can you explain what you mean by a clear path. For example, when I look at this example below: 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 I don't see the clear path between 1 - 1. Seems like it jumps from one square to another. So is a clear path just a straight or diagonal line between the next number in the sequence that is unobstructed by a non-used pair, or what? -jonathan On Wed, Mar 11, 2009 at 12:59 PM, Paul Steven paul_ste...@btinternet.comwrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul
Re: [Flashcoders] Advice on creating random grid of pairs for a game
Oh man... I just got to thinking along those lines and you beat me to it; however, I was thinking that, in order to avoid them being placed side by side, you could place a restriction on minimum distance and force it to be 2 or more. Then I was thinking your could cross hatch; however, I think yours might be a better puzzle. David Hershberger wrote: My first thought for your algorithm is to essentially follow the solution path that your user might end up taking. Your user will be clearing areas out, opening them up. Your algorithm could start with an empty grid and fill in around a starting seed, growing the tile array outward. 1) empty grid 2) generate random sequence of pairs with the blank embedded in it, such as 4,4,27,27,1,1,36,blank,36, etc 3) choose random location for first tile, and place it there, and take it off the pair queue. 4) choose an unfilled location which is 4-connected to any filled location, and put the next tile from the queue in it. 5) repeat 4) until the queue is empty. In this system, putting the blank in a location counts as filling that location, so the value for blank needs to be different than the initial values in the grid. So for example, if -1 means uninitialized and 0 means blank: -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,01 then 36,-1,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 36,27,01 This guarantees that a solution exists, because the algorithm essentially traces out the solution as it generates the level. This presumes that players are not allowed to move tiles around yet leave them on the board while solving the puzzle - it assumes that tiles just disappear from the grid when the user selects a pair which has a clear path between it, and that's the only interaction allowed. If they can slide them around, my algorithm will still produce solve-able puzzles, but they won't be as difficult as they could be, I think. :) Interesting problem! Dave On Wed, Mar 11, 2009 at 6:09 AM, Glen Pike g...@engineeredarts.co.ukwrote: Jobe Makar's book about Flash Games Demystified has a wordsearch that shuffles words in a grid - it will keep shuffling upto a point, then decide that it is getting nowhere and try again. There may be some examples of this sort of thing online - I have the book at home if not :) Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
i think he means that you have to get rid of 2 - 2 before you can get rid of 1 - 1 jonathan howe wrote: Hi, Paul, Can you explain what you mean by a clear path. For example, when I look at this example below: 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 I don't see the clear path between 1 - 1. Seems like it jumps from one square to another. So is a clear path just a straight or diagonal line between the next number in the sequence that is unobstructed by a non-used pair, or what? -jonathan On Wed, Mar 11, 2009 at 12:59 PM, Paul Steven paul_ste...@btinternet.comwrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created then finds it cannot any more clear paths to create the remainder of the grid. I would really appreciate any help cracking this function. Thanks Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com
Re: [Flashcoders] Advice on creating random grid of pairs for a game
Jonathan, When you match a pair, you pull them off the grid. Therefore after removing the pair of 2s, there are only empty squares in the path between the 1s. Dave On Wed, Mar 11, 2009 at 10:43 AM, jonathan howe jonathangh...@gmail.comwrote: Hi, Paul, Can you explain what you mean by a clear path. For example, when I look at this example below: 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 I don't see the clear path between 1 - 1. Seems like it jumps from one square to another. So is a clear path just a straight or diagonal line between the next number in the sequence that is unobstructed by a non-used pair, or what? -jonathan On Wed, Mar 11, 2009 at 12:59 PM, Paul Steven paul_ste...@btinternet.com wrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 Note that until they have matched the pair of 2's they cannot match the 1's as there would be no clear path between them. All my attempts so far to create this grid is resulting in script errors due to my code being unable to find a solution. My code basically gets about 60% of the board created
RE: [Flashcoders] Advice on creating random grid of pairs for a game
Thanks David for your suggested solution - it looks very promising. I am not sure what 4-connected means? But I think I understand the general idea I am just wondering if your solution would guarantee that there will always be a matching tile within a clear path? For example if the random sequence is 4,4,27,27,1,1,36,36, BLANK -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,27,04 then -1,-1,27 -1,01,04 -1,27,04 then -1,-1,27 -1,01,04 01,27,04 then -1,36,27 -1,01,04 01,27,04 then -1,36,27 36,01,04 01,27,04 then 00,36,27 36,01,04 01,27,04 -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hershberger Sent: 11 March 2009 17:40 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game My first thought for your algorithm is to essentially follow the solution path that your user might end up taking. Your user will be clearing areas out, opening them up. Your algorithm could start with an empty grid and fill in around a starting seed, growing the tile array outward. 1) empty grid 2) generate random sequence of pairs with the blank embedded in it, such as 4,4,27,27,1,1,36,blank,36, etc 3) choose random location for first tile, and place it there, and take it off the pair queue. 4) choose an unfilled location which is 4-connected to any filled location, and put the next tile from the queue in it. 5) repeat 4) until the queue is empty. In this system, putting the blank in a location counts as filling that location, so the value for blank needs to be different than the initial values in the grid. So for example, if -1 means uninitialized and 0 means blank: -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,01 then 36,-1,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 36,27,01 This guarantees that a solution exists, because the algorithm essentially traces out the solution as it generates the level. This presumes that players are not allowed to move tiles around yet leave them on the board while solving the puzzle - it assumes that tiles just disappear from the grid when the user selects a pair which has a clear path between it, and that's the only interaction allowed. If they can slide them around, my algorithm will still produce solve-able puzzles, but they won't be as difficult as they could be, I think. :) Interesting problem! /mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Advice on creating random grid of pairs for a game
I just wanted to mention that I don't think it should be entirely random. Perhaps you may want to add restrictions as I mentioned before, yet, make sure it has a maximum that steps higher and higher based on availability, and alternate between diagonal and side to side. So if there is no way 2 can be side by side or diagonal anymore, than max distance plus 1, check for a max distance of 1 square/slot between from available slots in the array, and so on. This would allow it to build out and use all available squares. Paul Steven wrote: Thanks David for your suggested solution - it looks very promising. I am not sure what 4-connected means? But I think I understand the general idea I am just wondering if your solution would guarantee that there will always be a matching tile within a clear path? For example if the random sequence is 4,4,27,27,1,1,36,36, BLANK -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,27,04 then -1,-1,27 -1,01,04 -1,27,04 then -1,-1,27 -1,01,04 01,27,04 then -1,36,27 -1,01,04 01,27,04 then -1,36,27 36,01,04 01,27,04 then 00,36,27 36,01,04 01,27,04 -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hershberger Sent: 11 March 2009 17:40 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game My first thought for your algorithm is to essentially follow the solution path that your user might end up taking. Your user will be clearing areas out, opening them up. Your algorithm could start with an empty grid and fill in around a starting seed, growing the tile array outward. 1) empty grid 2) generate random sequence of pairs with the blank embedded in it, such as 4,4,27,27,1,1,36,blank,36, etc 3) choose random location for first tile, and place it there, and take it off the pair queue. 4) choose an unfilled location which is 4-connected to any filled location, and put the next tile from the queue in it. 5) repeat 4) until the queue is empty. In this system, putting the blank in a location counts as filling that location, so the value for blank needs to be different than the initial values in the grid. So for example, if -1 means uninitialized and 0 means blank: -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,01 then 36,-1,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 36,27,01 This guarantees that a solution exists, because the algorithm essentially traces out the solution as it generates the level. This presumes that players are not allowed to move tiles around yet leave them on the board while solving the puzzle - it assumes that tiles just disappear from the grid when the user selects a pair which has a clear path between it, and that's the only interaction allowed. If they can slide them around, my algorithm will still produce solve-able puzzles, but they won't be as difficult as they could be, I think. :) Interesting problem! /mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Advice on creating random grid of pairs for a game
David, your algorithm works a treat - many thanks!! I just need to work out how to make it get more difficult for each of the 20 levels in the game. -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hershberger Sent: 11 March 2009 18:50 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Jonathan, When you match a pair, you pull them off the grid. Therefore after removing the pair of 2s, there are only empty squares in the path between the 1s. Dave On Wed, Mar 11, 2009 at 10:43 AM, jonathan howe jonathangh...@gmail.comwrote: Hi, Paul, Can you explain what you mean by a clear path. For example, when I look at this example below: 1,2,1 2,0,3 4,4,3 In this simple example, a user could clear all tiles by matching in the following order: 4 - 4 3 - 3 2 - 2 1 - 1 I don't see the clear path between 1 - 1. Seems like it jumps from one square to another. So is a clear path just a straight or diagonal line between the next number in the sequence that is unobstructed by a non-used pair, or what? -jonathan On Wed, Mar 11, 2009 at 12:59 PM, Paul Steven paul_ste...@btinternet.com wrote: Thanks Anthony Perhaps I am overcomplicating things but I am under the impression I need to ensure there is a clear path between each pair as I add them. My current algorithm is as follows: Step 1. Choose a random tile position (Random Row, Random Column) Step 2. Create an array of all possible tiles that this random tile can be paired with. To create this array, I use a path finding function to check there is a clear path between the 2 tiles Step 3. Choose one of the possible tiles from the array (currently I am just choosing a random one) and update grid data to reflect the 2 paired tiles are not on the grid. Repeat from Step 1 again This works fine until around 26 pairs have been placed then it gets stuck as it cannot find any more tiles that have a clear path. I have even added code to ensure that no isolated empty tiles are created as a result of creating a pairing. I have tried putting several timed break points in my code to restart the entire process if it fails to complete the entire grid. However it just doesn't seem to ever be able to generate an entire grid. Perhaps this process is really time consuming and I need to leave it for hours to generate a grid but I wouldn't have thought so. Any advice much appreciated! Thanks Paul -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Anthony Pace Sent: 11 March 2009 16:31 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game Sounds like one of my old high-school projects, I see several ways of doing what you want that make sense: * -random population always of an array 81 units long (80 if you want the last to always be blank) o --you could create a function that loops 41(or 40 if last always blank)times tracking the positions in a temp array and then returning the completed array (if 41st loop choosing unfilled position with a 0 for blank card/wild card) o --there are other ways of doing this; yet, they use more loops and if statements. * -random population of an array 81 units long and then shuffling the deck sort of o --shuffling by swapping only positions that have not been swapped (you could even isolate left and right sides to simulate a card deck getting shuffled by hand) o --shuffling by swapping from 1 to 81 to ensure all positions get swapped o --there are most definitely more ways of doing this * -static population and then shuffling the deck same as above. As well, although I think you may not need to be told this, when you want to display the positions, you need to know and track which row level of the grid you are on and what column position you are in, in order to properly find the x and y values; thus, in your case testing if the modulus of 9 equals 0 will be handy. I hope the concepts help, Anthony Paul Steven wrote: I am trying to write the code to populate a 9 by 9 grid of tiles with 40 pairs of matching tiles. I have 4 different types of tiles and I want to create a grid that has 40 matching pairs and 1 blank tile. I do not want the pairs all to be next to each other but there needs to be at least one solution that enables a player to match each and every pair. To match a pair there needs to be a clear path between each member of the pair. 1,2,1 2,0,3 4,4,3 In this simple
Re: [Flashcoders] Advice on creating random grid of pairs for a game
In the following grid, all bs are 4-connected to the one a: -, b, - b, a, b -, b, - So for the sample grid -1,-1,-1 -1,-1,04 -1,-1,04 the cells which are 4-connected to the initialized cells are here marked with xx: -1,-1,xx -1,xx,04 -1,xx,04 In other words, the cells to randomly choose the new tile placement from are the ones at the boundary of the already-initialized region. In the initial description of the problem it sounded like valid paths did not include diagonals, which is why I say 4-connected. If diagonals are OK, use 8-connected. Dave On Wed, Mar 11, 2009 at 1:49 PM, Anthony Pace anthony.p...@utoronto.cawrote: I just wanted to mention that I don't think it should be entirely random. Perhaps you may want to add restrictions as I mentioned before, yet, make sure it has a maximum that steps higher and higher based on availability, and alternate between diagonal and side to side. So if there is no way 2 can be side by side or diagonal anymore, than max distance plus 1, check for a max distance of 1 square/slot between from available slots in the array, and so on. This would allow it to build out and use all available squares. Paul Steven wrote: Thanks David for your suggested solution - it looks very promising. I am not sure what 4-connected means? But I think I understand the general idea I am just wondering if your solution would guarantee that there will always be a matching tile within a clear path? For example if the random sequence is 4,4,27,27,1,1,36,36, BLANK -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,-1,04 then -1,-1,27 -1,-1,04 -1,27,04 then -1,-1,27 -1,01,04 -1,27,04 then -1,-1,27 -1,01,04 01,27,04 then -1,36,27 -1,01,04 01,27,04 then -1,36,27 36,01,04 01,27,04 then 00,36,27 36,01,04 01,27,04 -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of David Hershberger Sent: 11 March 2009 17:40 To: Flash Coders List Subject: Re: [Flashcoders] Advice on creating random grid of pairs for a game My first thought for your algorithm is to essentially follow the solution path that your user might end up taking. Your user will be clearing areas out, opening them up. Your algorithm could start with an empty grid and fill in around a starting seed, growing the tile array outward. 1) empty grid 2) generate random sequence of pairs with the blank embedded in it, such as 4,4,27,27,1,1,36,blank,36, etc 3) choose random location for first tile, and place it there, and take it off the pair queue. 4) choose an unfilled location which is 4-connected to any filled location, and put the next tile from the queue in it. 5) repeat 4) until the queue is empty. In this system, putting the blank in a location counts as filling that location, so the value for blank needs to be different than the initial values in the grid. So for example, if -1 means uninitialized and 0 means blank: -1,-1,-1 -1,-1,-1 -1,-1,-1 then -1,-1,-1 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,-1,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,-1,-1 then -1,-1,04 -1,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,-1 then -1,-1,04 01,27,04 -1,27,01 then 36,-1,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 -1,27,01 then 36,00,04 01,27,04 36,27,01 This guarantees that a solution exists, because the algorithm essentially traces out the solution as it generates the level. This presumes that players are not allowed to move tiles around yet leave them on the board while solving the puzzle - it assumes that tiles just disappear from the grid when the user selects a pair which has a clear path between it, and that's the only interaction allowed. If they can slide them around, my algorithm will still produce solve-able puzzles, but they won't be as difficult as they could be, I think. :) Interesting problem! /mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders