Смекни!
smekni.com

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

}

if(v == 'c') {

iField += 2;

}

if(v == 'd') {

iField += 3;

}

if(v == 'e') {

iField += 4;

}

if(v == 'f') {

iField += 5;

}

if(v == 'g') {

iField += 6;

}

if(v == 'h') {

iField += 7;

}

//log('return ' + iField);

return self.fields[iField];

}

function getFigure(is_short, is_long, target, color, field) {

if(is_short || is_long) {

if(color == 'w') {

if(is_short) {

return new Array(self.figures[0], self.figures[7]);

}

if(is_long) {

return new Array(self.figures[0], self.figures[6]);

}

}

if(color == 'b') {

if(is_short) {

return new Array(self.figures[16], self.figures[23]);

}

if(is_long) {

return new Array(self.figures[16], self.figures[22]);

}

}

}

if(target == 'alive') {

return field.last_figure;

}

if(target == 'dead') {

return field.last_figure;

}

}

// Запрашиваем обновление позиции

new Ajax.Request(Chess.moves, {

method: 'get',

onSuccess: function(transport) {

self.all_moves = transport.responseText;

}

});

// Анализируемходыпартии

var splits = self.all_moves.split('|');

var moves = new Array();

for(i = 0; i < splits.length; i++) {

if(splits[i] != null) {

moves.push(splits[i

}

}

// Количество полученных ходов

var number = moves.length;

var current_number = self.last_move == null ? 1 : self.last_move.number;

if(number == current_number && self.last_move != null && self.last_move.color == 'w') {

var move_annotation = moves[number - 1];

if(move_annotation != '' && existsMove('b', moves[number - 1])) {

var move = Chess.createMove();

move.is_short_castling = isCastling('b', 'short', move_annotation);

move.is_long_castling = isCastling('b', 'long', move_annotation);

move.field_from = getField(move.is_short_castling, move.is_long_castling, 'from' , 'b', move_annotation);

move.field_to = getField(move.is_short_castling, move.is_long_castling, 'to', 'b', move_annotation);

move.figure = getFigure(move.is_short_castling, move.is_long_castling, 'alive', 'b', move.field_from);

move.figure_dead = getFigure(false, false, 'dead', 'b', move.field_to);

move.number = self.last_move != null ? ('b' == 'w' ? (self.last_move.number + 1) : self.last_move.number) : 1;

move.color = 'b';

move.field_from.last_figure = null;

move.field_to.last_figure = move.figure;

move.prev = self.last_move;

if(self.last_move != null) {

self.last_move.next = move;

}

self.last_move = move;

self.changes = true;

}

}

if(number > current_number) {

var c = ['w', 'b'];

for(var iMove = current_number; iMove <= number; iMove++) {

for(var ic = 0; ic < c.length; ic++) {

var move_annotation = moves[iMove - 1];

if(existsMove(c[ic], move_annotation)) {

var move = Chess.createMove();

if(self.first_move == null) {

self.first_move = move;

}

move.is_short_castling = isCastling(c[ic], 'short', move_annotation);

move.is_long_castling = isCastling(c[ic], 'long', move_annotation);

move.field_from = getField(move.is_short_castling, move.is_long_castling, 'from' , c[ic], move_annotation);

move.field_to = getField(move.is_short_castling, move.is_long_castling, 'to', c[ic], move_annotation);

move.figure = getFigure(move.is_short_castling, move.is_long_castling, 'alive', c[ic], move.field_from);

move.figure_dead = getFigure(move.is_short_castling, move.is_long_castling, 'dead', c[ic], move.field_to);

move.number = self.last_move != null ? (c[ic] == 'w' ? (self.last_move.number + 1) : self.last_move.number) : 1;

move.color = c[ic];

var ff = move.field_from;

var ft = move.field_to;

var fg = move.figure;

if(fg == null) {

continue;

}

move.field_from.last_figure = null;

move.field_to.last_figure = move.figure;

move.prev = self.last_move;

if(self.last_move != null) {

self.last_move.next = move;

}

self.last_move = move;

self.changes = true;

}

}

}

}

}

self.changes = false;

// Перерисовкапозиции

self.repaint = function() {

if(self.current_move == null) {

if(self.first_move == null) {

return;

}

self.current_move = self.first_move;

}

if(self.current_move.color == 'w' && self.white_number == self.current_move.number ||

self.current_move.color == 'b' && self.black_number == self.current_move.number) {

if(self.current_move.next != null) {

self.current_move = self.current_move.next;

}

return;

}

self.current_move.forward();

if(self.current_move.color == 'w') {

self.white_number++;

}

if(self.current_move.color == 'b') {

self.black_number++;

}

if(self.current_move.next != null) {

self.current_move = self.current_move.next;

}

}

// Инициализируем доску, создаем поля и фигуры

self.init = function() {

var v = new Array("A", "B", "C", "D", "E", "F", "G", "H");

var f = new Array("K", "Q", "B", "N", "R", "P");

var c = new Array("w", "b");

// Создаемпредставлениедоски - таблицу

var tableBoard = document.createElement('table');

tableBoard.border = 1;

tableBoard.align = "center";

tableBoard.width = "512px";

var tableBody = document.createElement('tbody');

tableBoard.appendChild(tableBody);

var tableDiv = $('board');

tableDiv.appendChild(tableBoard);

var rows = new Array(document.createElement('tr'), document.createElement('tr'), document.createElement('tr'), document.createElement('tr'),

document.createElement('tr'), document.createElement('tr'), document.createElement('tr'), document.createElement('tr'));

for(var iRow = 7; iRow >= 0; iRow--) {

tableBody.appendChild(rows[iRow]);

}

// создаемячейки

for(var iVertical = 0; iVertical < 8; iVertical++) {

for(var iHorizontal = 8; iHorizontal > 0; iHorizontal--) {

var cell = document.createElement('td');

cell.width = '64px';//(100 / 8).toString() + "%";

cell.height = cell.width;

cell.align = 'center';

cell.id = "field" + v[iVertical] + iHorizontal.toString();

rows[iHorizontal - 1].appendChild(cell);

}

}

// поля

log('creating fields ...');

var iField = 0;

for(var iHorizontal = 1; iHorizontal < 9; iHorizontal++) {

for(var iVertical = 0; iVertical < 8; iVertical++) {

self.fields[iField] = Chess.createField(v[iVertical], iHorizontal.toString());

iField++;

}

}

log('creating figure ...');

// фигуры

self.figures[0] = Chess.createFigure(c[0], f[0], ''); // wk - 0

self.figures[1] = Chess.createFigure(c[0], f[1], ''); // wq - 1

self.figures[2] = Chess.createFigure(c[0], f[2], v[2]); // wbC - 2

self.figures[3] = Chess.createFigure(c[0], f[2], v[5]); // wbF - 3

self.figures[4] = Chess.createFigure(c[0], f[3], v[1]); // wnB - 4

self.figures[5] = Chess.createFigure(c[0], f[3], v[6]); // wnG - 5

self.figures[6] = Chess.createFigure(c[0], f[4], v[0]); // wrA - 6

self.figures[7] = Chess.createFigure(c[0], f[4], v[7]); // wrH - 7

self.figures[8] = Chess.createFigure(c[0], f[5], v[0]); // wpA - 8

self.figures[9] = Chess.createFigure(c[0], f[5], v[1]); // wpB - 9

self.figures[10] = Chess.createFigure(c[0], f[5], v[2]); // wpC - 10

self.figures[11] = Chess.createFigure(c[0], f[5], v[3]); // wpD - 11

self.figures[12] = Chess.createFigure(c[0], f[5], v[4]); // wpE - 12

self.figures[13] = Chess.createFigure(c[0], f[5], v[5]); // wpF - 13

self.figures[14] = Chess.createFigure(c[0], f[5], v[6]); // wpG - 14

self.figures[15] = Chess.createFigure(c[0], f[5], v[7]); // wpH - 15

self.figures[16] = Chess.createFigure(c[1], f[0], ''); // bk - 16

self.figures[17] = Chess.createFigure(c[1], f[1], ''); // bq - 17

self.figures[18] = Chess.createFigure(c[1], f[2], v[2]); // bbC - 18

self.figures[19] = Chess.createFigure(c[1], f[2], v[5]); // bbF - 19

self.figures[20] = Chess.createFigure(c[1], f[3], v[1]); // bnB - 20

self.figures[21] = Chess.createFigure(c[1], f[3], v[6]); // bnG - 21

self.figures[22] = Chess.createFigure(c[1], f[4], v[0]); // brA - 22

self.figures[23] = Chess.createFigure(c[1], f[4], v[7]); // brH - 23

self.figures[24] = Chess.createFigure(c[1], f[5], v[0]); // bpA - 24

self.figures[25] = Chess.createFigure(c[1], f[5], v[1]); // bpB - 25

self.figures[26] = Chess.createFigure(c[1], f[5], v[2]); // bpC - 26

self.figures[27] = Chess.createFigure(c[1], f[5], v[3]); // bpD - 27

self.figures[28] = Chess.createFigure(c[1], f[5], v[4]); // bpE - 28

self.figures[29] = Chess.createFigure(c[1], f[5], v[5]); // bpF - 29

self.figures[30] = Chess.createFigure(c[1], f[5], v[6]); // bpG - 30

self.figures[31] = Chess.createFigure(c[1], f[5], v[7]); // bpH - 31

// расставляем исходную позицию

self.set_begin_position();

}

self.set_begin_position = function() {

log('initialize begin position ...');

self.fields[56].init_figure(self.figures[22]);

self.fields[57].init_figure(self.figures[20]);

self.fields[58].init_figure(self.figures[18]);

self.fields[59].init_figure(self.figures[17]);

self.fields[60].init_figure(self.figures[16]);

self.fields[61].init_figure(self.figures[19]);

self.fields[62].init_figure(self.figures[21]);

self.fields[63].init_figure(self.figures[23]);

self.fields[48].init_figure(self.figures[24]);

self.fields[49].init_figure(self.figures[25]);

self.fields[50].init_figure(self.figures[26]);

self.fields[51].init_figure(self.figures[27]);

self.fields[52].init_figure(self.figures[28]);

self.fields[53].init_figure(self.figures[29]);

self.fields[54].init_figure(self.figures[30]);

self.fields[55].init_figure(self.figures[31]);

self.fields[8].init_figure(self.figures[8]);

self.fields[9].init_figure(self.figures[9]);

self.fields[10].init_figure(self.figures[10]);

self.fields[11].init_figure(self.figures[11]);

self.fields[12].init_figure(self.figures[12]);

self.fields[13].init_figure(self.figures[13]);

self.fields[14].init_figure(self.figures[14]);

self.fields[15].init_figure(self.figures[15]);

self.fields[0].init_figure(self.figures[6]);

self.fields[1].init_figure(self.figures[4]);

self.fields[2].init_figure(self.figures[2]);

self.fields[3].init_figure(self.figures[1]);

self.fields[4].init_figure(self.figures[0]);

self.fields[5].init_figure(self.figures[3]);

self.fields[6].init_figure(self.figures[5]);

self.fields[7].init_figure(self.figures[7]);

}

self.set_end_position = function() {

}

self.set_position = function(number, color) {

}

self.next_move = function() {

}

self.prev_move = function() {

}

return self;

}

};

var board = Chess.createBoard();

// Периодическоеобновлениепозиции

new PeriodicalExecuter(board.refresh, Chess.refresh_delay);

// Периодическаяперерисовкапозиции

new PeriodicalExecuter(board.repaint, Chess.repaint_delay)