iGo Board Game looking for pause

Hi,
I’m quite proud of my first game (as a beginner) and i wanted to share it around.
The “engine” works as i expected and i coded a tiny IA for playing against the iPad. There is a little bit of work left to finish it but i stand in front some problems.

  1. I want to slow down the IA response because it plays as soon as the player touched the screen and its quite difficult to understand it’s move. Is there any “pause” function to do it ?

  2. It misses some splash screens at the beginning and at the end of the game. The first for choosing the opponent and the second one for congrats. I guess the first question as to do with the second.

Here is a video of the game : http://youtu.be/n_wA62RzcUo

forgive my language. By the way, is there any French Codea users ?
Thanks

Hi @Burobox - your game looks great. Building an AI to play GO is a non trivial exercise, so well done.

There is no sleep function in Lua so if you want to slow things down then you could roll your own delay function. How about something like:

-- Delay for a number of seconds.

function delayForSeconds(secs)
    secs = secs or 1
    local endTime = os.time() + secs
    while os.time() < endTime do end
end

This will of course lock everything up until it is done so not an ideal solution.

thanks @Reefwing. I’ll try it.
and…hummm, it’s not really a GO game. It’s much simplier and and the IA just plays the position where he wins as much stones as it can for the current turn only. Thanks anyway for compliment.

@Reefwing, that’s doesn’t seems to work. Even calling the delayForSeconds() function in setup(). Any idea why or where to call it ?

I tried this on blank project and nothing stops

--# Main

function setup()
    print("Hello World!")
    delayForSeconds(5)
    print("finished")
end


function draw()
 
end

function delayForSeconds(secs)
    secs = secs or 1
    local endtime = os.time() + secs
    while os.time() < endtime do end
end

@Burobox - I didn’t actually test the code, theoretically it should work (famous last words).

I will have a look at it tonight and see if I can work out what the problem is.

@burobox you could try the following approach:

--# Main

function setup() 
    time = 0
    endtime = 0
    delayForSeconds(5)
end


function draw()
    time = time + DeltaTime
    
    if time < endtime then 
        -- do nothing
        print("Hello World!")
        return
    end
    
    -- not delayed code here
    print("finished")
end

function delayForSeconds(secs)
    endtime = secs or 1
end

I hope this helps

Burobox, if Codea’s drawing operations are back buffered (I think so) then code like this:


function setup()
    print("Hello World!")
    delayForSeconds(5)
    print("finished")
end

will work like this:

  1. Print “Hello World” into back buffer.
  2. Wait. You won’t see anything because everything is in the back buffer, Codea didn’t have a chance to put it to the front.
  3. Print “finished”.
  4. Leave the script and go back to Codea which now has a chance to show everything at once.

What you need is some sort of state. After the player made his move you set the state to “thinking”, start a timer and leave draw(). As long as the AI is thinking (i.e. as long as the timer is running) you don’t make the computer’s move. If the timer runs out you make the move and set the state to “playing”.

@Burobox - I think @Doffer is on the right track with using DeltaTime in the draw() function. If you print() what come back from os.time you will see that it returns a VERY large number. The delay function I suggested actually works if you specify a delay greater than 65 seconds - which is obviously useless.

The approach I ended up with (using ElapsedTime) is shown below. This example programe will show you the frame rate every second - I did test this one!


function setup()
    initTimeValue = ElapsedTime
    frameCounter = 0
end

function draw()
    if (ElapsedTime - initTimeValue < 1) then
        frameCounter = frameCounter + 1
    else
        print("Frames per second: "..frameCounter)
        frameCounter = 0
        initTimeValue = ElapsedTime
    end
end

That looks great, @Burobox. Really polished game.

I believe there are quite a few French Codea users, including some on this forum, though I don’t recall all their usernames.

Tank you for helping.
While debugging the game with a lot of print(), I noticed that this function takes a lot of resource (or time) to execute. Even when the displayMode is FULLSCREEN. So, I had the idea to use it for slowing down the application. There are approximatively 270 print() every second. By this way, I can stop the draw() less than a second.

Is this an abomination for an experimented coder ? Tell me if my iPad will explode by using this code ? Will I burn in hell for this ?


function setup()
    displayMode(FULLSCREEN)
    start=ElapsedTime
    pause=3   
end


function draw()
    
    -- my unconventional pause
    for f=0,270*pause do
        print (f)
        clearOutput()
    end
    -- end of pause
    
    background(255, 255, 255, 255)
    fill(0, 0, 0, 255)
    text(ElapsedTime - start,WIDTH/2,HEIGHT/2)
    start=ElapsedTime
end

Indeed, it is an abomination. That’s the reason why old PCs (and I mean really old PCs) had the Turbo button; not to add a turbo to the processor but rather to take it out, so that games that relied on the speed of older processors were playable on faster ones.

I suggest to make your drawing code “stageful” (again, but now with code). This is very common and once you did it you can easily add things like a splash screen.

Untested, but this is what I suggest:


PLAYING = 0
THINKING = 1

gamestate = PLAYING

function draw()
    if gamestate == PLAYING then
        -- Your code for the player's move here.
        -- ...
        -- When done, start a timer.
        think_start_time = ElapsedTime
        gamestate = THINKING
    end
    if gamestate == THINKING then
        if ElapsedTime - think_start_time > 1 then
            -- Execute AI move here, then switch to player.
            gamestate = PLAYING
        end
    end
end

Nice-looking game, Burobox! That’s very impressive for a beginner!!

Just a note on naming – in English, we don’t call this game GO, but rather “Reversi”. You’ll also see it referred to by the trade name “Othello”.

Good luck with your game! I hope you’ll keep posting here about it.

@Goatherd I think Go is different from Reversi even though the tokens are similar.

Yep, @Simeon, that was the point I was trying to make. Sorry if I wasn’t clear.

Since Burobox’s app is called “iGo”, I would think it was a Go game, but in fact (from watching the video) it’s a Reversi game. In case this was a French-English translation issue, I just wanted to offer up the normal English name for the game.

And now, since I’m not offering anything helpful in the way of actual programming advice, I’ll step aside… :slight_smile:

I can confirm the name: it is Reversi.

I also recommend Codeslinger’s “state” system.

@Goatherd you are right. I played this game when i was 12 years old and I didn’t remember the name when I start this project. More than 30 years ago… thanks for recall.
I’ll find a custom name like Reverse or Reverso or anything else just before beginning a new project. Ideas are welcome.
Codeslinger’s “state” is now implemented and I start to rewrite all the code for optimization (and to assimilate what I did ) :-?