0
\$\begingroup\$

Im writing a turn-based multiplayer game that implements a chess timer. When the current players turn starts, all devices start timing for the current player, however, as his turn proceeds, the timers fall out of sync, and at the end of his turn(say for example he took 1 minute to make a move) his time on the other players devices are sometimes up to 10 seconds ahead or behind. I understand theoretically its impossible to time-sync devices down to the millisecond, but if i can get it atleast within a second, that will work just fine. I tried the following the try overcome this:

-Sync time across devices at the beginning and end of current players turn.

-Sync time whilst it is current players turn.

-Combination of the two.

The problem with these attempts is that they cause a lot of flickering on the clock, and, at the end of the turn, when other players see the current players time increasing by 5-10 secs, they feel cheated, and it just doesnt look good. Newbie here so be gentle with your responses :)

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

First of all, consider if you want to prevent cheating. If you do, you must have a server (that is, a computer which belongs to neither player) which not only enforces the legal moves but also calculates the remaining clock time. Then it is just a matter of accurately displaying that time.

If you aren't going to use a central server, then: My first concern is that you're seeing much more clock error than you should be. Modern computers and smartphones should not drift 10 seconds in 60 seconds. This suggests that you are not properly implementing timekeeping.

For example, one such mistake is to do things like request of the system "call me back in 1 second" to tick the game clock and do that repeatedly. This causes trouble because the time spent executing your callback adds to the overall time; you need to measure from some reference point (either when the game starts, or the time at which you expected a given callback to be called).

Another thing is to make sure your time synchronization takes network latency into consideration. Doing this right is hard (see NTP for details) but you don't need high accuracy, just to not have a consistent large error.


But still, there may be some error, and you'd like the clock on screen to not obviously jump. The way to deal with this is to not reset the local time; instead, adjust the rate-of-flow-of-time (“seconds per second”, if you will) so that the error is eliminated gradually. If the local displayed clock is behind whatever your devices sync to, then make it run a little bit faster, and so on.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot sir, i was making the "call me back in 1 second" mistake. A combination of the server as the central time-keeper plus the "seconds per second" mechanism did the trick \$\endgroup\$
    – lll
    Feb 15, 2016 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .