Should my board game be multi-threaded?

Tasnu Arakun
Unregistered
 
Post: #1
I'm working on a board game written in Cocoa/Obj-C. It's not the first time since I have previously helped writing a command line checkers game in Java. I've come to realise that Cocoa behaves slightly different.

I've been thinking of how to structure the game and come to the conclusion that it needs to be multithreaded. Maybe this is just Java talking, but i want a "game loop" like this:

Code:
while( ![ game hasEnded ] ) {
    move = [ player[ turn ] makeMoveForState:[ game state ] ];
    [ game performeMove:move ];
    [ view redraw ];
    turn = ( turn == BLACK ? WHITE : BLACK );
}

In Java this is fine - the game will still accept user input, as well as output information about the current game state. In Cocoa this is a big no since the loop will block the main thread and thus its run-loop. That's why it looks to me as if I need a special "game thread".

To make things more difficult the two players, Ai and Human, behave quite differently from each other.
The AI player performs a series of calculations then returns an answer. Here I'm thinking of starting yet another "AI thread" having the "game thread" periodically poll the AI for an answer.
The human player sits around waiting for user input. In this case I need to put the "game thread" to rest, waking it up when the user has made its choices.

I've written a few arcade games in Cocoa using one or two NSTimers to control physics and graphics. This however is different. So, am I on the right track? Is this the Cocoa way and what tools should I use?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
If the AI takes a long time to think, you should put that on a secondary thread. I would *not* put the game loop on a secondary thread. That just unnecessarily complicates matters.

You should be *very* sure you understand the pitfalls of multithreaded programming (no, Java experience doesn't really count; garbage collection alone fixes lots of problems) before you begin. That includes the particular pitfalls of Cocoa (much less is thread-safe than in Java). You'll find documentation on threading Cocoa in your Developer Documentation folder.
Quote this message in a reply
Tasnu Arakun
Unregistered
 
Post: #3
Okay, I understand. Thanks for the advice.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Obtaining GPU deviceID for each window/NSScreen in multi-display/multi-GPU app? dauerbach 1 3,494 Oct 8, 2012 11:58 AM
Last Post: OneSadCookie
  Easy platform for computer board game Lizard Man 6 5,617 Jan 22, 2010 03:37 PM
Last Post: Blacktiger
  On DisplayLink & Threaded GL TomorrowPlusX 6 4,958 Aug 12, 2009 02:34 PM
Last Post: TomorrowPlusX
  Mud with or without Multi-Threading ? MadPsycho 21 8,958 Oct 3, 2006 11:06 AM
Last Post: Dan Potter
  Threaded events in Cocoa VolganPoet 6 5,588 May 11, 2005 05:24 PM
Last Post: Andrew