Integrating algorithmic generators in corona

Hi everyone, new Corona SDK user here. Is it possible to some how integrate a board generator algorithm into Corona? I was thinking somewhere along the lines of a boggle board generator that would provide reasonable gameplay value. I tried using math.random based boards but they seem too random? The longest playable words were only a 4 letters long and it just wasn’t fun to get into. How would you guys structure the code around it? And would sprites be the way to go for placing the letter pieces onto the board? ( I kinda want the letter pieces be able to move around during the game )

Thanks in advance everyone. [import]uid: 148823 topic_id: 29161 reply_id: 329161[/import]

Hey there,

Games like Boggle and Scrabble draw from a fixed set of tiles. The letter counts have been weighted as well - i.e. more E’s than Q’s

You could represent this in a table like this:

local myLetters = [“a”,“a”,“a”,“a”,“a”,“b”,“b”,“c”,“c”,“d”,“d”,“d”,“e”,“e”,“e”,“e”,“e”,“e” etc…]

And when you go to build your board, pull a random letter from this table and then delete that letter from the table.

Then pick the next letter…

That way all of your boards will have the proper distribution, and you won’t get too many vowels, etc.

Does this make sense?

Joe [import]uid: 8444 topic_id: 29161 reply_id: 117254[/import]

Hey there-

See this tutorial, it is actually for a boggle board;
http://corona.techority.com/2012/03/10/boggle-in-under-350-lines-in-coronasdk-full-video-tutorial/

Peach :slight_smile:

Edit: Sorry, had this open from earlier and hadn’t seen reply. Seems like Joe beat me to it. Though re logic above may still be useful. [import]uid: 52491 topic_id: 29161 reply_id: 117267[/import]

Yea that would solve the distribution part, but how would I map the letters to the tiles? Would I use sprite instances like in Peach’s Boggle tutorial?

and what’s the difference between math.random and math.randomseed? I’ve read the docs and I can’t seem to wrap my head around it. Thanks for the quick help. [import]uid: 148823 topic_id: 29161 reply_id: 117339[/import]

Hi funmobster,

The [lua]math.random()[/lua] function returns a random number. It returns either a decimal between 0 and 1, or an integer, depending on what arguments you pass to the function.

(Note: It’s actually impossible for a digital computer to generate a truly random number, since computers are deterministic. So, technically, the numbers you get are “pseudo”-random, which means they’re generated by an algorithm, but one whose results look to be essentially random.)

[lua]math.randomSeed()[/lua] does not return anything at all. Instead, you might say it “primes” or “gets ready” the random number generator to be ready to serve random numbers to you. If you imagine an analogy to drawing random numbered balls out of a jar, [lua]math.randomSeed()[/lua] is kind of like the initial shaking of the jar.

What’s neat is that if you set [lua]math.randomSeed()[/lua] with the same seed at the start of your program, then the random number generator will return the same sequence of random numbers every time you run your program. That’s useful for debugging. The example code at http://docs.coronalabs.com/api/library/math/randomseed.html shows this effect in action.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 29161 reply_id: 117353[/import]

Thanks for the replies guys, I’m still wondering about the implementation. So, lets say I setup my sprites like Peach:

[lua] local letterSheet = sprite.newSpriteSheet( “filenameSpriteSheet.png”, widthOfSprite, heightOfSprite )
local letterSet = sprite.newSpriteSet( letterSheet, 1, 26 )
sprite.add( letterSet, “letters”, 1, 26, 1000, 1 )[/lua]

and then create a table of the distribution of letters that I want for the game:

[lua]local myLetters = [“a”,“a”,“a”,“a”,“a”,“b”,“b”,“c”,“c”,“d”,“d”,“d”,“e”,“e”,“e”,“e”,“e”,“e” etc…][/lua]

How would I control creating sprite instances with the distribution set by myLetters?
Or I guess how woud I go about “pulling” these letters from my distrubuted table with sprite instances? [import]uid: 148823 topic_id: 29161 reply_id: 117474[/import]

Some developments:

I’ve been scouring the internets for examples and resources of how to handle different objects within the same table, and I believe maybe the most effective way to do this would be with keys and pairs? Would this effectively map all letter images of “B” per say to the “B” letter within the distribution list, so the game can read that when the “B” image is interacted with it will know its “B”?

Any help would be greatly appreciated, and thanks to those that have already replied. [import]uid: 148823 topic_id: 29161 reply_id: 117617[/import]

guys i am developing a 2d game in which a character will animate on the stage and says funny comments when i tap at a button a random word will be generated from the dictionary and displayed in a label i just have problem in generating a random word from the dictionary.thanks. [import]uid: 160139 topic_id: 29161 reply_id: 117713[/import]

Have you read in dictionary/word list and added it to a table? [import]uid: 52491 topic_id: 29161 reply_id: 117852[/import]

I have added a dictionary and built it. I would like to be able to randomly pick letters from a table and create display objects out of them. Basically in the way that Joe described. I’m just not sure how to pick from the table and create the display object in an efficient manner and also have the code be able to recognize the letter that is being created.

Peach, I see from your Boggle code that this is how you do it:

[lua]letter[#letter].currentFrame = math.random(1,26)
letter[#letter].val = letterList[letter[#letter].currentFrame][/lua]

I see you do this through a loop and randomly pick from the frames of the sprite sheet. I would like to essentially be able to do this without creating a sprite sheet that is 200 (or however many letters will be generated) sprites big. I not really sure how I would be able to link my 26 character big sprite sheet to a 200 letter long list that has the correct distribution of letters.

Any help would be greatly appreciated as I still have no idea how to do this. Thanks for your help everyone. [import]uid: 148823 topic_id: 29161 reply_id: 117960[/import]

The sprite sheet wouldn’t have to be 200 frames big - it would be 26 - unless you are using a language other than English? [import]uid: 52491 topic_id: 29161 reply_id: 118004[/import]

Thanks for the reply, currently the sprite sheet is indeed 26 frames big. And the [lua]local distributedList[/lua] table is 288 strings big filled with 9 "A"s, 6 "B"s, etc … What I would like for the code to do is to randomly pick a letter from [lua]distributedList[/lua] (i.e. “C”) and then display the letter using a sprite instance (I guess it would be frame 3?) and then remove this string from the table to allow for the probability of picking other letters to increase. At the same time, the sprite instance would have to be somehow told that its instance’s value is “C” so when the player touches the image, the game can register a letter “C”. I’m not really sure how to do this?

Any help would be appreciated, and thanks again for replying. [import]uid: 148823 topic_id: 29161 reply_id: 118006[/import]

You would know which letter the sprite was showing based on the frame, 3 would be “C” as you said.

For picking the letter and showing appropriate sprite you would get a random letter as you said, then use string.byte to get letter value and subtract 64. This is because C will return 67, so 67 - 64 = 3, which you’d use to set the frame.

Make sense? [import]uid: 52491 topic_id: 29161 reply_id: 118139[/import]

Awesome! That seems to work great! Thanks for the help, one last quick thing. Can you please explain the masking you did in the Boggle code?

Specifically how to be able to achieve the same highlighting effect but with moving letter tiles instead of static ones? Some thoughts: Would I need to create a separate mask object per letter object that is moving around? Or is it possible to just create one mask object and just replicate it for every time it needs to be displayed

The code you wrote for the masks used in the Boggle clone:
[lua]local maskGroup = display.newGroup()

for x = 1, 4 do
for y = 1, 4 do
mask[#mask+1] = display.newImage(“mask.png”)
mask[#mask].x = x*70-15
mask[#mask].y = y*65+70
mask[#mask].isVisible = false
maskGroup:insert(mask[#mask])
end
end

– this is the line of code that was written into the each of the letter tiles
letter[#letter].mask = mask[#letter][/lua]

Thanks so much for your help Peach, its been very useful! [import]uid: 148823 topic_id: 29161 reply_id: 118349[/import]

What I used weren’t actually real masks, I just called them that.

For shifting letters you’d likely just check/move the position of the appropriate “mask” when it was time to display it. Creating one when it needed to be displayed may make sense, though in the case of the boggle clone they were all used so frequently it made more sense to just toggle the visibility. [import]uid: 52491 topic_id: 29161 reply_id: 118470[/import]

So would I have to use a “enterFrame” event listener to constant update the positions of the masks? I’m a little confused as to how to implement this. Would I still have to create a table and then index as many masks as there are letters in the game?

A little background would probably help: the gist of the game is that there are letters falling from above the screen and as they enter the screen, the player is able to swipe around the screen to select the letters. The effect that I should come from the masks is just a simple highlighting to allow the player to distinguish from selected letters and unselected letters. Per round the number of letters can reach as high as 300.

I’m guessing since the letters are moving, only toggling the visibility of the masks would not be enough. There would have to be some way that the mask itself can follow the moving of the letter as it move down the screen. I’m really confused as to how to actually do this.

Any help would be very gratefully appreciated. And thanks Peach for replying, you’ve been a great amount of help. [import]uid: 148823 topic_id: 29161 reply_id: 118518[/import]

Since you’re moving I would actually just move the masks with the letters when you move them. I’m guessing that you’ll simply have a group or two which are dragged around?

:slight_smile: [import]uid: 52491 topic_id: 29161 reply_id: 118644[/import]

Well whats happening is that I have a display group and a function that creates the letters and is first inserted into a table and then the table is inserted into the display group. And each of these letters are physics bodies, as defined in their function. The function to create the letters is then performed indefinitely with a randomized delay interval. How would the masks be able to track the movement of the letters? Would the mask defining function have to be separate or can it be encapsulated within the letter creation function?

Here’s some of the code that I have:
[lua]local lettersGroup = display.newGroup

local function recordDrops( event )
t = event.target
if t.state == “unspent” and prevLetter == nil or t.state == “unspent” then
if event.phase == “began” or event.phase == “moved” then
enteredText.text = enteredText.text…t.val
t.state = “spent”
prevLetter = t
end
end
end

local fallingLetters = {}

local function createFallingLetters()
fallingLetters[#fallingLetters+1] = sprite.newSprite( dropletSet )
fallingLetters[#fallingLetters].x = math.random( 40, 590 )
fallingLetters[#fallingLetters].y = -40
fallingLetters[#fallingLetters].currentFrame = math.random( 1, 26 )
fallingLetters[#fallingLetters].val = letterList[fallingLetters[#fallingLetters].currentFrame]
fallingLetters[#fallingLetters].state = “unspent”
physics.addBody( fallingLetters[#fallingLetters] )
fallingLetters[#fallingLetters].isSensor = true
fallingLetters[#fallingLetters]:addEventListener( “touch”, recordTouch )
lettersGroup:insert( fallingLetters[#fallingLetters] )
end

timer.performWithDelay( math.random( 300, 700), createFallingLetters, 0 )[/lua]

Thank you so much for all the help, you guys are awesome! [import]uid: 148823 topic_id: 29161 reply_id: 118786[/import]

Because you’d be creating a mask per letter you should be able to use mask[someNumer].x = fallingLetters[sameNumber].x and obviously for Y as well.

If you are using a group for letters and one for masks however you could simply have a Runtime listener for maskGroup.x, maskGroup.y = lettersGroup.x, lettersGroup.y [import]uid: 52491 topic_id: 29161 reply_id: 118852[/import]

So I tried doing this and it doesn’t work:

[lua]local masks = {}

local function createMask()
masks[#masks+1] = display.newCircle( 0,0,40 )
masks[#masks]:setFillColor( 255,255,0 )
masks[#masks].x = fallingLetters[#masks].x
masks[#masks].y = fallingLetters[#masks].y
end[/lua]

The [lua]createMask()[/lua] is then added to the [lua]recordDrops()[/lua], so it is called every time when the letter value is recorded and the letter’s state is changed to “spent”. The problem is that the masks stay at the x and y coordinates of where its called but doesn’t move with the letter its supposed to be masking. How would I correct this? [import]uid: 148823 topic_id: 29161 reply_id: 118889[/import]