home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / Game.java < prev    next >
Text File  |  1998-05-08  |  7KB  |  238 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.server;
  21.  
  22. import java.util.* ;
  23. import borland.samples.apps.chess.client.ServerMessage;
  24.  
  25. public class Game
  26. {
  27.   String white;
  28.   String moves ;
  29.   String whitesLastMove;
  30.   String black;
  31.   String info = ":";
  32.   String whiteTime;
  33.   String blackTime;
  34.   String moveTime;
  35.   String whiteId  = null;
  36.   String blackId  = null;
  37.   String filename = null;
  38.   private int suspendCount = 0;
  39.   int offerFlag = 0;
  40.   int movenum;
  41.   boolean whitetomove;
  42.   Vector kibitzer;
  43.   private String result;
  44.   private boolean gameover;
  45.  
  46.   public Game (String white, String black) {
  47.     this(white,black,"");
  48.   }
  49.  
  50.   public Game (String white, String black, String info) {
  51.     this.white = white;
  52.     this.black = black;
  53.     this.info = info;
  54.     movenum = 0;
  55.     whitetomove = true;
  56.     kibitzer =  new  Vector();
  57.     result = "";
  58.     gameover = false;
  59.     whitesLastMove = " ";
  60.     moves = " ";
  61.     whiteTime = "0";
  62.     blackTime = "0";
  63.   }
  64.  
  65.   static final int DRAWOFFER = 2;
  66.   static final int ABORTOFFER = 3;
  67.  
  68.   public void setOffer(String offerer,int Offer) {
  69.     offerFlag = Offer;
  70.     if (offerer.equals(white))
  71.       offerFlag = Offer * 2;
  72.   }
  73.   public boolean isOver() {
  74.     return gameover;
  75.   }
  76.   public String getOffer(String offeree) {
  77.     if (offerFlag == 0)
  78.       return null;
  79.     int value = offerFlag ;
  80.     if (offeree.equals(black))
  81.       value = offerFlag / 2;
  82.     System.out.println("Offer was " + value);
  83.     if (value == Game.DRAWOFFER )
  84.       return "OfferDraw";
  85.     else
  86.       if (value == Game.ABORTOFFER )
  87.         return "OfferAbort";
  88.     return null;
  89.   }
  90.   public String getWhite() {
  91.     return white;
  92.   }
  93.   public String getBlack() {
  94.     return black;
  95.   }
  96.   public String getWhiteId() {
  97.     return whiteId;
  98.   }
  99.   public String getBlackId() {
  100.     return blackId;
  101.   }
  102.  
  103.   public String getPlayers() {
  104.     return white + " - " + black ;
  105.   }
  106.  
  107.   public String toString() {
  108.     return white + " - " + black  + info;
  109.   }
  110.  
  111.   public void gameOver(String result) {
  112.     this.result = result;
  113.     gameover = true;
  114.     moves =  " [Result \"" + result + "\"]" + moves;
  115.     chat(new ServerMessage(0,"Chat","Game Over " + result));
  116.     info = " " + result + " " + movenum + " Moves"   ;
  117.   }
  118.  
  119.   public void setInfo() {
  120.      if (gameover)
  121.         return;
  122.      String suspend = "";
  123.      if (suspendCount > 0)
  124.         suspend = " (Suspended)";
  125.      String tomove = " Black to move";
  126.      int move = movenum;
  127.      if (whitetomove)   {
  128.        move++;
  129.        tomove = " White to move";
  130.      }
  131.      info = ": Move " + move + tomove + suspend;
  132.   }
  133.  
  134.   public synchronized void addMove(ServerMessage smsg) {
  135.        int index = smsg.msg.indexOf(' ');
  136.        String time = smsg.msg.substring(0,index);
  137.        String move = smsg.msg.substring(index+1);
  138.        if (whitetomove)
  139.           whiteTime = time;
  140.        else
  141.           blackTime = time;
  142.        if (!gameover)
  143.          if (whitetomove) {
  144.            movenum++;
  145.            whitetomove = false;
  146.          }
  147.          else
  148.            whitetomove = true;
  149.        setInfo();
  150.  
  151.        if (move.startsWith("@")) {
  152.           index = move.indexOf(' ') ;
  153.           moves = moves + " " + move.substring(0,index);
  154.           move = move.substring(index+1);
  155.        }
  156.        if (move.endsWith("_")) {
  157.          whitesLastMove = move.substring(0,move.length() - 1);
  158.          moves = moves + " " + whitesLastMove ;
  159.        }
  160.        else {
  161.          index = whitesLastMove.length();
  162.          if (index > 0 && move.startsWith(whitesLastMove))
  163.              moves = moves + " " + move.substring(index);
  164.          else  {
  165.            System.out.println(move + " doesn't begin like");
  166.            System.out.println(whitesLastMove + " =whites last move");
  167.            moves = moves + " " + move ;
  168.          }
  169.        }
  170.        //ServerMessage smsg = new ServerMessage(0,"Moved",movedata);
  171.        SendThread s;
  172.        for (int i=0;i< kibitzer.size();i++) {
  173.          s = (SendThread)kibitzer.elementAt(i);
  174.          System.out.println("send move to kibitzer " + s.portnum );
  175.          if (s.portnum != smsg.port )
  176.            s.addMsg(smsg);
  177.        }
  178.   }
  179.  
  180.   public boolean gameSuspended()  {
  181.     suspendCount++;
  182.     setInfo();
  183.     if (kibitzer.size()==0  && suspendCount > 1)
  184.       return true;
  185.     return false;
  186.   }
  187.  
  188.   public void gameResumed() {
  189.     suspendCount--;
  190.     setInfo();
  191.   }
  192.  
  193.   public synchronized void chat(ServerMessage smsg) {
  194.     for (int i=0;i<kibitzer.size();i++) {
  195.       SendThread s = (SendThread)kibitzer.elementAt(i);
  196.       smsg.port = 0;
  197.       s.addMsg(smsg);
  198.       //synchronized (s) {
  199.       //  s.msgque.addElement(smsg);
  200.       //  s.notify();
  201.       //}
  202.     }
  203.   }
  204.  
  205.   public String getMoves() {
  206.     return "[White \""   + white + "\"] "   +
  207.            "[Black \""   + black + "\"] "   +
  208.            "[Blacktime " + blackTime + "] " +
  209.            "[Whitetime " + whiteTime + "] " +
  210.            "[Movetime "  + moveTime  + "] " +
  211.            moves ;
  212.   }
  213.  
  214.   public synchronized void addObserver(SendThread user) {
  215.     ServerMessage smsg = new ServerMessage(user.portnum,"Chat",user.name.user + " joins");
  216.     chat(smsg);
  217.     kibitzer.addElement(user);
  218.     ServerMessage msg = new ServerMessage(user.portnum,"MoveList", getMoves());
  219.     user.addMsg(msg);
  220.     //synchronized (user) {
  221.     //  user.msgque.addElement(msg);
  222.     //  user.notify();
  223.     //}
  224.   }
  225.  
  226.   public synchronized  boolean removeObserver(SendThread user){
  227.     if (kibitzer.size() > 0)
  228.       kibitzer.removeElement(user);
  229.     if (kibitzer.size()==0 && gameover)
  230.       return true; //okay to delete game
  231.     else {
  232.       ServerMessage msg = new ServerMessage(user.portnum,"Chat",user.name.user  + " parts");
  233.       chat(msg);
  234.       return false;
  235.     }
  236.   }
  237. } //end of class Game
  238.