home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 October A
/
Pcwk10a98.iso
/
Inprise
/
TRIAL
/
JBUILDER
/
JSAMPLES.Z
/
Game.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
7KB
|
238 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
package borland.samples.apps.chess.server;
import java.util.* ;
import borland.samples.apps.chess.client.ServerMessage;
public class Game
{
String white;
String moves ;
String whitesLastMove;
String black;
String info = ":";
String whiteTime;
String blackTime;
String moveTime;
String whiteId = null;
String blackId = null;
String filename = null;
private int suspendCount = 0;
int offerFlag = 0;
int movenum;
boolean whitetomove;
Vector kibitzer;
private String result;
private boolean gameover;
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";
}
static final int DRAWOFFER = 2;
static final int ABORTOFFER = 3;
public void setOffer(String offerer,int Offer) {
offerFlag = Offer;
if (offerer.equals(white))
offerFlag = Offer * 2;
}
public boolean isOver() {
return gameover;
}
public String getOffer(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 String getWhite() {
return white;
}
public String getBlack() {
return black;
}
public String getWhiteId() {
return whiteId;
}
public String getBlackId() {
return blackId;
}
public String getPlayers() {
return white + " - " + black ;
}
public String toString() {
return white + " - " + black + info;
}
public void gameOver(String result) {
this.result = result;
gameover = true;
moves = " [Result \"" + result + "\"]" + moves;
chat(new ServerMessage(0,"Chat","Game Over " + result));
info = " " + result + " " + movenum + " Moves" ;
}
public void setInfo() {
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 synchronized void addMove(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 boolean gameSuspended() {
suspendCount++;
setInfo();
if (kibitzer.size()==0 && suspendCount > 1)
return true;
return false;
}
public void gameResumed() {
suspendCount--;
setInfo();
}
public synchronized void chat(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 String getMoves() {
return "[White \"" + white + "\"] " +
"[Black \"" + black + "\"] " +
"[Blacktime " + blackTime + "] " +
"[Whitetime " + whiteTime + "] " +
"[Movetime " + moveTime + "] " +
moves ;
}
public synchronized void addObserver(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 boolean removeObserver(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;
}
}
} //end of class Game