home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
ChessRules.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
59KB
|
1,782 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.client;
import java.io.*;
import borland.samples.apps.chess.client.board.*;
import java.util.Vector;
import java.awt.Point;
import borland.jbcl.model.*;
import borland.jbcl.util.*;
import java.util.ResourceBundle;
public class ChessRules implements WritableMatrixModel {
ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
String msg; //diagnostic
String subroutine; //diagnostic
String playerB = res.getString("BLACK"); //PGN tag value - black player's name
String playerW = res.getString("WHITE"); //PGN tag value - white player's name
String whiteElo; //PGN tag value
String blackElo; //PGN tag value
String result; //PGN tag value
String date; //PGN tag value
String round; //PGN tag value
String site; //PGN tag value
String event; //PGN tag value
String info; //extra game info
long[] playerTime = new long [2]; //private PGN tag value - time remaining
long lMoveTime; //private PGN tag value - time increment to add per move
int maxmoves = 510; //hack - use Vector instead of array
int piececount;
boolean isMate = false;
boolean isStalemate = false;
int piecetosq;
int legalmove;
KingPos[] kingPos = new KingPos[2]; //keep track of each kings position
MoveNode[][] move = new MoveNode[maxmoves][2]; //the array of moves
int movecount = -1;
public static final int Black = 1;
public static final int White = 0;
static final String pieceChar[] = {"","","N","B","R","Q","K"};
static final String fileChar[] = {"a","b","c","d","e","f","g","h"} ;
int count;
int color;
String reason;
Vector squares; //no good reason for class scope
public ChessRules() {
kingPos[0] = new KingPos(Chessboard.WHITEPIECE);
kingPos[1] = new KingPos(Chessboard.BLACKPIECE);
move[0][0] = new MoveNode();
move[0][1] = new MoveNode();
squares = new Vector();
//System.out.println("ChessRules xtor");
init();
}
public int addMove(int movesub,int clrsub,String moveNotation,int movenum,int prevsub) {
//System.out.println("ChessRules.addMove " + movesub + "clr=" + clrsub + " "+ moveNotation);
if (movesub != count ) {
count++;
move[count][0] = new MoveNode();
move[count][1] = new MoveNode();
}
movecount = count;
move[count][clrsub].movenum = movenum;
if (determinePreviousPosition(clrsub,count,prevsub)== true) {
move[movecount][clrsub].movetext = moveNotation;
int clr = Chessboard.WHITEPIECE;
int oppClr = 1;
if (clrsub != 0){
clr = Chessboard.BLACKPIECE;
oppClr = 0;
}
processmove(movecount,clr,move[movecount][clrsub]);
//System.out.println("Added " + moveNotation + "clrsub=" + clrsub + "Move" + movecount);
kingCheckCount(kingPos[oppClr],move[movecount][clrsub]) ;
}
color = clrsub;
if (color == White)
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ROW_ADDED));
else
//processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ITEM_CHANGED, new MatrixLocation(movecount, 0)));
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ITEM_CHANGED, new MatrixLocation(movecount-1, 1 + color)));
return movecount;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getLastMoveNumber() {
return movecount;
}
public int getWhoMovedLast(){
return color;
}
public String getPlayerB(){
return playerB;
}
public void setPlayerB(String name) {
playerB = name;
}
public String getWhiteElo() {
return whiteElo;
}
public void setWhiteElo(String rating) {
this.whiteElo = rating;
}
public String getBlackElo() {
return blackElo;
}
public void setBlackElo(String rating) {
this.blackElo = rating;
}
public long getMoveTimeInterval() {
return lMoveTime;
}
public long getWhiteTimeRemaining() {
return playerTime[0];
}
public long getBlackTimeRemaining() {
return playerTime[1];
}
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return result;
}
public void setDate(String date) {
this.date = date;
}
public void setEvent(String name) {
this.event = name;
}
public void setSite(String name) {
this.site = name;
}
public String getTags() {
String tag = "";
if (event != null && event.length() > 0)
tag = tag + "[Event \"" + event + "\"]\r\n";
if (site != null && site.length() > 0)
tag = tag + "[Site \"" + site + "\"]\r\n";
if (date != null && date.length() > 0)
tag = tag + "[Date \"" + date + "\"]\r\n";
if (round != null && round.length() > 0)
tag = tag + "[Round \"" + round + "\"]\r\n";
tag = tag + "[White \"" + playerW + "\"]\r\n[Black \"" + playerB +"\"]\r\n" ;
if (whiteElo != null && whiteElo.length() > 0)
tag = tag + "[WhiteElo \"" + whiteElo + "\"]\r\n";
if (whiteElo != null && blackElo.length() > 0)
tag = tag + "[BlackElo \"" + blackElo + "\"]\r\n";
if (result != null && result.length() > 0)
tag = tag + "[Result \"" + result + "\"]\r\n" ;
return tag;
}
public void setRound(String round) {
this.round = round;
}
public String getPlayerW() {
return playerW;
}
public void setPlayerW(String name) {
playerW = name;
}
public void setPieceValues(int arraySub,int color,Boardsquares pos) {
move[arraySub][color].pieceValue.init(pos);
}
public void setPieceColors(int arraySub,int color,Boardsquares pos) {
move[arraySub][color].pieceColor.init(pos);
}
public String getComment(int arraySub,int color){
return move[arraySub][color].comment;
}
public int getPreviousSubscript(int arraySub,int color){
return move[arraySub][color].prevsub;
}
public int isGameOver() {
if (isMate)
return 1;
else
if (isStalemate)
return 2;
else
return 0;
}
public String getMoveText(int arraySub,int color) {
return move[arraySub][color].movetext;
}
public void setComment(int arraySub,int color,String string) {
move[arraySub][color].comment = string;
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ITEM_CHANGED, new MatrixLocation(arraySub-1, 3)));
}
public Boardsquares getPieceValues(int arraySub,int color){
return move[arraySub][color].pieceValue;
}
public Boardsquares getPieceColors(int arraySub,int color){
return move[arraySub][color].pieceColor;
}
public void init() {
isMate = false;
isStalemate = false;
movecount = 0;
move[0][1].movenum = 0;
move[0][1].nestlevel = 0;
move[0][1].prevsub = 0;
playerTime[0] = 0;
playerTime[1] = 0;
lMoveTime = 0;
color = Black;
count = 0;
info = "";
whiteElo = "";
blackElo = "";
result=null;
date="";
round="";
site="";
event="";
}
public int getMoveNumber(int arraySub,int color){
return move[arraySub][color].movenum;
}
public void setMove(int move,int color) {
movecount = move;
this.color = color;
}
public int getArraySize() {
return count;
}
// Fill Move array by parsing an input stream
public void parsePGNFile (Reader is) throws Exception {
count = 0;
StreamTokenizer st = new StreamTokenizer(is);
if (st != null)
subroutine = "parsePGN Read";
st.wordChars(91,93) ;
st.wordChars(40,45) ;
st.wordChars(61,63);
st.wordChars('@','@');
st.wordChars('/','/');
st.wordChars('_','_');
st.wordChars('?','?');
st.wordChars('!','!');
st.wordChars('=','=');
st.whitespaceChars(10,13);
boolean boring = false;
String val = "?";
subroutine = "parsePGN loop ";
int nestlevel = 0;
int prevsub = -1;
info = "";
try {
scan:
while (true)
switch (st.nextToken()) {
case StreamTokenizer.TT_EOF:
break scan;
case StreamTokenizer.TT_WORD:
subroutine = "ParsePGN Word ";
val = st.sval;
//System.out.println("newword " + val);
move[count][color].comment = move[count][color].comment +" " +val;
if (val.charAt(0) == '{' ||
val.charAt(0) == '(' )
nestlevel++;
if (val.charAt(0) == ')' ||
val.charAt(0) == '}' )
nestlevel--;
if (val.charAt(0) == '[') {
boring = true;
if (val.equals ("[Black")) {
st.nextToken();
playerB = st.sval;
boring = false;
}
else
if (val.equals("[White")) {
st.nextToken();
playerW = st.sval;
//System.out.println("Set white to " + playerW);
boring = false;
}
else
if (val.equals("[Whitetime")) {
if (st.nextToken()==StreamTokenizer.TT_NUMBER)
playerTime[0] = (long) st.nval;
}
else
if (val.equals("[Blacktime")) {
if (st.nextToken()==StreamTokenizer.TT_NUMBER)
playerTime[1] = (long) st.nval;
}
else
if (val.equals("[Movetime")) {
if (st.nextToken()==StreamTokenizer.TT_NUMBER)
lMoveTime = (long) st.nval ;
lMoveTime = lMoveTime * 1000;
}
else {
if (val.equals("[Site")) {
st.nextToken();
site = st.sval;
boring = false;
}
else
if (val.equals("[Event")) {
st.nextToken();
event = st.sval;
boring = false;
}
else
if (val.equals("[Date")) {
st.nextToken();
date = st.sval;
boring = false;
}
else
if (val.equals("[Round")) {
st.nextToken();
round = st.sval;
boring = false;
}
else
if (val.equals("[WhiteElo")) {
st.nextToken();
whiteElo = st.sval;
boring = false;
}
else
if (val.equals("[BlackElo")) {
st.nextToken();
blackElo = st.sval;
boring = false;
}
else
if (val.equals("[Result")) {
st.nextToken();
result = st.sval;
boring = false;
}
else {
st.nextToken();
}
info = info + st.sval + "\r\n";
}
break;
}
if (val.endsWith("]") == true) {
boring = false;
}
break;
case StreamTokenizer.TT_NUMBER:
subroutine = "ParsePGN num ";
if (boring == true)
break;
int movenum = (int) st.nval;
if (movenum == 7)
boring = false;
count++ ;
move[count][1] = new MoveNode();
move[count][0] = new MoveNode();
//System.out.println("parsePGN num count = " + count);
if (count >= movenum - 1 && movenum > 0) {
if (prevsub > 0) {
move[count][0].prevsub = prevsub;
move[count][1].prevsub = prevsub;
prevsub = -1;
}
else {
move[count][0].prevsub = count -1;
move[count][1].prevsub = count;
}
move[count][0].movetext = "...";
move[count][1].movetext = "...";
move[count][0].nestlevel = nestlevel;
move[count][1].nestlevel = nestlevel;
int i=0;
//System.out.println("parsePGN num movenum = " + movenum);
while (true) {
st.nextToken();
if (st.ttype == StreamTokenizer.TT_WORD) {
if (!st.sval.equals("_") && i == 0)
move[count][1].prevsub = count;
if (i < 2) {
move[count][i].movenum = movenum;
move[count][i].movetext = st.sval;
i++;
}
else {
move[count][color].comment = move[count][color].comment +" " +st.sval;
}
if (st.sval.startsWith("@")){
//System.out.println("found variation marker " + st.sval);
if (st.sval.length() > 1){
prevsub = Integer.parseInt(st.sval.substring(1));
System.out.println("variation is a continuation of subscript " + prevsub);
}
}
}
else
if (st.ttype == StreamTokenizer.TT_NUMBER &&
st.nval == 0) {
if (i == 0 || move[count][0].movenum != 0)
i++;
}
else {
st.pushBack() ;
break;
}
}
}
default:
subroutine = "ParsePGN default ";
val = " " + st.ttype;
//System.out.println("weird char" + val);
break;
}
if (move[count][0].movetext.equals("...") && move[count][1].movetext.equals("..."))
count--;//misinterpreted the result as a move?
is.close();
//System.out.println(subroutine + count);
}
catch (Exception e) {
System.out.println(subroutine + e);
}
if (st.ttype != StreamTokenizer.TT_EOF)
throw new Exception(st.toString());
//GeneratePositions();
}
boolean determinePreviousPosition(int clr,int sub,int prevsub) {
try{
//System.out.println("CV DeterminePP");
int previousSub;
int prevColor;
int adjustment;
int i;
if (clr + sub == 0)
return true;
if (clr == 0) {
adjustment = 1;
previousSub = sub -1 ;
prevColor = 1;
}
else {
adjustment = 0;
previousSub = sub;
prevColor = 0;
}
if (prevsub != sub)
previousSub = prevsub;
for (i = previousSub;i >= 0;i--) {
//System.out.println("CV DeterminePP forloop "+ String.valueOf(sub)+ " " + String.valueOf(clr) );
if ((move[i][prevColor].movenum == (move[sub][clr].movenum - adjustment )) &&
move[i][prevColor].nestlevel <= move[sub][clr].nestlevel) {
move[sub][clr].prevsub = i;
move[sub][clr].copy(move[i][prevColor]);
//System.out.println("prev move subscript is " + i + "," + prevColor);
subroutine = "DeterminePositions" + sub;
return true;
}
}
}
catch(Exception e ) { System.out.println("Determine postions" + e);}
return false;
}
void generatePositions() {
this.color = ChessRules.Black;
subroutine = "generatePositions";
//System.out.println(subroutine);
try {
int clr;
int movenum;
int rank;
int file;
int sub = 0;
int[] color = new int[2];
color[0] = Chessboard.WHITEPIECE;
color[1] = Chessboard.BLACKPIECE;
//initialize king positions for verifing legal moves
for (file = 0;file < 8;file++)
for (rank = 0;rank < 8;rank++) {
if (move[0][1].pieceValue.value(file,rank) == Chessboard.KING){
clr = move[0][1].pieceColor.value(file,rank);
if (clr != 0)
clr = 1;
kingPos[clr].setKingPos(file,rank);
}
}
for (sub = 1;sub<=count;sub++) {
for (clr = 0;clr < 2;clr++) {
legalmove = 0;
piececount = 0;
piecetosq = 0;
if ( determinePreviousPosition(clr,sub,move[sub][clr].prevsub)== true) {
processmove(sub,color[clr],move[sub][clr]);
//statusLine.setText("fin "+sub);
if (legalmove == 0) { //diagnostic info when illegal move encountered
System.out.println("Illegal move " + move[sub][clr].movetext + move[sub][clr].comment);
int prevclr = clr;
this.color = ChessRules.White;
if (clr == 0)
prevclr = 1;
move[sub][clr].movenum = 0 ;
move[sub][clr].prevsub = -1;
move[sub+clr -1][prevclr].comment = move[sub][clr].movetext + move[sub+clr -1][prevclr].comment ;
move[sub][clr].movetext = " " ;
}
}
} //end inner for loop
} // end outer for loop
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.STRUCTURE_CHANGED));
}
catch (Exception e ) {
System.out.println("Generate positions " + e);
e.printStackTrace();
}
}
void processmove(int sub,int clr ,MoveNode Pos) {
String movestr = new String(Pos.movetext);
//System.out.println("P "+sub+" "+ clr+" " +movestr);
if (movestr.length() < 2)
return;
char tempchar;
char fromrank = ' ';
char fromfile = ' ';
char torank = ' ';
char tofile = ' ';
int clrsub = 0;
if (clr != 0)
clrsub = 1;
int oldpiece = 0; //first piece mentioned in move
int piecevalue = 0; //last piece mentioned in move e4Q
boolean take=false;
int charsub = 0;
int rank;
int file;
scan:
while (true) {
tempchar = movestr.charAt(charsub);
charsub++;
switch(tempchar) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
fromfile = tofile;
tofile = tempchar;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
fromrank = torank;
torank = tempchar;
break;
case 'x':
take = true;
break;
case 'R':
oldpiece = piecevalue;
piecevalue = Chessboard.ROOK;
break;
case 'K':
piecevalue = Chessboard.KING;
break;
case 'Q':
oldpiece = piecevalue;
piecevalue = Chessboard.QUEEN;
break;
case 'N':
oldpiece = piecevalue;
piecevalue = Chessboard.KNIGHT;
break;
case 'B':
oldpiece = piecevalue;
piecevalue = Chessboard.BISHOP;
break;
case 'O':
if (clr == Chessboard.BLACKPIECE)
rank = 7;
else
rank = 0;
if (movestr.regionMatches(charsub-1,"O-O-O",0,5)) {
if (Pos.qCastleForfeited[clrsub]== false &&
kingPos[clrsub].isInCheck() == false) {
Pos.assign(0,rank,0,0);
Pos.assign(3,rank,Chessboard.ROOK,clr);
Pos.assign(2,rank,Chessboard.KING,clr);
Pos.assign(4,rank,0,0);
Pos.qCastleForfeited[clrsub] = true;
kingPos[clrsub].setKingPos(2,-1);
legalmove++;
}
return;
}
else
if (movestr.regionMatches(charsub-1,"O-O",0,3)){
if (Pos.kCastleForfeited[clrsub]== false &&
kingPos[clrsub].isInCheck() == false) {
Pos.kCastleForfeited[clrsub] = true;
Pos.assign(4,rank,0,0);
Pos.assign(5,rank,Chessboard.ROOK,clr);
Pos.assign(6,rank,Chessboard.KING,clr);
Pos.assign(7,rank,0,0);
kingPos[clrsub].setKingPos(6,-1);
legalmove++;
}
return;
}
break scan;
default:
break;
}
if (tempchar != ' ' && piecevalue == 0)
piecevalue = Chessboard.PAWN;
if (charsub == movestr.length() )
break scan;
}
if (oldpiece > Chessboard.PAWN)
return;
else
if (oldpiece == 0)
oldpiece = piecevalue;
if (tofile == ' ' || torank == ' ')
return;
rank = getRankSubscript(torank);
file = getFileSubscript(tofile);
if (oldpiece == Chessboard.KING){
kingPos[clrsub].setKingPos(file,rank);
}
if (oldpiece == Chessboard.PAWN) {
if ( fromfile == ' ')
fromfile = tofile;
}
if (findFromSquare(Pos,kingPos[clrsub],piecevalue,oldpiece,clr,sub,fromrank,fromfile,rank,file))
Pos.assign(file,rank,piecevalue,clr);
else {
System.out.println("No move" + movestr);
//statusLine.setText("No move "+movestr);
}
}
boolean findFromSquare(
MoveNode Pos,KingPos king,int piecevalue,int oldpiece,
int clr,int sub,char fromrank,char fromfile,
int toranksub,int tofilesub) {
//fromfile may or may not contain the file the piece came from
//fromrank may or may not contain the rank the piece came from
//System.out.println("FFromSquare "+oldpiece+clr+sub+fromrank+fromfile+toranksub+tofilesub);
int startrank = 0;
int startfile = 0;
int endfile = 7;
int endrank = 7;
int rank;
int file;
if (fromrank != ' ') {
startrank = getRankSubscript(fromrank);
endrank = startrank;
}
if (fromfile != ' ') {
startfile = getFileSubscript(fromfile);
endfile = startfile;
}
for (rank=startrank;rank <=endrank;rank++) {
for (file = startfile;file <=endfile;file++) {
if (Pos.pieceValue.value(file,rank) == oldpiece &&
Pos.pieceColor.value(file,rank) == clr) {
piececount++;
if (isLegalMove(Pos,king,sub,oldpiece,piecevalue,clr,rank,file,toranksub,tofilesub)) {
legalmove++;
Pos.assign(file,rank,0,0);
if (Pos.enpassantFile == -2)
Pos.assign(tofilesub,rank,0,0);
return true;
}
}
}
}
return false;
}
boolean isValidMove(MoveNode Pos,int sub,int piece,int clr,int rank,
int file,int toranksub,int tofilesub) {
int rankdiff = rank-toranksub;
int rankdir = -1;
int filedir = -1;
int lowfile;
int highfile;
int ranksub;
int filesub;
int i,j;
int colorsub = 0 ;
int eprank = 5;
if (clr == Chessboard.BLACKPIECE){
eprank = 2;
colorsub = 1;
}
boolean returnval = false;
if (rankdiff < 0) {
rankdir = 1;
rankdiff = toranksub - rank;
}
int filediff = file-tofilesub;
if (filediff < 0) {
filediff = tofilesub - file;
filedir = 1;
}
int enpassantFile = -1;
if (Pos.pieceValue.value(tofilesub,toranksub) > 0 &&
Pos.pieceColor.value(tofilesub,toranksub) == clr)
return false; //can't take your own pieces
switch(piece) {
case Chessboard.PAWN:
if (filediff > 1)
break;
else
if (filediff == 1 && rankdiff == 1)
if (Pos.pieceValue.value(tofilesub,toranksub) == 0) {
//System.out.println("isValidMove - Pos.enpassantFile=" + Pos.enpassantFile);
if (toranksub == eprank &&
clr != Pos.pieceColor.value(tofilesub,rank) &&
Pos.pieceValue.value(tofilesub,rank) == Chessboard.PAWN &&
Pos.enpassantFile == tofilesub) {
enpassantFile = -2;
returnval = true;
break;
}
else
break;
}
boolean capture = (Pos.pieceValue.value(tofilesub,toranksub) != 0);
if (filediff == 0 && capture)
break;
if (filediff == 1 && !capture )
break;
if (rankdiff != 1 && filediff == 1)
break;
if (capture && Pos.pieceColor.value(tofilesub,toranksub) == clr)
break;
if (clr == Chessboard.BLACKPIECE) {
if (rank -1 == toranksub) {
returnval = true;
break;
}
if (rank == 6 && toranksub ==4 &&
Pos.pieceValue.value(file,5)==0) {
enpassantFile = tofilesub;
//System.out.println("enpassant possibility next move");
returnval = true;
break;
}
}
else {
if (rank + 1 == toranksub) {
returnval = true;
break;
}
if (rank == 1 && toranksub == 3 &&
Pos.pieceValue.value(file,2)==0) {
enpassantFile = tofilesub;
//System.out.println("enpassant possibility next move");
returnval = true;
break;
}
}
break;
case Chessboard.KNIGHT:
if ((rankdiff ==2 && filediff == 1) ||
(rankdiff ==1 && filediff == 2)) {
returnval = true;
break;
}
break;
case Chessboard.KING:
if (rankdiff < 2 && filediff <2)
returnval = true;
else
if (rankdiff == 0 && filediff == 2 && (toranksub == 0 || toranksub == 7))
if (tofilesub == 2 && Pos.pieceValue.value(3,toranksub) == 0 &&
Pos.pieceValue.value(2,toranksub) == 0 &&
Pos.pieceValue.value(1,toranksub) == 0 &&
Pos.pieceValue.value(0,toranksub) == Chessboard.ROOK &&
Pos.qCastleForfeited[colorsub] == false &&
kingPos[colorsub].isInCheck() == false)
returnval = true;
else
if (tofilesub == 6 && Pos.pieceValue.value(5,toranksub) == 0 &&
Pos.pieceValue.value(6,toranksub) == 0 &&
Pos.pieceValue.value(7,toranksub) == Chessboard.ROOK &&
Pos.kCastleForfeited[colorsub] == false &&
kingPos[colorsub].isInCheck() == false)
returnval = true;
break;
case Chessboard.ROOK:
if (rankdiff == 0 ) {
for (i=file+filedir;i!=tofilesub;i=i+filedir) {
if (Pos.pieceValue.value(i,rank) != 0)
return false;
}
returnval = true;
break;
}
else {
if (filediff == 0) {
for (i=rank+rankdir;i!=toranksub;i=i+rankdir) {
if (Pos.pieceValue.value(file,i) != 0)
return false;
}
returnval = true;
break;
}
}
break;
case Chessboard.BISHOP:
if (filediff == rankdiff) {
for (i=1;i<filediff;i++) {
ranksub = rank + rankdir;
filesub = file + filedir;
if (Pos.pieceValue.value(filesub,ranksub) != 0)
return false;
}
returnval = true;
}
break;
case Chessboard.QUEEN:
if (filediff == rankdiff) {
for (i=1;i<filediff;i++) {
ranksub = rank + rankdir;
filesub = file + filedir;
if (Pos.pieceValue.value(filesub,ranksub) != 0)
return false;
}
returnval = true;
}
else
if (rankdiff == 0) {
for (i=file+filedir;i!=tofilesub;i=i+filedir) {
if (Pos.pieceValue.value(i,rank) != 0)
return false;
}
returnval = true;
}
else {
if (filediff == 0) {
for (i=rank+rankdir;i!=toranksub;i=i+rankdir) {
if (Pos.pieceValue.value(file,i) != 0)
return false;
}
returnval = true;
}
}
break;
}//end of switch statment
Pos.enpassantFile = enpassantFile;
return returnval;
}
boolean isLegalMove(MoveNode Pos,KingPos king,int sub,int piece,
int newpiece,int clr,int rank,int file,
int toranksub,int tofilesub) {
boolean rc = true;
int tmpp = Pos.enpassantFile;
if (isValidMove(Pos,sub,piece,clr,rank,file,toranksub,tofilesub)){
int toPiece = Pos.pieceValue.value(tofilesub,toranksub);
int toColor = Pos.pieceColor.value(tofilesub,toranksub);
Pos.assign(file,rank,0,0);
Pos.assign(tofilesub,toranksub,newpiece,clr);
int pieceTaken = 0;
int pieceColor = 0;
if (Pos.enpassantFile == -2) {
System.out.println("En Passant!");
pieceTaken = Pos.pieceValue.value(tofilesub,rank);
pieceColor = Pos.pieceColor.value(tofilesub,rank);
Pos.assign(tofilesub,rank,0,0);
}
if (isLegalPosition(Pos,king,sub,piece,clr,rank,file,toranksub,tofilesub))
rc = true;
else {
//System.out.println("isLegalPosition=false");
rc = false;
}
Pos.assign(file,rank,piece,clr);
Pos.assign(tofilesub,toranksub,toPiece,toColor);
if (Pos.enpassantFile == -2)
Pos.assign(tofilesub,rank,pieceTaken,pieceColor);
}
else {
//System.out.println("isValidMove=false");
rc = false;
}
//if (tmpp != Pos.enpassantFile)
// System.out.println("ep file changed " + piece + clr + rank + file + toranksub + tofilesub);
return rc;
}
boolean isLegalPosition(MoveNode Pos,KingPos king,int sub,int piece,int clr,
int rank,int file,int toranksub,int tofilesub) {
int colorsub = 0;
if (clr == Chessboard.BLACKPIECE)
colorsub = 1;
//now make sure there is no discovered check created;
// System.out.println("legalm "+sub+piece+clr+rank+file+toranksub+tofilesub);
if (Pos.pieceValue.value(king.getKingFile(),king.getKingRank()) != Chessboard.KING )
findKing(clr,colorsub,Pos);
piecetosq++;
int rankdiff;
int rankdir;
int filediff;
int filedir;
if (piece != Chessboard.KING ) {
if (king.isInCheck()) { //was king in check ?
//System.out.println("King was in check");
if (kingCheckCount(king,Pos)) { //is it still in check?
System.out.println("King is still in check");
return false;
}
else
return true;
}
else {
//System.out.println("King was safe before");
//just need to make sure a discovered check was not created
rankdiff = rank-kingPos[colorsub].getKingRank();
rankdir = 1;
filedir = 1;
if (rankdiff < 0) {
rankdir = -1;
rankdiff = kingPos[colorsub].getKingRank() - rank;
}
filediff = file - kingPos[colorsub].getKingFile();
if (filediff < 0) {
filediff = kingPos[colorsub].getKingFile() - file;
filedir = -1;
}
int badpiece = 9;//just in case
if (filediff == 0) {
filedir = 0;
badpiece = Chessboard.ROOK;
}
if (rankdiff == 0) {
rankdir = 0;
badpiece = Chessboard.ROOK;
}
if (filediff == rankdiff)
badpiece = Chessboard.BISHOP;
if (filediff == rankdiff || rankdiff == 0 || filediff == 0) {
msg = 'K'+fileChar[kingPos[colorsub].getKingFile()]+String.valueOf(kingPos[colorsub].getKingRank()+1) ;
return notInCheck(Pos,king,file,rank,tofilesub,toranksub,filedir,rankdir,kingPos[colorsub].getKingColor(),badpiece);
}
}//end looking for discovered check
}
else {
rankdir = toranksub - rank;
filedir = tofilesub - file;
if (filedir == 2) //king moves 2 squares when castling
filedir = 1;
if (filedir == -2)
filedir = -1;
while (true) {
rank = rank + rankdir;
file = file + filedir;
kingPos[colorsub].setKingPos(file,rank);
if (kingCheckCount(kingPos[colorsub],Pos)) {
//System.out.println("King can't move there");
return false;
}
if (file == tofilesub)
return true;
}
}
return true;
}
boolean kingCheckCount(KingPos king,MoveNode Pos) {
//System.out.println("Is King Safe at " + king.GetKingFile() + king.GetKingRank() + "?");
king.kingInCheck(false); //initialize
int file = king.getKingFile();
int rank = king.getKingRank();
msg = fileChar[file]+String.valueOf(rank+1) ;
int clr = king.getKingColor();
int filesub;
int ranksub;
int i;
int j;
int badpiece;
for (i=-1;i<2;i++)
for (j=-1;j<2;j++) {
if (i!= 0 && j != 0) {
filesub = file + i;
ranksub = rank + (2* j);
if (Pos.pieceValue.value(filesub,ranksub) == Chessboard.KNIGHT &&
Pos.pieceColor.value(filesub,ranksub) != clr ) {
king.setAttacker(Chessboard.KNIGHT,filesub,ranksub);
}
else {
filesub = file +(2 * i);
ranksub = rank + j ;
if (Pos.pieceValue.value(filesub,ranksub) == Chessboard.KNIGHT &&
Pos.pieceColor.value(filesub,ranksub) != clr )
king.setAttacker(Chessboard.KNIGHT,filesub,ranksub);
}
}
if (i!= 0 || j!= 0) {
if (i==0 || j == 0)
badpiece = Chessboard.ROOK;
else
badpiece = Chessboard.BISHOP;
if (notInCheck(Pos,king,file,rank,file,rank,i,j,clr,badpiece)==false) {
filesub = file;
ranksub = rank ;
findattacker:
while(true){
filesub = filesub + i;
ranksub = ranksub + j ;
int attacker;
if ((attacker = Pos.pieceValue.value(filesub,ranksub))!= 0) {
king.setAttacker(attacker,filesub,ranksub);
break findattacker;
}
}
}
}
}
//System.out.println("Is King Safe at " + king.GetKingFile() + king.GetKingRank() + "? " +!king.isInCheck());
return king.isInCheck();
}
public void checkForStalemate(MoveNode pos,KingPos king,int clr) {
if (clr != 0)
clr = Chessboard.BLACKPIECE;
for (int rank=0; rank < 8; rank++) {
for (int file = 0; file < 8; file++) {
if (pos.pieceColor.value(file,rank) == clr) {
int piece = pos.pieceValue.value(file,rank);
possibleSquares(file,rank,pos);
for (int i = 0; i < squares.size(); i++) {
Point potentialSquare = (Point) squares.elementAt(i);
//System.out.println("is " + piece + "(" + file + "," + rank + ") to "
// + potentialSquare.x + "," + potentialSquare.y + "a legal move?" );
if (isLegalMove(pos,king,0,piece,piece,clr,rank,file,potentialSquare.y,
potentialSquare.x))
return ;
}
}
}
}
System.out.println("Stalemate");
isStalemate = true;
}
void possibleSquares(int file,int rank,MoveNode pos) {
int piece = pos.pieceValue.value(file,rank);
int clr = pos.pieceColor.value(file,rank);
int rankLowLimit = 0;
int rankHighLimit = 7;
int fileLowLimit = 0;
int fileHighLimit = 7;
squares.removeAllElements();
switch (piece) {
case Chessboard.PAWN:
int dir = 1;
if (clr == Chessboard.BLACKPIECE)
dir = -1;
squares.addElement(new Point(file,rank + dir));
if (file != 0)
squares.addElement(new Point(file -1,rank + dir));
if (rank != 7)
squares.addElement(new Point(file +1,rank + dir));
if ((rank == 6 && dir == -1) || (rank == 1 && dir == 1))
squares.addElement(new Point(file,rank + dir + dir));
break;
case Chessboard.KNIGHT:
int rankdiff;
for (int i= file-2; i< file + 3; i++) {
if (i >= 0 && i != file && i < 8) {
rankdiff = ((file + i + 1)/2) - ((file+i) /2) + 1;
if (i >= 0 && i < 8 && rank + rankdiff < 8)
squares.addElement(new Point(i,rank + rankdiff));
if (i >= 0 && i < 8 && rank - rankdiff >= 0)
squares.addElement(new Point(i,rank - rankdiff));
}
}
break;
case Chessboard.KING:
rankLowLimit = 0;
rankHighLimit = 7;
fileLowLimit = 0;
fileHighLimit = 7;
if (rank > 0)
rankLowLimit = rank -1;
if (rank != 7)
rankHighLimit = rank + 1;
if (file > 0)
fileLowLimit = file -1;
if (file != 7)
fileHighLimit = file + 1;
case Chessboard.QUEEN:
case Chessboard.ROOK:
if (piece != Chessboard.KING) {
rankLowLimit = 0;
rankHighLimit = 7;
fileLowLimit = 0;
fileHighLimit = 7;
}
int i;
for (i = rankLowLimit; i <= rankHighLimit; i++)
if (i != rank)
squares.addElement(new Point(file,i));
for (i = fileLowLimit; i <= fileHighLimit; i++)
if (i != file)
squares.addElement(new Point(i,rank));
if (piece == Chessboard.ROOK)
break;
case Chessboard.BISHOP:
if (piece != Chessboard.KING) {
rankLowLimit = 0;
rankHighLimit = 7;
fileLowLimit = 0;
fileHighLimit = 7;
if (rank > file)
rankLowLimit = rank - file;
else {
fileLowLimit = file - rank ;
rankHighLimit = 7 - file + rank;
}
}
int fileSub = fileLowLimit;
for (i=rankLowLimit; i<= rankHighLimit; i++){
if (i != rank && fileSub < 8)
squares.addElement(new Point(fileSub,i));
fileSub++;
}
if (piece != Chessboard.KING) {
rankLowLimit = 0;
rankHighLimit = 7;
fileLowLimit = 0;
fileHighLimit = 7;
if (rank + file < 7) {
rankHighLimit = rank + file;
fileHighLimit = rank + file;
}
else
rankLowLimit = file + rank - 7;
}
fileSub = fileHighLimit;
for (i=rankLowLimit; i<= rankHighLimit; i++){
if (i != rank && fileSub >= 0)
squares.addElement(new Point(fileSub,i));
fileSub--;
}
break;
}
}
boolean isMated(KingPos king,MoveNode Pos) {
boolean[][] safesquare = new boolean[3][3];
//System.out.println("isMated?" + king.getKingColor());
int rank = king.getKingRank();
int file = king.getKingFile();
int KRank;
int KFile;
int i;
int j;
KingPos temp = new KingPos(king.getKingColor());
for (i= 0;i<3;i++){
KRank = rank - 1 + i;
for (j=0;j<3;j++){
KFile = file - 1 + j;
if (KRank < 0 || KRank > 7 || KFile < 0 || KFile > 7)
safesquare [i][j] = false;//cannot flee off the board
else
if (Pos.pieceValue.value(KFile,KRank) != 0 &&
Pos.pieceColor.value(KFile,KRank) == king.getKingColor()) {
//System.out.println("cannot flee to" + KFile + KRank);
safesquare [i][j] = false; //can't move there
}
else
safesquare [i][j] = true; //so far
if (safesquare [i][j] == true && (i !=1 || j!= 1)) {
temp.setKingPos(KFile,KRank);
if (!kingCheckCount(temp,Pos))
return false;
}
}
}
return !canBlock(king,Pos);
}
boolean canBlock(KingPos king,MoveNode Pos) {
//System.out.println("canBlock " + king.kingCheckCount);
if (king.isDoubleCheck()) {
return false;
}
int clr = king.getKingColor();
KingPos temp = new KingPos(clr);
temp.setKingPos(king.getKingFile(),king.getKingRank());
int attacker = king.getAttacker();
int BlockRank = king.getAttackerRank();
int BlockFile = king.getAttackerFile();
int fileinc = king.getKingFile() - BlockFile;
int rankinc = king.getKingRank() - BlockRank;
if (fileinc > 1) fileinc = 1;
if (rankinc < -1) rankinc = -1;
if (rankinc > 1) rankinc = 1;
if (fileinc < -1) fileinc = -1;
//look on each square and see if there is a piece that can
//make a legalmove to one of the squares that would block the check (or take the piece)
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++){
int piece = Pos.pieceValue.value(i,j);
if (piece != 0 && Pos.pieceColor.value(i,j) == clr &&
piece != Chessboard.KING) {
int rank = BlockRank;
int file = BlockFile;
//System.out.println("Can " + piece + " at " + i + j + " block at " + file + rank);
while(true) {
if (!temp.isInCheck())
temp.setAttacker(attacker,BlockFile,BlockRank);
if (isLegalMove(Pos,temp,movecount,piece,piece,clr,j,i,rank,file)){
//System.out.println("Can Block=yes" );
return true;
}
if (attacker == Chessboard.KNIGHT)
break;
rank = rank + rankinc;
file = file + fileinc;
if (king.kingPosEquals(file,rank))
break; //tried all the blocking squares
}
}//end if
}
}
return false;
}
boolean notInCheck(MoveNode Pos,KingPos king,int file,int rank,int tofilesub,
int toranksub,int filedir,int rankdir,int clr,int badpiece){
int i;
int ranksub;
int filesub;
int pawnDir = -1;
if (king.getKingColor() == Chessboard.BLACKPIECE)
pawnDir = +1;
ranksub = king.getKingRank() + rankdir;
filesub = king.getKingFile() + filedir;
for (i=0;i<7;i++){
if (ranksub > 7 || ranksub < 0 || filesub < 0 || filesub > 7)
return true;
if (ranksub == toranksub && filesub == tofilesub)
return true;
msg = msg + " " +fileChar[filesub]+String.valueOf(ranksub+1);
if (Pos.pieceValue.value(filesub,ranksub) != 0 &&
(filesub != file || ranksub != rank)) {
if (Pos.pieceColor.value(filesub,ranksub) != clr){
if (Pos.pieceValue.value(filesub,ranksub) == Chessboard.QUEEN ||
Pos.pieceValue.value(filesub,ranksub) == badpiece) {
msg = "Discovered check "+fileChar[filesub]+String.valueOf(ranksub+1)+
"-"+fileChar[file]+String.valueOf(rank+1);
System.out.println(msg);
return false;
}
else
if (Pos.pieceValue.value(filesub,ranksub) == Chessboard.PAWN &&
ranksub + pawnDir == rank &&
(filesub - king.getKingFile() == 1 || king.getKingFile() - filesub == 1)) {
//System.out.println("Pawn Check?");
return false;
}
else
if (Pos.pieceValue.value(filesub,ranksub) == Chessboard.KING &&
filesub - king.getKingFile() <= 1 && king.getKingFile()-filesub <= 1 &&
ranksub - king.getKingRank() <= 1 && king.getKingRank()-ranksub <= 1) {
//System.out.println("Kings too close?");
return false;
}
else
return true;
}
else
if (Pos.pieceValue.value(filesub,ranksub) != Chessboard.KING)
return true;
}
ranksub = ranksub + rankdir;
filesub = filesub + filedir;
}
return true;
}
void findKing(int Kcolor,int sub,MoveNode Pos) {
//find the king so we can tell what pieces are pinned
int i = 0;
int j = 0;
for (i=0;i<8;i++) {
for (j=0;j<8;j++) {
if (Pos.pieceValue.value(i,j) == Chessboard.KING &&
Pos.pieceColor.value(i,j) == Kcolor) {
kingPos[sub].setKingPos(i,j);
return;
}
}
}
}
int getFileSubscript(int file) {
switch (file) {
case 'a':
file = 0;
break;
case 'b':
file = 1;
break;
case 'c':
file = 2;
break;
case 'd':
file = 3;
break;
case 'e':
file = 4;
break;
case 'f':
file = 5;
break;
case 'g':
file = 6;
break;
case 'h':
file = 7;
break;
default:
file = 8;
break;
}
return file;
}
int getRankSubscript(char rank) {
switch (rank) {
case '1':
rank = 0;
break;
case '2':
rank = 1;
break;
case '3':
rank = 2;
break;
case '4':
rank = 3;
break;
case '5':
rank = 4;
break;
case '6':
rank = 5;
break;
case '7':
rank = 6;
break;
case '8':
rank = 7;
break;
default:
rank = 8;
break;
}
return rank;
}
public boolean claimDraw() {
int oppColor = Chessboard.WHITEPIECE;
if (color == 0)
oppColor = Chessboard.BLACKPIECE;
reason = "because the applet screwed up";
boolean rc = false;
if (move[movecount][color].pieceValues(oppColor) < 5) {
rc = true;
reason = "Insufficient material" ;
}
else {
int piececount = move[movecount][color].pieceCount();
int fiftyMoveCount = 1;
int matchingPos = 1;
String MatchingMsg= "Duplicate positions on moves " + movecount;
for (int i= movecount-1;i>0;i--) {
if (move[movecount][color].matchingPawnSkeleton(move[i][color]) &&
move[i][color].pieceCount() == piececount) {
fiftyMoveCount++;
if (move[i][color].equals(move[movecount][color])) {
matchingPos++;
MatchingMsg = MatchingMsg + ", " + i;
//System.out.println("match-" + movecount + color + " " + i + color);
if (matchingPos == 3) {
reason = MatchingMsg;
rc = true;
break;
}
}
}
else
break;
}
if (fiftyMoveCount > 50){
rc = true;
reason = "50 move rule" ;
}
}
return rc;
}
public String getReason() {
return reason;
}
public boolean moveInfo(MoveTuple tuple,int sideThatMadeMove,boolean mustMoveOwnPieces ,boolean checkOnly) {
//System.out.println("ChessRules.moveInfo, sideThatMadeMove =" + sideThatMadeMove + "mustMoveOwnPieces =" + mustMoveOwnPieces + "tuple=" + tuple.toString());
if (tuple.fromFile == tuple.toFile &&
tuple.fromRank == tuple.toRank) {
reason = "No move";
}
else {
int slop = 0;
int clr = 1;
if (tuple.pieceColor == 0) {
slop = 1;
clr = 0;
}
int sub = movecount;
MoveNode Pos = new MoveNode();
Pos.copy(move[movecount][color]) ;
if (clr != color && (mustMoveOwnPieces == false || clr == sideThatMadeMove)){
if (isLegalMove(Pos,kingPos[clr],sub,tuple.pieceValue,tuple.newpieceValue,tuple.pieceColor,
tuple.fromRank,tuple.fromFile,tuple.toRank,tuple.toFile)) {
if (!checkOnly)
newMove(tuple,Pos) ;
if (color == White)
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ROW_ADDED));
else {
//processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ITEM_CHANGED, new MatrixLocation(movecount, 0)));
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.ITEM_CHANGED, new MatrixLocation(movecount-1, 1 + color)));
}
return true;
}
else {
reason = "not legal " + pieceChar[tuple.pieceValue]
+ fileChar[tuple.toFile]
+ String.valueOf(tuple.toRank+1) + " " + msg;
msg = " ";
}
}
else {
if (clr != color )
reason = "not your turn "+ String.valueOf(clr)+String.valueOf(color)+String.valueOf(sideThatMadeMove);
else {
String whoseturn;
if (color == White)
whoseturn = new String("White's");
else
whoseturn = new String("Black's");
reason ="It's " + whoseturn + " turn. "+ pieceChar[tuple.pieceValue]+fileChar[tuple.toFile]+String.valueOf(tuple.toRank+1);
}
}
}
System.out.println("ChessRules.moveInfo returning false " + reason);
return false;
}
public void newMove(MoveTuple tuple,MoveNode Pos) {
try {
//System.out.println("ChessRules.newMove");
int slop = 0;
int clr = 1;
if (tuple.pieceColor == 0) {
slop = 1;
clr = 0;
}
String moveChar=" ";
int i;
int j;
int sub = movecount;
//MoveNode Pos = new MoveNode();
//Pos.copy(move[movecount][color]) ;
String fromChar = "";
String fromPiece = pieceChar[tuple.pieceValue];
String takeChar = "";
if (move[movecount][color].pieceValue.value(tuple.toFile,tuple.toRank)!= 0)
takeChar = "x";
msg = " ";
if (tuple.pieceValue == Chessboard.PAWN) {
//System.out.println("Enpassant possibility = " + Pos.enpassantFile);
if (tuple.fromFile != tuple.toFile) {
takeChar = "x";
fromChar = fileChar[tuple.fromFile] ;
if (Pos.enpassantFile == -2)
Pos.assign(tuple.toFile,tuple.fromRank,0,0); //en passant
}
moveChar = fromChar + takeChar + fileChar[tuple.toFile] + String.valueOf(tuple.toRank + 1);
}
else {
if (tuple.pieceValue == Chessboard.KING ) {
Pos.qCastleForfeited[clr] = true;
Pos.kCastleForfeited[clr] = true;
if (tuple.toFile-tuple.fromFile == 2) {
Pos.assign(7,tuple.fromRank,0,0);
Pos.assign(5,tuple.fromRank,Chessboard.ROOK,tuple.pieceColor);
moveChar = "O-O";
}
else {
if (tuple.fromFile-tuple.toFile == 2) {
Pos.assign(0,tuple.fromRank,0,0);
Pos.assign(3,tuple.fromRank,Chessboard.ROOK,tuple.pieceColor);
moveChar = "O-O-O";
}
else
moveChar = fromPiece + takeChar + fileChar[tuple.toFile] + String.valueOf(tuple.toRank + 1);
}
}
else {
if (tuple.pieceValue == Chessboard.ROOK ) {
if (tuple.fromFile == 0 ) {
if (tuple.fromRank == 0)
Pos.qCastleForfeited[0] = true;
else
if (tuple.fromRank == 7)
Pos.qCastleForfeited[1] = true;
}
else {
if (tuple.fromFile == 7 )
if (tuple.fromRank == 0)
Pos.kCastleForfeited[0] = true;
else
if (tuple.fromRank == 7)
Pos.kCastleForfeited[1] = true;
}
}
//System.out.println("Check for ambigous notation");
forloops:
for (i=0;i<8;i++) { //check if there are two pieces that can move to the ending square
for (j=0;j<8;j++) {
if (Pos.pieceValue.value(i,j) == tuple.pieceValue
&& (i!=tuple.fromFile || j != tuple.fromRank)
&& (Pos.pieceColor.value(i,j) == tuple.pieceColor))
if (isLegalMove(Pos,kingPos[clr],sub,tuple.pieceValue,tuple.newpieceValue,
tuple.pieceColor,j,i,tuple.toRank,tuple.toFile)) {
if (tuple.fromFile == i)
fromChar = String.valueOf( 1+tuple.fromRank);
else
fromChar = fileChar[tuple.fromFile];
System.out.println("Supplemented the notation to avoid Ambiguous notation " + fromChar);
break forloops;
}
}
}
moveChar = fromPiece + fromChar + takeChar + fileChar[tuple.toFile] + String.valueOf(tuple.toRank + 1);
}
}
Pos.assign(tuple.fromFile,tuple.fromRank,0,0);
Pos.assign(tuple.toFile,tuple.toRank,tuple.newpieceValue,tuple.pieceColor);
int newcount;
if (movecount != count)
newcount = count + 1; //a new line
else
newcount = count + slop; //a new line if last move was by black
//System.out.println(String.valueOf(newcount)+" "+String.valueOf(count)+" "+String.valueOf(clr)+" ");
if (newcount > count) {
//System.out.println("initializing move array at subscript "+ newcount);
move[newcount][0] = new MoveNode();
move[newcount][1] = new MoveNode();
count++;
}
//System.out.println("bc Enpassant possibility = " + Pos.enpassantFile);
move[newcount][clr].copy(Pos);
if (tuple.newpieceValue != tuple.pieceValue)
moveChar = moveChar + pieceChar[tuple.newpieceValue] ;
if (( kingCheckCount(kingPos[color],Pos))) {
moveChar = moveChar + '+';
if (isMate = isMated(kingPos[color],Pos)) {
moveChar = moveChar + '+';
}
}
else {
//System.out.println("cfs Enpassant possibility = " + Pos.enpassantFile);
checkForStalemate(Pos,kingPos[color],color);
}
move[newcount][clr].movenum = move[movecount][slop].movenum + slop;
move[newcount][clr].nestlevel = move[movecount][clr].nestlevel;
move[newcount][clr].prevsub = movecount;
move[newcount][clr].movetext = moveChar;
//System.out.println("Enpassant possibility ("+ newcount + ","+clr+ ") = " + move[newcount][clr].enpassantFile);
int oldmovesub = newcount;
if (movecount != count) // we be starting a variation
oldmovesub = movecount;
movecount = newcount;
color = clr;
}
catch (Exception e) {
e.printStackTrace();
}
return;
}
public boolean canSet(int row, int col, boolean edit) {
if (row == count &&(col == 1 || col == 2 ))
return true;
if (col ==3)
return true;
return false;
// borland.jbcl.model.WritableMatrixModel method;
}
public void set(int parm1, int parm2, Object parm3) {
// borland.jbcl.model.WritableMatrixModel method;
}
public void touched(int parm1, int parm2) {
// borland.jbcl.model.WritableMatrixModel method;
}
public boolean isVariableRows() {
return true;
// borland.jbcl.model.WritableMatrixModel method;
}
public void addRow() {
// borland.jbcl.model.WritableMatrixModel method;
}
public void addRow(int parm1) {
// borland.jbcl.model.WritableMatrixModel method;
}
public void removeRow(int parm1) {
// borland.jbcl.model.WritableMatrixModel method;
}
public void removeAllRows() {
// borland.jbcl.model.WritableMatrixModel method;
}
public boolean isVariableColumns() {
return false;
}
public void addColumn() {
// borland.jbcl.model.WritableMatrixModel method;
}
public void addColumn(int parm1) {
// borland.jbcl.model.WritableMatrixModel method;
}
public void removeColumn(int parm1) {
// borland.jbcl.model.WritableMatrixModel method;
}
public void removeAllColumns() {
// borland.jbcl.model.WritableMatrixModel method;
}
public void enableModelEvents(boolean enable) {
if (events != enable) {
events = enable;
if (enable)
processModelEvent(new MatrixModelEvent(this, MatrixModelEvent.STRUCTURE_CHANGED));
}
}
public Object get(int row, int col) {
// borland.jbcl.model.MatrixModel method;
if (count > 0)
row++;
if (col == 0)
return String.valueOf(getMoveNumber(row,White)) + '.';
if (col == 1)
return getMoveText(row,White);
if (col == 2)
return getMoveText(row,Black);
if (col == 3)
return getComment(row,White) ;
if (col == 4)
return move[row][White];
if (col == 5)
return move[row][Black];
return null;
}
public MatrixLocation find(Object parm1) {
// borland.jbcl.model.MatrixModel method;
return null;
}
public int getRowCount() {
return count;
// borland.jbcl.model.MatrixModel method;
}
public int getColumnCount() {
return 4;
// borland.jbcl.model.MatrixModel method;
}
public void addModelListener(MatrixModelListener listener) {
modelListeners.add(listener);
// borland.jbcl.model.MatrixModel method;
}
public void removeModelListener(MatrixModelListener listener) {
modelListeners.remove(listener);
// borland.jbcl.model.MatrixModel method;
}
protected void processModelEvent(MatrixModelEvent e) {
if (events && modelListeners.hasListeners())
modelListeners.dispatch(e);
}
private EventMulticaster modelListeners = new EventMulticaster();
private boolean events = true;
}