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.
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 ?
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.
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
--# 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
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:
Print “Hello World” into back buffer.
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.
Print “finished”.
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
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
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…
@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 ) :-?