Word game

I don’t play games, but I try to code parts of them just for something to do. So here’s a start to some word game that’s going around. It’s up to you to create a larger wordTab of 5 letter words. Use the parameter sliders to change the size and position of the board to fit your device. Once you get it set, you can enter those values into the parameter default so you don’t have to do it again. Just start entering letters. A green background means that letter is in the right position. A yellow background means the letter is somewhere in the word. There’s no kind of edits, so things might go wrong. There’s also no ending code when you run out of spaces or match the word. When you start, a random word is picked from the table for you to match.

Edit: Made some changes. Colors don’t update until a line is filled in.

viewer.mode=OVERLAY

function setup()
    parameter.integer("size",20,100,85)
    parameter.integer("xOffset",50,700,200)
    parameter.integer("yOffset",50,700,400)
    wordTab={"wrong","seven","right","bring","chase","angle","break","great"}
    word=wordTab[math.random(#wordTab)]
    rectMode(CENTER)

    tab={}
    for x=1,5 do
        tab[x]={}
        for y=1,6 do
            tab[x][y]=""
        end
    end
    strokeWidth(4)
    showKeyboard()
    xPos,yPos=1,6
    lineNbr=7
end

function draw()
    background()
    fontSize(size*.7)
    for x=1,5 do
        for y=6,1,-1 do
            fill(180)
            checkLetters(x,y)
            rect(x*size+xOffset,y*size+yOffset,size)
            fill(255,0,0)
            text(string.upper(tab[x][y]),x*size+xOffset,y*size+yOffset)
        end
    end
end

function checkLetters(x,y)
    if y>=lineNbr then
        if atPosition(tab[x][y],x) then
            fill(0,255,0)
        elseif inWord(tab[x][y]) then
            fill(255,255,0)
        end
    end
end

function inWord(letter)
    if letter~="" and string.find(word,letter)~=nil then
        return true
    end
    return false
end

function atPosition(letter,pos)
    if letter~="" and letter==string.sub(word,pos,pos) then
        return true
    end
    return false
end

function keyboard(key)
    tab[xPos][yPos]=key 
    xPos=xPos+1
    if xPos>5 then
        lineNbr=yPos
        xPos=1
        if yPos>0 then
            yPos=yPos-1
        end
    end
end

@dave1707 - very neat, so far I’ve got the basics in but have been concentrating on the word list. Also recently been playing with building a keyboard subset.

I think Codea could use a configurable keyboard and joypad system, been playing with that but slow progress at moment. No time.

Thanks for the code - I like the scalability, not something we’ve tended to do in the past but makes the code more generally applicable.

Here’s some code that will create a table of 16,131 5 letter words. The .png image below contains the 16,131 words. Copy the image and put it in the documents.Dropbox folder as word5. Run the code and the table wordTab will contain the words.

function setup()
    -- 16,131     5 letter words
    img=readImage(asset.documents.Dropbox.word5)
    wordTab={}
    str=""
    for y=1,img.height do
        for x=1,img.width do
            r,g,b,a=img:get(x,y)
            buildWord(r)
            buildWord(g)
            buildWord(b)
        end
    end
    print(#wordTab)
end

function buildWord(c)
    if c~=0 then
        str=str..string.char(c)
        if #str==5 then
            table.insert(wordTab,str)
            str=""        
        end    
    end
end

@dave1707 - I have trialled the compression with up to 6000 words so far and read the image, but the image I used was the WordX.png not the WordX@2x.png. Is there no difference between these?

Also, since my word list is all capitals, I have modified your atPosition() function to read either case.


function atPosition(letter,pos)
    if letter~="" and letter==string.sub(word,pos,pos) or letter == string.upper(string.sub(word,pos,pos))then
        return true
    end
    return false
end

Initial try with a keyboard - attached image.

Oh, and thanks for the database image.

@Bri_G When I first posted my word5.png image and tried to use up it, it was only half the width and height and only about 4,000 words. When I switched to the 2x image, it was the correct size.

Your keyboard looks nice.

Very good example, clean code?

@dave1707 - hmm, not quite sure what’s happening here. Does the @x2 image open by default or the lower resolution image? In your code above you address it with just the name:

img=readImage(asset.documents.Dropbox.word5)

Also, I downloaded your image from above, renamed it to word5.png, ran your code and printed out the last entry using print(wordTab{#wordTab-1}) and it printed four random characters out - not a word.

@Bri_G I put the image in photos and then in the Codea Dropbox folder, I add it from photos. It shows as word5 with a size of 164x164. When the code is run, the table should have a size of 16131. If you add the line print(wordTab[16131]) it should print zymin as the last word.

The way I copy it to Codea is to tap on the .png file above. That opens the file and shows a larger image. I long press on the image and an option is to add to photos. I add it to photos then from the Codea Dropbox folder I do the + which give the option to copy from photos. You give it a name word5 and use. It then shows as word5 with a size of 164x164.

How did you folx create the word PNG? Kind of clever.

@RonJeffries It did it all by itself. I was surprised by that because I thought it was going to be .jpg . It would be nice to see the extensions of the files in Codea.

@dave1707 - hmmm again, funny. Tried your saving technique but didn’t use dropbox. Copied it straight to my Codea folder, from Photos, and renamed as word5. Ran again and got an error but that was corrected by rebooting Codea. Tested as you suggest above the number of words quoted was 64524 and the word printed was agsdr. Also, the size of the file in the Codea folder was quted at 328 x 328. Does iOS/Codea upgrade the graphics when stored in the Codea folder.

p.s. didn’t use Dropbox as I find it a little unreliable.

pps. Exported from Photos to an external drive and it saved as IMG_1126.PNG and 328 x 328 pixels. Are you sure your system isn’t loading the image.png and not the image@2x.png.

@Bri_G Looks like the file was 328x328 instead of 164x164. That would give a size 4x more which would be 16131 x 4 or 64524. As for Dropbox, I don’t mean the Dropbox app, but the Codea Dropbox folder. I always use the Codea Dropbox folder because anytime I mess something up there, I can sync it with the Dropbox app and see the files there with their extensions. I can then delete them there and sync with Codea to get rid of them in the Codea Dropbox folder.

As for the word5 file, you need the 164x164.

@dave1707 - resolved the image issue by iterating width and height of image by 2. Must say, some weird words were in there but checked a few and they were genuine. Many foreign words and names

@Bri_G There’s usually 2 files that get created for an image. One regular and one retina. The larger file gives a sharper picture when displayed, but in this case where I’m not using it for a picture, it’s just messing things up. That’s why the smaller sized image 164x164 needed to be used. In the case of the 328x328 file, every other pixel was probably all zeroes which messed things up converting it back to words. By iterating the image by 2, you were skipping over the unused pixels.

@Bri_G Those words are from a dictionary file I downloaded long ago. I just pulled all the 5 letter words from it to create a new file. There are a lot of weird words in it. I was looking for a common word file but never found one I liked.

@dave1707 I’m not understanding or missed a message in my scan. How did you get the PNG image to have the words in it to begin with?

@RonJeffries - @dave1707 created the image from the saveImage() command. If you follow the above code he converted the texts to pixel colours and placed them in an empty image, which when complete was saved.

By default, I think, that image is saved as a png or PNG.

There are many sources of dictionary words on the net to build your own. But @dave1707 has already built this one and provided the image.

@dave1707 - resolved the image issue by iterating width and height of image by 2. Must say, some weird words thrown up, mainly names on a global basis on regional dialects.

Thanks again.

@RonJeffries First I created a table of 5 letter words from a dictionary file I already had. Then I had to get how many words I ended up with. I multiplied that by 5 to get the total number of letters. I divided that by 3 because I can put the ascii value in the red, green, blue (r,g,b,255) of each color pixel. I took the square root of that so I ended up with a square image size. Then it was just a matter of reading thru the file, converting each letter to its ascii value and writing a color pixel every 3 letters. Then you just reverse that to get the letters back out of the image.

@dave1707 - just out of curiosity, can you use the ‘a’ transparency level as a fourth variable in this ?