Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I built an othello game that can be played by 2 humans , human and computer and two computers.

Everything works correctly but the problem is that when 2 computers play with each other the fram doesn't appear until the game is finish.

I think it is becase the 2 computers is very fast, for that i tried to use the timer but it give me many errors and the result of the game is completely wrong.

Also i used the (thread.sleep) but it didn't solve the problem...

can you help me please..

thank you
Posted
Updated 8-May-11 21:11pm
v4
Comments
CPallini 17-Mar-11 16:55pm    
Since Thread.Sleep should do the trick, I guess you should provide some code.
Henry Minute 17-Mar-11 17:11pm    
It might also help if you say how the two computers are connected - local network or internet or what?
caabool_oo 17-Mar-11 18:56pm    
no the 2 computers play in the same computer but with different functions that related to the game.

1 solution

The problem is that Computer_Play() is a recursive function because it keeps calling itself, and your GUI likely isn't going to update until the function returns (when the computer has finished playing).

So what you might want to do is set up a timer to call a single iteration of the Computer_Play function until the game is over. Kinda like:

timer_tick()
{
  boolean end1 =board.Check_GameEnd();
  System.out.println(" is game end :  =" +end1);
  if(!end1 && ((board.P1.turn==true && board.P1.name.matches("Computer")) || (board.P2.turn==true && board.P2.name.matches("Computer"))))
  {
    Computer_Play();
  }
  else
  {
    //Stop the timer
  }
}


And make it so Computer_Play never calls itself.

EDIT: A quick example of using a timer can be found here: Java Timer Example[^]
The only difference is that you'd need to execute the function more that once, so you need to use a different overload for the schedule function, or explicitly schedule the next call each time.

Timer myTimer;
...
myTimer = new Timer();
myTimer.schedule(new DoComputerPlay(), 0, 1000); //Keep calling the function with a 1 second delay

class DoComputerPlay extends TimerTask
{
  public void run()
  {
    Computer_Play();

    if( computer has finished )
      myTimer.cancel();
  }
}
 
Share this answer
 
v2
Comments
caabool_oo 17-Mar-11 19:51pm    
I didn't understand how to set up the timer. Can you give me an example please, and how to stop the timer.

thank you for your replay..
Anthony Mushrow 17-Mar-11 20:08pm    
Answer updated.
caabool_oo 17-Mar-11 23:51pm    
Sorry for late :|

i use the similar way befor and i try it now ,it give me errors and the result is completely wrong. this is the erroe

Exception in thread "Timer-24" java.lang.IndexOutOfBoundsException: Index: 10, Size: 5

at java.util.ArrayList.RangeCheck(ArrayList.java:547)

at java.util.ArrayList.get(ArrayList.java:322)

at OthelloGame.Search.MiniMax_Search(Search.java:61)

at OthelloGame.OthelloBoard.Computer_Play(OthelloBoard.java:236)

at OthelloGame.OthelloBoard$DoComputerPlay.run(OthelloBoard.java:275)

at java.util.TimerThread.mainLoop(Timer.java:512)

at java.util.TimerThread.run(Timer.java:462)

and it is reapeted many time with different index and size.

i didn't use this two line :
if( computer has finished )
myTimer.cancel();

I use only
myTime.cancel();
after
Computer_Play();


because i didn't know when the computer finish..

please help me and sorry for late.
[no name] 19-Mar-11 0:37am    
then what is your problem can you tell me in details
[no name] 19-Mar-11 1:08am    
You couldn't reply on my previous post please see this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900