47755 (588501), страница 16
Текст из файла (страница 16)
}
}
}
rdgtChessboard.java
---
class rdgtChessboard {
Integer address;
rdgtHistory history = new rdgtHistory(this);
rdgtSnapshot snapshot = new rdgtSnapshot(this);
rdgtFenhax myFenhax = new rdgtFenhax();
boolean alive;
boolean dumped;
boolean updated;
rdgtDatabase db;
rdgtChessBoardForm board = null;
public rdgtChessboard(Integer _address, rdgtDatabase _db) {
address = _address;
db = _db;
alive = true;
dumped = false;
updated = false;
update_html();
board = new rdgtChessBoardForm();
new Thread(new Runnable() {
public void run() {
board.setVisible(true);
}
}).start();
}
rdgtSnapshot get_snapshot() {
return snapshot;
}
int get_address() {
return address.intValue();
}
public void print_position() {
board.printPosition(snapshot.debugprint());
board.printClock(snapshot.debugprint_clock());
}
String print_figure(int figure) {
if(figure == rdgtProtocol.EMPTY) {
return " ";
} else if (figure == rdgtProtocol.WPAWN) {
return "P";
} else if(figure == rdgtProtocol.WROOK) {
return "R";
} else if(figure == rdgtProtocol.WKNIGHT) {
return "N";
} else if(figure == rdgtProtocol.WBISHOP) {
return "B";
} else if(figure == rdgtProtocol.WQUEEN) {
return "Q";
} else if(figure == rdgtProtocol.WKING) {
return "K";
} else if (figure == rdgtProtocol.BPAWN) {
return "p";
} else if(figure == rdgtProtocol.BROOK) {
return "r";
} else if(figure == rdgtProtocol.BKNIGHT) {
return "n";
} else if(figure == rdgtProtocol.BBISHOP) {
return "b";
} else if(figure == rdgtProtocol.BQUEEN) {
return "q";
} else if(figure == rdgtProtocol.BKING) {
return "k";
} else {
return " ";
}
}
void set_alive(boolean x) {
alive = x;
update_html();
}
void set_dumped(boolean x) {
dumped = x;
}
void set_updated(boolean x) {
updated = x;
history.append(snapshot);
}
boolean get_alive() {
return alive;
}
boolean get_dumped() {
return dumped;
}
boolean get_updated() {
return updated;
}
void set_clockdata(boolean running, boolean batteryLow, boolean frontViewLeftSideHigh, boolean blacksTurn, boolean whitesTurn, int secW, int secB) {
snapshot.set_clockdata(running, batteryLow, frontViewLeftSideHigh, blacksTurn, whitesTurn, secW, secB);
snapshot.debugprint_clock();
}
void update_html() {
rdgtMysql.update_snapshot(snapshot);
String fen = rdgtMysql.snapshot2fen(snapshot);
myFenhax.add(fen);
rdgtMysql.update_moves(rdgtFenhax.getMoves(true, false), rdgtFenhax.getMoves(true, true), address.intValue());
}
void set_emptyboard() {
snapshot.set_emptyboard();
history.append(snapshot);
}
void set_boarddump(int[] all64, int startpos) {
snapshot.set_boarddump(all64, startpos);
dumped = true;
history.append(snapshot);
}
void set_fieldupdate(int piece, int pos) {
snapshot.set_fieldupdate(piece, pos);
updated = true;
history.append(snapshot);
}
}
rdgtConfig.java
---
class rdgtConfig {
String getDbServer() { return server; }
String getDbUser() { return user; }
String getDbPass() { return pass; }
String getDbBase() { return dbase; }
boolean useDbServer() { return mysql; }
boolean usePgnWriter() { return pgn; }
String getPgnFileName() { return pgnfile; }
String server= "localhost";
String user= "root";
String pass= "zar1562";
String dbase= "rdgt_development";
boolean mysql = true;
boolean pgn = false;
String pgnfile = "default.pgn";
}
rdgtController.java
---
class rdgtController extends rdgtProtocol {
rdgtDatabase db;
rdgtInterpreter ipr;
rdgtReceiver rec;
rdgtSerialport ser;
rdgtSender snd;
public rdgtController() {
db = new rdgtDatabase();
ipr = new rdgtInterpreter(db);
rec = new rdgtReceiver(ipr);
ser = new rdgtSerialport(rec);
snd = new rdgtSender(ser);
}
boolean connect(String comport) {
if (ser.open(comport)==false) {
System.out.println("Could not open "+comport);
return false;
}
System.out.println("COM port opened: "+comport);
return true;
}
int find_all_boards() {
System.out.println("Scanning for boards...");
db.unalive_all();
int alive = 0;
while (true) {
try {Thread.sleep(300);} catch(Throwable t) {}
snd.send(DGT_BUS_PING, 0);
try {Thread.sleep(1100);} catch(Throwable t) {}
int alivenow = db.count_alive();
if (alivenow == alive) break;
alive = alivenow;
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
snd.send(DGT_BUS_IGNORE_NEXT_BUS_PING, b.get_address());
}
}
}
System.out.println("Scanning completed, found "+Integer.toString(alive)+" boards.\n");
return alive;
}
void dump_all_active_boards() {
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
b.set_dumped(false);
for (int i=0; i<3; i++) { if (b.get_dumped()==true) break;
snd.send(DGT_BUS_SEND_BRD, b.get_address());
try {Thread.sleep(200);} catch(Throwable t) {}
}
if (b.get_dumped()==false) {
System.out.println("Board "+Integer.toString(b.get_address())+" does not reply to dump_board.");
b.set_alive(false);
}
}
}
}
void poll_all_active_boards() {
int TIMEOUT = 100;
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
if (b.get_alive()==true) {
snd.send(DGT_BUS_SEND_CLK, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}
b.set_updated(false);
snd.send(DGT_BUS_SEND_CHANGES, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}
for (int i=0; i<2; i=i+1) {
if (b.get_updated()==true) break;
snd.send(DGT_BUS_REPEAT_CHANGES, b.get_address());
try {Thread.sleep(TIMEOUT);} catch(Throwable t) {}}
if (b.get_updated()==false) {
System.out.println("Board "+Integer.toString(b.get_address())+" does not reply anymore.");
b.set_alive(false);
} else {
b.print_position();
}
}
}
}
}
rdgtDatabase.java
---
class rdgtDatabase {
Hashtable boards;
public rdgtDatabase() {
boards = new Hashtable();
}
rdgtChessboard get_board(int address) {
Integer _address = new Integer(address);
rdgtChessboard b = (rdgtChessboard)(boards.get(_address));
if (b==null) {
System.out.println("New board found: ID="+_address.toString());
b = new rdgtChessboard(_address, this);
boards.put(_address, b);
}
return b;
}
int get_boardcount() {
return boards.size();
}
Enumeration get_boards() {
return boards.elements();
}
void unalive_all() {
for (Enumeration e = boards.elements() ; e.hasMoreElements() ;) {
((rdgtChessboard)(e.nextElement())).set_alive(false);
}
}
int count_alive() {
int x = 0;
for (Enumeration e = boards.elements() ; e.hasMoreElements() ;) {
if (((rdgtChessboard)(e.nextElement())).get_alive()==true) x++;
}
return x;
}
}
rdgtFenboard.java
---
class rdgtFenboard {
int[] itsBoard;
String itsFen;
public rdgtFenboard(){
itsBoard = new int[64];
for(int i=0;i<64;i++)
itsBoard[i]='.';
itsFen="";
}
public rdgtFenboard(String fen){ setBoard(fen); }
public rdgtFenboard(int[] board) { setBoard(board); }
public boolean setBoard(String fen){
if(!validateFen(fen))
return false;
itsBoard=fen2board(fen);
itsFen=fen;
return true;
}
public int getPieceAt(int x, int y){
if(x7 || y7) return 0;
return itsBoard[y*8+x];
}
public boolean setBoard(int[] board){
if(board.length != 64)
return false;
itsBoard=board;
return true;
}
public void print(){
printBoard(itsBoard);
}
public int[] getBoard() { return itsBoard; }
public rdgtFenboard mask(rdgtFenboard b){
rdgtFenboard output;
output=new rdgtFenboard(mask(getBoard(),b.getBoard()));
return output;
}
public int diffCount(rdgtFenboard b){
rdgtFenboard m = mask(b);
int[] table=m.getBoard();
int output=0;
for(int i=0;i<64;i++){
if(table[i]!='.')
output++;
}
return output;
}
int[] fen2board(String aFen){
int[] board = new int[64];
int square=0;
int chr=0;
while(square<64){
char c = aFen.charAt(chr);
if(c=='r'||c=='R'||c=='n'||c=='N'||c=='b'||c=='B'||c=='q'||c=='Q'||c=='k'||c=='K'||c=='p'||c=='P'){
board[square]=c;
square++;
}
if(c>='1' && c<='8'){
for(int i=0;i board[square]='.'; square++; } } chr++; } return board; } String[] outputBoard(int[] board){ String s=""; String[] out = new String[8]; for(int chr=0;chr<64;chr++){ if(board[chr]<0) s += "-" + (char)(Math.abs(board[chr])); else if(board[chr]==0) s += ", "; else s += (char)board[chr] + " "; if((chr+1)%8==0){ out[chr/8]=s; s=""; } } return out; } void printBoard(int[] board){ String[] out = outputBoard(board); for(int i=0;i<8;i++){ System.out.println(out[i]); } System.out.println(); } int[] mask(int[] b1, int[] b2){ int[] output=new int[64]; for(int i=0;i<64;i++){ if(b2[i]!=b1[i]){ if(b2[i]=='.') output[i]=-b1[i]; else output[i]=b2[i]; } else output[i] = '.';//b2[i]-b1[i]; } return output; } boolean validateFen(String aFen){ /* Only check board part of FEN */ if(aFen.indexOf(' ')>0){ aFen = aFen.substring(0,aFen.indexOf(' ')); } int s=0; for(int c=0;c if(aFen.charAt(c) == '/') s++; } if(s!=7) return false; int x=0; for(int c=0;c if(aFen.charAt(c) == '/'){ if(x!=8) return false; else x=0; } else { if(aFen.charAt(c) >= '1' && aFen.charAt(c)<= '8') x+= aFen.charAt(c)-'1' ; x++; } } if(x!=8) return false; return true; } } rdgtFendiff.java --- class rdgtFendiff { char piece; boolean added; int square; rdgtFenboard before; rdgtFenboard after; public rdgtFenboard getBefore() { return before; } public rdgtFenboard getAfter() { return after; } public char getPiece() { return piece; } public boolean isAdded() { return added; } public int getSquare() { return square; } public boolean isWhite(){ if(piece>'A' && piece < 'Z') return true; else return false; } public boolean equals(rdgtFendiff other){ if(getPiece() != other.getPiece()) return false; if(getSquare() != other.getSquare()) return false; if(isAdded() != other.isAdded()) return false; return true; } public rdgtFendiff() { secured=false; } public rdgtFendiff(char piece, boolean added, int square){ set(piece,added,square); } public rdgtFendiff(int piece, int square){ if(piece<0){ this.piece = (char)(-piece); added=false; } else { this.piece=(char)piece; added=true; } this.square=square; } public rdgtFendiff(int piece,int square, rdgtFenboard before, rdgtFenboard after){ this.before=before; this.after=after; if(piece<0){ this.piece = (char)(-piece); added=false; } else { this.piece=(char)piece; added=true; } this.square=square; } public void set(char piece, boolean added, int square){ this.piece=piece; this.added=added; this.square=square; } public String toString() { String output = ""+piece; if(added) output += "@"; else output += "&"; output += (char)((square)%8+'a') + "" + (8-square/8); return output; } } rdgtFenhax.java --- public class rdgtFenhax { static boolean debug=false; static boolean rotated=false; public rdgtFenhax(){ boards=new ArrayList(); moves=new movelist(); diffTable=new ArrayList(); result="?-?"; plyCount=0; } static String getMoves(boolean shortNotation, boolean nice) { calcMoves(); return moves.getAll(shortNotation,nice); } static String getMoves(){ return getMoves(false,false); } static int getPlyCount(){ calcMoves(); return moves.size(); } static void clear(){ boards.clear(); moves.clear(); diffTable.clear(); result="?-?"; } static String getResult() { return result; } static boolean add(String aFen){ rdgtFenboard f = new rdgtFenboard(); if(!f.setBoard(aFen)) return false; boards.add(f); if(aFen.indexOf("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")>=0){ clear(); boards.add(f); } if((aFen.indexOf("rnbq1bnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQ1BNR") >= 0)|| (aFen.indexOf("rnbqkbnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQ1BNR") >= 0)|| (aFen.indexOf("rnbq1bnr/pppppppp") >=0 && aFen.indexOf("PPPPPPPP/RNBQKBNR") >= 0)){ int[] b=f.getBoard(); boolean black=false; boolean white=false; for(int i=16;i<=47;i++){ if(b[i] == 'k') black=true; if(b[i] == 'K') white=true; } if(black && white) result="1/2-1/2"; else if(black) result="0-1"; else if(white) result="1-0"; } if(boards.size()>=2){ rdgtFenboard last = (rdgtFenboard)boards.get(boards.size()-1); rdgtFenboard nextlast = (rdgtFenboard)boards.get(boards.size()-2); if(last.diffCount(nextlast) < 5){ rdgtFenboard m=nextlast.mask(last); int[] mask = m.getBoard(); for(int i=0;i<64;i++){ if(mask[i] != '.' && mask[i]<0){ diffTable.add(new rdgtFendiff(mask[i],i,nextlast,last)); } } for(int i=0;i<64;i++){ if(mask[i] != '.' && mask[i]>0){ diffTable.add(new rdgtFendiff(mask[i],i,nextlast,last)); } } } else { clear(); boards.add(f); } } return true; } static void calcMoves(){ moves.clear(); if(debug){ for(int i=0;i System.out.print(i + ":" + ((rdgtFendiff)diffTable.get(i)).toString() + " "); } System.out.print("\n"); } int offset=0; int length=diffTable.size(); while(offset int start=offset; int end=-1; boolean turn = isWhite( ((rdgtFendiff)diffTable.get(offset)).getPiece() ); int i=start; while(end==-1){ rdgtFendiff test = (rdgtFendiff)diffTable.get(i); if(turn != isWhite(test.getPiece()) && test.isAdded()){ end=i-1; } else{ i++; if(i>=length) end=length-1; } if(turn==isWhite(test.getPiece()) && test.isAdded()){ offset=i; } } if(offset==start) offset++; if(debug) System.out.print(start + "-"+end+" "); movelist chunkMoves = new movelist(true,true); for(int s=start;s rdgtFendiff a = (rdgtFendiff)diffTable.get(s); for(int e=s+1;e<=end;e++){ rdgtFendiff b = (rdgtFendiff)diffTable.get(e); if(a.getPiece() == b.getPiece() && a.getSquare() != b.getSquare()){ if(!a.isAdded() && b.isAdded()){ String special=""; if(b.getPiece()=='P' && b.getSquare()/8==0){ for(int j=e+1;j<=end;j++){ rdgtFendiff prom = (rdgtFendiff)diffTable.get(j); if(prom.isAdded() && prom.isWhite()){ if(a.getSquare()%8 != b.getSquare()%8) special+=pos2xy(a.getSquare()).substring(0,1)+"x"; special+=pos2xy(b.getSquare())+"="+prom.getPiece(); b=prom; } } } if(b.getPiece()=='p' && b.getSquare()/8==7){ for(int j=e+1;j<=end;j++){ rdgtFendiff prom = (rdgtFendiff)diffTable.get(j); if(prom.isAdded() && prom.isWhite()){ if(a.getSquare()%8 != b.getSquare()%8) special+=pos2xy(a.getSquare()).substring(0,1)+"x"; special+=pos2xy(b.getSquare())+"="+prom.getPiece(); b=prom; } } } chunkMoves.add(new move(a,b,special)); } } } } if(chunkMoves.size()==0){ rdgtFendiff a = (rdgtFendiff)diffTable.get(start); for(int s=start;s<=end;s++){ rdgtFendiff b = (rdgtFendiff)diffTable.get(s); if(b.isAdded()){ if(a.getPiece()=='P' && a.getSquare()/8==1 && !a.isAdded()){ String special=""; if(a.getSquare()%8 != b.getSquare()%8) special+=pos2xy(a.getSquare()).substring(0,1)+"x"; special+=pos2xy(b.getSquare())+"="+b.getPiece(); chunkMoves.add(new move(a,b,special)); } else if(a.getPiece()=='p' && a.getSquare()/8==6 && !a.isAdded()){ String special=""; if(a.getSquare()%8 != b.getSquare()%8) special+=pos2xy(a.getSquare()).substring(0,1)+"x"; special+=pos2xy(b.getSquare())+"="+b.getPiece(); chunkMoves.add(new move(a,b,special)); } else chunkMoves.add(new move(a,b,""+a.getPiece()+"@"+pos2xy(a.getSquare()))); } } } for(int j=0;j if(debug) System.out.print( (chunkMoves.get(j)).toString() + " "); moves.add(chunkMoves.get(j)); } if(debug) System.out.print("\n"); } } static String pos2xy(int pos){ String s = "" + (char)((pos)%8+'a') + "" + (8-pos/8); return s; } static boolean isWhite(char piece){ if(piece>'A' && piece < 'Z') return true; else return false; } static movelist moves; static ArrayList boards; static ArrayList diffTable; static String result; static int plyCount; } class move { rdgtFendiff from; rdgtFendiff to; boolean take; String special; public move(rdgtFendiff from, rdgtFendiff to){ this.from=from; this.to=to; take=false; special=""; } public move(rdgtFendiff from, rdgtFendiff to, String special){ this.from=from; this.to=to; take=false; this.special=special; } public rdgtFendiff getFrom(){ return from; } public rdgtFendiff getTo(){ return to; } public char getPiece(){ return from.getPiece(); } public int getFromSquare() { return from.getSquare(); } public int getToSquare() { return to.getSquare(); } public String getFromCor() { return pos2xy(getFromSquare()); } public String getToCor() { return pos2xy(getToSquare()); } public boolean isWhite() { return from.isWhite(); } public String longNotation() { return moveString(false); } public String shortNotation() { return moveString(true); } public void setTake(boolean take){ this.take=take; } public boolean getTake() { return take; } String moveString(boolean shortNotation){ if(!special.equals("")) { if(isCheck(isWhite())) return special + "+"; else return special; } boolean ep=false; char takeBit='-'; if(take) takeBit='x'; String s=""; if(getPiece()!='p' && getPiece()!='P') s+=new String(""+getPiece()).toUpperCase(); if(!shortNotation) s+=getFromCor()+takeBit; else { if(getPiece()=='R' || getPiece()=='r') if(findRookLength(getToSquare()%8,getToSquare()/8,getPiece())) s+=getFromCor()+takeBit; if(getPiece()=='N' || getPiece()=='n') if(findNightLength(getToSquare()%8,getToSquare()/8,getPiece())) s+=getFromCor()+takeBit; if(getPiece()=='B' || getPiece()=='b') if(findBishopLength(getToSquare()%8,getToSquare()/8,getPiece())) s+=getFromCor()+takeBit; if(getPiece()=='Q' || getPiece()=='q') if(findQueenLength(getToSquare()%8,getToSquare()/8,getPiece())) s+=getFromCor()+takeBit; if(getPiece()=='P' || getPiece()=='p') if( !getFromCor().substring(0,1).equals( getToCor().substring(0,1) ) ) { s+=getFromCor().substring(0,1)+"x"; if(!take) ep=true; } if(take && s.length()>=1){ if(s.charAt(s.length()-1) != 'x') s+="x"; } } s+=getToCor(); if(ep) s+=" ep."; /** Is it a check? **/ if(isCheck(isWhite())) s+='+'; return s; } boolean isCheck(boolean white){ int k=-1; int K=-1; for(int i=0;i<64;i++){ if(to.getAfter().getPieceAt(i%8,i/8) == 'k') k=i; if(to.getAfter().getPieceAt(i%8,i/8) == 'K') K=i; } boolean check=false; if(white){ if(findQueenLength(k%8,k/8,'Q')) check=true; if(findRookLength(k%8,k/8,'R')) check=true; if(findBishopLength(k%8,k/8,'B')) check=true; if(findNightLength(k%8,k/8,'N')) check=true; if(findPawnLength(k%8,k/8,'P')) check=true; } else{ if(findQueenLength(K%8,K/8,'q')) check=true; if(findRookLength(K%8,K/8,'r')) check=true; if(findBishopLength(K%8,K/8,'b')) check=true; if(findNightLength(K%8,K/8,'n')) check=true; if(findPawnLength(K%8,K/8,'p')) check=true; } return check; } /** Is there a night on a night-distance from square? **/ boolean findNightLength(int x, int y,char n){ rdgtFenboard b = to.getAfter(); if(b.getPieceAt(x-1,y-2) == n) return true; if(b.getPieceAt(x+1,y-2) == n) return true; if(b.getPieceAt(x-2,y-1) == n) return true; if(b.getPieceAt(x+2,y-1) == n) return true; if(b.getPieceAt(x-2,y+1) == n) return true; if(b.getPieceAt(x+2,y+1) == n) return true; if(b.getPieceAt(x-1,y+2) == n) return true; if(b.getPieceAt(x+1,y+2) == n) return true; return false; } boolean findRookLength(int x, int y, char r){ rdgtFenboard b = to.getAfter(); int xx=0; int yy=0; do{ xx++; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); xx=0; do{ xx--; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); xx=0; do{ yy--; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); yy=0; do{ yy++; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); return false; } boolean findBishopLength(int x, int y, char r){ rdgtFenboard b = to.getAfter(); int xx=0; int yy=0; do{ xx++; yy++; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); xx=0; yy=0; do{ xx--; yy--; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); xx=0; yy=0; do{ yy--; xx++; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); yy=0; xx=0; do{ yy++; xx--; if(b.getPieceAt(x+xx,y+yy) == r) return true; }while(b.getPieceAt(x+xx,y+yy) == '.'); return false; } boolean findQueenLength(int x, int y, char q){ if(findRookLength(x,y,q)) return true; if(findBishopLength(x,y,q)) return true; return false; } boolean findPawnLength(int x, int y, char p){ rdgtFenboard b = to.getAfter(); if(p=='P'){ if(b.getPieceAt(x-1,y-1)==p) return true; if(b.getPieceAt(x+1,y-1)==p) return true; } else { if(b.getPieceAt(x-1,y-1)==p) return true; if(b.getPieceAt(x+1,y-1)==p) return true; } return false; } public String toString(){ return ""+from.toString() + "-" + to.toString(); } public boolean equals(move other){ if(!from.equals( other.getFrom() )) return false; if(!to.equals( other.getTo() )) return false; return true; } String pos2xy(int pos){ String s = "" + (char)((pos)%8+'a') + "" + (8-pos/8); return s; } } class movelist { ArrayList moves; boolean unique; boolean noSmooth; public void clear(){moves.clear(); } public void remove(int n) { moves.remove(n); } public movelist(){ moves=new ArrayList(); unique=false; } public movelist(boolean unique, boolean noSmooth) { moves=new ArrayList(); this.unique=unique; this.noSmooth=noSmooth; } public move get(int n){ return (move)moves.get(n); } public int size() { return moves.size(); } public String toString() { return getAll(false,false); } public void print(boolean shortNotation) { System.out.println("Moves"); System.out.print(getAll(shortNotation,true)); } void fixTakes(){ for(int i=1;i int toSquare = ((move)moves.get(i)).getToSquare(); char p = (char) ((move)moves.get(i-1)).getTo().getAfter().getBoard()[toSquare]; if(p!='.'){ move m=(move)moves.get(i); m.setTake(true); moves.set(i,m); } } } public String getAll(boolean shortNotation,boolean nice){ fixTakes(); String output=""; boolean lastColor=false; int n=1; for(int i=0;i move m=(move)moves.get(i); if(m.isWhite() && lastColor==false){ if(nice) output += n+".\t"; else output += n+". "; n++; } if(n==1 && !m.isWhite()) { if(nice) output += "1.\t...\t\t"; else output += "1. ... "; } lastColor=m.isWhite(); int length=output.length(); if(shortNotation) output += m.shortNotation()+" "; else output += m.longNotation()+" "; if(nice){ if(!m.isWhite()) output += "\n"; else{ if(output.length()-length < 8) output += "\t"; if(output.length()-length < 12) output += "\t"; } } } return output; } public void add(move aMove){ if(!unique){ moves.add(aMove); return; } for(int i=0;i if( ((move)moves.get(i)).equals(aMove)) return; } if(moves.size()>=1){ move m=(move)moves.get(moves.size()-1); // O-O if(m.moveString(false).equals("Ke1-g1") && aMove.moveString(false).equals("Rh1-f1")){ moves.remove(moves.size()-1); moves.add(new move(aMove.getFrom(),aMove.getTo(),"O-O")); return; } // O-O-O if(m.moveString(false).equals("Ke1-c1") && aMove.moveString(false).equals("Ra1-d1")){ moves.remove(moves.size()-1); moves.add(new move(aMove.getFrom(),aMove.getTo(),"O-O-O")); return; } // O-O if(m.moveString(false).equals("Ke8-g8") && aMove.moveString(false).equals("Rh8-f8")){ moves.remove(moves.size()-1); moves.add(new move(aMove.getFrom(),aMove.getTo(),"O-O")); return; } // O-O-O if(m.moveString(false).equals("Ke8-c8") && aMove.moveString(false).equals("Ra8-d8")){ moves.remove(moves.size()-1); moves.add(new move(aMove.getFrom(),aMove.getTo(),"O-O-O")); return; } } moves.add(aMove); } public void removeSmooth(){ for(int a=0;a move aMove=(move)moves.get(a); for(int i=0;i move test = (move)moves.get(i); if( test.getPiece() == aMove.getPiece()){ if( aMove.getFrom().getSquare() == test.getTo().getSquare() || aMove.getTo().getSquare() == test.getFrom().getSquare() ){ if(a==i){ moves.remove(i); i=moves.size(); a=0; } else if(i>a){ moves.remove(i); moves.remove(a); i=moves.size(); a=0; } else { moves.remove(a); moves.remove(i); i=moves.size(); a=0; } } } } } } } rdgtHistory.java --- class rdgtHistory { Vector history = new Vector(); rdgtSnapshot lastadded = null; rdgtSnapshot last = null; rdgtChessboard board; public rdgtHistory(rdgtChessboard _board) { board = _board; } void append(rdgtSnapshot x) { if(history.size()==0){ history.addElement(x.copy()); board.update_html(); } else if(x.sameas((rdgtSnapshot)history.lastElement())==false ){ history.addElement(x.copy()); board.update_html(); } } void debugprint() { System.out.println("======= History for board: "+board.address.toString()); for (Enumeration e = history.elements(); e.hasMoreElements() ;) { ((rdgtSnapshot)(e.nextElement())).debugprint(); } } } rdgtHtml.java --- class rdgtHtml extends rdgtProtocol { static String extension = ".html"; static String indexfilename = "board_index"; static String historyprefix = "boardhistory_"; static String snapshotprefix = "boardsnapshot_"; static void print_index(rdgtDatabase db) { StringBuffer s = new StringBuffer(2000); s.append("\n\n \n\n"); s.append(" s.append(" for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) { rdgtChessboard b = (rdgtChessboard)(e.nextElement()); s.append(" s.append(Integer.toString(b.get_address())); s.append("\n if (b.get_alive()==true) s.append("Online"); else s.append("Offline"); s.append(" s.append(b.get_snapshot().get_time().toString()); s.append("\n s.append(snapshotprefix); s.append(Integer.toString(b.get_address())); s.append(extension); s.append("\">Link s.append(historyprefix); s.append(Integer.toString(b.get_address())); s.append(extension); s.append("\">Link\n"); } s.append("\n\n\n"); if (writefile(indexfilename+extension, s.toString()) == false) { System.err.println("Error: Failed to write indexfile."); return; } } static void print_history(rdgtHistory h) { StringBuffer s = new StringBuffer(2000); s.append("\n\n"); s.append(" s.append(Integer.toString(h.board.get_address())); s.append("\n"); int i = 0; for (Enumeration e = h.history.elements() ; e.hasMoreElements() ;) { rdgtSnapshot ss = (rdgtSnapshot)(e.nextElement()); i=i+1; s.append(" s.append(Integer.toString(i)); s.append(" "); s.append(ss.get_time().toString()); s.append(boardview(ss)); s.append("\n"); } s.append("\n\n"); if (writefile(historyprefix + Integer.toString(h.board.get_address()) + extension, s.toString()) == false) { System.err.println("Error: Failed to write historyfile."); return; } } static String boardview(rdgtSnapshot b) { StringBuffer s = new StringBuffer(1000); s.append(" boolean filled = true; for (int i=0; i<8; i=i+1) { s.append(" for (int j=0; j<8; j=j+1) { s.append(" int p = b.pieces[(i*8)+j]; if (p==EMPTY ) s.append("empty"); else if (p==WPAWN ) s.append("w_pawn"); else if (p==WROOK ) s.append("w_rook"); else if (p==WKNIGHT) s.append("w_knight"); else if (p==WBISHOP) s.append("w_bishop"); else if (p==WKING ) s.append("w_king"); else if (p==WQUEEN ) s.append("w_queen"); else if (p==BPAWN ) s.append("b_pawn"); else if (p==BROOK ) s.append("b_rook"); else if (p==BKNIGHT) s.append("b_knight"); else if (p==BBISHOP) s.append("b_bishop"); else if (p==BKING ) s.append("b_king"); else if (p==BQUEEN ) s.append("b_queen"); if (filled) s.append("1"); else s.append("2"); filled = !filled; s.append(".gif\">"); } filled = !filled; s.append("\n"); } s.append("\n"); return s.toString(); } static void print_snapshot(rdgtSnapshot ss) { StringBuffer s = new StringBuffer(2000); s.append("\n\n \n\n"); s.append(" s.append(Integer.toString(ss.board.get_address())); s.append("\n s.append(ss.get_time().toString()); s.append("\n"); s.append(boardview(ss)); s.append("\n\n\n"); if (writefile(snapshotprefix + Integer.toString(ss.board.get_address()) + extension, s.toString()) == false) { System.err.println("Error: Failed to write snapshotfile."); return; } } static boolean writefile(String filename, String content) { FileWriter f = null; for (int i=0; i<10; i=i+1) { try { f = new FileWriter(filename); break; } catch (Throwable t) { System.err.println("Error opening "+filename+" for writing. Retrying..."); } } if (f==null) { System.out.println("Failed to open file "+filename); return false; } try { f.write(content); f.close(); } catch (Exception e) { System.out.println("Error writing file "+filename+". ("+e.toString()+")"); return false; } return true; } } rdgtInterpreter.java --- class rdgtInterpreter extends rdgtProtocol { rdgtDatabase db; public rdgtInterpreter(rdgtDatabase _db) { db = _db; } public boolean interpret(int[] data) { int len = ((int) data[1] * 0x80) + (int) data[2]; int address = ((int) data[3] * 0x80) + (int) data[4]; String tmp = "Len:" + Integer.toString(len) + " Address:" + Integer.toString(address); if (data[0] == DGT_BUS_PING) { System.out.println("Received message: PING " + tmp); } else if (data[0] == DGT_BUS_UPDATE) { System.out.println("Received message: UPDATE " + tmp); } else if (data[0] == DGT_BUS_BWTIME) { System.out.println("Received message: BWTIME " + tmp); } else { System.out.println("Received message: (some other message) " + tmp); } if (check_checksum(data) == false) { System.out.println("Message failed checksum test"); return false; } if (data[0] == DGT_BUS_PING) { if (data.length != 6) { // address+data System.out.println("Illegal: Got a BUS_PING with wrong size"); return false; } db.get_board(address).set_alive(true); return true; } else if (data[0] == DGT_BUS_BRD_DUMP) { if (data.length != (6 + 64)) { System.out.println("Illegal: Got a BUS_BRD_DUMP with wrong size"); return false; } rdgtChessboard c = db.get_board(address); c.set_boarddump(data, 5); return true; } else if (data[0] == DGT_BUS_UPDATE) { byte pr[] = new byte[data.length - 5]; for (int i = 5; i < data.length; i++) { pr[i - 5] = (byte) data[i]; } System.out.println(new String(pr)); rdgtChessboard c = db.get_board(address); for (int i = 5; i < data.length - 1; i++) { if (data[i] == EE_POWERUP) { } else if (data[i] == EE_EOF) { } else if (data[i] == EE_FOURROWS) { } else if (data[i] == EE_EMPTYBOARD) { c.set_emptyboard(); } else if (data[i] == EE_DOWNLOADED) { } else if (data[i] == EE_BEGINPOS) { } else if (data[i] == EE_BEGINPOS_ROT) { } else if (data[i] == EE_START_TAG) { } else if (data[i] == EE_WATCHDOG_ACTION) { } else if (data[i] == EE_NOP) { } else if (data[i] >= 0x40 && data[i] <= 0x5F) { // 2-byte piece update c.set_fieldupdate(data[i] & 0x0F, data[i + 1]); i++; } else if ((data[i] >= 0x60 && data[i] = 0x70 && data[i] <= 0x79)) { i += 2; } else { System.out.println("Unknown data in DGT_BUS_UPDATE command."); return false; } } c.set_updated(true); return true; } else if (data[0] == DGT_BUS_BWTIME) { if (data.length != 13) { System.out.println("Illegal: Got a DGT_BUS_BWTIME with wrong size (!=10)"); return false; } if ((data[11] & 0x20) != 0) { System.out.println("(No clock connected)"); return true; } boolean running = (data[11] & 0x01) != 0; boolean batteryLow = (data[11] & 0x04) != 0; boolean frontViewLeftSideHigh = (data[11] & 0x02) != 0; boolean blackPlayersTurn = (data[11] & 0x08) != 0; boolean whitePlayersTurn = (data[11] & 0x10) != 0; int hoursW = data[5] & 0x0F; int minutesW = (data[6] & 0x0F) + ((data[6] >> 4) * 10); int secondsW = (data[7] & 0x0F) + ((data[7] >> 4) * 10); int hoursB = data[8] & 0x0F; int minutesB = (data[9] & 0x0F) + ((data[9] >> 4) * 10); int secondsB = (data[10] & 0x0F) + ((data[10] >> 4) * 10); secondsW = (((hoursW * 60) + minutesW) * 60) + secondsW; secondsB = (((hoursB * 60) + minutesB) * 60) + secondsB; db.get_board(address).set_clockdata(running, batteryLow, frontViewLeftSideHigh, blackPlayersTurn, whitePlayersTurn, secondsW, secondsB); return true; } else if (data[0] == DGT_BUS_FROM_START) { } else if (data[0] == DGT_BUS_START_GAME_WRITTEN) { } else if (data[0] == DGT_BUS_VERSION) { } System.err.println("Unknown message (checksum was ok)."); return true; } boolean check_checksum(int[] data) { int sum = 0; for (int i = 0; i < (data.length - 1); i = i + 1) { sum = (sum + data[i]) & 0x7F; } if (sum == data[data.length - 1]) { return true; } return false; } } rdgtMysql.java --- class rdgtMysql extends rdgtProtocol { static String server= "localhost"; static String user= "root"; static String pass= "zar1562"; static String dbase= "rdgt_development"; static String update_table= "games"; static Connection conn = null; static Statement stmt = null; static boolean rotated=false; static String connect() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://"+server+"/"+dbase+"?user="+user+"&password="+pass); return ""; } catch (SQLException e) { return e.getSQLState(); } catch (Exception e) { return "0"; } } static boolean disconnect() { if (conn != null) { try { conn.close(); return true; } catch (SQLException SQLE) { return false; } } else return false; } static String execute(String expr) { try { stmt = null; stmt = conn.createStatement(); stmt.executeUpdate(expr); return ""; } catch (SQLException e) { return e.getSQLState(); } catch (Exception e) { return "0"; } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException SQLE) { ; } } } } static String snapshot2fen (rdgtSnapshot b) { StringBuffer s = new StringBuffer(100); int counter = 0; for (int i=0; i<8; i++) { for (int j=0; j<8; j++) { if ((counter != 0) && (b.pieces[(i*8)+j] != EMPTY)) { s.append(counter); counter = 0; } switch(b.pieces[(i*8)+j]) { case WPAWN:s.append("P"); break; case WROOK: s.append("R"); break; case WKNIGHT: s.append("N"); break; case WBISHOP: s.append("B"); break; case WKING: s.append("K"); break; case WQUEEN: s.append("Q"); break; case BPAWN: s.append("p"); break; case BROOK: s.append("r"); break; case BKNIGHT: s.append("n"); break; case BBISHOP: s.append("b"); break; case BKING: s.append("k"); break; case BQUEEN: s.append("q"); break; default: counter++; break; } } if (counter != 0) s.append(counter); s.append("/"); counter = 0; } String outfen= s.toString().substring(0,s.toString().length()-1); switch(b.board.address.intValue()){ case 3737: case 3811: case 2896: case 3740: case 3588: case 2897: default: rotated=false; break; } if(rotated){ String newFen=""; for(int i=outfen.length()-1;i>=0;i--){ newFen += outfen.charAt(i); } outfen=newFen; } return outfen; } static void update_snapshot(rdgtSnapshot b) { if (conn != null) { String fen = snapshot2fen(b); String turn; if(rotated){ if(b.isWhiteTurn()) turn="Black"; else turn="White"; } else{ if(b.isWhiteTurn()) turn="White"; else turn="Black"; } String sql_command = "UPDATE "+update_table+" SET fen = '"+fen+"',wtime='"+b.getTimeWhite()+"',btime='"+b.getTimeBlack()+"',turn='"+turn+"' WHERE dgt_active like '%Fen%' AND dgt_board = "+b.board.address; execute(sql_command); System.out.println(b.board.address+" "+fen+" "+b.getTimeWhite()+" "+b.getTimeBlack()+" "+turn.substring(0,1)); } } static void update_moves(String moves, String movestable, int board_id) { if (conn != null) { int t=0; if(moves.length()>20) t=moves.length()-20; System.out.println(board_id + " Moves: " + moves.substring(t)); String sql_command = "UPDATE games SET moves = '"+moves+"' WHERE dgt_active like '%Moves%' AND dgt_board = "+board_id; execute(sql_command); } } } rdgtProtocol.java --- class rdgtProtocol { static int DGT_TO_BUSMODE = 0x4a; static int DGT_BUS_SEND_CLK = 0x01 | 0x80; static int DGT_BUS_SEND_BRD = 0x02 | 0x80; static int DGT_BUS_SEND_CHANGES = 0x03 | 0x80; static int DGT_BUS_REPEAT_CHANGES = 0x04 | 0x80; static int DGT_BUS_SET_START_GAME = 0x05 | 0x80; static int DGT_BUS_SEND_FROM_START = 0x06 | 0x80; static int DGT_BUS_PING = 0x07 | 0x80; static int DGT_BUS_END_BUSMODE = 0x08 | 0x80; static int DGT_BUS_RESET = 0x09 | 0x80; static int DGT_BUS_IGNORE_NEXT_BUS_PING = 0x0a | 0x80; static int DGT_BUS_SEND_VERSION = 0x0b | 0x80; static int DGT_BUS_BRD_DUMP = 0x03 | 0x80; static int DGT_BUS_BWTIME = 0x04 | 0x80; static int DGT_BUS_UPDATE = 0x05 | 0x80; static int DGT_BUS_FROM_START = 0x06 | 0x80; static int DGT_BUS_START_GAME_WRITTEN = 0x08 | 0x80; static int DGT_BUS_VERSION = 0x09 | 0x80; static boolean is_busmessage(int m) { return (m== DGT_BUS_BRD_DUMP || m == DGT_BUS_BWTIME || m == DGT_BUS_UPDATE || m == DGT_BUS_FROM_START || m == DGT_BUS_PING || m == DGT_BUS_START_GAME_WRITTEN || m == DGT_BUS_VERSION); } static final int EMPTY = 0x00; static final int WPAWN = 0x01; static final int WROOK = 0x02;Chessboards
\n\n");
Board id Status Time of last move Snapshot History\n"); \n "); "); "); History for Chessboard: ");
Move: ");
\n");
"); 10>Snapshot of Chessboard: ");
");
Характеристики
Список файлов ВКР















