views:

293

answers:

7

I want to create a simple game like tic tac toe where the human user is playing against the computer. The computer function takes a couple of milliseconds to run but I would like to give the perception of the computer taking 5 seconds to make a move. Which method should I use?

1) Create two memory threads. One for the computer and one for the human user. When the computer is taking 5 seconds to imitate thinking, the human user thread is paused for 5 seconds.

2) Disable input devices for 5 seconds using timer or dispatchertimer

3) Any better methods you can think of..

Thanks!

Edit - The question is about the how and now the why. 5 seconds is just an example. I prefer 1-2 seconds but for example purposes I just chose 5. So please focus on what is the best way to do this and not on the 5 seconds. Thanks again.

+1  A: 

Because the human is using the GUI as its interface, you can't pause its thread. Pausing the GUI thread would disable your form from repainting, thus not showing the computer's move.

I'd just disable all game related input, and show an animation of the computer working its brain.

deltreme
Or a progress bar?
Mr Roys
Technically that's an animation as well :P
deltreme
True, true ... but a progress bar (and certain animations) show how long the player has to wait
Mr Roys
+3  A: 

you could just make the cursor into an hourglass and disable the main form (which will stop the user from inputting anything), start a timer for 5 secs (i'd make this time configurable), when the timer fires enable the main form and set the cursor back to normal again. Something like this:

Call this method when the user has made their move:

private void UserHasMoved()
        {
        Enabled = false;
        timer1.Interval = 5000;
        timer1.Start ();            
        }

then the event for the timer:

private void timer1_Tick(object sender, EventArgs e)
        {
        Enabled = true;
        timer1.Stop ();
        }
Sam Holder
+2  A: 

wouldn't this be annoying to the faster players? Maybe a second for the turn, but you'll soon find out that 5 seconds is too much. Either way, do not pause the UI thread, because the interface would 'freeze'.

Axarydax
A: 

Certainly a Timer would be the way to go.

On top of the suggestions made I would also be tempted to create a Settings page where the user could change the thinking time from 5 seconds. This will enable them to manually reduce the delay between moves.

kevchadders
+1  A: 

From your question it seems you do not want the computer play to be so lightning fast that the human player can't see it. For that effect, why don't you display some random animation showing possible plays, and ultimately animating the chosen move.

Edit: I have to agree that disabling input devices is a bit harsh. Maybe switch the application to a state in which any input by the user cannot affect the board, but can affect other commands, such as save, restart or quit.

Daniel C.S.
+3  A: 

Noise and Blinking lights = digital thought.

Rev up the CPU to 100% with an infinite loop to generate heat.

Start looping through all the files to get the hard drive light blinking and making spinning noises.

Run a shell command to change directory to the optical drive to make the optical drive spin.

Set caps lock, numlock and scroll lock on and off, some keyboards have lights for that.

Check to see if your motherboard supports any additional blinking lights.

Ah! Good point. Many fans have API access, so turning the fans on to 100% is cool. The revert to normal could be dangerous though, because if you accidentally turn off the fans for good, it could be serious damage.

MatthewMartin
Give your GPU something to do too. My graphics card fan sounds like a vacuum cleaner :-)
Incredulous Monk
+1  A: 

Two very important usability principles:

  • Don't block the UI thread! Only disable the game control so that the player cannot perform a step during the thinking.
  • Show a visual feedback to the user indicating the thinking and that he can't perform the step for a while.

And of course, balance the exact duration of thinking carefully. Treat the players' time as very precious.

thSoft