if ((evt.target == miNextPlayer) && (gameparams.size() > 0)) {
// skip current player
sendTurn(nextturn);
nextturn = nextPlayer(nextturn);
}
if ((evt.target == miNewBoard) && (newboardframe == null)) {
if (winFrame != null) winFrame.dispose();
winFrame = null;
newboardframe = new SmartFrame(this);
newboardframe.setTitle("NicNacNoe: New Game");
Panel p = new RemoteFileDialog(com,"games.txt",this);
newboardframe.add("North",new Label("Currently, there are "+com.getUsers().size()+ " players who would like to play. Which game would you like to start?"));
newboardframe.add("Center",p);
newboardframe.pack();
newboardframe.show();
return true;
}
if (evt.target instanceof RemoteFileDialog) {
if (evt.arg == null ) {
newboardframe.dispose();
newboardframe = null;
return true;
}
Hashtable pars = loadParams((String)evt.arg);
if (pars != null) {
Vector users = com.getUsers();
int minplayers = ((Dimension)pars.get(Board.NUMPLAYERS)).width;
int maxplayers = ((Dimension)pars.get(Board.NUMPLAYERS)).height;
if (users.size() < minplayers) {
new SmartFrame("You need at least "+minplayers+" players for this game.");
return true;
}
if (users.size() > maxplayers) {
new SmartFrame("At the most, "+maxplayers+
" players can play this game. The other will have to watch");
}
gameparams = pars;
int numplayers = maxplayers;
if (users.size() < maxplayers) numplayers = users.size();
Enumeration e = users.elements();
byte[] scores = new byte[numplayers];
int[] ids = new int[numplayers];
int i = 0;
while (e.hasMoreElements() && (i < numplayers)) {
User u = (User)e.nextElement();
ids[i] = u.getID();
scores[i] = 0;
i++;
}
gameparams.put(Board.IDS,ids);
gameparams.put(Board.SCORES,scores);
com.sendToAll(new Msg(MSG_NEW_BOARD,gameparams));
newboardframe.dispose();
newboardframe = null;
return true;
}
}
return false;
}
public void showYourTurn() {
hideNotYourTurn();
if (miInformTurn.getState()) {
if (yourTurnFrame == null) {
yourTurnFrame = new SmartFrame(this,
"OK, thanks.", new Event(this,EVT_CANCEL_INFORM,null),
"I know. Shut up!", new Event(this,EVT_DISABLE_INFORM,null));
yourTurnFrame.setTitle("NicNacNoe: your turn...");
Panel p = new Panel();
// too make it look nicer under netscape win 95
p.setLayout(new VertLayout(VertLayout.STRETCH));
p.add(new Label(""));
p.add(new Label("It is now your turn.",Label.CENTER));
p.add(new Label(""));
yourTurnFrame.add("North", p);
yourTurnFrame.pack();
yourTurnFrame.show();
}
}
}
public void showNotYourTurn() {
hideYourTurn();
if (notYourTurnFrame == null) {
notYourTurnFrame = new SmartFrame("It's not your turn!");
}
}
public void hideNotYourTurn() {
if (notYourTurnFrame != null) {
notYourTurnFrame.dispose();
notYourTurnFrame = null;
}
}
public void hideYourTurn() {
if (yourTurnFrame != null) {
yourTurnFrame.dispose();
yourTurnFrame = null;
}
}
public boolean handleEvent(Event evt) {
if (evt.id == EVT_DISABLE_INFORM) {
miInformTurn.setState(false);
hideYourTurn();
return true;
}
if (evt.id == EVT_CANCEL_INFORM) {
hideYourTurn();
return true;
}
return super.handleEvent(evt);
}
public boolean mouseDown(Event evt,int x,int y) {
if (evt.target == board) {
if (whoseturn == com.getMyID()) {
if (board.get(x,y) == Board.EMPTY) {
controls.disable(whoseturn);
whoseturn = -1;
sendPutPiece(new Piece(x,y,com.getMyID()));
board.set(new Piece(x,y,com.getMyID()));
}
}
else {
showNotYourTurn();
}
}
return true;
}
/**
* Handle an incoming message. This is called by the ComObj.
*/
public synchronized boolean handleMsg(Msg msg) {
if (msg.type == Msg.NEW_USER_INFO) {
if (gameparams != null) updateUserInfo(msg.from);
}
else if (msg.type == MSG_NEW_BOARD) {
gameparams = (Hashtable)msg.arg;
whoseturn = -1;
nextturn = -1;
// We have to set the PLAYERS field, since it could not be sent...