📄 asynchronousplayer.java
字号:
// AsynchronousPlayer.java
/**
* @author Sean Bridges
* @version 1.0.1
*
* The asynchronous player blocks the getMove call until
* its makeMove() method is called. Can support multiple
* threads waiting. They are all woken up when makeMove is called.
*
* Currently you cannot tell when the asynchronous player
* has a thread waiting for moves. The class is designed mostly
* to interact with UI's that produce a sequence of moves
* in response to user inputs, such as a mouse click, and dont care
* about the current state of the game.
*/
public class AsynchronousPlayer extends DefaultPlayer
{
//------------------------------------------
//instance variables
private Move lastMove = null;
/** Creates new AsynchronousPlayer */
public AsynchronousPlayer(String name, int number)
{
super(name, number);
}
/*
* Passed a copy of the board, asked what move it would like to make.
* This call will block until makeMove(aMove) is called, at which time
* aMove will be returned.
*
*/
public synchronized Move getMove(Board b)
{
try
{
wait();
}
//if we are interrupted, then it means the game
//is over, and we no one cares about what we return,
//so return null.
catch(InterruptedException e)
{
return null;
}
return lastMove;
}
/**
* Make a move.
* If there are any threads waiting in the getMove()
* method, wakes them up, and passes them this move.
* If no threads waiting, then does nothing
*/
public synchronized void makeMove(Move aMove)
{
lastMove = aMove;
notifyAll();
}
}//end class AsynchronousPlayer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -