Обеспечение всемирной трансляции спортивных шахматных соревнований с применением разработанного в ходе проекта законченного программного продукта

Дипломная работа - Компьютеры, программирование

Другие дипломы по предмету Компьютеры, программирование

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) 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<c-1+1;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 */

0){"> if(aFen.indexOf( )>0){

aFen = aFen.substring(0,aFen.indexOf( ));

}

int s=0;

for(int c=0;c<aFen.length();c++){

if(aFen.charAt(c) == /) s++;

}

if(s!=7) return false;

 

int x=0;

for(int c=0;c<aFen.length();c++){

if(aFen.charAt(c) == /){

if(x!=8) return false;

else x=0;

}

else

{

=1&&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 equa