Обеспечение всемирной трансляции спортивных шахматных соревнований с применением разработанного в ходе проекта законченного программного продукта
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<diffTable.size();i++){
System.out.print(i + ":" + ((rdgtFendiff)diffTable.get(i)).toString() + " ");
}
System.out.print("n");
}
int offset=0;
int length=diffTable.size();
while(offset<length){
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<end;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<chunkMoves.size();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<moves.size();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<moves.size();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...tt";
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<moves.size();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<moves.size();a++){
move aMove=(move)moves.get(a);
for(int i=0;i<moves.size();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("<html>n<head>n <meta http-equiv=refresh content=3>n</head>n");
s.append("<body><h1>Chessboards</h1>n<table border=1 cellspacing=0 cellpadding=10>n");
s.append("<tr><th>Board id<th>Status<th>Time of last move<th>Snapshot<th>Historyn");
for (Enumeration e = db.get_boards() ; e.hasMoreElements() ;) {
rdgtChessboard b = (rdgtChessboard)(e.nextElement());
s.append("<tr>n<td valign=top>");
s.append(Integer.toString(b.get_address()));
s.append("n<td>");
if (b.get_alive()==true) s.append("Online"); else s.append("Offline");
s.append("<td>");
s.append(b.get_snapshot().get_time().toString());
s.append("n<td><a href="");
s.append(snapshotprefix);
s.append(Integer.toString(b.get_address()));
s.append(extension);
s.append("">Link</a><td><a href="");
s.append(historyprefix);
s.append(Integer.toString(b.get_address()));
s.append(extension);
s.append("">Link</a>n");
}
s.append("</table>n</body>n</html>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("<html>n<head></head>n");
s.append("<body><h1>History for Chessboard: ");
s.append(Integer.toString(h.board.get_address()));
s.append("</h1>n");
int i = 0;
for (Enumeration e = h.history.elements() ; e.hasMoreElements() ;) {
rdgtSnapshot ss = (rdgtSnapshot)(e.nextElement());
i=i+1;
s.append("<p><hr size=1><p><h3>Move: ");
s.append(Integer.toString(i));
s.append(" ");
s.append(ss.get_time().toString());
s.append(boardview(ss));
s.append("n");
}
s.append("</body>n</html>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("<table border=0 cellpadding=0 cellspacing=0>n");
boolean filled = true;
for (int i=0; i<8; i=i+1) {
s.append("<tr>");
for (int j=0; j<8; j=j+1) {
s.append("<td><img src="images/");
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("</tr>n");
}
s.append("n</table>");
return s.toString();
}
static void print_snapshot(rdgtSnapshot ss) {
StringBuffer s = new StringBuffer(2000);
s.append("<html>n<head>n <meta http-equiv=refresh content=4>n</head>n");
s.append("<body><h1>Snapshot of Chessboard: ");
s.append(Integer.toString(ss.board.get_address()));
s.append("</h1>n<h3>");
s.append(ss.get_time().toString());
s.append("</h3>n");
s.append(boardview(ss));
s.append("n</body>n</html>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] <= 0x69) || (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;
static final int WKNIGHT = 0x03;
static final int WBISHOP = 0x04;
static final int WKING = 0x05;
static