FileDocCategorySizeDatePackage
Game.javaAPI DocExample6781Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.server

Game

public class Game extends Object

Fields Summary
String
white
String
moves
String
whitesLastMove
String
black
String
info
String
whiteTime
String
blackTime
String
moveTime
String
whiteId
String
blackId
String
filename
private int
suspendCount
int
offerFlag
int
movenum
boolean
whitetomove
Vector
kibitzer
private String
result
private boolean
gameover
static final int
DRAWOFFER
static final int
ABORTOFFER
Constructors Summary
public Game(String white, String black)


        
    this(white,black,"");
  
public Game(String white, String black, String info)

    this.white = white;
    this.black = black;
    this.info = info;
    movenum = 0;
    whitetomove = true;
    kibitzer =  new  Vector();
    result = "";
    gameover = false;
    whitesLastMove = " ";
    moves = " ";
    whiteTime = "0";
    blackTime = "0";
  
Methods Summary
public synchronized voidaddMove(borland.samples.apps.chess.client.ServerMessage smsg)

       int index = smsg.msg.indexOf(' ");
       String time = smsg.msg.substring(0,index);
       String move = smsg.msg.substring(index+1);
       if (whitetomove)
          whiteTime = time;
       else
          blackTime = time;
       if (!gameover)
         if (whitetomove) {
           movenum++;
           whitetomove = false;
         }
         else
           whitetomove = true;
       setInfo();

       if (move.startsWith("@")) {
          index = move.indexOf(' ") ;
          moves = moves + " " + move.substring(0,index);
          move = move.substring(index+1);
       }
       if (move.endsWith("_")) {
         whitesLastMove = move.substring(0,move.length() - 1);
         moves = moves + " " + whitesLastMove ;
       }
       else {
         index = whitesLastMove.length();
         if (index > 0 && move.startsWith(whitesLastMove))
             moves = moves + " " + move.substring(index);
         else  {
           System.out.println(move + " doesn't begin like");
           System.out.println(whitesLastMove + " =whites last move");
           moves = moves + " " + move ;
         }
       }
       //ServerMessage smsg = new ServerMessage(0,"Moved",movedata);
       SendThread s;
       for (int i=0;i< kibitzer.size();i++) {
         s = (SendThread)kibitzer.elementAt(i);
         System.out.println("send move to kibitzer " + s.portnum );
         if (s.portnum != smsg.port )
           s.addMsg(smsg);
       }
  
public synchronized voidaddObserver(SendThread user)

    ServerMessage smsg = new ServerMessage(user.portnum,"Chat",user.name.user + " joins");
    chat(smsg);
    kibitzer.addElement(user);
    ServerMessage msg = new ServerMessage(user.portnum,"MoveList", getMoves());
    user.addMsg(msg);
    //synchronized (user) {
    //  user.msgque.addElement(msg);
    //  user.notify();
    //}
  
public synchronized voidchat(borland.samples.apps.chess.client.ServerMessage smsg)

    for (int i=0;i<kibitzer.size();i++) {
      SendThread s = (SendThread)kibitzer.elementAt(i);
      smsg.port = 0;
      s.addMsg(smsg);
      //synchronized (s) {
      //  s.msgque.addElement(smsg);
      //  s.notify();
      //}
    }
  
public voidgameOver(java.lang.String result)

    this.result = result;
    gameover = true;
    moves =  " [Result \"" + result + "\"]" + moves;
    chat(new ServerMessage(0,"Chat","Game Over " + result));
    info = " " + result + " " + movenum + " Moves"   ;
  
public voidgameResumed()

    suspendCount--;
    setInfo();
  
public booleangameSuspended()

    suspendCount++;
    setInfo();
    if (kibitzer.size()==0  && suspendCount > 1)
      return true;
    return false;
  
public java.lang.StringgetBlack()

    return black;
  
public java.lang.StringgetBlackId()

    return blackId;
  
public java.lang.StringgetMoves()

    return "[White \""   + white + "\"] "   +
           "[Black \""   + black + "\"] "   +
           "[Blacktime " + blackTime + "] " +
           "[Whitetime " + whiteTime + "] " +
           "[Movetime "  + moveTime  + "] " +
           moves ;
  
public java.lang.StringgetOffer(java.lang.String offeree)

    if (offerFlag == 0)
      return null;
    int value = offerFlag ;
    if (offeree.equals(black))
      value = offerFlag / 2;
    System.out.println("Offer was " + value);
    if (value == Game.DRAWOFFER )
      return "OfferDraw";
    else
      if (value == Game.ABORTOFFER )
        return "OfferAbort";
    return null;
  
public java.lang.StringgetPlayers()

    return white + " - " + black ;
  
public java.lang.StringgetWhite()

    return white;
  
public java.lang.StringgetWhiteId()

    return whiteId;
  
public booleanisOver()

    return gameover;
  
public synchronized booleanremoveObserver(SendThread user)

    if (kibitzer.size() > 0)
      kibitzer.removeElement(user);
    if (kibitzer.size()==0 && gameover)
      return true; //okay to delete game
    else {
      ServerMessage msg = new ServerMessage(user.portnum,"Chat",user.name.user  + " parts");
      chat(msg);
      return false;
    }
  
public voidsetInfo()

     if (gameover)
        return;
     String suspend = "";
     if (suspendCount > 0)
        suspend = " (Suspended)";
     String tomove = " Black to move";
     int move = movenum;
     if (whitetomove)   {
       move++;
       tomove = " White to move";
     }
     info = ": Move " + move + tomove + suspend;
  
public voidsetOffer(java.lang.String offerer, int Offer)


       
    offerFlag = Offer;
    if (offerer.equals(white))
      offerFlag = Offer * 2;
  
public java.lang.StringtoString()

    return white + " - " + black  + info;