How good is your memory

Here’s something I threw together that I thought might interest one of the new coders. You can take this and add whatever you want to spruce it up. You can modify the font, the text, the colors, the menu, whatever. I’m not saving any of the scores when the game ends, so maybe you can add a routine to save the data and read it back in. The purpose of this is to tap a red square to reveal a number. You have to select the numbers in order starting at 1. If the number isn’t in order, it will be covered when the next square is selected. You have to remember where the numbers were to keep your tap count low. The square size will increase after each game or you can select a square size.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    rectMode(CENTER)
    displayMode(FULLSCREEN)
    max=12
    play=false
    side=0
    level=0
    score={}
    for z=1,max do
        score[z]=0
    end
    showMenu=true
end

function setup2()
    play=true
    tries=0
    side=side+1
    if side>max then
        side=max
    end
    level=side
    squares=side*side
    offsetX=WIDTH/2-side/2*50-25
    offsetY=HEIGHT/2-side/2*50-25
    nbrTab={}    -- table for numbers that are in order
    for z=1,squares do
        zz=tostring(z)
        nbrTab[z]=false
    end
    tab={}    -- table of random numbers
    for z=1,squares do    -- fill squares with random numbers
        zz=getRandomNbr()
        tab[z]=vec2(zz,0)
    end  
    next=1              
end

function menu()
    fontSize(40)
    str=[[
                       How good is your memory.
            
            The object is to select the numbers in order.
    
               Tap a red square to reveal a number.
        If that number is 1 or the next number in order, 
      it will remain showing when you tap another square. 
         If it's not the next number, then it will be covered 
             again when you select another red square.
            
                            Double tap to start.
    ]]
    text(str,WIDTH/2,HEIGHT/2)
    fontSize(20)
end

function getRandomNbr()    -- get random number not selected already
    n=math.random(1,squares)
    nn=tostring(n)
    if not nbrTab[nn] then
        nbrTab[nn]=true
        return(n)
    else
        loop=true
        while loop do
            n=n+1
            if n>squares then
                n=1
            end
            nn=tostring(n)
            if not nbrTab[nn] then
                nbrTab[nn]=true
                return(n)
            end
            if n>200 then
                close()    -- while loop error, exit
            end
        end
    end
end
    
function draw()
    background(40, 40, 50)
    fill(255)
    
    if showMenu then
        menu()
        return
    end
    
    strokeWidth(2)
    textMode(CORNER)
    for z=1,max do
        text("Level "..z.."    "..score[z],WIDTH-170,HEIGHT-z*60)
    end
    
    textMode(CENTER)
    z=1
    for y=1,side do
        for x=1,side do
            stroke(255)
            fill(255,0,0)
            if tab[z].y==1 or tab[z].y==2 then
                fill(255)
            end
            rect(x*50+offsetX,y*50+offsetY,48,48)
            fill(255,0,0)
            text(tab[z].x,x*50+offsetX,y*50+offsetY)
            z=z+1
        end
    end
    
    fill(255)
    if not play then
        text("Number of tries for level "..level.." was "..tries,WIDTH/2,HEIGHT-20)
        text("Double tap for next level.",WIDTH/2,HEIGHT-50)
        text("Tap a level at the right to goto that level.",WIDTH/2,HEIGHT-80)
    else
        text("Current number of tries   "..tries,WIDTH/2,HEIGHT-40)
        text("Select a red square to reveal a number.",WIDTH/2,40)
    end
end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount==2 and not play then
            showMenu=false
            setup2()
            return
        end
        if t.x>WIDTH-200 and not showMenu then    -- select level
            for x=1,max do
                if t.y>HEIGHT-x*60-30 and t.y<HEIGHT-x*60+30 then 
                    side=x-1
                    setup2()
                    return
                end
            end
            return
        end
        if play then
            for z=1,squares do    -- reset selected out of order number
                if tab[z].y==1 then
                    tab[z].y=0
                end
            end
            z=1
            for y=1,side do
                for x=1,side do
                    if t.x>(x*50+offsetX-25) and t.x<(x*50+offsetX+25) and
                        t.y>(y*50+offsetY-25) and t.y<(y*50+offsetY+25) then
                        tries=tries+1
                        if tab[z].y~=2 then    -- square selected
                            tab[z].y=1    -- show number
                        end
                        if tab[z].x==next then    -- number in order
                            tab[z].y=2    -- set to keep showing
                            next=next+1
                            if next>squares then    -- game over
                                score[level]=tries
                                play=false
                            end
                        end
                    end
                    z=z+1
                end
            end 
        end  
   end
end

@dave1707 Sounds pretty cool. Interesting idea to provide a basic game for “newer learners” (some people find being called a noob offensive) to add to. The game doesn’t start when you double tap for me. I got it to work once but that was it.

@Goatboy76 I think I see what the problem is. If you tap too close to the right side of the screen, I’m getting into a routine I shouldn’t. Double tap in the center of the screen for now. See if that works. I’ll correct that and repost the new code. Also I changed newbies to new coders.

@Goatboy76. I corrected the above code. It should work now. Thanks for pointing out the problem.

@dave1707 I haven’t tried the code yet but I’ve been thinking about this myself for the last couple of days. You can occasionally get the impression from the forums that Codea isn’t really for beginners which is weird when it’s so obviously such a great way to get into programming. The syntax is relatively simple, there’s loads of built in shortcuts and libraries - not to mention the code completion, built in reference and ‘GarageBand of coding’ style pickers. I’ve only been playing around for a couple of weeks (most of that reading tutorials etc) and not only am I already on my way to a fairly nifty shoot em up/ puzzle hybrid but my programming confidence (if not my actual ability) has gone from zx81 era basic to messing around with xcode and javascript.

My point? (I think I had one anyway…) Most of this was learnt poking around in other people’s code that they had kindly left lying around the web and it led me to thinking about how useful it would be to have a set of fairly complete but well documented beginner templates of the most popular genres like the kind packaged with Construct2, for example - shooters, platforms, infinite runners and jumpers etc. I’m sure there are those who might think it goes against the DIY ethos of Codea but, on the contrary, I think it would accomplish at least two important things: 1) Curious beginners like me would gain huge confidence and learn lots from poking around the comments and code, and 2) it might make Codea way more accessible to the GamePress/Construct 2 crowd who just want an easy way to make their own games on iPad by using their own graphics, hopefully bringing loads more revenue into TLL and providing them with more time to develop it for everyone else.

What do you think? Apologies everyone for another long post, I’ve really got to work on my concision… (You should see my code!)

@ScottDafydd It would be nice to have a bunch of simple starter programs that new coders can start with, but there are so many different ways of doing things. It’s probably impossible to give everyone a correct starter. That’s why I like to just post simple examples for new coders to look at. I think the simpler the better. There are a lot of classes already created that do a lot of things, but from a new coder point of view, all that code could be too much to understand. Those classes will be usefull once the new code gains more experience. From my point of view, the simpler the better.

@dave1707. Not too simple! Trivial solutions often don’t show what’s really going on. I am all for middle-of-the-road examples with plenty of comments to explain the whys and wherefores.

@syntonica I’m not very good when it comes to putting comments in code. Even when I worked, my boss always asked where the comments were. I always said, “The code is the comments, it’s always up to date and correct”.

@ScottDafydd - I agree entirely with your direction, and I don’t think it conflicts with anything. I’m sure we are all grateful for any help we can get in learning something new.

I think I said this in a different thread, but the big problem is the developers are part time, and our community is small and totally voluntary. We would all love to have templates as you describe, but the problem is resources and willpower (most users probably prefer to be working on their own projects).

I started out creating some step by step projects eg which showed how to bounce a ball and then get more complex, but - like West with his FoggyBummer - I never knew how much of it got used. So I moved onto blogging and writing ebooks as a way of sharing knowledge.

But there is definitely room for some good templates, and if anyone picks up on your suggestion, I will be happy to help. They will probably require a good code editor, because the range of skills and training of Codea users varies enormously, and you would want the templates to be consistent as well as easy to use.

Regardless, it’s nice to have someone thinking this way, so please don’t give up. \m/

@Dave1707 lol. Your position is ok as long as your code will never have to be extended or reworked by someone else. Which is not the case anymore since you retired! Unless your company shut down? So i understand your boss! L-)

@Jmv38 Yes, I’m retired so the only coding I do is here. I don’t think not putting in comments is going to have an adverse effect on anyone. They can always ask me to explain something they don’t understand. As for my company shutting down, they’re still going strong. I worked for Macy’s, you may have heard of them.

@dave1707 Good code is indeed self-documenting, but often we get too clever for our britches. That’s where comments are needed. I have code that I look at and wonder, What the heck was I thinking?

@syntonica @dave1707

I did a lot of research a while back, on improving spreadsheet quality, which has the same issue with explanatory comments and general clarity, and I came to a guiding rule “make it easy to check”, in other words it’s not enough just to get it right, because someone else needs to be able to check it properly. And with spreadsheets, good checking is crucial.

The less time checkers spend scratching their heads wondering what is going on, and the more time they spend actually understanding what the code is doing, the better, and the fewer mistakes they are likely to make (or miss).